Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -2,23 +2,23 @@ import gradio as gr
|
|
2 |
import requests
|
3 |
import spaces
|
4 |
|
5 |
-
API_URL = "https://api-inference.huggingface.co/models/
|
6 |
headers = {"Authorization": "Bearer hf_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"}
|
7 |
|
8 |
@spaces.GPU
|
9 |
|
10 |
def analyze_sentiment(text):
|
11 |
-
payload = {
|
12 |
-
"inputs": f"Analyze the sentiment of the following text and respond with either 'heureux' or 'malheureux': {text}"
|
13 |
-
}
|
14 |
try:
|
15 |
response = requests.post(API_URL, headers=headers, json=payload)
|
16 |
-
response.raise_for_status()
|
17 |
result = response.json()
|
18 |
|
19 |
-
if isinstance(result, list) and len(result) > 0 and
|
20 |
-
|
21 |
-
|
|
|
|
|
22 |
else:
|
23 |
return "Erreur: Format de réponse inattendu"
|
24 |
except requests.exceptions.RequestException as e:
|
|
|
2 |
import requests
|
3 |
import spaces
|
4 |
|
5 |
+
API_URL = "https://api-inference.huggingface.co/models/distilbert-base-uncased-finetuned-sst-2-english"
|
6 |
headers = {"Authorization": "Bearer hf_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"}
|
7 |
|
8 |
@spaces.GPU
|
9 |
|
10 |
def analyze_sentiment(text):
|
11 |
+
payload = {"inputs": text}
|
|
|
|
|
12 |
try:
|
13 |
response = requests.post(API_URL, headers=headers, json=payload)
|
14 |
+
response.raise_for_status()
|
15 |
result = response.json()
|
16 |
|
17 |
+
if isinstance(result, list) and len(result) > 0 and isinstance(result[0], list):
|
18 |
+
# Le modèle renvoie généralement une liste de scores pour les étiquettes NEGATIVE et POSITIVE
|
19 |
+
sentiment_scores = result[0]
|
20 |
+
sentiment = "heureux" if sentiment_scores[1] > sentiment_scores[0] else "malheureux"
|
21 |
+
return sentiment
|
22 |
else:
|
23 |
return "Erreur: Format de réponse inattendu"
|
24 |
except requests.exceptions.RequestException as e:
|