Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,57 +2,70 @@ import streamlit as st
|
|
2 |
from transformers import pipeline
|
3 |
from langdetect import detect
|
4 |
import pandas as pd
|
|
|
5 |
|
6 |
-
#
|
7 |
st.set_page_config(
|
8 |
-
page_title="🌍
|
9 |
page_icon="🎯",
|
10 |
layout="centered",
|
11 |
)
|
12 |
|
13 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
st.markdown(
|
15 |
-
"""
|
16 |
<style>
|
17 |
-
.stApp {
|
18 |
-
background-color:
|
19 |
-
}
|
|
|
|
|
|
|
|
|
|
|
20 |
</style>
|
21 |
""",
|
22 |
unsafe_allow_html=True
|
23 |
)
|
24 |
|
25 |
-
#
|
26 |
-
def sentiment_color(score):
|
27 |
-
"""Retourne une couleur hex en fonction du score (0=rouge, 1=vert)"""
|
28 |
-
red = int(255 * (1 - score))
|
29 |
-
green = int(255 * score)
|
30 |
-
return f'rgb({red},{green},0)'
|
31 |
-
|
32 |
-
# Charger modèle
|
33 |
@st.cache_resource
|
34 |
def load_model():
|
35 |
return pipeline("text-classification", model="tabularisai/multilingual-sentiment-analysis")
|
36 |
|
37 |
classifier = load_model()
|
38 |
|
39 |
-
#
|
40 |
-
|
41 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
|
43 |
-
#
|
44 |
-
|
|
|
|
|
|
|
45 |
|
46 |
if st.button("🔎 Analyser"):
|
47 |
if not user_input.strip():
|
48 |
-
st.warning("⚠️ Merci d'entrer au moins une phrase.")
|
49 |
else:
|
50 |
phrases = [phrase.strip() for phrase in user_input.split(';') if phrase.strip()]
|
51 |
-
|
52 |
st.info(f"Nombre de phrases détectées : {len(phrases)}")
|
53 |
results = []
|
54 |
|
55 |
-
with st.spinner("Analyse en cours..."):
|
56 |
for phrase in phrases:
|
57 |
try:
|
58 |
lang = detect(phrase)
|
@@ -63,23 +76,26 @@ if st.button("🔎 Analyser"):
|
|
63 |
sentiment = analysis["label"]
|
64 |
score = round(analysis["score"], 2)
|
65 |
|
66 |
-
|
67 |
"Texte": phrase,
|
68 |
"Langue": lang,
|
69 |
"Sentiment": sentiment,
|
70 |
"Score": score
|
71 |
-
}
|
|
|
|
|
72 |
|
73 |
-
#
|
74 |
if "negative" in sentiment.lower():
|
|
|
75 |
st.error(f"😞 Texte : {phrase}")
|
76 |
-
st.markdown('<div style="position: fixed;top: 0;left: 0;width: 100%;height: 100%;background: rgba(255,0,0,0.1);z-index: 9999;"></div>', unsafe_allow_html=True)
|
77 |
elif "positive" in sentiment.lower():
|
|
|
78 |
st.success(f"😊 Texte : {phrase}")
|
79 |
else:
|
80 |
st.warning(f"😐 Texte : {phrase}")
|
81 |
|
82 |
-
# Barre
|
83 |
color = sentiment_color(score)
|
84 |
st.markdown(
|
85 |
f"""
|
@@ -91,12 +107,26 @@ if st.button("🔎 Analyser"):
|
|
91 |
unsafe_allow_html=True
|
92 |
)
|
93 |
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
101 |
st.markdown("---")
|
102 |
-
st.markdown("🔗 [Voir le modèle
|
|
|
2 |
from transformers import pipeline
|
3 |
from langdetect import detect
|
4 |
import pandas as pd
|
5 |
+
import io
|
6 |
|
7 |
+
# --- Configuration de la page ---
|
8 |
st.set_page_config(
|
9 |
+
page_title="🌍 Analyseur de Sentiment Multilingue V4",
|
10 |
page_icon="🎯",
|
11 |
layout="centered",
|
12 |
)
|
13 |
|
14 |
+
# --- Mode Dark/Light ---
|
15 |
+
theme = st.sidebar.selectbox("🎨 Choisissez le thème :", ["Clair", "Sombre"])
|
16 |
+
if theme == "Clair":
|
17 |
+
background_color = "#f0f2f6"
|
18 |
+
else:
|
19 |
+
background_color = "#222222"
|
20 |
+
|
21 |
st.markdown(
|
22 |
+
f"""
|
23 |
<style>
|
24 |
+
.stApp {{
|
25 |
+
background-color: {background_color};
|
26 |
+
}}
|
27 |
+
textarea {{
|
28 |
+
border: 2px solid #5DADE2 !important;
|
29 |
+
border-radius: 10px !important;
|
30 |
+
background-color: #ffffff !important;
|
31 |
+
}}
|
32 |
</style>
|
33 |
""",
|
34 |
unsafe_allow_html=True
|
35 |
)
|
36 |
|
37 |
+
# --- Charger le modèle ---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
@st.cache_resource
|
39 |
def load_model():
|
40 |
return pipeline("text-classification", model="tabularisai/multilingual-sentiment-analysis")
|
41 |
|
42 |
classifier = load_model()
|
43 |
|
44 |
+
# --- Initialiser session_state pour historique ---
|
45 |
+
if 'history' not in st.session_state:
|
46 |
+
st.session_state.history = []
|
47 |
+
|
48 |
+
# --- Fonction pour colorer les barres ---
|
49 |
+
def sentiment_color(score):
|
50 |
+
red = int(255 * (1 - score))
|
51 |
+
green = int(255 * score)
|
52 |
+
return f'rgb({red},{green},0)'
|
53 |
|
54 |
+
# --- Interface principale ---
|
55 |
+
st.title("🎯 Analyseur de Sentiment Multilingue - V4")
|
56 |
+
st.write("Analysez vos textes avec détection automatique de langue, visualisation dynamique, historique et téléchargement. 🚀")
|
57 |
+
|
58 |
+
user_input = st.text_area("✍️ Entrez vos phrases séparées par un point-virgule ';'", height=180)
|
59 |
|
60 |
if st.button("🔎 Analyser"):
|
61 |
if not user_input.strip():
|
62 |
+
st.warning("⚠️ Merci d'entrer au moins une phrase complète.")
|
63 |
else:
|
64 |
phrases = [phrase.strip() for phrase in user_input.split(';') if phrase.strip()]
|
|
|
65 |
st.info(f"Nombre de phrases détectées : {len(phrases)}")
|
66 |
results = []
|
67 |
|
68 |
+
with st.spinner("Analyse en cours... ⏳"):
|
69 |
for phrase in phrases:
|
70 |
try:
|
71 |
lang = detect(phrase)
|
|
|
76 |
sentiment = analysis["label"]
|
77 |
score = round(analysis["score"], 2)
|
78 |
|
79 |
+
result_entry = {
|
80 |
"Texte": phrase,
|
81 |
"Langue": lang,
|
82 |
"Sentiment": sentiment,
|
83 |
"Score": score
|
84 |
+
}
|
85 |
+
results.append(result_entry)
|
86 |
+
st.session_state.history.append(result_entry)
|
87 |
|
88 |
+
# Réactions
|
89 |
if "negative" in sentiment.lower():
|
90 |
+
st.toast("🚨 Sentiment négatif détecté !", icon="⚡")
|
91 |
st.error(f"😞 Texte : {phrase}")
|
|
|
92 |
elif "positive" in sentiment.lower():
|
93 |
+
st.balloons()
|
94 |
st.success(f"😊 Texte : {phrase}")
|
95 |
else:
|
96 |
st.warning(f"😐 Texte : {phrase}")
|
97 |
|
98 |
+
# Barre colorée de score
|
99 |
color = sentiment_color(score)
|
100 |
st.markdown(
|
101 |
f"""
|
|
|
107 |
unsafe_allow_html=True
|
108 |
)
|
109 |
|
110 |
+
# --- Résultats stockés et affichés ---
|
111 |
+
if st.session_state.history:
|
112 |
+
st.markdown("---")
|
113 |
+
st.subheader("📄 Historique des Analyses")
|
114 |
+
df_history = pd.DataFrame(st.session_state.history)
|
115 |
+
st.dataframe(df_history)
|
116 |
+
|
117 |
+
# Bouton pour télécharger l'historique
|
118 |
+
csv = df_history.to_csv(index=False)
|
119 |
+
buffer = io.BytesIO()
|
120 |
+
buffer.write(csv.encode())
|
121 |
+
buffer.seek(0)
|
122 |
+
|
123 |
+
st.download_button(
|
124 |
+
label="📥 Télécharger l'historique en CSV",
|
125 |
+
data=buffer,
|
126 |
+
file_name="historique_sentiment.csv",
|
127 |
+
mime="text/csv"
|
128 |
+
)
|
129 |
+
|
130 |
+
# --- Footer ---
|
131 |
st.markdown("---")
|
132 |
+
st.markdown("🔗 [Voir le modèle Hugging Face](https://huggingface.co/tabularisai/multilingual-sentiment-analysis) | App réalisée avec ❤️ en Python et Streamlit.")
|