File size: 814 Bytes
77b3501
165652d
b8f917c
77b3501
21cdd54
165652d
67013df
21cdd54
165652d
67013df
77b3501
165652d
 
 
 
 
 
 
 
 
 
 
 
 
 
77b3501
165652d
77b3501
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import os
from dotenv import load_dotenv
from transformers import pipeline
import gradio as gr

# Load environment variables
load_dotenv()

# Access the Hugging Face API key
hf_api_key = os.getenv('HF_API_KEY')

# Initialize the summarization pipeline
summarizer = pipeline(
    "summarization", 
    model="sshleifer/distilbart-cnn-12-6",  # Corrected model name
    token=hf_api_key
)

def summarize(input_text):
    try:
        # Limit input text to 1024 tokens to avoid potential issues
        output = summarizer(input_text, max_length=130, min_length=30, do_sample=False)
        return output[0]['summary_text']
    except Exception as e:
        return f"An error occurred: {str(e)}"

# Create and launch the Gradio interface
demo = gr.Interface(fn=summarize, inputs="text", outputs="text")
demo.launch()