alex-abb commited on
Commit
2da4e53
1 Parent(s): d7b565c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -8
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/meta-llama/Meta-Llama-3-8B-Instruct"
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() # Vérifie si la requête a réussi
17
  result = response.json()
18
 
19
- if isinstance(result, list) and len(result) > 0 and 'generated_text' in result[0]:
20
- sentiment = result[0]['generated_text'].strip().lower()
21
- return "heureux" if "heureux" in sentiment else "malheureux"
 
 
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: