alex-abb commited on
Commit
f946cbe
1 Parent(s): 5131b33

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -10
app.py CHANGED
@@ -1,26 +1,28 @@
1
  import requests
2
  import gradio as gr
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/meta-llama/Meta-llama-3-8B-Instruct"
11
  headers = {"Authorization": f"Bearer {api_token}"}
12
 
13
- @spaces.GPU
14
-
15
  def query(payload):
16
  response = requests.post(API_URL, headers=headers, json=payload)
17
- return response.json()
 
 
 
18
 
19
  def detect_sentiment(message):
20
  prompt = f"Détecte le sentiment de ce message. Réponds par 'positif' ou 'négatif' :\nMessage : \"{message}\""
21
  response = query({"inputs": prompt})
22
- sentiment = response[0]['generated_text'].strip().lower()
23
- return sentiment
 
 
 
 
24
 
25
  def sentiment_analysis_interface(message):
26
  sentiment = detect_sentiment(message)
@@ -36,4 +38,4 @@ iface = gr.Interface(
36
  )
37
 
38
  # Lancer l'interface
39
- iface.launch()
 
1
  import requests
2
  import gradio as gr
3
+ import os
 
 
4
 
5
  api_token = os.environ.get("TOKEN")
6
 
 
7
  API_URL = "https://api-inference.huggingface.co/models/meta-llama/Meta-llama-3-8B-Instruct"
8
  headers = {"Authorization": f"Bearer {api_token}"}
9
 
 
 
10
  def query(payload):
11
  response = requests.post(API_URL, headers=headers, json=payload)
12
+ if response.status_code == 200:
13
+ return response.json()
14
+ else:
15
+ raise Exception(f"Request failed with status code {response.status_code}: {response.text}")
16
 
17
  def detect_sentiment(message):
18
  prompt = f"Détecte le sentiment de ce message. Réponds par 'positif' ou 'négatif' :\nMessage : \"{message}\""
19
  response = query({"inputs": prompt})
20
+ generated_texts = response.get('generated_text', [])
21
+ if generated_texts:
22
+ sentiment = generated_texts[0].strip().lower()
23
+ return sentiment
24
+ else:
25
+ raise Exception("No generated_text found in API response")
26
 
27
  def sentiment_analysis_interface(message):
28
  sentiment = detect_sentiment(message)
 
38
  )
39
 
40
  # Lancer l'interface
41
+ iface.launch()