File size: 3,544 Bytes
46ab82d
d40194c
46ab82d
c7c939f
d40194c
c7c939f
27d491a
d40194c
46ab82d
c7c939f
 
 
 
 
 
 
 
 
 
e12782e
 
 
 
 
 
 
 
1d86225
c7c939f
 
 
 
 
 
 
27d491a
 
1d86225
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c7c939f
65f34ea
5ec7823
c7c939f
d40194c
c7c939f
 
11c0599
 
1d86225
 
 
 
 
 
11c0599
1d86225
 
11c0599
 
46ab82d
c7c939f
d40194c
46ab82d
c7c939f
 
 
 
46ab82d
 
d40194c
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
import gradio as gr
from transformers import pipeline

# মডেল লোড করো
emotion_classifier = pipeline(
    "text-classification",
    model="Toshifumi/bert-base-multilingual-cased-finetuned-emotion"
)

# Human-friendly emotion mapping
humanized_map = {
    "label_0": "😠 রাগ (Anger)",
    "label_1": "😢 দুঃখ (Sadness)",
    "label_2": "😊 আনন্দিত (Joy)",
    "label_3": "❤️ ভালোবাসা (Love)",
    "label_4": "😨 ভয় (Fear)",
    "label_5": "😲 বিস্ময় (Surprise)",
    "label_6": "😐 নিরপেক্ষ (Neutral)",

    "LABEL_0": "😠 রাগ (Anger)",
    "LABEL_1": "😢 দুঃখ (Sadness)",
    "LABEL_2": "😊 আনন্দিত (Joy)",
    "LABEL_3": "❤️ ভালোবাসা (Love)",
    "LABEL_4": "😨 ভয় (Fear)",
    "LABEL_5": "😲 বিস্ময় (Surprise)",
    "LABEL_6": "😐 নিরপেক্ষ (Neutral)",

    # Extra fallback for plain text labels
    "Anger": "😠 রাগ (Anger)",
    "Sadness": "😢 দুঃখ (Sadness)",
    "Joy": "😊 আনন্দিত (Joy)",
    "Love": "❤️ ভালোবাসা (Love)",
    "Fear": "😨 ভয় (Fear)",
    "Surprise": "😲 বিস্ময় (Surprise)",
    "Neutral": "😐 নিরপেক্ষ (Neutral)"
}

# Extra keyword-based fallback mapping
keyword_map = {
    "happy": "😊 আনন্দিত (Joy)",
    "খুশি": "😊 আনন্দিত (Joy)",
    "sad": "😢 দুঃখ (Sadness)",
    "দুঃখ": "😢 দুঃখ (Sadness)",
    "love": "❤️ ভালোবাসা (Love)",
    "ভালবাসা": "❤️ ভালোবাসা (Love)",
    "ভালোবাসা": "❤️ ভালোবাসা (Love)",
    "fear": "😨 ভয় (Fear)",
    "ভয়": "😨 ভয় (Fear)",
    "angry": "😠 রাগ (Anger)",
    "রাগ": "😠 রাগ (Anger)",
    "surprise": "😲 বিস্ময় (Surprise)",
    "বিস্ময়": "😲 বিস্ময় (Surprise)"
}

# Emotion detect function
def detect_emotion(text):
    if not text.strip():
        return "⚠️ অনুগ্রহ করে একটি বার্তা লিখুন।"
    try:
        result = emotion_classifier(text)[0]
        label = result["label"]
        score = round(result["score"] * 100, 2)
        emotion = humanized_map.get(label)

        if not emotion:
            # Keyword fallback
            for word in keyword_map:
                if word in text.lower():
                    return f"{keyword_map[word]} (keyword match)"
            return f"🤔 অজানা (Unknown) — মডেল লেবেল: {label} (score: {score}%)"

        return f"{emotion} (score: {score}%)"
    except Exception as e:
        return f"❌ সমস্যা হয়েছে: {str(e)}"

# Interface
interface = gr.Interface(
    fn=detect_emotion,
    inputs=gr.Textbox(label="✍️ মেসেজ লিখুন (বাংলা / English)", placeholder="আমি আজ অনেক খুশি..."),
    outputs=gr.Textbox(label="🧠 সনাক্তকৃত অনুভূতি"),
    title="🌐 Bilingual Emotion Detector",
    description="এই AI টুলটি বাংলা ও ইংরেজি টেক্সট থেকে মানুষের আবেগ শনাক্ত করে (যেমন: 😊 আনন্দ, 😢 দুঃখ, 😠 রাগ)।"
)

interface.launch()