Spaces:
Running
Running
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +12 -23
src/streamlit_app.py
CHANGED
|
@@ -3,61 +3,50 @@ from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
|
| 3 |
import torch
|
| 4 |
import pandas as pd
|
| 5 |
|
| 6 |
-
st.set_page_config(page_title="Analyse de Sentiment", layout="wide")
|
| 7 |
|
| 8 |
-
st.title(" Analyse de Sentiment")
|
| 9 |
-
st.write("
|
| 10 |
|
| 11 |
-
# Chargement
|
| 12 |
@st.cache_resource
|
| 13 |
def load_model():
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
| 15 |
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 16 |
model = AutoModelForSequenceClassification.from_pretrained(model_id)
|
| 17 |
return tokenizer, model
|
| 18 |
|
| 19 |
-
with st.spinner("Chargement du modèle..."):
|
| 20 |
tokenizer, model = load_model()
|
| 21 |
|
| 22 |
-
# Zone de saisie
|
| 23 |
user_input = st.text_area(
|
| 24 |
"Entrez vos avis (un avis par ligne) :",
|
| 25 |
-
placeholder="Exemple :\nCe produit est top !\nJe n'aime pas du tout
|
| 26 |
height=200
|
| 27 |
)
|
| 28 |
|
| 29 |
if st.button("Analyser les avis"):
|
| 30 |
-
# 1. Séparer le texte par ligne et enlever les lignes vides
|
| 31 |
lines = [line.strip() for line in user_input.split("\n") if line.strip() != ""]
|
| 32 |
|
| 33 |
if not lines:
|
| 34 |
st.warning("Veuillez entrer au moins un texte.")
|
| 35 |
else:
|
| 36 |
-
st.info(f"Analyse de {len(lines)} texte(s) en cours...")
|
| 37 |
-
|
| 38 |
-
# 2. Tokenisation du BATCH (toutes les lignes d'un coup)
|
| 39 |
-
# padding=True va aligner la taille des textes pour le modèle
|
| 40 |
inputs = tokenizer(lines, return_tensors="pt", truncation=True, padding=True)
|
| 41 |
|
| 42 |
-
# 3. Inférence
|
| 43 |
with torch.no_grad():
|
| 44 |
outputs = model(**inputs)
|
| 45 |
|
| 46 |
-
# 4. Récupération des prédictions pour chaque ligne
|
| 47 |
logits = outputs.logits
|
| 48 |
predictions = torch.argmax(logits, dim=-1).tolist()
|
| 49 |
|
| 50 |
-
# 5. Préparation des résultats pour l'affichage
|
| 51 |
results = []
|
| 52 |
for text, pred in zip(lines, predictions):
|
| 53 |
sentiment = "POSITIF ✨" if pred == 1 else "NÉGATIF 🚨"
|
| 54 |
results.append({"Texte/Avis": text, "Sentiment": sentiment})
|
| 55 |
|
| 56 |
-
# 6. Affichage sous forme de joli tableau interactif avec Pandas
|
| 57 |
df = pd.DataFrame(results)
|
| 58 |
-
st.dataframe(df, use_container_width=True)
|
| 59 |
-
|
| 60 |
-
# Optionnel : Petit résumé statistique
|
| 61 |
-
pos_count = sum(1 for r in results if "POSITIF" in r["Sentiment"])
|
| 62 |
-
neg_count = len(results) - pos_count
|
| 63 |
-
st.write(f"**Résumé :** {pos_count} positif(s) et {neg_count} négatif(s).")
|
|
|
|
| 3 |
import torch
|
| 4 |
import pandas as pd
|
| 5 |
|
| 6 |
+
st.set_page_config(page_title="Analyse de Sentiment", page_icon="📊", layout="wide")
|
| 7 |
|
| 8 |
+
st.title("📊 Analyse de Sentiment - Modèle Fusionné")
|
| 9 |
+
st.write("Application d'analyse de sentiment optimisée avec RoBERTa (Merged Model).")
|
| 10 |
|
| 11 |
+
# Chargement direct et simple (vu que le modèle est fusionné avec son tokenizer)
|
| 12 |
@st.cache_resource
|
| 13 |
def load_model():
|
| 14 |
+
# L'identifiant exact de ton dépôt sur Hugging Face
|
| 15 |
+
model_id = "Diary14/roberta-sentiment-lora"
|
| 16 |
+
|
| 17 |
+
# Hugging Face va trouver le tokenizer et le modèle dans le même dossier
|
| 18 |
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 19 |
model = AutoModelForSequenceClassification.from_pretrained(model_id)
|
| 20 |
return tokenizer, model
|
| 21 |
|
| 22 |
+
with st.spinner("Chargement du modèle fusionné en cours..."):
|
| 23 |
tokenizer, model = load_model()
|
| 24 |
|
| 25 |
+
# Zone de saisie
|
| 26 |
user_input = st.text_area(
|
| 27 |
"Entrez vos avis (un avis par ligne) :",
|
| 28 |
+
placeholder="Exemple :\nCe produit est top !\nJe n'aime pas du tout...",
|
| 29 |
height=200
|
| 30 |
)
|
| 31 |
|
| 32 |
if st.button("Analyser les avis"):
|
|
|
|
| 33 |
lines = [line.strip() for line in user_input.split("\n") if line.strip() != ""]
|
| 34 |
|
| 35 |
if not lines:
|
| 36 |
st.warning("Veuillez entrer au moins un texte.")
|
| 37 |
else:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
inputs = tokenizer(lines, return_tensors="pt", truncation=True, padding=True)
|
| 39 |
|
|
|
|
| 40 |
with torch.no_grad():
|
| 41 |
outputs = model(**inputs)
|
| 42 |
|
|
|
|
| 43 |
logits = outputs.logits
|
| 44 |
predictions = torch.argmax(logits, dim=-1).tolist()
|
| 45 |
|
|
|
|
| 46 |
results = []
|
| 47 |
for text, pred in zip(lines, predictions):
|
| 48 |
sentiment = "POSITIF ✨" if pred == 1 else "NÉGATIF 🚨"
|
| 49 |
results.append({"Texte/Avis": text, "Sentiment": sentiment})
|
| 50 |
|
|
|
|
| 51 |
df = pd.DataFrame(results)
|
| 52 |
+
st.dataframe(df, use_container_width=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|