Spaces:
Sleeping
Sleeping
import streamlit as st | |
from transformers import pipeline | |
from langdetect import detect | |
import pandas as pd | |
# Configurer la page | |
st.set_page_config( | |
page_title="🌍 Analyse de Sentiment Multilingue V3", | |
page_icon="🎯", | |
layout="centered", | |
) | |
# CSS custom pour fond clair | |
st.markdown( | |
""" | |
<style> | |
.stApp { | |
background-color: #f0f2f6; | |
} | |
</style> | |
""", | |
unsafe_allow_html=True | |
) | |
# Fonction pour colorer un slider | |
def sentiment_color(score): | |
"""Retourne une couleur hex en fonction du score (0=rouge, 1=vert)""" | |
red = int(255 * (1 - score)) | |
green = int(255 * score) | |
return f'rgb({red},{green},0)' | |
# Charger modèle | |
def load_model(): | |
return pipeline("text-classification", model="tabularisai/multilingual-sentiment-analysis") | |
classifier = load_model() | |
# Titre | |
st.title("🎯 Analyseur de Sentiment Multilingue - V3") | |
st.write("Analysez vos textes avec détection de langue, sentiment et visualisation dynamique. 🚀") | |
# Zone utilisateur | |
user_input = st.text_area("✍️ Entrez vos phrases séparées par un point-virgule ';'", height=150) | |
if st.button("🔎 Analyser"): | |
if not user_input.strip(): | |
st.warning("⚠️ Merci d'entrer au moins une phrase.") | |
else: | |
phrases = [phrase.strip() for phrase in user_input.split(';') if phrase.strip()] | |
st.info(f"Nombre de phrases détectées : {len(phrases)}") | |
results = [] | |
with st.spinner("Analyse en cours..."): | |
for phrase in phrases: | |
try: | |
lang = detect(phrase) | |
except: | |
lang = "indéterminée" | |
analysis = classifier(phrase)[0] | |
sentiment = analysis["label"] | |
score = round(analysis["score"], 2) | |
results.append({ | |
"Texte": phrase, | |
"Langue": lang, | |
"Sentiment": sentiment, | |
"Score": score | |
}) | |
# Animation en cas de sentiment négatif | |
if "negative" in sentiment.lower(): | |
st.error(f"😞 Texte : {phrase}") | |
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) | |
elif "positive" in sentiment.lower(): | |
st.success(f"😊 Texte : {phrase}") | |
else: | |
st.warning(f"😐 Texte : {phrase}") | |
# Barre Slider personnalisée | |
color = sentiment_color(score) | |
st.markdown( | |
f""" | |
<div style="margin:10px 0;"> | |
<div style="height:20px;width:100%;background:linear-gradient(to right, {color} {score*100}%, #d3d3d3 {score*100}%);border-radius:10px;"></div> | |
<center><small><b>Score de confiance : {score:.0%}</b></small></center> | |
</div> | |
""", | |
unsafe_allow_html=True | |
) | |
# Résumé tableau | |
st.markdown("---") | |
st.subheader("📄 Résumé des analyses") | |
df_results = pd.DataFrame(results) | |
st.dataframe(df_results) | |
# Footer | |
st.markdown("---") | |
st.markdown("🔗 [Voir le modèle sur Hugging Face](https://huggingface.co/tabularisai/multilingual-sentiment-analysis) | Application réalisée avec ❤️ en Python.") | |