Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -5,8 +5,10 @@ from huggingface_hub import InferenceClient
|
|
5 |
import pandas as pd
|
6 |
import os
|
7 |
import asyncio
|
|
|
8 |
|
9 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
|
|
10 |
|
11 |
# Fonction pour appeler l'API Zephyr avec des paramètres ajustés
|
12 |
async def call_zephyr_api(prompt, mode, hf_token=HF_TOKEN):
|
@@ -29,12 +31,19 @@ async def call_zephyr_api(prompt, mode, hf_token=HF_TOKEN):
|
|
29 |
# Chargement du modèle de sentiment pour analyser les réponses
|
30 |
classifier = pipeline("sentiment-analysis", model="mrm8488/distilroberta-finetuned-financial-news-sentiment-analysis")
|
31 |
|
32 |
-
# Modèles de traduction
|
33 |
translator_to_en = pipeline("translation", model="Helsinki-NLP/opus-mt-mul-en")
|
34 |
|
35 |
-
#
|
36 |
def safe_translate_to_fr(text, max_length=512):
|
37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
|
39 |
# Fonction pour suggérer le meilleur modèle
|
40 |
def suggest_model(text):
|
@@ -49,39 +58,30 @@ def suggest_model(text):
|
|
49 |
# Fonction pour créer une jauge de sentiment
|
50 |
def create_sentiment_gauge(sentiment, score):
|
51 |
score_percentage = score * 100
|
52 |
-
|
53 |
-
|
54 |
-
elif sentiment.lower() == "positive":
|
55 |
color = "#2E8B57"
|
56 |
elif sentiment.lower() == "negative":
|
57 |
color = "#DC143C"
|
58 |
-
else:
|
59 |
-
color = "#A9A9A9"
|
60 |
|
61 |
html = f"""
|
62 |
<div style='width: 100%; max-width: 300px; margin: 10px 0;'>
|
63 |
-
<div style='background-color: #D3D3D3; border-radius: 5px; height: 20px; position: relative;
|
64 |
-
<div style='background-color: {color}; width: {score_percentage}%; height: 100%; border-radius: 5px;
|
65 |
-
|
66 |
-
<span style='position: absolute; top: 0; left: 50%; transform: translateX(-50%); color: #0A1D37; font-size: 12px; line-height: 20px; font-weight: 600;'>
|
67 |
-
{score_percentage:.1f}%
|
68 |
-
</span>
|
69 |
-
</div>
|
70 |
-
<div style='text-align: center; font-size: 14px; margin-top: 5px; color: #E0E0E0;'>
|
71 |
-
Sentiment: {sentiment}
|
72 |
</div>
|
|
|
73 |
</div>
|
74 |
"""
|
75 |
return html
|
76 |
|
77 |
-
# Fonction d'analyse
|
78 |
async def full_analysis(text, mode, detail_mode, count, history):
|
79 |
if not text:
|
80 |
yield "Entrez une phrase.", "", "", "", 0, history, "", "Aucune analyse effectuée."
|
81 |
return
|
82 |
|
83 |
-
|
84 |
-
yield "Analyse en cours... (Étape 1 : Détection de la langue)", "", "", "", count, history, "", "Étape 1 : Détection de la langue"
|
85 |
|
86 |
try:
|
87 |
lang = detect(text)
|
@@ -93,17 +93,15 @@ async def full_analysis(text, mode, detail_mode, count, history):
|
|
93 |
else:
|
94 |
text_en = text
|
95 |
|
96 |
-
yield "Analyse en cours... (Étape 2 : Analyse du sentiment)", "", "", "", count, history, "", "
|
97 |
|
98 |
-
# Analyse du sentiment avec RoBERTa sur le texte d'entrée
|
99 |
result = await asyncio.to_thread(classifier, text_en)
|
100 |
result = result[0]
|
101 |
sentiment_output = f"Sentiment prédictif : {result['label']} (Score: {result['score']:.2f})"
|
102 |
sentiment_gauge = create_sentiment_gauge(result['label'], result['score'])
|
103 |
|
104 |
-
yield "Analyse en cours... (Étape 3 : Explication
|
105 |
|
106 |
-
# Appel à Zephyr pour expliquer l'impact basé sur le sentiment
|
107 |
explanation_prompt = f"""<|system|>
|
108 |
You are a professional financial analyst AI with expertise in economic forecasting.
|
109 |
</s>
|
@@ -112,14 +110,13 @@ Given the following question about a potential economic event: "{text}"
|
|
112 |
|
113 |
The predicted sentiment for this event is: {result['label'].lower()}.
|
114 |
|
115 |
-
Assume the event happens
|
116 |
</s>
|
117 |
<|assistant|>"""
|
118 |
explanation_en = await call_zephyr_api(explanation_prompt, mode)
|
119 |
|
120 |
-
yield "Analyse en cours... (Étape 4 : Traduction)", "", "", "", count, history, "", "
|
121 |
|
122 |
-
# Traduction (désactivée pour éviter l'erreur NLTK)
|
123 |
explanation_fr = safe_translate_to_fr(explanation_en)
|
124 |
|
125 |
count += 1
|
@@ -131,9 +128,9 @@ Assume the event happens (e.g., if the question is "Will the Federal Reserve rai
|
|
131 |
"Explication_FR": explanation_fr
|
132 |
})
|
133 |
|
134 |
-
yield sentiment_output, text, explanation_en, explanation_fr, count, history, sentiment_gauge, "Analyse terminée."
|
135 |
|
136 |
-
#
|
137 |
def download_history(history):
|
138 |
if not history:
|
139 |
return None
|
@@ -142,197 +139,62 @@ def download_history(history):
|
|
142 |
df.to_csv(file_path, index=False)
|
143 |
return file_path
|
144 |
|
145 |
-
#
|
146 |
def launch_app():
|
147 |
custom_css = """
|
148 |
-
|
149 |
-
|
150 |
body {
|
151 |
background: linear-gradient(135deg, #0A1D37 0%, #1A3C34 100%);
|
152 |
-
color: #E0E0E0;
|
153 |
font-family: 'Inter', sans-serif;
|
154 |
-
|
155 |
-
padding:
|
156 |
}
|
157 |
-
|
158 |
.gr-box {
|
159 |
background: #2A4A43 !important;
|
160 |
-
border-radius: 16px !important;
|
161 |
border: 1px solid #FFD700 !important;
|
162 |
-
|
163 |
-
padding:
|
164 |
-
|
165 |
-
transition: transform 0.2s ease, box-shadow 0.3s ease !important;
|
166 |
-
}
|
167 |
-
|
168 |
-
.gr-box:hover {
|
169 |
-
transform: translateY(-5px) !important;
|
170 |
-
box-shadow: 0 12px 24px rgba(255, 215, 0, 0.4) !important;
|
171 |
-
}
|
172 |
-
|
173 |
-
.gr-textbox, .gr-dropdown {
|
174 |
-
background: #3A5A52 !important;
|
175 |
-
border: 2px solid #FFD700 !important;
|
176 |
-
border-radius: 10px !important;
|
177 |
-
color: #E0E0E0 !important;
|
178 |
-
font-size: 18px !important;
|
179 |
-
padding: 15px !important;
|
180 |
-
transition: border-color 0.3s ease, box-shadow 0.3s ease !important;
|
181 |
-
}
|
182 |
-
|
183 |
-
.gr-textbox:focus, .gr-dropdown:focus {
|
184 |
-
border-color: #FFD700 !important;
|
185 |
-
box-shadow: 0 0 12px rgba(255, 215, 0, 0.5) !important;
|
186 |
}
|
187 |
-
|
188 |
.gr-button {
|
189 |
-
background: linear-gradient(90deg, #FFD700
|
190 |
-
color: #0A1D37
|
191 |
-
|
192 |
-
border
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
transition: transform 0.1s ease, box-shadow 0.3s ease !important;
|
197 |
-
box-shadow: 0 4px 12px rgba(255, 215, 0, 0.3) !important;
|
198 |
}
|
199 |
-
|
200 |
.gr-button:hover {
|
201 |
-
transform: translateY(-
|
202 |
-
box-shadow: 0
|
203 |
-
}
|
204 |
-
|
205 |
-
h1, h2, h3 {
|
206 |
-
color: #FFD700 !important;
|
207 |
-
font-weight: 700 !important;
|
208 |
-
text-shadow: 0 2px 4px rgba(0, 0, 0, 0.3) !important;
|
209 |
-
animation: fadeIn 1s ease-in-out;
|
210 |
-
}
|
211 |
-
|
212 |
-
@keyframes fadeIn {
|
213 |
-
from { opacity: 0; transform: translateY(-10px); }
|
214 |
-
to { opacity: 1; transform: translateY(0); }
|
215 |
-
}
|
216 |
-
|
217 |
-
.gr-row {
|
218 |
-
margin: 25px 0 !important;
|
219 |
-
}
|
220 |
-
|
221 |
-
.gr-column {
|
222 |
-
padding: 20px !important;
|
223 |
-
}
|
224 |
-
|
225 |
-
label {
|
226 |
-
color: #FFD700 !important;
|
227 |
-
font-weight: 600 !important;
|
228 |
-
font-size: 18px !important;
|
229 |
-
margin-bottom: 10px !important;
|
230 |
-
display: flex !important;
|
231 |
-
align-items: center !important;
|
232 |
-
}
|
233 |
-
|
234 |
-
label::before {
|
235 |
-
font-family: "Font Awesome 6 Free";
|
236 |
-
font-weight: 900;
|
237 |
-
margin-right: 10px;
|
238 |
-
}
|
239 |
-
|
240 |
-
.gr-textbox label::before {
|
241 |
-
content: '\\f201';
|
242 |
-
}
|
243 |
-
|
244 |
-
.gr-html label::before {
|
245 |
-
content: '\\f080';
|
246 |
-
}
|
247 |
-
|
248 |
-
.gr-file label::before {
|
249 |
-
content: '\\f019';
|
250 |
-
}
|
251 |
-
|
252 |
-
.economic-question-section {
|
253 |
-
background: rgba(26, 60, 52, 0.95) !important;
|
254 |
-
border-radius: 16px;
|
255 |
-
padding: 30px;
|
256 |
-
margin: 25px 0;
|
257 |
-
box-shadow: 0 8px 20px rgba(0, 0, 0, 0.5);
|
258 |
-
}
|
259 |
-
|
260 |
-
.economic-question-section .gr-textbox {
|
261 |
-
background: rgba(46, 74, 67, 0.85) !important;
|
262 |
-
border: 2px solid #FFD700 !important;
|
263 |
-
box-shadow: 0 4px 12px rgba(255, 215, 0, 0.3) !important;
|
264 |
-
font-size: 20px !important;
|
265 |
-
padding: 20px !important;
|
266 |
-
}
|
267 |
-
|
268 |
-
.economic-question-section .gr-textbox:focus {
|
269 |
-
border-color: #FFD700 !important;
|
270 |
-
box-shadow: 0 0 14px rgba(255, 215, 0, 0.6) !important;
|
271 |
-
}
|
272 |
-
|
273 |
-
.options-section {
|
274 |
-
display: flex;
|
275 |
-
flex-direction: column;
|
276 |
-
gap: 20px;
|
277 |
-
margin-top: 20px;
|
278 |
-
}
|
279 |
-
|
280 |
-
.options-section .gr-dropdown {
|
281 |
-
width: 220px !important;
|
282 |
-
}
|
283 |
-
|
284 |
-
.options-section .gr-dropdown label::before {
|
285 |
-
content: '\\f0c9';
|
286 |
-
}
|
287 |
-
|
288 |
-
.progress-message {
|
289 |
-
color: #FFD700 !important;
|
290 |
-
font-style: italic;
|
291 |
-
margin-bottom: 15px;
|
292 |
}
|
293 |
"""
|
294 |
|
295 |
with gr.Blocks(theme=gr.themes.Base(), css=custom_css) as iface:
|
296 |
-
gr.Markdown("# 📈 Analyse Financière Premium
|
297 |
-
gr.Markdown("Posez une question
|
298 |
|
299 |
count = gr.State(0)
|
300 |
history = gr.State([])
|
301 |
|
302 |
-
with gr.Row(
|
303 |
with gr.Column(scale=2):
|
304 |
-
|
305 |
-
|
306 |
-
|
307 |
-
|
308 |
-
)
|
309 |
-
with gr.Column(scale=1, elem_classes=["options-section"]):
|
310 |
-
mode_selector = gr.Dropdown(
|
311 |
-
choices=["Rapide", "Équilibré", "Précis"],
|
312 |
-
value="Équilibré",
|
313 |
-
label="Mode (longueur et style de réponse)"
|
314 |
-
)
|
315 |
-
detail_mode_selector = gr.Dropdown(
|
316 |
-
choices=["Normal", "Expert"],
|
317 |
-
value="Normal",
|
318 |
-
label="Niveau de détail (simplicité ou technicité)"
|
319 |
-
)
|
320 |
|
321 |
-
|
322 |
-
|
323 |
-
reset_graph_btn = gr.Button("Réinitialiser")
|
324 |
-
download_btn = gr.Button("Télécharger CSV")
|
325 |
|
326 |
with gr.Row():
|
327 |
-
|
328 |
-
|
329 |
-
|
330 |
-
|
331 |
-
|
332 |
-
|
333 |
-
with gr.Row():
|
334 |
-
explanation_output_en = gr.Textbox(label="Explication en Anglais")
|
335 |
-
explanation_output_fr = gr.Textbox(label="Explication en Français")
|
336 |
|
337 |
download_file = gr.File(label="Fichier CSV")
|
338 |
|
|
|
5 |
import pandas as pd
|
6 |
import os
|
7 |
import asyncio
|
8 |
+
import deepl
|
9 |
|
10 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
11 |
+
DEEPL_API_KEY = os.getenv("DEEPL_API_KEY") # Assurez-vous d'ajouter votre clé API DeepL dans les variables d'environnement
|
12 |
|
13 |
# Fonction pour appeler l'API Zephyr avec des paramètres ajustés
|
14 |
async def call_zephyr_api(prompt, mode, hf_token=HF_TOKEN):
|
|
|
31 |
# Chargement du modèle de sentiment pour analyser les réponses
|
32 |
classifier = pipeline("sentiment-analysis", model="mrm8488/distilroberta-finetuned-financial-news-sentiment-analysis")
|
33 |
|
34 |
+
# Modèles de traduction
|
35 |
translator_to_en = pipeline("translation", model="Helsinki-NLP/opus-mt-mul-en")
|
36 |
|
37 |
+
# Traduction en français avec DeepL
|
38 |
def safe_translate_to_fr(text, max_length=512):
|
39 |
+
if not DEEPL_API_KEY:
|
40 |
+
return "Erreur : Clé API DeepL manquante. Veuillez configurer DEEPL_API_KEY dans les variables d'environnement."
|
41 |
+
try:
|
42 |
+
translator = deepl.Translator(DEEPL_API_KEY)
|
43 |
+
result = translator.translate_text(text, target_lang="FR")
|
44 |
+
return result.text
|
45 |
+
except Exception as e:
|
46 |
+
return f"Erreur de traduction DeepL : {str(e)}"
|
47 |
|
48 |
# Fonction pour suggérer le meilleur modèle
|
49 |
def suggest_model(text):
|
|
|
58 |
# Fonction pour créer une jauge de sentiment
|
59 |
def create_sentiment_gauge(sentiment, score):
|
60 |
score_percentage = score * 100
|
61 |
+
color = "#A9A9A9"
|
62 |
+
if sentiment.lower() == "positive":
|
|
|
63 |
color = "#2E8B57"
|
64 |
elif sentiment.lower() == "negative":
|
65 |
color = "#DC143C"
|
|
|
|
|
66 |
|
67 |
html = f"""
|
68 |
<div style='width: 100%; max-width: 300px; margin: 10px 0;'>
|
69 |
+
<div style='background-color: #D3D3D3; border-radius: 5px; height: 20px; position: relative;'>
|
70 |
+
<div style='background-color: {color}; width: {score_percentage}%; height: 100%; border-radius: 5px;'></div>
|
71 |
+
<span style='position: absolute; top: 0; left: 50%; transform: translateX(-50%); font-weight: bold;'>{score_percentage:.1f}%</span>
|
|
|
|
|
|
|
|
|
|
|
|
|
72 |
</div>
|
73 |
+
<div style='text-align: center; margin-top: 5px;'>Sentiment : {sentiment}</div>
|
74 |
</div>
|
75 |
"""
|
76 |
return html
|
77 |
|
78 |
+
# Fonction d'analyse
|
79 |
async def full_analysis(text, mode, detail_mode, count, history):
|
80 |
if not text:
|
81 |
yield "Entrez une phrase.", "", "", "", 0, history, "", "Aucune analyse effectuée."
|
82 |
return
|
83 |
|
84 |
+
yield "Analyse en cours... (Étape 1 : Détection de la langue)", "", "", "", count, history, "", "Détection de la langue"
|
|
|
85 |
|
86 |
try:
|
87 |
lang = detect(text)
|
|
|
93 |
else:
|
94 |
text_en = text
|
95 |
|
96 |
+
yield "Analyse en cours... (Étape 2 : Analyse du sentiment)", "", "", "", count, history, "", "Analyse du sentiment"
|
97 |
|
|
|
98 |
result = await asyncio.to_thread(classifier, text_en)
|
99 |
result = result[0]
|
100 |
sentiment_output = f"Sentiment prédictif : {result['label']} (Score: {result['score']:.2f})"
|
101 |
sentiment_gauge = create_sentiment_gauge(result['label'], result['score'])
|
102 |
|
103 |
+
yield "Analyse en cours... (Étape 3 : Explication IA)", "", "", "", count, history, "", "Génération de l'explication"
|
104 |
|
|
|
105 |
explanation_prompt = f"""<|system|>
|
106 |
You are a professional financial analyst AI with expertise in economic forecasting.
|
107 |
</s>
|
|
|
110 |
|
111 |
The predicted sentiment for this event is: {result['label'].lower()}.
|
112 |
|
113 |
+
Assume the event happens. Explain why this event would likely have a {result['label'].lower()} economic impact.
|
114 |
</s>
|
115 |
<|assistant|>"""
|
116 |
explanation_en = await call_zephyr_api(explanation_prompt, mode)
|
117 |
|
118 |
+
yield "Analyse en cours... (Étape 4 : Traduction en français)", "", "", "", count, history, "", "Traduction en français"
|
119 |
|
|
|
120 |
explanation_fr = safe_translate_to_fr(explanation_en)
|
121 |
|
122 |
count += 1
|
|
|
128 |
"Explication_FR": explanation_fr
|
129 |
})
|
130 |
|
131 |
+
yield sentiment_output, text, explanation_en, explanation_fr, count, history, sentiment_gauge, "✅ Analyse terminée."
|
132 |
|
133 |
+
# Historique CSV
|
134 |
def download_history(history):
|
135 |
if not history:
|
136 |
return None
|
|
|
139 |
df.to_csv(file_path, index=False)
|
140 |
return file_path
|
141 |
|
142 |
+
# Lancement Gradio avec l'interface restaurée
|
143 |
def launch_app():
|
144 |
custom_css = """
|
145 |
+
/* CSS restauré à la version précédente, avant les changements esthétiques non demandés */
|
|
|
146 |
body {
|
147 |
background: linear-gradient(135deg, #0A1D37 0%, #1A3C34 100%);
|
|
|
148 |
font-family: 'Inter', sans-serif;
|
149 |
+
color: #E0E0E0;
|
150 |
+
padding: 20px;
|
151 |
}
|
|
|
152 |
.gr-box {
|
153 |
background: #2A4A43 !important;
|
|
|
154 |
border: 1px solid #FFD700 !important;
|
155 |
+
border-radius: 12px !important;
|
156 |
+
padding: 20px !important;
|
157 |
+
box-shadow: 0px 4px 12px rgba(255, 215, 0, 0.4);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
158 |
}
|
|
|
159 |
.gr-button {
|
160 |
+
background: linear-gradient(90deg, #FFD700, #D4AF37);
|
161 |
+
color: #0A1D37;
|
162 |
+
font-weight: bold;
|
163 |
+
border: none;
|
164 |
+
border-radius: 8px;
|
165 |
+
padding: 12px 24px;
|
166 |
+
transition: transform 0.2s;
|
|
|
|
|
167 |
}
|
|
|
168 |
.gr-button:hover {
|
169 |
+
transform: translateY(-2px);
|
170 |
+
box-shadow: 0 6px 12px rgba(255, 215, 0, 0.5);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
171 |
}
|
172 |
"""
|
173 |
|
174 |
with gr.Blocks(theme=gr.themes.Base(), css=custom_css) as iface:
|
175 |
+
gr.Markdown("# 📈 Analyse Financière Premium avec IA")
|
176 |
+
gr.Markdown("**Posez une question économique.** L'IA analyse et explique l'impact.")
|
177 |
|
178 |
count = gr.State(0)
|
179 |
history = gr.State([])
|
180 |
|
181 |
+
with gr.Row():
|
182 |
with gr.Column(scale=2):
|
183 |
+
input_text = gr.Textbox(lines=4, label="Votre question économique")
|
184 |
+
with gr.Column(scale=1):
|
185 |
+
mode_selector = gr.Dropdown(choices=["Rapide", "Équilibré", "Précis"], value="Équilibré", label="Mode de réponse")
|
186 |
+
detail_mode_selector = gr.Dropdown(choices=["Normal", "Expert"], value="Normal", label="Niveau de détail")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
187 |
|
188 |
+
analyze_btn = gr.Button("Analyser")
|
189 |
+
download_btn = gr.Button("Télécharger l'historique")
|
|
|
|
|
190 |
|
191 |
with gr.Row():
|
192 |
+
sentiment_output = gr.Textbox(label="Sentiment prédictif")
|
193 |
+
displayed_prompt = gr.Textbox(label="Votre question", interactive=False)
|
194 |
+
explanation_output_en = gr.Textbox(label="Explication en anglais")
|
195 |
+
explanation_output_fr = gr.Textbox(label="Explication en français")
|
196 |
+
sentiment_gauge = gr.HTML()
|
197 |
+
progress_message = gr.Textbox(label="Progression", interactive=False)
|
|
|
|
|
|
|
198 |
|
199 |
download_file = gr.File(label="Fichier CSV")
|
200 |
|