File size: 1,325 Bytes
fb64d41
5131b33
f946cbe
4ffc5f1
5131b33
867427f
5131b33
d43b4cf
e843947
604d57b
 
f946cbe
 
 
 
5131b33
 
 
 
f946cbe
 
 
 
 
 
5131b33
 
 
 
 
 
 
 
 
5acfd7e
5131b33
 
b96ef93
 
5acfd7e
f946cbe
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import requests
import gradio as gr
import os

api_token = os.environ.get("TOKEN")

API_URL = "https://api-inference.huggingface.co/models/meta-llama/Meta-llama-3-8B-Instruct"
headers = {"Authorization": f"Bearer {api_token}"}

def query(payload):
    response = requests.post(API_URL, headers=headers, json=payload)
    if response.status_code == 200:
        return response.json()
    else:
        raise Exception(f"Request failed with status code {response.status_code}: {response.text}")

def detect_sentiment(message):
    prompt = f"Détecte le sentiment de ce message. Réponds par 'positif' ou 'négatif' :\nMessage : \"{message}\""
    response = query({"inputs": prompt})
    generated_texts = response.get('generated_text', [])
    if generated_texts:
        sentiment = generated_texts[0].strip().lower()
        return sentiment
    else:
        raise Exception("No generated_text found in API response")

def sentiment_analysis_interface(message):
    sentiment = detect_sentiment(message)
    return sentiment

# Créer l'interface Gradio
iface = gr.Interface(
    fn=sentiment_analysis_interface,
    inputs="text",
    outputs="text",
    title="Détection de Sentiment",
    description="Entrez un message pour détecter si le sentiment est positif ou négatif.",
)

# Lancer l'interface
iface.launch()