Spaces:
Sleeping
Sleeping
File size: 6,044 Bytes
ffb571b e29f507 14aa24c d179496 4504964 ffb571b e29f507 ec011c8 e29f507 14aa24c ec011c8 ffb571b e29f507 14aa24c 4504964 ffb571b 5b0e9a8 ffb571b 5b0e9a8 ffb571b 5b0e9a8 ffb571b 5b0e9a8 e29f507 ffb571b e29f507 14aa24c e29f507 4504964 d179496 850981e d179496 14aa24c 850981e ffb571b 850981e e29f507 850981e ffb571b e29f507 ffb571b 4504964 ffb571b 4504964 e29f507 4504964 ffb571b 4504964 850981e ffb571b e29f507 850981e ffb571b 850981e ffb571b 5b0e9a8 ffb571b e29f507 ffb571b e29f507 ffb571b e29f507 4504964 ffb571b |
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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 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 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
import gradio as gr
import requests
from transformers import pipeline
from langdetect import detect
import pandas as pd
import textstat
import matplotlib.pyplot as plt
import os
HF_TOKEN = os.getenv("HF_TOKEN")
# Fonction pour appeler l'API Mistral-7B
def call_mistral_api(prompt, hf_token=HF_TOKEN):
API_URL = "https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.2"
headers = {"Authorization": f"Bearer {hf_token}"}
payload = {"inputs": prompt, "parameters": {"max_new_tokens": 300}}
response = requests.post(API_URL, headers=headers, json=payload, timeout=60)
response.raise_for_status()
return response.json()[0]["generated_text"]
# Chargement du modèle de sentiment
classifier = pipeline("sentiment-analysis", model="mrm8488/distilroberta-finetuned-financial-news-sentiment-analysis")
# Modèles de traduction
translator_to_en = pipeline("translation", model="Helsinki-NLP/opus-mt-mul-en")
translator_to_fr = pipeline("translation", model="Helsinki-NLP/opus-mt-en-fr")
# Fonction pour suggérer le meilleur modèle
def suggest_model(text):
word_count = len(text.split())
if word_count < 50:
return "Rapide"
elif word_count <= 200:
return "Équilibré"
else:
return "Précis"
# Fonction pour générer un graphique de clarté
def plot_clarity(clarity_scores):
plt.figure(figsize=(8, 4))
plt.plot(range(1, len(clarity_scores) + 1), clarity_scores, marker='o')
plt.title("Évolution du Score de Clarté")
plt.xlabel("Numéro d'analyse")
plt.ylabel("Score de Clarté")
plt.ylim(0, 100)
plt.grid(True)
return plt.gcf()
# Fonction pour reset le graphique
def reset_clarity_graph():
return [], plot_clarity([])
# Fonction d'analyse
def full_analysis(text, mode, detail_mode, count, history, clarity_scores):
if not text:
return "Entrez une phrase.", "", "", 0, history, clarity_scores, None, None
try:
lang = detect(text)
except:
lang = "unknown"
if lang != "en":
text = translator_to_en(text, max_length=512)[0]['translation_text']
result = classifier(text)[0]
sentiment_output = f"Sentiment : {result['label']} (Score: {result['score']:.2f})"
prompt = f"""
You are a financial analyst AI.
Based on the following financial news: \"{text}\",
explain clearly why the sentiment is {result['label'].lower()}.
{"Write a concise paragraph." if detail_mode == "Normal" else "Write a detailed explanation over multiple paragraphs."}
"""
explanation_en = call_mistral_api(prompt)
explanation_fr = translator_to_fr(explanation_en, max_length=512)[0]['translation_text']
clarity_score = textstat.flesch_reading_ease(explanation_en)
clarity_scores.append(clarity_score)
count += 1
history.append({
"Texte": text,
"Sentiment": result['label'],
"Score": f"{result['score']:.2f}",
"Explication_EN": explanation_en,
"Explication_FR": explanation_fr,
"Clarté": f"{clarity_score:.1f}"
})
return sentiment_output, explanation_en, explanation_fr, clarity_score, count, history, clarity_scores, plot_clarity(clarity_scores)
# Fonction pour télécharger historique CSV
def download_history(history):
if not history:
return None
df = pd.DataFrame(history)
file_path = "/tmp/analysis_history.csv"
df.to_csv(file_path, index=False)
return file_path
# Interface Gradio
def launch_app():
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:
gr.Markdown("# 📈 Analyse Financière Premium + Explication IA", elem_id="title")
gr.Markdown("Entrez une actualité financière. L'IA analyse et explique en anglais/français. Choisissez votre mode d'explication.")
count = gr.State(0)
history = gr.State([])
clarity_scores = gr.State([])
with gr.Row():
input_text = gr.Textbox(lines=4, placeholder="Entrez une actualité ici...", label="Texte à analyser")
with gr.Row():
mode_selector = gr.Dropdown(
choices=["Rapide", "Équilibré", "Précis"],
value="Équilibré",
label="Mode recommandé selon la taille"
)
detail_mode_selector = gr.Dropdown(
choices=["Normal", "Expert"],
value="Normal",
label="Niveau de détail"
)
analyze_btn = gr.Button("Analyser")
reset_graph_btn = gr.Button("Reset Graphique")
download_btn = gr.Button("Télécharger CSV")
with gr.Row():
sentiment_output = gr.Textbox(label="Résultat du Sentiment")
with gr.Row():
with gr.Column():
explanation_output_en = gr.Textbox(label="Explication en Anglais")
with gr.Column():
explanation_output_fr = gr.Textbox(label="Explication en Français")
clarity_score_output = gr.Textbox(label="Score de Clarté (Flesch Reading Ease)")
clarity_plot = gr.Plot(label="Graphique des Scores de Clarté")
download_file = gr.File(label="Fichier CSV")
input_text.change(lambda t: gr.update(value=suggest_model(t)), inputs=[input_text], outputs=[mode_selector])
analyze_btn.click(
full_analysis,
inputs=[input_text, mode_selector, detail_mode_selector, count, history, clarity_scores],
outputs=[sentiment_output, explanation_output_en, explanation_output_fr, clarity_score_output, count, history, clarity_scores, clarity_plot]
)
reset_graph_btn.click(
reset_clarity_graph,
outputs=[clarity_scores, clarity_plot]
)
download_btn.click(
download_history,
inputs=[history],
outputs=[download_file]
)
iface.launch()
if __name__ == "__main__":
launch_app()
|