Spaces:
Sleeping
Sleeping
Upload App.py
Browse files
app.py
ADDED
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from dotenv import load_dotenv
|
2 |
+
import os
|
3 |
+
import gradio as gr
|
4 |
+
import requests
|
5 |
+
from newspaper import Article
|
6 |
+
import time
|
7 |
+
from langdetect import detect
|
8 |
+
|
9 |
+
|
10 |
+
# Load environment variables from .env file
|
11 |
+
load_dotenv()
|
12 |
+
API_KEY = os.getenv('API_KEY')
|
13 |
+
|
14 |
+
# Define the Hugging Face API URL
|
15 |
+
API_URL = "https://api-inference.huggingface.co/models/facebook/bart-large-cnn"
|
16 |
+
|
17 |
+
|
18 |
+
# Set up headers for the API request
|
19 |
+
headers = {"Authorization": f"Bearer {API_KEY}"}
|
20 |
+
|
21 |
+
# Function to query the Hugging Face API
|
22 |
+
def query(payload):
|
23 |
+
for _ in range(5): # Try 5 times
|
24 |
+
response = requests.post(API_URL, headers=headers, json=payload)
|
25 |
+
result = response.json()
|
26 |
+
if 'error' in result and 'currently loading' in result['error']:
|
27 |
+
time.sleep(5) # Wait for 5 seconds before retrying
|
28 |
+
else:
|
29 |
+
return result
|
30 |
+
return {"error": "Model is still loading. Please try again later."}
|
31 |
+
|
32 |
+
|
33 |
+
|
34 |
+
# Function to summarize text
|
35 |
+
def summarize(text, minL=20, maxL=300):
|
36 |
+
output = query({
|
37 |
+
"inputs": text,
|
38 |
+
"parameters": {
|
39 |
+
"min_length": minL,
|
40 |
+
"max_length": maxL
|
41 |
+
}
|
42 |
+
})
|
43 |
+
if "error" in output:
|
44 |
+
return f"Error: {output['error']}"
|
45 |
+
if not isinstance(output, list) or not output:
|
46 |
+
return "Error: Unexpected response format."
|
47 |
+
if "summary_text" not in output[0]:
|
48 |
+
return "Error: 'summary_text' key not found in the response."
|
49 |
+
return output[0]['summary_text']
|
50 |
+
|
51 |
+
def Choices(choice,input_text,minL,maxL):
|
52 |
+
if choice=="URL":
|
53 |
+
try:
|
54 |
+
article=Article(input_text,language="en")
|
55 |
+
article.download()
|
56 |
+
article.parse()
|
57 |
+
article.nlp()
|
58 |
+
text=article.text
|
59 |
+
except Exception as e:
|
60 |
+
return f"Error: Unable to fetch article. {str(e)}"
|
61 |
+
else:
|
62 |
+
text=input_text
|
63 |
+
return summarize(text, minL, maxL)
|
64 |
+
|
65 |
+
# Create Gradio interface
|
66 |
+
demo = gr.Interface(
|
67 |
+
fn=Choices,
|
68 |
+
inputs=[
|
69 |
+
gr.Radio(choices=["URL", "Paragraph"], label="Input Type", value="URL"),
|
70 |
+
gr.Textbox(lines=10, placeholder="Enter text here..."),
|
71 |
+
gr.Number(value=20, label="Minimum Length"),
|
72 |
+
gr.Number(value=300, label="Maximum Length"),
|
73 |
+
],
|
74 |
+
outputs="textbox",
|
75 |
+
title="Text Summarizer",
|
76 |
+
description="Enter text to summarize using Hugging Face BART model."
|
77 |
+
)
|
78 |
+
|
79 |
+
# Launch the interface
|
80 |
+
demo.launch()
|