File size: 1,481 Bytes
d46fa4f
fb64d41
4ffc5f1
f27292a
fb64d41
 
efba1b1
4ffc5f1
 
62f2a72
fb64d41
 
 
d7b565c
 
 
 
 
 
 
 
 
 
 
 
 
 
1a67d0c
fb64d41
d7b565c
efba1b1
fb64d41
 
 
 
 
 
3db2cf3
 
d7b565c
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
import gradio as gr
import requests
import spaces 

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

@spaces.GPU

def analyze_sentiment(text):
    payload = {
        "inputs": f"Analyze the sentiment of the following text and respond with either 'heureux' or 'malheureux': {text}"
    }
    try:
        response = requests.post(API_URL, headers=headers, json=payload)
        response.raise_for_status()  # Vérifie si la requête a réussi
        result = response.json()
        
        if isinstance(result, list) and len(result) > 0 and 'generated_text' in result[0]:
            sentiment = result[0]['generated_text'].strip().lower()
            return "heureux" if "heureux" in sentiment else "malheureux"
        else:
            return "Erreur: Format de réponse inattendu"
    except requests.exceptions.RequestException as e:
        return f"Erreur de requête: {str(e)}"
    except Exception as e:
        return f"Erreur inattendue: {str(e)}"

def gradio_interface(input_text):
    return analyze_sentiment(input_text)

iface = gr.Interface(
    fn=gradio_interface,
    inputs=gr.Textbox(lines=3, placeholder="Entrez votre texte ici..."),
    outputs=gr.Label(num_top_classes=1),
    title="Analyseur de Sentiment",
    description="Entrez un texte pour déterminer si le sentiment est 'heureux' ou 'malheureux'."
)

iface.launch(share=True)