Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,28 +1,32 @@
|
|
|
|
1 |
import gradio as gr
|
2 |
import requests
|
3 |
import spaces
|
4 |
-
import os
|
5 |
-
|
6 |
-
|
7 |
-
api_token = os.environ.get("TOKEN")
|
8 |
|
|
|
|
|
9 |
|
10 |
API_URL = "https://api-inference.huggingface.co/models/distilbert-base-uncased-finetuned-sst-2-english"
|
11 |
headers = {"Authorization": f"Bearer {api_token}"}
|
12 |
-
|
13 |
-
|
14 |
@spaces.GPU
|
|
|
15 |
def analyze_sentiment(text):
|
16 |
payload = {"inputs": text}
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
|
27 |
def gradio_interface(input_text):
|
28 |
return analyze_sentiment(input_text)
|
@@ -30,9 +34,4 @@ def gradio_interface(input_text):
|
|
30 |
iface = gr.Interface(
|
31 |
fn=gradio_interface,
|
32 |
inputs=gr.Textbox(lines=3, placeholder="Entrez votre texte ici..."),
|
33 |
-
outputs=gr.Label(
|
34 |
-
title="Analyseur de Sentiment",
|
35 |
-
description="Entrez un texte pour déterminer si le sentiment est 'heureux' ou 'malheureux'."
|
36 |
-
)
|
37 |
-
|
38 |
-
iface.launch()
|
|
|
1 |
+
import os
|
2 |
import gradio as gr
|
3 |
import requests
|
4 |
import spaces
|
|
|
|
|
|
|
|
|
5 |
|
6 |
+
# Récupérer le token depuis les secrets
|
7 |
+
api_token = os.environ.get("HF_API_TOKEN")
|
8 |
|
9 |
API_URL = "https://api-inference.huggingface.co/models/distilbert-base-uncased-finetuned-sst-2-english"
|
10 |
headers = {"Authorization": f"Bearer {api_token}"}
|
|
|
|
|
11 |
@spaces.GPU
|
12 |
+
|
13 |
def analyze_sentiment(text):
|
14 |
payload = {"inputs": text}
|
15 |
+
try:
|
16 |
+
response = requests.post(API_URL, headers=headers, json=payload)
|
17 |
+
response.raise_for_status() # Lève une exception pour les codes d'erreur HTTP
|
18 |
+
result = response.json()
|
19 |
+
|
20 |
+
if isinstance(result, list) and len(result) > 0 and isinstance(result[0], list):
|
21 |
+
sentiment_scores = result[0]
|
22 |
+
sentiment = "heureux" if sentiment_scores[1] > sentiment_scores[0] else "malheureux"
|
23 |
+
return sentiment
|
24 |
+
else:
|
25 |
+
return "Erreur: Format de réponse inattendu"
|
26 |
+
except requests.RequestException as e:
|
27 |
+
return f"Erreur de requête: {str(e)}"
|
28 |
+
except Exception as e:
|
29 |
+
return f"Erreur inattendue: {str(e)}"
|
30 |
|
31 |
def gradio_interface(input_text):
|
32 |
return analyze_sentiment(input_text)
|
|
|
34 |
iface = gr.Interface(
|
35 |
fn=gradio_interface,
|
36 |
inputs=gr.Textbox(lines=3, placeholder="Entrez votre texte ici..."),
|
37 |
+
outputs=gr.Label(num_top_
|
|
|
|
|
|
|
|
|
|