Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,38 +1,33 @@
|
|
|
|
|
| 1 |
import os
|
| 2 |
import requests
|
| 3 |
import spaces
|
| 4 |
import gradio as gr
|
| 5 |
-
import re
|
| 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 |
@spaces.GPU
|
| 13 |
|
| 14 |
def query(payload):
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
def analyze_sentiment(text):
|
| 19 |
-
prompt = f"
|
| 20 |
-
|
| 21 |
output = query({
|
| 22 |
"inputs": prompt,
|
| 23 |
})
|
| 24 |
-
|
|
|
|
| 25 |
if isinstance(output, list) and len(output) > 0:
|
| 26 |
-
|
| 27 |
-
if 'positif' in response:
|
| 28 |
-
return "positif"
|
| 29 |
-
elif 'négatif' in response:
|
| 30 |
-
return "négatif"
|
| 31 |
-
else:
|
| 32 |
-
return "Sentiment non détecté"
|
| 33 |
else:
|
| 34 |
return "Erreur: Réponse inattendue de l'API"
|
| 35 |
-
|
| 36 |
demo = gr.Interface(
|
| 37 |
fn = analyze_sentiment,
|
| 38 |
inputs=["text"],
|
|
|
|
| 1 |
+
import torch
|
| 2 |
import os
|
| 3 |
import requests
|
| 4 |
import spaces
|
| 5 |
import gradio as gr
|
|
|
|
| 6 |
|
| 7 |
api_token = os.environ.get("TOKEN")
|
| 8 |
|
|
|
|
| 9 |
API_URL = "https://api-inference.huggingface.co/models/meta-llama/Meta-Llama-3-8B-Instruct"
|
| 10 |
headers = {"Authorization": f"Bearer {api_token}"}
|
| 11 |
+
|
| 12 |
@spaces.GPU
|
| 13 |
|
| 14 |
def query(payload):
|
| 15 |
+
response = requests.post(API_URL, headers=headers, json=payload)
|
| 16 |
+
return response.json()
|
| 17 |
+
|
| 18 |
def analyze_sentiment(text):
|
| 19 |
+
prompt = f"Tu es un analyseur de sentiment. Ton rôle est d'évaluer le sentiment général du texte fourni. Réponds uniquement par 'positif' ou 'négatif'. N'ajoute aucune explication. Voici le texte à analyser : {text}"
|
| 20 |
+
|
| 21 |
output = query({
|
| 22 |
"inputs": prompt,
|
| 23 |
})
|
| 24 |
+
|
| 25 |
+
# Assurez-vous de gérer correctement la sortie de l'API
|
| 26 |
if isinstance(output, list) and len(output) > 0:
|
| 27 |
+
return output[0].get('generated_text', 'Erreur: Réponse inattendue')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
else:
|
| 29 |
return "Erreur: Réponse inattendue de l'API"
|
| 30 |
+
|
| 31 |
demo = gr.Interface(
|
| 32 |
fn = analyze_sentiment,
|
| 33 |
inputs=["text"],
|