Spaces:
Runtime error
Runtime error
Update app.py
Browse files
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 |
-
|
12 |
return response.json()
|
13 |
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
def analyze_sentiment(text):
|
18 |
output = query({
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
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 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
|
|
|
|
38 |
else:
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
|
45 |
demo = gr.Interface(
|
46 |
-
fn
|
47 |
-
inputs=
|
48 |
-
outputs=
|
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()
|