File size: 6,895 Bytes
808e015
1c36c9e
 
808e015
1c36c9e
 
 
 
 
 
 
 
 
 
 
 
808e015
 
1c36c9e
 
 
808e015
 
 
 
1c36c9e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
808e015
 
 
 
1c36c9e
 
 
 
 
 
f9fe2bd
808e015
 
 
1c36c9e
 
808e015
1c36c9e
 
 
 
 
 
 
 
 
 
 
 
 
808e015
1c36c9e
 
808e015
 
1c36c9e
 
 
808e015
 
 
1c36c9e
808e015
 
1c36c9e
 
 
 
 
 
f9fe2bd
808e015
 
 
1c36c9e
 
 
 
 
 
 
 
 
808e015
 
 
 
1c36c9e
808e015
1c36c9e
808e015
 
1c36c9e
808e015
 
 
 
 
1c36c9e
 
 
 
 
 
 
 
 
 
 
 
 
 
808e015
f9fe2bd
1c36c9e
 
 
 
808e015
 
 
6eb7d15
1c36c9e
 
 
 
 
 
 
 
 
 
6eb7d15
808e015
1c36c9e
f9fe2bd
 
 
1c36c9e
 
 
f9fe2bd
1c36c9e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f9fe2bd
1c36c9e
 
 
 
 
 
 
 
 
f9fe2bd
1c36c9e
 
 
 
 
 
f9fe2bd
 
 
1c36c9e
 
 
 
 
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import gradio as gr
from transformers import pipeline, AutoModelForSequenceClassification, AutoTokenizer
import torch

# 🌍 MULTILINGUAL MODEL - Supports 58 languages including Turkish and English
# Model: cardiffnlp/twitter-xlm-roberta-base-sentiment-multilingual
# Dataset: Twitter data in multiple languages
# Classes: Negative (0), Neutral (1), Positive (2)

MODEL_NAME = "cardiffnlp/twitter-xlm-roberta-base-sentiment-multilingual"

# Load model and tokenizer
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME)

# Create pipeline for sentiment analysis
sentiment_pipeline = pipeline(
    "sentiment-analysis",
    model=model,
    tokenizer=tokenizer,
    return_all_scores=True
)

def analyze_sentiment(text):
    """
    Analyzes sentiment of text in 58+ languages including Turkish and English.
    
    Args:
        text (str): Input text to analyze (any supported language)
        
    Returns:
        dict: {
            "sentiment": "positive" | "neutral" | "negative",
            "score": float (0.0 to 1.0),
            "scores": {
                "positive": float,
                "neutral": float,
                "negative": float
            },
            "language_detected": bool (future feature),
            "error": str (optional)
        }
    """
    if not text or text.strip() == "":
        return {
            "sentiment": "neutral",
            "score": 0.33,
            "scores": {
                "positive": 0.33,
                "neutral": 0.34,
                "negative": 0.33
            },
            "error": "Empty text"
        }
    
    try:
        # Get all scores from the model
        results = sentiment_pipeline(text)[0]
        
        # Parse results - model returns list of dicts with label and score
        scores_dict = {}
        for item in results:
            label = item['label'].lower()
            score = item['score']
            
            # Map model labels to our format
            if 'negative' in label or label == 'label_0':
                scores_dict['negative'] = round(score, 4)
            elif 'neutral' in label or label == 'label_1':
                scores_dict['neutral'] = round(score, 4)
            elif 'positive' in label or label == 'label_2':
                scores_dict['positive'] = round(score, 4)
        
        # Determine the dominant sentiment
        max_sentiment = max(scores_dict.items(), key=lambda x: x[1])
        
        return {
            "sentiment": max_sentiment[0],
            "score": max_sentiment[1],
            "scores": scores_dict
        }
        
    except Exception as e:
        print(f"❌ Error in sentiment analysis: {str(e)}")
        return {
            "sentiment": "neutral",
            "score": 0.33,
            "scores": {
                "positive": 0.33,
                "neutral": 0.34,
                "negative": 0.33
            },
            "error": str(e)
        }

