File size: 1,764 Bytes
8d8b048
72bb836
 
96772eb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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.")