Aditya67 commited on
Commit
476b9b5
·
verified ·
1 Parent(s): deb15e6
Files changed (1) hide show
  1. app.py +82 -80
app.py CHANGED
@@ -1,80 +1,82 @@
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()
 
 
 
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()