alex-abb commited on
Commit
d43b4cf
·
verified ·
1 Parent(s): 69045ed

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -21
app.py CHANGED
@@ -3,34 +3,26 @@ import requests
3
  import spaces
4
  import os
5
 
6
- api_key = os.getenv("TOKEN")
7
 
8
- API_URL = "https://api-inference.huggingface.co/models/distilbert-base-uncased-finetuned-sst-2-english"
9
- headers = {"Authorization": "Bearer api_key"}
10
-
11
- @spaces.GPU
12
 
13
 
 
 
14
 
15
 
 
16
  def analyze_sentiment(text):
17
  payload = {"inputs": text}
18
- try:
19
- response = requests.post(API_URL, headers=headers, json=payload)
20
- response.raise_for_status()
21
- result = response.json()
22
-
23
- if isinstance(result, list) and len(result) > 0 and isinstance(result[0], list):
24
- # Le modèle renvoie généralement une liste de scores pour les étiquettes NEGATIVE et POSITIVE
25
- sentiment_scores = result[0]
26
- sentiment = "heureux" if sentiment_scores[1] > sentiment_scores[0] else "malheureux"
27
- return sentiment
28
- else:
29
- return "Erreur: Format de réponse inattendu"
30
- except requests.exceptions.RequestException as e:
31
- return f"Erreur de requête: {str(e)}"
32
- except Exception as e:
33
- return f"Erreur inattendue: {str(e)}"
34
 
35
  def gradio_interface(input_text):
36
  return analyze_sentiment(input_text)
 
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
+ response = requests.post(API_URL, headers=headers, json=payload)
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
 
27
  def gradio_interface(input_text):
28
  return analyze_sentiment(input_text)