File size: 2,740 Bytes
f20705e
 
 
849429a
dfc9138
 
 
 
f20705e
 
5127527
 
dfc9138
5127527
 
 
849429a
abcf621
5127527
849429a
5127527
 
 
 
 
 
 
 
 
 
 
 
 
 
f20705e
abcf621
 
dfc9138
 
 
81b63b9
849429a
abcf621
dfc9138
 
 
 
 
 
 
5127527
dfc9138
 
5127527
dfc9138
 
849429a
 
 
dfc9138
 
abcf621
849429a
 
dfc9138
abcf621
5127527
abcf621
849429a
 
5127527
dfc9138
 
5127527
 
 
dfc9138
f20705e
abcf621
f20705e
abcf621
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import gradio as gr
from transformers import pipeline

# 1. Chargement du modèle multilingue
analyseur = pipeline(
    "sentiment-analysis", 
    model="cardiffnlp/twitter-xlm-roberta-base-sentiment"
)

def analyser_texte(texte):
    if not texte or not texte.strip():
        return {}
    
    try:
        # Analyse du texte par l'IA
        resultats = analyseur(texte)
        
        # Dictionnaire pour stocker les scores
        scores_formattes = {"🟢 Positif": 0.0, "🔴 Négatif": 0.0, "🟡 Neutre": 0.0}
        
        for res in resultats:
            label_brut = res['label'].lower()
            score = float(res['score'])
            
            if "positive" in label_brut:
                scores_formattes["🟢 Positif"] = score
            elif "negative" in label_brut:
                scores_formattes["🔴 Négatif"] = score
            else:
                scores_formattes["🟡 Neutre"] = score
                
        return scores_formattes
    except Exception as e:
        return {"⚠️ Erreur d'analyse": 1.0}

# 2. Interface Graphique Moderne
with gr.Blocks() as interface:
    
    gr.Markdown(
        """
        # 🌍 Analyseur de sentiments - Multilingue Dynamique
        *L'IA analyse votre texte en temps réel et affiche les résultats sous forme de graphique !*
        *(Example avec un "a" = code : ce n'est pas une faute d'orthographe ;))*
        """
    )
    
    with gr.Row():
        with gr.Column():
            texte_entree = gr.Textbox(
                lines=4, 
                placeholder="Tapez votre texte ici...",
                label="✍️ Votre texte (Toutes langues)"
            )
            bouton_envoyer = gr.Button("⚡ Analyser", variant="primary")
            
        with gr.Column():
            graphique_sortie = gr.Label(
                num_top_classes=3,
                label="📊 Répartition du sentiment"
            )
    
    # Événements en temps réel
    texte_entree.change(fn=analyser_texte, inputs=texte_entree, outputs=graphique_sortie)
    bouton_envoyer.click(fn=analyser_texte, inputs=texte_entree, outputs=graphique_sortie)
    
    # Correction de la typo ici : 'examples' avec un 'a'
    gr.Examples(
        examples=[
            ["Je trouve cette application fantastique et super jolie !"],
            ["I am deeply disappointed by the quality of this service."],
            ["L'ambiance est correcte, mais sans plus."]
        ],
        inputs=texte_entree,
        outputs=graphique_sortie,
        fn=analyser_texte,
        run_on_click=True
    )

# Lancement avec l'application du thème Soft
if __name__ == "__main__":
    interface.launch(theme=gr.themes.Soft(primary_hue="indigo", secondary_hue="slate"))