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