Update app.py
Browse files
app.py
CHANGED
@@ -20,6 +20,16 @@ if not HF_TOKEN:
|
|
20 |
st.error("Erreur : Clé API Hugging Face (HF_TOKEN) manquante. Veuillez configurer HF_TOKEN dans les variables d'environnement.")
|
21 |
st.stop()
|
22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
# Fonction pour appeler l'API Zephyr avec des paramètres ajustés
|
24 |
async def call_zephyr_api(prompt, mode, hf_token=HF_TOKEN):
|
25 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta", token=hf_token)
|
@@ -39,20 +49,13 @@ async def call_zephyr_api(prompt, mode, hf_token=HF_TOKEN):
|
|
39 |
st.error(f"❌ Erreur d'appel API Hugging Face : {str(e)}")
|
40 |
return None
|
41 |
|
42 |
-
# Chargement du modèle de sentiment (forcé sur CPU)
|
43 |
-
classifier = pipeline("sentiment-analysis", model="mrm8488/distilroberta-finetuned-financial-news-sentiment-analysis", device="cpu")
|
44 |
-
|
45 |
-
# Modèles de traduction
|
46 |
-
translator_to_en = pipeline("translation", model="Helsinki-NLP/opus-mt-mul-en")
|
47 |
-
translator_to_fr = pipeline("translation", model="Helsinki-NLP/opus-mt-en-fr")
|
48 |
-
|
49 |
# Traduction en français avec Helsinki-NLP
|
50 |
def safe_translate_to_fr(text, max_length=512):
|
51 |
try:
|
52 |
sentences = sent_tokenize(text)
|
53 |
translated_sentences = []
|
54 |
for sentence in sentences:
|
55 |
-
translated = translator_to_fr(sentence, max_length=max_length)[0]['translation_text']
|
56 |
translated_sentences.append(translated)
|
57 |
return " ".join(translated_sentences)
|
58 |
except Exception as e:
|
@@ -88,7 +91,7 @@ def create_sentiment_gauge(sentiment, score):
|
|
88 |
"""
|
89 |
return html
|
90 |
|
91 |
-
# Fonction d'analyse
|
92 |
async def full_analysis(text, mode, detail_mode, history):
|
93 |
if not text:
|
94 |
st.warning("Entrez une phrase.")
|
@@ -107,13 +110,13 @@ async def full_analysis(text, mode, detail_mode, history):
|
|
107 |
progress_bar.progress(25)
|
108 |
|
109 |
if lang != "en":
|
110 |
-
text_en = translator_to_en(text, max_length=512)[0]['translation_text']
|
111 |
else:
|
112 |
text_en = text
|
113 |
|
114 |
-
# Étape 2 : Analyse du sentiment
|
115 |
status_text.write("Analyse en cours... (Étape 2 : Analyse du sentiment)")
|
116 |
-
result = classifier(text_en) #
|
117 |
result = result[0]
|
118 |
sentiment_output = f"Sentiment prédictif : {result['label']} (Score: {result['score']:.2f})"
|
119 |
sentiment_gauge = create_sentiment_gauge(result['label'], result['score'])
|
|
|
20 |
st.error("Erreur : Clé API Hugging Face (HF_TOKEN) manquante. Veuillez configurer HF_TOKEN dans les variables d'environnement.")
|
21 |
st.stop()
|
22 |
|
23 |
+
# Initialisation des modèles dans st.session_state pour éviter le rechargement
|
24 |
+
if 'classifier' not in st.session_state:
|
25 |
+
st.session_state.classifier = pipeline("sentiment-analysis", model="mrm8488/distilroberta-finetuned-financial-news-sentiment-analysis", device="cpu")
|
26 |
+
|
27 |
+
if 'translator_to_en' not in st.session_state:
|
28 |
+
st.session_state.translator_to_en = pipeline("translation", model="Helsinki-NLP/opus-mt-mul-en", device="cpu")
|
29 |
+
|
30 |
+
if 'translator_to_fr' not in st.session_state:
|
31 |
+
st.session_state.translator_to_fr = pipeline("translation", model="Helsinki-NLP/opus-mt-en-fr", device="cpu")
|
32 |
+
|
33 |
# Fonction pour appeler l'API Zephyr avec des paramètres ajustés
|
34 |
async def call_zephyr_api(prompt, mode, hf_token=HF_TOKEN):
|
35 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta", token=hf_token)
|
|
|
49 |
st.error(f"❌ Erreur d'appel API Hugging Face : {str(e)}")
|
50 |
return None
|
51 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
# Traduction en français avec Helsinki-NLP
|
53 |
def safe_translate_to_fr(text, max_length=512):
|
54 |
try:
|
55 |
sentences = sent_tokenize(text)
|
56 |
translated_sentences = []
|
57 |
for sentence in sentences:
|
58 |
+
translated = st.session_state.translator_to_fr(sentence, max_length=max_length)[0]['translation_text']
|
59 |
translated_sentences.append(translated)
|
60 |
return " ".join(translated_sentences)
|
61 |
except Exception as e:
|
|
|
91 |
"""
|
92 |
return html
|
93 |
|
94 |
+
# Fonction d'analyse
|
95 |
async def full_analysis(text, mode, detail_mode, history):
|
96 |
if not text:
|
97 |
st.warning("Entrez une phrase.")
|
|
|
110 |
progress_bar.progress(25)
|
111 |
|
112 |
if lang != "en":
|
113 |
+
text_en = st.session_state.translator_to_en(text, max_length=512)[0]['translation_text']
|
114 |
else:
|
115 |
text_en = text
|
116 |
|
117 |
+
# Étape 2 : Analyse du sentiment
|
118 |
status_text.write("Analyse en cours... (Étape 2 : Analyse du sentiment)")
|
119 |
+
result = st.session_state.classifier(text_en) # Utilisation du modèle depuis st.session_state
|
120 |
result = result[0]
|
121 |
sentiment_output = f"Sentiment prédictif : {result['label']} (Score: {result['score']:.2f})"
|
122 |
sentiment_gauge = create_sentiment_gauge(result['label'], result['score'])
|