Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,12 +1,12 @@
|
|
1 |
import streamlit as st
|
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
|
10 |
page_icon="🎯",
|
11 |
layout="centered",
|
12 |
)
|
@@ -34,26 +34,41 @@ st.markdown(
|
|
34 |
unsafe_allow_html=True
|
35 |
)
|
36 |
|
37 |
-
# ---
|
38 |
@st.cache_resource
|
39 |
-
def
|
40 |
return pipeline("text-classification", model="tabularisai/multilingual-sentiment-analysis")
|
41 |
|
42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
|
44 |
# --- Initialiser l'historique ---
|
45 |
if 'history' not in st.session_state:
|
46 |
st.session_state.history = []
|
47 |
|
48 |
-
# --- Fonction pour
|
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 -
|
56 |
-
st.write("Analysez vos textes avec détection
|
57 |
|
58 |
user_input = st.text_area("✍️ Entrez vos phrases séparées par un point-virgule ';'", height=180)
|
59 |
|
@@ -68,11 +83,11 @@ if st.button("🔎 Analyser"):
|
|
68 |
with st.spinner("Analyse en cours... ⏳"):
|
69 |
for phrase in phrases:
|
70 |
try:
|
71 |
-
lang =
|
72 |
-
except:
|
73 |
-
lang = "
|
74 |
|
75 |
-
analysis =
|
76 |
sentiment = analysis["label"]
|
77 |
score = round(analysis["score"], 2)
|
78 |
|
@@ -85,7 +100,7 @@ if st.button("🔎 Analyser"):
|
|
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}")
|
@@ -105,7 +120,7 @@ if st.button("🔎 Analyser"):
|
|
105 |
unsafe_allow_html=True
|
106 |
)
|
107 |
|
108 |
-
# Barre colorée
|
109 |
color = sentiment_color(score)
|
110 |
st.markdown(
|
111 |
f"""
|
@@ -117,14 +132,14 @@ if st.button("🔎 Analyser"):
|
|
117 |
unsafe_allow_html=True
|
118 |
)
|
119 |
|
120 |
-
# ---
|
121 |
if st.session_state.history:
|
122 |
st.markdown("---")
|
123 |
st.subheader("📄 Historique des Analyses")
|
124 |
df_history = pd.DataFrame(st.session_state.history)
|
125 |
st.dataframe(df_history)
|
126 |
|
127 |
-
# Bouton pour télécharger
|
128 |
csv = df_history.to_csv(index=False)
|
129 |
buffer = io.BytesIO()
|
130 |
buffer.write(csv.encode())
|
@@ -139,4 +154,4 @@ if st.session_state.history:
|
|
139 |
|
140 |
# --- Footer ---
|
141 |
st.markdown("---")
|
142 |
-
st.markdown("🔗 [Voir le modèle
|
|
|
1 |
import streamlit as st
|
2 |
+
from transformers import pipeline, AutoTokenizer, AutoModelForSequenceClassification
|
|
|
3 |
import pandas as pd
|
4 |
+
import torch
|
5 |
import io
|
6 |
|
7 |
# --- Configuration de la page ---
|
8 |
st.set_page_config(
|
9 |
+
page_title="🌍 Analyseur de Sentiment Multilingue V5",
|
10 |
page_icon="🎯",
|
11 |
layout="centered",
|
12 |
)
|
|
|
34 |
unsafe_allow_html=True
|
35 |
)
|
36 |
|
37 |
+
# --- Chargement des modèles ---
|
38 |
@st.cache_resource
|
39 |
+
def load_sentiment_model():
|
40 |
return pipeline("text-classification", model="tabularisai/multilingual-sentiment-analysis")
|
41 |
|
42 |
+
@st.cache_resource
|
43 |
+
def load_language_model():
|
44 |
+
tokenizer = AutoTokenizer.from_pretrained("papluca/xlm-roberta-base-language-detection")
|
45 |
+
model = AutoModelForSequenceClassification.from_pretrained("papluca/xlm-roberta-base-language-detection")
|
46 |
+
return tokenizer, model
|
47 |
+
|
48 |
+
sentiment_model = load_sentiment_model()
|
49 |
+
tokenizer_lang, model_lang = load_language_model()
|
50 |
+
|
51 |
+
# --- Détecteur de langue IA ---
|
52 |
+
def detect_language(text):
|
53 |
+
inputs = tokenizer_lang(text, return_tensors="pt", truncation=True, max_length=512)
|
54 |
+
outputs = model_lang(**inputs)
|
55 |
+
predicted_class = torch.argmax(outputs.logits, dim=1)
|
56 |
+
label = model_lang.config.id2label[predicted_class.item()]
|
57 |
+
return label
|
58 |
|
59 |
# --- Initialiser l'historique ---
|
60 |
if 'history' not in st.session_state:
|
61 |
st.session_state.history = []
|
62 |
|
63 |
+
# --- Fonction pour couleur de la barre ---
|
64 |
def sentiment_color(score):
|
65 |
red = int(255 * (1 - score))
|
66 |
green = int(255 * score)
|
67 |
return f'rgb({red},{green},0)'
|
68 |
|
69 |
# --- Interface principale ---
|
70 |
+
st.title("🎯 Analyseur de Sentiment Multilingue - V5")
|
71 |
+
st.write("Analysez vos textes avec détection IA de langue et sentiment, historique et téléchargement. 🚀")
|
72 |
|
73 |
user_input = st.text_area("✍️ Entrez vos phrases séparées par un point-virgule ';'", height=180)
|
74 |
|
|
|
83 |
with st.spinner("Analyse en cours... ⏳"):
|
84 |
for phrase in phrases:
|
85 |
try:
|
86 |
+
lang = detect_language(phrase)
|
87 |
+
except Exception as e:
|
88 |
+
lang = "Erreur"
|
89 |
|
90 |
+
analysis = sentiment_model(phrase)[0]
|
91 |
sentiment = analysis["label"]
|
92 |
score = round(analysis["score"], 2)
|
93 |
|
|
|
100 |
results.append(result_entry)
|
101 |
st.session_state.history.append(result_entry)
|
102 |
|
103 |
+
# Réactions
|
104 |
if "negative" in sentiment.lower():
|
105 |
st.toast("🚨 Sentiment négatif détecté !", icon="⚡")
|
106 |
st.error(f"😞 Texte : {phrase}")
|
|
|
120 |
unsafe_allow_html=True
|
121 |
)
|
122 |
|
123 |
+
# Barre colorée
|
124 |
color = sentiment_color(score)
|
125 |
st.markdown(
|
126 |
f"""
|
|
|
132 |
unsafe_allow_html=True
|
133 |
)
|
134 |
|
135 |
+
# --- Historique des analyses ---
|
136 |
if st.session_state.history:
|
137 |
st.markdown("---")
|
138 |
st.subheader("📄 Historique des Analyses")
|
139 |
df_history = pd.DataFrame(st.session_state.history)
|
140 |
st.dataframe(df_history)
|
141 |
|
142 |
+
# Bouton pour télécharger CSV
|
143 |
csv = df_history.to_csv(index=False)
|
144 |
buffer = io.BytesIO()
|
145 |
buffer.write(csv.encode())
|
|
|
154 |
|
155 |
# --- Footer ---
|
156 |
st.markdown("---")
|
157 |
+
st.markdown("🔗 [Voir le modèle de sentiment](https://huggingface.co/tabularisai/multilingual-sentiment-analysis) | 🔗 [Voir le modèle de langue](https://huggingface.co/papluca/xlm-roberta-base-language-detection) | Réalisé avec ❤️ et IA.")
|