Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,20 +1,18 @@
|
|
|
|
1 |
import gradio as gr
|
2 |
import requests
|
3 |
from bs4 import BeautifulSoup
|
4 |
|
5 |
-
# Hugging Face API config
|
6 |
API_URL = "https://api-inference.huggingface.co/models/cardiffnlp/twitter-roberta-base-sentiment"
|
7 |
-
HF_TOKEN = "
|
8 |
headers = {"Authorization": f"Bearer {HF_TOKEN}"}
|
9 |
|
10 |
-
# Label mapping
|
11 |
label_map = {
|
12 |
"LABEL_0": "Negative",
|
13 |
"LABEL_1": "Neutral",
|
14 |
"LABEL_2": "Positive"
|
15 |
}
|
16 |
|
17 |
-
# Fetch and extract text from a URL
|
18 |
def fetch_url_text(url):
|
19 |
try:
|
20 |
headers_req = {'User-Agent': 'Mozilla/5.0'}
|
@@ -25,7 +23,6 @@ def fetch_url_text(url):
|
|
25 |
except Exception as e:
|
26 |
return f"URL error: {e}"
|
27 |
|
28 |
-
# Sentiment analysis logic
|
29 |
def analyze_sentiment(text_input, file_upload, url_input):
|
30 |
text = ""
|
31 |
if file_upload:
|
@@ -55,7 +52,6 @@ def analyze_sentiment(text_input, file_upload, url_input):
|
|
55 |
except Exception as e:
|
56 |
return f"❌ JSON parse error: {e}"
|
57 |
|
58 |
-
# Gradio UI
|
59 |
demo = gr.Interface(
|
60 |
fn=analyze_sentiment,
|
61 |
inputs=[
|
|
|
1 |
+
import os
|
2 |
import gradio as gr
|
3 |
import requests
|
4 |
from bs4 import BeautifulSoup
|
5 |
|
|
|
6 |
API_URL = "https://api-inference.huggingface.co/models/cardiffnlp/twitter-roberta-base-sentiment"
|
7 |
+
HF_TOKEN = os.getenv("HF_TOKEN")
|
8 |
headers = {"Authorization": f"Bearer {HF_TOKEN}"}
|
9 |
|
|
|
10 |
label_map = {
|
11 |
"LABEL_0": "Negative",
|
12 |
"LABEL_1": "Neutral",
|
13 |
"LABEL_2": "Positive"
|
14 |
}
|
15 |
|
|
|
16 |
def fetch_url_text(url):
|
17 |
try:
|
18 |
headers_req = {'User-Agent': 'Mozilla/5.0'}
|
|
|
23 |
except Exception as e:
|
24 |
return f"URL error: {e}"
|
25 |
|
|
|
26 |
def analyze_sentiment(text_input, file_upload, url_input):
|
27 |
text = ""
|
28 |
if file_upload:
|
|
|
52 |
except Exception as e:
|
53 |
return f"❌ JSON parse error: {e}"
|
54 |
|
|
|
55 |
demo = gr.Interface(
|
56 |
fn=analyze_sentiment,
|
57 |
inputs=[
|