File size: 3,502 Bytes
546c04a
 
e6455d3
 
546c04a
9449219
8085a83
9449219
 
8085a83
 
546c04a
9449219
e6455d3
 
 
 
 
 
 
 
 
 
 
9449219
 
 
 
 
 
 
 
546c04a
 
 
 
 
 
9449219
 
 
546c04a
9449219
 
 
 
e6455d3
 
546c04a
e6455d3
 
 
 
 
9449219
e6455d3
 
 
 
 
 
 
9449219
 
 
e6455d3
 
 
9449219
 
e6455d3
 
9449219
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e6455d3
 
 
546c04a
 
9449219
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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
@st.cache_resource
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.")