Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -3,21 +3,15 @@ import requests
|
|
3 |
import pandas as pd
|
4 |
import textstat
|
5 |
import os
|
6 |
-
from transformers import pipeline
|
7 |
|
8 |
-
#
|
9 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
10 |
|
11 |
-
#
|
12 |
def call_zephyr_api(prompt, hf_token=HF_TOKEN):
|
13 |
API_URL = "https://api-inference.huggingface.co/models/HuggingFaceH4/zephyr-7b-beta"
|
14 |
headers = {"Authorization": f"Bearer {hf_token}"}
|
15 |
-
payload = {
|
16 |
-
"inputs": prompt,
|
17 |
-
"parameters": {
|
18 |
-
"max_new_tokens": 300
|
19 |
-
}
|
20 |
-
}
|
21 |
|
22 |
try:
|
23 |
response = requests.post(API_URL, headers=headers, json=payload, timeout=60)
|
@@ -26,33 +20,46 @@ def call_zephyr_api(prompt, hf_token=HF_TOKEN):
|
|
26 |
except Exception as e:
|
27 |
raise gr.Error(f"❌ Erreur d'appel API Hugging Face : {str(e)}")
|
28 |
|
29 |
-
# Chargement du pipeline de sentiment
|
30 |
-
classifier = pipeline("sentiment-analysis", model="mrm8488/distilroberta-finetuned-financial-news-sentiment-analysis")
|
31 |
-
|
32 |
# Fonction principale d'analyse
|
33 |
def full_analysis(text, history):
|
34 |
if not text:
|
35 |
-
return "Entrez une phrase.", "", 0, history
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
|
37 |
-
|
38 |
-
|
39 |
-
detected_sentiment =
|
40 |
|
41 |
-
|
|
|
|
|
|
|
42 |
prompt_explanation = f"""
|
43 |
You are a financial analyst AI.
|
44 |
-
|
|
|
45 |
\"{text}\"
|
46 |
-
|
|
|
|
|
|
|
47 |
Write a concise paragraph.
|
48 |
"""
|
49 |
explanation = call_zephyr_api(prompt_explanation)
|
50 |
|
51 |
-
# 3.
|
52 |
clarity_score = textstat.flesch_reading_ease(explanation)
|
53 |
-
clarity_score = max(0, min(clarity_score, 100)) # Limité 0-100
|
54 |
|
55 |
-
# 4.
|
56 |
history.append({
|
57 |
"Texte": text,
|
58 |
"Sentiment": detected_sentiment.capitalize(),
|
@@ -60,9 +67,9 @@ Write a concise paragraph.
|
|
60 |
"Explication": explanation
|
61 |
})
|
62 |
|
63 |
-
return detected_sentiment.capitalize(), explanation, int(clarity_score)
|
64 |
|
65 |
-
# Fonction pour télécharger historique
|
66 |
def download_history(history):
|
67 |
if not history:
|
68 |
return None
|
@@ -71,10 +78,10 @@ def download_history(history):
|
|
71 |
df.to_csv(file_path, index=False)
|
72 |
return file_path
|
73 |
|
74 |
-
# Interface
|
75 |
def launch_app():
|
76 |
with gr.Blocks() as iface:
|
77 |
-
gr.Markdown("# 📈 Analyse Financière Premium - Zephyr7B
|
78 |
|
79 |
with gr.Row():
|
80 |
input_text = gr.Textbox(label="Entrez votre question financière", lines=3)
|
@@ -85,7 +92,8 @@ def launch_app():
|
|
85 |
|
86 |
sentiment_output = gr.Textbox(label="Sentiment Détecté")
|
87 |
explanation_output = gr.Textbox(label="Explication de l'IA", lines=5)
|
88 |
-
|
|
|
89 |
file_output = gr.File(label="Fichier CSV")
|
90 |
|
91 |
history = gr.State([])
|
@@ -93,7 +101,7 @@ def launch_app():
|
|
93 |
analyze_btn.click(
|
94 |
full_analysis,
|
95 |
inputs=[input_text, history],
|
96 |
-
outputs=[sentiment_output, explanation_output,
|
97 |
)
|
98 |
|
99 |
download_btn.click(
|
@@ -106,4 +114,3 @@ def launch_app():
|
|
106 |
|
107 |
if __name__ == "__main__":
|
108 |
launch_app()
|
109 |
-
|
|
|
3 |
import pandas as pd
|
4 |
import textstat
|
5 |
import os
|
|
|
6 |
|
7 |
+
# Récupération du token Hugging Face
|
8 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
9 |
|
10 |
+
# Fonction pour appeler l'API Zephyr-7B
|
11 |
def call_zephyr_api(prompt, hf_token=HF_TOKEN):
|
12 |
API_URL = "https://api-inference.huggingface.co/models/HuggingFaceH4/zephyr-7b-beta"
|
13 |
headers = {"Authorization": f"Bearer {hf_token}"}
|
14 |
+
payload = {"inputs": prompt}
|
|
|
|
|
|
|
|
|
|
|
15 |
|
16 |
try:
|
17 |
response = requests.post(API_URL, headers=headers, json=payload, timeout=60)
|
|
|
20 |
except Exception as e:
|
21 |
raise gr.Error(f"❌ Erreur d'appel API Hugging Face : {str(e)}")
|
22 |
|
|
|
|
|
|
|
23 |
# Fonction principale d'analyse
|
24 |
def full_analysis(text, history):
|
25 |
if not text:
|
26 |
+
return "Entrez une phrase.", "", 0, history, 0
|
27 |
+
|
28 |
+
# 1. Demander à Zephyr de détecter le sentiment
|
29 |
+
prompt_sentiment = f"""
|
30 |
+
You are a financial news sentiment detector.
|
31 |
+
|
32 |
+
Given the following news text:
|
33 |
+
\"{text}\"
|
34 |
+
|
35 |
+
Respond only with one word: positive, neutral, or negative.
|
36 |
|
37 |
+
Do not add any explanation or extra text.
|
38 |
+
"""
|
39 |
+
detected_sentiment = call_zephyr_api(prompt_sentiment).lower()
|
40 |
|
41 |
+
if detected_sentiment not in ["positive", "neutral", "negative"]:
|
42 |
+
detected_sentiment = "neutral"
|
43 |
+
|
44 |
+
# 2. Demander à Zephyr d'expliquer
|
45 |
prompt_explanation = f"""
|
46 |
You are a financial analyst AI.
|
47 |
+
|
48 |
+
Given the following financial news:
|
49 |
\"{text}\"
|
50 |
+
|
51 |
+
The detected sentiment is: {detected_sentiment}.
|
52 |
+
|
53 |
+
Now explain clearly why the sentiment is {detected_sentiment}.
|
54 |
Write a concise paragraph.
|
55 |
"""
|
56 |
explanation = call_zephyr_api(prompt_explanation)
|
57 |
|
58 |
+
# 3. Calculer la clarté
|
59 |
clarity_score = textstat.flesch_reading_ease(explanation)
|
60 |
+
clarity_score = max(0, min(clarity_score, 100)) # Limité entre 0-100
|
61 |
|
62 |
+
# 4. Sauvegarder dans l'historique
|
63 |
history.append({
|
64 |
"Texte": text,
|
65 |
"Sentiment": detected_sentiment.capitalize(),
|
|
|
67 |
"Explication": explanation
|
68 |
})
|
69 |
|
70 |
+
return detected_sentiment.capitalize(), explanation, clarity_score, history, int(clarity_score)
|
71 |
|
72 |
+
# Fonction pour télécharger l'historique
|
73 |
def download_history(history):
|
74 |
if not history:
|
75 |
return None
|
|
|
78 |
df.to_csv(file_path, index=False)
|
79 |
return file_path
|
80 |
|
81 |
+
# Gradio Interface
|
82 |
def launch_app():
|
83 |
with gr.Blocks() as iface:
|
84 |
+
gr.Markdown("# 📈 Analyse Financière Premium - Zephyr7B")
|
85 |
|
86 |
with gr.Row():
|
87 |
input_text = gr.Textbox(label="Entrez votre question financière", lines=3)
|
|
|
92 |
|
93 |
sentiment_output = gr.Textbox(label="Sentiment Détecté")
|
94 |
explanation_output = gr.Textbox(label="Explication de l'IA", lines=5)
|
95 |
+
clarity_score_text = gr.Textbox(label="Score de Clarté (%)")
|
96 |
+
clarity_slider = gr.Slider(0, 100, label="Clarté (%)", interactive=False)
|
97 |
file_output = gr.File(label="Fichier CSV")
|
98 |
|
99 |
history = gr.State([])
|
|
|
101 |
analyze_btn.click(
|
102 |
full_analysis,
|
103 |
inputs=[input_text, history],
|
104 |
+
outputs=[sentiment_output, explanation_output, clarity_score_text, history, clarity_slider]
|
105 |
)
|
106 |
|
107 |
download_btn.click(
|
|
|
114 |
|
115 |
if __name__ == "__main__":
|
116 |
launch_app()
|
|