AluaZhantleuova01's picture
final4
96772eb
import streamlit as st
from transformers import pipeline
classifier = pipeline("text-classification", model="cardiffnlp/twitter-roberta-base-emotion")
emoji_map = {
'joy': "😊",
'anger': "😡",
'sadness': "😢",
'fear': "😨",
'surprise': "😲",
'love': "❤️",
'neutral': "😐",
'optimism': "🌞"
}
def classify_text(text):
"""Классификация текста и возврат эмоций."""
try:
results = classifier(text)
return results
except Exception as e:
st.error(f"Error during classification: {e}")
return []
def manual_check(text):
"""Ручная проверка текста на ключевые слова."""
for emotion, emoji in emoji_map.items():
if emotion in text.lower():
return [{'label': emotion, 'score': 1.0}]
return None
st.title("Emotion Classification with Emoticons")
st.write("Введите текст, и модель предскажет эмоции с эмодзи.")
text_input = st.text_area("Enter text to classify:")
if text_input.strip():
manual_result = manual_check(text_input)
if manual_result:
results = manual_result
else:
results = classify_text(text_input)
if results:
st.subheader("Predicted Emotions:")
for result in results:
emotion = result['label'].lower()
confidence = result['score']
emoji = emoji_map.get(emotion, "🤔")
st.write(f"{emoji} {emotion.capitalize()}: {confidence:.2%}")
else:
st.write("No emotions detected. Please try again.")
else:
st.info("Please enter text to analyze emotions.")