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"))