alex-abb commited on
Commit
4c4e8e3
1 Parent(s): f946cbe

Update app.py

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