ImHadis's picture
Update app.py
134e421 verified
raw
history blame
1.17 kB
import os
from dotenv import load_dotenv
import requests
import gradio as gr
# Load environment variables
load_dotenv()
# Access the Hugging Face API key
hf_api_key = os.getenv('HF_API_KEY')
# Set up the API endpoint
API_URL = "https://api-inference.huggingface.co/models/sshleifer/distilbart-cnn-12-6"
headers = {"Authorization": f"Bearer {hf_api_key}"}
def query(payload):
response = requests.post(API_URL, headers=headers, json=payload)
return response.json()
def summarize(input_text):
try:
output = query({
"inputs": input_text,
"parameters": {"max_length": 130, "min_length": 30}
})
if isinstance(output, list) and len(output) > 0 and 'summary_text' in output[0]:
return output[0]['summary_text']
elif isinstance(output, dict) and 'error' in output:
return f"API Error: {output['error']}"
else:
return f"Unexpected response format: {output}"
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()