Spaces:
Sleeping
Sleeping
Update
Browse files
app.py
CHANGED
@@ -6,13 +6,14 @@ import time
|
|
6 |
from langdetect import detect
|
7 |
import nltk
|
8 |
|
9 |
-
|
|
|
|
|
10 |
# Load environment variables from .env file
|
11 |
API_KEY = os.getenv('API_KEY')
|
12 |
|
13 |
print(f"API_KEY: {'Loaded' if API_KEY else 'Not Loaded'}")
|
14 |
|
15 |
-
|
16 |
# Ensure the API key is loaded
|
17 |
if not API_KEY:
|
18 |
raise ValueError("API_KEY is missing. Please set it in the secret variables in the Hugging Face Spaces settings.")
|
@@ -20,7 +21,6 @@ if not API_KEY:
|
|
20 |
# Define the Hugging Face API URL
|
21 |
API_URL = "https://api-inference.huggingface.co/models/facebook/bart-large-cnn"
|
22 |
|
23 |
-
|
24 |
# Set up headers for the API request
|
25 |
headers = {"Authorization": f"Bearer {API_KEY}"}
|
26 |
|
@@ -35,8 +35,6 @@ def query(payload):
|
|
35 |
return result
|
36 |
return {"error": "Model is still loading. Please try again later."}
|
37 |
|
38 |
-
|
39 |
-
|
40 |
# Function to summarize text
|
41 |
def summarize(text, minL=20, maxL=300):
|
42 |
output = query({
|
@@ -54,18 +52,18 @@ def summarize(text, minL=20, maxL=300):
|
|
54 |
return "Error: 'summary_text' key not found in the response."
|
55 |
return output[0]['summary_text']
|
56 |
|
57 |
-
def Choices(choice,input_text,minL,maxL):
|
58 |
-
if choice=="URL":
|
59 |
try:
|
60 |
-
article=Article(input_text,language="en")
|
61 |
article.download()
|
62 |
article.parse()
|
63 |
article.nlp()
|
64 |
-
text=article.text
|
65 |
except Exception as e:
|
66 |
return f"Error: Unable to fetch article. {str(e)}"
|
67 |
else:
|
68 |
-
text=input_text
|
69 |
return summarize(text, minL, maxL)
|
70 |
|
71 |
# Create Gradio interface
|
@@ -84,3 +82,4 @@ demo = gr.Interface(
|
|
84 |
|
85 |
# Launch the interface
|
86 |
demo.launch()
|
|
|
|
6 |
from langdetect import detect
|
7 |
import nltk
|
8 |
|
9 |
+
# Download the 'punkt' tokenizer
|
10 |
+
nltk.download('punkt')
|
11 |
+
|
12 |
# Load environment variables from .env file
|
13 |
API_KEY = os.getenv('API_KEY')
|
14 |
|
15 |
print(f"API_KEY: {'Loaded' if API_KEY else 'Not Loaded'}")
|
16 |
|
|
|
17 |
# Ensure the API key is loaded
|
18 |
if not API_KEY:
|
19 |
raise ValueError("API_KEY is missing. Please set it in the secret variables in the Hugging Face Spaces settings.")
|
|
|
21 |
# Define the Hugging Face API URL
|
22 |
API_URL = "https://api-inference.huggingface.co/models/facebook/bart-large-cnn"
|
23 |
|
|
|
24 |
# Set up headers for the API request
|
25 |
headers = {"Authorization": f"Bearer {API_KEY}"}
|
26 |
|
|
|
35 |
return result
|
36 |
return {"error": "Model is still loading. Please try again later."}
|
37 |
|
|
|
|
|
38 |
# Function to summarize text
|
39 |
def summarize(text, minL=20, maxL=300):
|
40 |
output = query({
|
|
|
52 |
return "Error: 'summary_text' key not found in the response."
|
53 |
return output[0]['summary_text']
|
54 |
|
55 |
+
def Choices(choice, input_text, minL, maxL):
|
56 |
+
if choice == "URL":
|
57 |
try:
|
58 |
+
article = Article(input_text, language="en")
|
59 |
article.download()
|
60 |
article.parse()
|
61 |
article.nlp()
|
62 |
+
text = article.text
|
63 |
except Exception as e:
|
64 |
return f"Error: Unable to fetch article. {str(e)}"
|
65 |
else:
|
66 |
+
text = input_text
|
67 |
return summarize(text, minL, maxL)
|
68 |
|
69 |
# Create Gradio interface
|
|
|
82 |
|
83 |
# Launch the interface
|
84 |
demo.launch()
|
85 |
+
|