def gradio_interface(text):
    """
    Formats sentiment analysis for beautiful web display.
    
    Args:
        text (str): Input text
        
    Returns:
        str: Formatted markdown with emojis and detailed scores
    """
    result = analyze_sentiment(text)
    
    sentiment = result['sentiment']
    score = result['score']
    scores = result.get('scores', {})
    
    # Emoji mapping for visual appeal
    emoji_map = {
        'positive': '😊',
        'negative': '😢',
        'neutral': '😐'
    }
    
    emoji = emoji_map.get(sentiment, '😐')
    
    # Build beautiful markdown output
    output = f"## {emoji} Sentiment: **{sentiment.upper()}**\n\n"
    output += f"### 📊 Confidence: {score * 100:.1f}%\n\n"
    output += "---\n\n"
    output += "### 🎯 Detailed Scores:\n\n"
    
    # Add progress bars for each sentiment
    if scores:
        for sent_type, sent_score in sorted(scores.items(), key=lambda x: x[1], reverse=True):
            emoji_small = emoji_map.get(sent_type, '•')
            percentage = sent_score * 100
            bar_length = int(percentage / 5)  # 20 chars max
            bar = "█" * bar_length + "░" * (20 - bar_length)
            output += f"{emoji_small} **{sent_type.capitalize()}**: {bar} {percentage:.1f}%\n\n"
    
    if "error" in result and result["error"] != "Empty text":
        output += f"\n\n⚠️ **Note:** {result['error']}"
    
    output += "\n\n---\n\n"
    output += "✨ **Multilingual AI** • 🌍 Supports 58+ languages including Turkish and English"
    
    return output

def api_analyze(text):
    """
    Direct API endpoint for programmatic access.
    Returns raw JSON response.
    
    Args:
        text (str): Input text in any supported language
        
    Returns:
        dict: Sentiment analysis result
    """
    return analyze_sentiment(text)

# 🎨 Beautiful Gradio Interface
demo = gr.Interface(
    fn=gradio_interface,
    inputs=gr.Textbox(
        lines=5,
        placeholder="Bir metin girin... / Enter text to analyze...",
        label="📝 Input Text (Turkish, English, or 56+ other languages)"
    ),
    outputs=gr.Markdown(label="🤖 AI Sentiment Analysis Result"),
    title="🌍 Multilingual Chat Sentiment Analysis",
    description="""
    **Advanced AI-powered sentiment analysis** supporting **58+ languages** including Turkish and English.
    
    🚀 **Powered by:** XLM-RoBERTa (Twitter-trained multilingual model)
    
    ✨ **Features:**
    - 🇹🇷 Turkish language support
    - 🇬🇧 English language support  
    - 🌍 56+ additional languages
    - 😊😐😢 Positive, Neutral, Negative classification
    - 📊 Detailed confidence scores
    - ⚡ Real-time analysis
    
    **Perfect for:** Chat applications, social media monitoring, customer feedback analysis
    """,
    examples=[
        ["This is amazing! I absolutely love it! 🎉"],
        ["I'm so disappointed and sad about this situation... 😞"],
        ["The weather is okay today, nothing special."],
        ["Bu harika! Çok mutluyum! 🎊"],
        ["Gerçekten çok kötü bir deneyim yaşadım. 😠"],
        ["İyi, fena değil. Normal bir gün."],
        ["¡Esto es increíble! Me encanta! 🌟"],
        ["Je suis très content de ce service! ❤️"],
        ["Это ужасно! Я очень разочарован. 😤"]
    ],
    theme=gr.themes.Soft(
        primary_hue="blue",
        secondary_hue="cyan"
    ),
    api_name="analyze",
    allow_flagging="never"
)

if __name__ == "__main__":
    print("🚀 Starting Multilingual Sentiment Analysis Service...")
    print(f"📦 Model: {MODEL_NAME}")
    print("🌍 Languages: Turkish, English, Spanish, French, Arabic, Russian, and 52+ more")
    print("✅ Server starting on http://127.0.0.1:7860")
    demo.launch(server_name="0.0.0.0", server_port=7860)