Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,77 +1,60 @@
|
|
|
|
1 |
from transformers import pipeline
|
2 |
from langdetect import detect
|
3 |
-
import gradio as gr
|
4 |
import pandas as pd
|
|
|
5 |
|
6 |
-
# Chargement modèle de sentiment
|
7 |
classifier = pipeline(
|
8 |
"sentiment-analysis",
|
9 |
model="mrm8488/distilroberta-finetuned-financial-news-sentiment-analysis"
|
10 |
)
|
11 |
|
12 |
# Modèles de traduction
|
13 |
-
translator_to_en = pipeline(
|
14 |
-
|
15 |
-
model="Helsinki-NLP/opus-mt-mul-en"
|
16 |
-
)
|
17 |
-
translator_to_fr = pipeline(
|
18 |
-
"translation",
|
19 |
-
model="Helsinki-NLP/opus-mt-en-fr"
|
20 |
-
)
|
21 |
-
|
22 |
-
# Modèles disponibles
|
23 |
-
MODEL_OPTIONS = {
|
24 |
-
"Flan-T5 Small (rapide)" : "google/flan-t5-small",
|
25 |
-
"Flan-T5 Base (équilibré)" : "google/flan-t5-base",
|
26 |
-
"Flan-T5 Large (précis)" : "google/flan-t5-large",
|
27 |
-
}
|
28 |
|
29 |
-
#
|
30 |
-
|
31 |
-
model_name = MODEL_OPTIONS.get(model_choice, "google/flan-t5-small")
|
32 |
-
return pipeline("text2text-generation", model=model_name)
|
33 |
|
34 |
# Fonction pour suggérer le meilleur modèle
|
35 |
def suggest_model(text):
|
36 |
word_count = len(text.split())
|
37 |
if word_count < 50:
|
38 |
-
|
39 |
elif word_count <= 200:
|
40 |
-
|
41 |
else:
|
42 |
-
|
43 |
-
return suggestion
|
44 |
|
45 |
-
# Fonction
|
46 |
-
def full_analysis(text,
|
47 |
if not text:
|
48 |
-
return "Entrez une phrase.", "", "",
|
49 |
|
50 |
-
# Charger modèle sélectionné
|
51 |
-
explainer = load_explainer(model_choice)
|
52 |
-
|
53 |
-
# Détection de langue
|
54 |
try:
|
55 |
lang = detect(text)
|
56 |
except:
|
57 |
lang = "unknown"
|
58 |
|
59 |
-
# Traduction si besoin
|
60 |
if lang != "en":
|
61 |
text = translator_to_en(text, max_length=512)[0]['translation_text']
|
62 |
|
63 |
-
# Analyse du sentiment
|
64 |
result = classifier(text)[0]
|
65 |
sentiment_output = f"Sentiment : {result['label']} (Score: {result['score']:.2f})"
|
66 |
|
67 |
-
|
68 |
-
|
69 |
-
|
|
|
|
|
|
|
70 |
|
71 |
-
|
72 |
explanation_fr = translator_to_fr(explanation_en, max_length=512)[0]['translation_text']
|
73 |
|
74 |
-
|
|
|
75 |
count += 1
|
76 |
history.append({
|
77 |
"Texte": text,
|
@@ -79,12 +62,12 @@ def full_analysis(text, model_choice, count, history):
|
|
79 |
"Score": f"{result['score']:.2f}",
|
80 |
"Explication_EN": explanation_en,
|
81 |
"Explication_FR": explanation_fr,
|
82 |
-
"
|
83 |
})
|
84 |
|
85 |
-
return sentiment_output, explanation_en, explanation_fr, count, history, None
|
86 |
|
87 |
-
# Fonction pour
|
88 |
def download_history(history):
|
89 |
if not history:
|
90 |
return None
|
@@ -93,76 +76,61 @@ def download_history(history):
|
|
93 |
df.to_csv(file_path, index=False)
|
94 |
return file_path
|
95 |
|
96 |
-
# Interface
|
97 |
-
|
98 |
-
gr.
|
99 |
-
|
|
|
|
|
|
|
|
|
|
|
100 |
|
101 |
-
|
102 |
-
|
103 |
|
104 |
-
|
105 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
106 |
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
112 |
)
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
with gr.Column():
|
125 |
-
explanation_output_en = gr.Textbox(label="Explication en Anglais")
|
126 |
-
with gr.Column():
|
127 |
-
explanation_output_fr = gr.Textbox(label="Explication en Français")
|
128 |
-
|
129 |
-
with gr.Row():
|
130 |
-
analysis_count_display = gr.Textbox(label="Nombre total d'analyses", interactive=False)
|
131 |
-
|
132 |
-
download_file = gr.File(label="Téléchargement du CSV")
|
133 |
-
|
134 |
-
# Suggestions dynamiques du modèle
|
135 |
-
input_text.change(
|
136 |
-
lambda text: gr.update(value=suggest_model(text)),
|
137 |
-
inputs=[input_text],
|
138 |
-
outputs=[model_selector]
|
139 |
-
).then(
|
140 |
-
lambda text: gr.update(value=f"💡 Suggestion IA : {suggest_model(text)}"),
|
141 |
-
inputs=[input_text],
|
142 |
-
outputs=[model_suggestion]
|
143 |
-
)
|
144 |
-
|
145 |
-
# Actions boutons
|
146 |
-
analyze_btn.click(
|
147 |
-
lambda: gr.update(visible=True),
|
148 |
-
outputs=[loading_text]
|
149 |
-
).then(
|
150 |
-
full_analysis,
|
151 |
-
inputs=[input_text, model_selector, count, history],
|
152 |
-
outputs=[sentiment_output, explanation_output_en, explanation_output_fr, count, history, download_file]
|
153 |
-
).then(
|
154 |
-
lambda c: gr.update(value=f"{c} analyses réalisées"),
|
155 |
-
inputs=[count],
|
156 |
-
outputs=[analysis_count_display]
|
157 |
-
).then(
|
158 |
-
lambda: gr.update(visible=False),
|
159 |
-
outputs=[loading_text]
|
160 |
-
)
|
161 |
-
|
162 |
-
download_btn.click(
|
163 |
-
download_history,
|
164 |
-
inputs=[history],
|
165 |
-
outputs=[download_file]
|
166 |
-
)
|
167 |
-
|
168 |
-
iface.launch()
|
|
|
1 |
+
import gradio as gr
|
2 |
from transformers import pipeline
|
3 |
from langdetect import detect
|
|
|
4 |
import pandas as pd
|
5 |
+
import textstat
|
6 |
|
7 |
+
# Chargement du modèle de sentiment
|
8 |
classifier = pipeline(
|
9 |
"sentiment-analysis",
|
10 |
model="mrm8488/distilroberta-finetuned-financial-news-sentiment-analysis"
|
11 |
)
|
12 |
|
13 |
# Modèles de traduction
|
14 |
+
translator_to_en = pipeline("translation", model="Helsinki-NLP/opus-mt-mul-en")
|
15 |
+
translator_to_fr = pipeline("translation", model="Helsinki-NLP/opus-mt-en-fr")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
+
# Modèle explicatif CPU-friendly
|
18 |
+
explainer = pipeline("text2text-generation", model="facebook/blenderbot-1B-distill")
|
|
|
|
|
19 |
|
20 |
# Fonction pour suggérer le meilleur modèle
|
21 |
def suggest_model(text):
|
22 |
word_count = len(text.split())
|
23 |
if word_count < 50:
|
24 |
+
return "Rapide"
|
25 |
elif word_count <= 200:
|
26 |
+
return "Équilibré"
|
27 |
else:
|
28 |
+
return "Précis"
|
|
|
29 |
|
30 |
+
# Fonction d'analyse
|
31 |
+
def full_analysis(text, mode, detail_mode, count, history):
|
32 |
if not text:
|
33 |
+
return "Entrez une phrase.", "", "", 0, history, None
|
34 |
|
|
|
|
|
|
|
|
|
35 |
try:
|
36 |
lang = detect(text)
|
37 |
except:
|
38 |
lang = "unknown"
|
39 |
|
|
|
40 |
if lang != "en":
|
41 |
text = translator_to_en(text, max_length=512)[0]['translation_text']
|
42 |
|
|
|
43 |
result = classifier(text)[0]
|
44 |
sentiment_output = f"Sentiment : {result['label']} (Score: {result['score']:.2f})"
|
45 |
|
46 |
+
prompt = f"""
|
47 |
+
You are a financial analyst AI.
|
48 |
+
Based on the following financial news: \"{text}\",
|
49 |
+
explain clearly why the sentiment is {result['label'].lower()}.
|
50 |
+
{"Write a concise paragraph." if detail_mode == "Normal" else "Write a detailed explanation over multiple paragraphs."}
|
51 |
+
"""
|
52 |
|
53 |
+
explanation_en = explainer(prompt, max_length=300 if detail_mode == "Expert" else 150)[0]['generated_text']
|
54 |
explanation_fr = translator_to_fr(explanation_en, max_length=512)[0]['translation_text']
|
55 |
|
56 |
+
clarity_score = textstat.flesch_reading_ease(explanation_en)
|
57 |
+
|
58 |
count += 1
|
59 |
history.append({
|
60 |
"Texte": text,
|
|
|
62 |
"Score": f"{result['score']:.2f}",
|
63 |
"Explication_EN": explanation_en,
|
64 |
"Explication_FR": explanation_fr,
|
65 |
+
"Clarté": f"{clarity_score:.1f}"
|
66 |
})
|
67 |
|
68 |
+
return sentiment_output, explanation_en, explanation_fr, clarity_score, count, history, None
|
69 |
|
70 |
+
# Fonction pour télécharger historique CSV
|
71 |
def download_history(history):
|
72 |
if not history:
|
73 |
return None
|
|
|
76 |
df.to_csv(file_path, index=False)
|
77 |
return file_path
|
78 |
|
79 |
+
# Interface Gradio
|
80 |
+
def launch_app():
|
81 |
+
with gr.Blocks(theme=gr.themes.Base(), css="body {background-color: #0D1117; color: white;} .gr-button {background-color: #161B22; border: 1px solid #30363D;}") as iface:
|
82 |
+
|
83 |
+
gr.Markdown("# 📈 Analyse Financière Premium + Explication IA", elem_id="title")
|
84 |
+
gr.Markdown("Entrez une actualité financière. L'IA analyse et explique en anglais/français. Choisissez votre mode d'explication.")
|
85 |
+
|
86 |
+
count = gr.State(0)
|
87 |
+
history = gr.State([])
|
88 |
|
89 |
+
with gr.Row():
|
90 |
+
input_text = gr.Textbox(lines=4, placeholder="Entrez une actualité ici...", label="Texte à analyser")
|
91 |
|
92 |
+
with gr.Row():
|
93 |
+
mode_selector = gr.Dropdown(
|
94 |
+
choices=["Rapide", "Équilibré", "Précis"],
|
95 |
+
value="Équilibré",
|
96 |
+
label="Mode recommandé selon la taille"
|
97 |
+
)
|
98 |
+
detail_mode_selector = gr.Dropdown(
|
99 |
+
choices=["Normal", "Expert"],
|
100 |
+
value="Normal",
|
101 |
+
label="Niveau de détail"
|
102 |
+
)
|
103 |
|
104 |
+
analyze_btn = gr.Button("Analyser")
|
105 |
+
download_btn = gr.Button("Télécharger CSV")
|
106 |
+
|
107 |
+
with gr.Row():
|
108 |
+
sentiment_output = gr.Textbox(label="Résultat du Sentiment")
|
109 |
+
|
110 |
+
with gr.Row():
|
111 |
+
with gr.Column():
|
112 |
+
explanation_output_en = gr.Textbox(label="Explication en Anglais")
|
113 |
+
with gr.Column():
|
114 |
+
explanation_output_fr = gr.Textbox(label="Explication en Français")
|
115 |
+
|
116 |
+
clarity_score_output = gr.Textbox(label="Score de Clarté (Flesch Reading Ease)")
|
117 |
+
download_file = gr.File(label="Fichier CSV")
|
118 |
+
|
119 |
+
input_text.change(lambda t: gr.update(value=suggest_model(t)), inputs=[input_text], outputs=[mode_selector])
|
120 |
+
|
121 |
+
analyze_btn.click(
|
122 |
+
full_analysis,
|
123 |
+
inputs=[input_text, mode_selector, detail_mode_selector, count, history],
|
124 |
+
outputs=[sentiment_output, explanation_output_en, explanation_output_fr, clarity_score_output, count, history, download_file]
|
125 |
)
|
126 |
+
|
127 |
+
download_btn.click(
|
128 |
+
download_history,
|
129 |
+
inputs=[history],
|
130 |
+
outputs=[download_file]
|
131 |
+
)
|
132 |
+
|
133 |
+
iface.launch()
|
134 |
+
|
135 |
+
if __name__ == "__main__":
|
136 |
+
launch_app()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|