sebtick commited on
Commit
9449219
·
verified ·
1 Parent(s): a715c2a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -40
app.py CHANGED
@@ -3,66 +3,56 @@ from transformers import pipeline
3
  from langdetect import detect
4
  import pandas as pd
5
 
6
- # Configuration de la page
7
  st.set_page_config(
8
- page_title="🌍 Analyse de Sentiment Multilingue V2",
9
- page_icon="🔎",
10
  layout="centered",
11
  )
12
 
13
- # CSS personnalisé pour rendre l'app plus douce visuellement
14
  st.markdown(
15
  """
16
  <style>
17
  .stApp {
18
  background-color: #f0f2f6;
19
  }
20
- .big-font {
21
- font-size:20px !important;
22
- }
23
  </style>
24
  """,
25
  unsafe_allow_html=True
26
  )
27
 
28
- # Titre principal
29
- st.title("🔎 Analyseur de Sentiment Multilingue")
30
- st.write("**Bienvenue !** Cette application détecte automatiquement la langue et analyse les sentiments de vos textes. 📖")
31
-
32
- # Sidebar d'information
33
- with st.sidebar:
34
- st.header("📚 À propos de l'application")
35
- st.write("""
36
- - Modèle IA : tabularisai/multilingual-sentiment-analysis
37
- - Analyse de texte rapide
38
- - Détection de la langue
39
- """)
40
- st.markdown("---")
41
- st.write("Réalisé avec ❤️ en Python et Streamlit")
42
-
43
- # Charger le modèle
44
  @st.cache_resource
45
  def load_model():
46
  return pipeline("text-classification", model="tabularisai/multilingual-sentiment-analysis")
47
 
48
  classifier = load_model()
49
 
50
- # Entrée utilisateur
51
- st.subheader("✍️ Entrez vos phrases :")
52
- user_input = st.text_area("Entrez plusieurs phrases séparées par un point-virgule ';'", height=150)
53
 
54
- # Bouton
55
- if st.button("🔍 Lancer l'analyse"):
 
 
56
  if not user_input.strip():
57
  st.warning("⚠️ Merci d'entrer au moins une phrase.")
58
  else:
59
  phrases = [phrase.strip() for phrase in user_input.split(';') if phrase.strip()]
60
 
61
  st.info(f"Nombre de phrases détectées : {len(phrases)}")
62
-
63
  results = []
64
 
65
- with st.spinner("Analyse en cours..."):
66
  for phrase in phrases:
67
  try:
68
  lang = detect(phrase)
@@ -70,23 +60,43 @@ if st.button("🔍 Lancer l'analyse"):
70
  lang = "indéterminée"
71
 
72
  analysis = classifier(phrase)[0]
 
 
 
73
  results.append({
74
  "Texte": phrase,
75
  "Langue": lang,
76
- "Sentiment": analysis["label"],
77
- "Score": round(analysis["score"], 2)
78
  })
79
 
80
- # Affichage sous forme de tableau
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81
  df_results = pd.DataFrame(results)
82
  st.dataframe(df_results)
83
 
84
- # Résultats graphiques
85
- st.subheader("📊 Visualisation des scores de confiance")
86
- st.bar_chart(df_results.set_index("Texte")["Score"])
87
-
88
- st.success("✅ Analyse terminée avec succès !")
89
-
90
  # Footer
91
  st.markdown("---")
92
- st.markdown("🔗 [Voir le modèle Hugging Face](https://huggingface.co/tabularisai/multilingual-sentiment-analysis) | Projet open-source disponible sur demande.")
 
3
  from langdetect import detect
4
  import pandas as pd
5
 
6
+ # Configurer la page
7
  st.set_page_config(
8
+ page_title="🌍 Analyse de Sentiment Multilingue V3",
9
+ page_icon="🎯",
10
  layout="centered",
11
  )
12
 
13
+ # CSS custom pour fond clair
14
  st.markdown(
15
  """
16
  <style>
17
  .stApp {
18
  background-color: #f0f2f6;
19
  }
 
 
 
20
  </style>
21
  """,
22
  unsafe_allow_html=True
23
  )
24
 
25
+ # Fonction pour colorer un slider
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
+ # Titre
40
+ st.title("🎯 Analyseur de Sentiment Multilingue - V3")
41
+ st.write("Analysez vos textes avec détection de langue, sentiment et visualisation dynamique. 🚀")
42
 
43
+ # Zone utilisateur
44
+ user_input = st.text_area("✍️ Entrez vos phrases séparées par un point-virgule ';'", height=150)
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)
 
60
  lang = "indéterminée"
61
 
62
  analysis = classifier(phrase)[0]
63
+ sentiment = analysis["label"]
64
+ score = round(analysis["score"], 2)
65
+
66
  results.append({
67
  "Texte": phrase,
68
  "Langue": lang,
69
+ "Sentiment": sentiment,
70
+ "Score": score
71
  })
72
 
73
+ # Animation en cas de sentiment négatif
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 Slider personnalisée
83
+ color = sentiment_color(score)
84
+ st.markdown(
85
+ f"""
86
+ <div style="margin:10px 0;">
87
+ <div style="height:20px;width:100%;background:linear-gradient(to right, {color} {score*100}%, #d3d3d3 {score*100}%);border-radius:10px;"></div>
88
+ <center><small><b>Score de confiance : {score:.0%}</b></small></center>
89
+ </div>
90
+ """,
91
+ unsafe_allow_html=True
92
+ )
93
+
94
+ # Résumé tableau
95
+ st.markdown("---")
96
+ st.subheader("📄 Résumé des analyses")
97
  df_results = pd.DataFrame(results)
98
  st.dataframe(df_results)
99
 
 
 
 
 
 
 
100
  # Footer
101
  st.markdown("---")
102
+ st.markdown("🔗 [Voir le modèle sur Hugging Face](https://huggingface.co/tabularisai/multilingual-sentiment-analysis) | Application réalisée avec ❤️ en Python.")