alex-abb commited on
Commit
1b27ac3
·
verified ·
1 Parent(s): 05bab40

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -31
app.py CHANGED
@@ -1,51 +1,43 @@
1
- import os
2
  import requests
3
- import gradio as gr
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
  return response.json()
13
 
14
-
15
-
16
-
17
  def analyze_sentiment(text):
18
  output = query({
19
- "inputs": f'''<|begin_of_text|>
20
- <|start_header_id|>system<|end_header_id|>
21
- you are a feeling analyser and you'll say only "positive" if i'm feeling positive and "negativ" if i'm feeling sad <|eot_id|>
22
- <|start_header_id|>user<|end_header_id|>
23
- {text}
24
- <|eot_id|>
25
- <|start_header_id|>assistant<|end_header_id|>
26
- '''
27
- })
 
28
 
29
- # Assurez-vous de gérer correctement la sortie de l'API
30
- if isinstance(output, list) and len(output) > 0:
31
- return output[0].get('generated_text', 'Erreur: Réponse inattendue')
32
  if isinstance(output, list) and len(output) > 0:
33
  response = output[0].get('generated_text', '').strip().lower()
34
- if 'positive' in response:
35
- return 'positive'
36
- elif 'negative' in response:
37
- return 'negativ'
 
 
38
  else:
39
- return "Erreur: Réponse inattendue"
40
-
41
-
42
-
43
-
44
 
45
  demo = gr.Interface(
46
- fn = query,
47
- inputs=["text"],
48
- outputs=["text"]
49
  )
50
 
51
  demo.launch()
 
1
+ import os
2
  import requests
3
+ import gradio as gr
4
 
5
  api_token = os.environ.get("TOKEN")
 
6
  API_URL = "https://api-inference.huggingface.co/models/meta-llama/Meta-Llama-3-8B-Instruct"
7
  headers = {"Authorization": f"Bearer {api_token}"}
8
 
9
  def query(payload):
10
+ response = requests.post(API_URL, headers=headers, json=payload)
11
  return response.json()
12
 
 
 
 
13
  def analyze_sentiment(text):
14
  output = query({
15
+ "inputs": f'''<|begin_of_text|>
16
+ <|start_header_id|>system<|end_header_id|>
17
+ You are a feeling analyser and you'll say only "positive" if I'm feeling positive and "negative" if I'm feeling sad
18
+ <|eot_id|>
19
+ <|start_header_id|>user<|end_header_id|>
20
+ {text}
21
+ <|eot_id|>
22
+ <|start_header_id|>assistant<|end_header_id|>
23
+ '''
24
+ })
25
 
 
 
 
26
  if isinstance(output, list) and len(output) > 0:
27
  response = output[0].get('generated_text', '').strip().lower()
28
+ if 'positive' in response:
29
+ return 'positive'
30
+ elif 'negative' in response:
31
+ return 'negative'
32
+ else:
33
+ return "Erreur: Réponse inattendue"
34
  else:
35
+ return "Erreur: Réponse inattendue de l'API"
 
 
 
 
36
 
37
  demo = gr.Interface(
38
+ fn=analyze_sentiment,
39
+ inputs="text",
40
+ outputs="text"
41
  )
42
 
43
  demo.launch()