DinoFrog commited on
Commit
850981e
·
verified ·
1 Parent(s): b8567c9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +73 -20
app.py CHANGED
@@ -8,35 +8,88 @@ classifier = pipeline(
8
  model="mrm8488/distilroberta-finetuned-financial-news-sentiment-analysis"
9
  )
10
 
11
- # Chargement du modèle de traduction FR->EN (peut aussi servir pour d'autres langues si tu veux étendre)
12
- translator = pipeline("translation", model="Helsinki-NLP/opus-mt-mul-en")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
 
 
14
  def analyze_sentiment(text):
 
 
15
  if not text:
16
- return "Entrez une phrase."
17
-
 
18
  try:
19
- # Détection de la langue
20
  lang = detect(text)
21
  except:
22
  lang = "unknown"
23
 
24
- # Traduction si la langue n'est pas l'anglais
25
  if lang != "en":
26
- text = translator(text, max_length=512)[0]['translation_text']
27
 
28
- # Analyse de sentiment
29
  result = classifier(text)[0]
30
- return f"Sentiment : {result['label']} (Score: {result['score']:.2f})"
31
-
32
- # Interface utilisateur
33
- iface = gr.Interface(
34
- fn=analyze_sentiment,
35
- inputs=gr.Textbox(lines=4, placeholder="Entrez une actualité financière ici..."),
36
- outputs="text",
37
- title="Analyse de Sentiment Financier",
38
- description="Entrez une news financière. Le modèle détecte automatiquement la langue et analyse le sentiment."
39
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40
 
41
- # Lancement local
42
- iface.launch()
 
8
  model="mrm8488/distilroberta-finetuned-financial-news-sentiment-analysis"
9
  )
10
 
11
+ # Modèle génératif pour créer des explications
12
+ explainer = pipeline(
13
+ "text2text-generation",
14
+ model="google/flan-t5-small"
15
+ )
16
+
17
+ # Modèle de traduction multilingue -> anglais
18
+ translator_to_en = pipeline(
19
+ "translation",
20
+ model="Helsinki-NLP/opus-mt-mul-en"
21
+ )
22
+
23
+ # Modèle de traduction anglais -> français
24
+ translator_to_fr = pipeline(
25
+ "translation",
26
+ model="Helsinki-NLP/opus-mt-en-fr"
27
+ )
28
+
29
+ # Variables globales pour stocker le dernier texte/sentiment
30
+ last_text = ""
31
+ last_sentiment = ""
32
 
33
+ # Fonction d'analyse de sentiment
34
  def analyze_sentiment(text):
35
+ global last_text, last_sentiment
36
+
37
  if not text:
38
+ return "Entrez une phrase.", "", ""
39
+
40
+ # Détection de la langue
41
  try:
 
42
  lang = detect(text)
43
  except:
44
  lang = "unknown"
45
 
46
+ # Traduire si nécessaire
47
  if lang != "en":
48
+ text = translator_to_en(text, max_length=512)[0]['translation_text']
49
 
50
+ # Analyse du sentiment
51
  result = classifier(text)[0]
52
+ sentiment_output = f"Sentiment : {result['label']} (Score: {result['score']:.2f})"
53
+
54
+ # Sauvegarder pour explication
55
+ last_text = text
56
+ last_sentiment = result['label']
57
+
58
+ return sentiment_output, "", ""
59
+
60
+ # Fonction pour générer explication en anglais + français
61
+ def generate_explanation():
62
+ if not last_text or not last_sentiment:
63
+ return "", "Aucune analyse récente trouvée."
64
+
65
+ # Préparer le prompt
66
+ prompt = f"Explain why the following financial news is {last_sentiment.lower()}: \"{last_text}\""
67
+ explanation_en = explainer(prompt, max_length=100)[0]['generated_text']
68
+
69
+ # Traduire en français
70
+ explanation_fr = translator_to_fr(explanation_en, max_length=512)[0]['translation_text']
71
+
72
+ return explanation_en, explanation_fr
73
+
74
+ # Interface Gradio
75
+ with gr.Blocks() as iface:
76
+ gr.Markdown("# 📈 Analyse de Sentiment Financier Multilingue + Explication 🌍")
77
+ gr.Markdown("Entrez une actualité financière. L'analyse détecte le sentiment et peut expliquer en anglais **et en français** !")
78
+
79
+ with gr.Row():
80
+ input_text = gr.Textbox(lines=4, placeholder="Entrez votre actualité financière ici...")
81
+
82
+ with gr.Row():
83
+ sentiment_output = gr.Textbox(label="Résultat du sentiment")
84
+
85
+ with gr.Row():
86
+ explanation_output_en = gr.Textbox(label="Explication (anglais)")
87
+ explanation_output_fr = gr.Textbox(label="Explication (français)")
88
+
89
+ analyze_btn = gr.Button("Analyser")
90
+ explain_btn = gr.Button("Générer explication")
91
+
92
+ analyze_btn.click(analyze_sentiment, inputs=[input_text], outputs=[sentiment_output, explanation_output_en, explanation_output_fr])
93
+ explain_btn.click(generate_explanation, outputs=[explanation_output_en, explanation_output_fr])
94
 
95
+ iface.launch()