ImHadis's picture
Update app.py
165652d verified
raw
history blame
814 Bytes
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()