Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
import torch
|
| 3 |
from transformers import pipeline, AutoModelForSequenceClassification, AutoTokenizer
|
| 4 |
import librosa
|
|
@@ -15,19 +15,17 @@ print("π Starting Enhanced Hindi Speech Sentiment Analysis App...")
|
|
| 15 |
# 1. LOAD MODELS
|
| 16 |
# ============================================
|
| 17 |
|
| 18 |
-
# Load
|
| 19 |
-
print("π Loading
|
| 20 |
try:
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
sentiment_model = AutoModelForSequenceClassification.from_pretrained(sentiment_model_name)
|
| 24 |
sentiment_pipeline = pipeline(
|
| 25 |
"text-classification",
|
| 26 |
-
model=
|
| 27 |
-
tokenizer=sentiment_tokenizer,
|
| 28 |
top_k=None
|
| 29 |
)
|
| 30 |
-
print("β
|
| 31 |
except Exception as e:
|
| 32 |
print(f"β Error loading sentiment model: {e}")
|
| 33 |
raise
|
|
@@ -265,21 +263,29 @@ def enhanced_sentiment_analysis(text, prosodic_features, raw_results):
|
|
| 265 |
"""
|
| 266 |
Enhanced sentiment analysis combining text and prosodic features
|
| 267 |
"""
|
| 268 |
-
# Parse raw results
|
| 269 |
sentiment_scores = {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 270 |
label_mapping = {
|
| 271 |
-
'negative': 'Negative',
|
| 272 |
-
'neutral': 'Neutral',
|
| 273 |
-
'positive': 'Positive',
|
| 274 |
'LABEL_0': 'Negative',
|
| 275 |
'LABEL_1': 'Neutral',
|
| 276 |
-
'LABEL_2': 'Positive'
|
|
|
|
|
|
|
|
|
|
| 277 |
}
|
| 278 |
|
| 279 |
for result in raw_results[0]:
|
| 280 |
-
label = result['label']
|
| 281 |
-
|
| 282 |
-
|
|
|
|
| 283 |
|
| 284 |
# Ensure all three sentiments exist
|
| 285 |
for sentiment in ['Negative', 'Neutral', 'Positive']:
|
|
@@ -495,7 +501,7 @@ demo = gr.Interface(
|
|
| 495 |
|
| 496 |
### β¨ Advanced Features:
|
| 497 |
- **ποΈ IndicWhisper ASR** - Specialized Hindi transcription model
|
| 498 |
-
- **π§
|
| 499 |
- **π΅ Prosodic Analysis** - Voice tone, pitch, energy detection
|
| 500 |
- **π Mixed Emotion Detection** - Handles complex feelings
|
| 501 |
- **π Hinglish Support** - Works with Hindi + English mix
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
import torch
|
| 3 |
from transformers import pipeline, AutoModelForSequenceClassification, AutoTokenizer
|
| 4 |
import librosa
|
|
|
|
| 15 |
# 1. LOAD MODELS
|
| 16 |
# ============================================
|
| 17 |
|
| 18 |
+
# Load Hindi Sentiment Model
|
| 19 |
+
print("π Loading Hindi sentiment analysis model...")
|
| 20 |
try:
|
| 21 |
+
# Use LondonStory's Hindi sentiment model
|
| 22 |
+
sentiment_model_name = "LondonStory/txlm-roberta-hindi-sentiment"
|
|
|
|
| 23 |
sentiment_pipeline = pipeline(
|
| 24 |
"text-classification",
|
| 25 |
+
model=sentiment_model_name,
|
|
|
|
| 26 |
top_k=None
|
| 27 |
)
|
| 28 |
+
print("β
Hindi sentiment model loaded successfully")
|
| 29 |
except Exception as e:
|
| 30 |
print(f"β Error loading sentiment model: {e}")
|
| 31 |
raise
|
|
|
|
| 263 |
"""
|
| 264 |
Enhanced sentiment analysis combining text and prosodic features
|
| 265 |
"""
|
| 266 |
+
# Parse raw results - handle different model formats
|
| 267 |
sentiment_scores = {}
|
| 268 |
+
|
| 269 |
+
# Check if results are in the expected format
|
| 270 |
+
if not raw_results or not isinstance(raw_results, list) or len(raw_results) == 0:
|
| 271 |
+
print("β οΈ Unexpected sentiment results format")
|
| 272 |
+
return {'Negative': 0.33, 'Neutral': 0.34, 'Positive': 0.33}, 0.34, False
|
| 273 |
+
|
| 274 |
+
# LondonStory model uses: LABEL_0 (Negative), LABEL_1 (Neutral), LABEL_2 (Positive)
|
| 275 |
label_mapping = {
|
|
|
|
|
|
|
|
|
|
| 276 |
'LABEL_0': 'Negative',
|
| 277 |
'LABEL_1': 'Neutral',
|
| 278 |
+
'LABEL_2': 'Positive',
|
| 279 |
+
'negative': 'Negative',
|
| 280 |
+
'neutral': 'Neutral',
|
| 281 |
+
'positive': 'Positive'
|
| 282 |
}
|
| 283 |
|
| 284 |
for result in raw_results[0]:
|
| 285 |
+
label = result['label']
|
| 286 |
+
score = result['score']
|
| 287 |
+
mapped_label = label_mapping.get(label, 'Neutral')
|
| 288 |
+
sentiment_scores[mapped_label] = score
|
| 289 |
|
| 290 |
# Ensure all three sentiments exist
|
| 291 |
for sentiment in ['Negative', 'Neutral', 'Positive']:
|
|
|
|
| 501 |
|
| 502 |
### β¨ Advanced Features:
|
| 503 |
- **ποΈ IndicWhisper ASR** - Specialized Hindi transcription model
|
| 504 |
+
- **π§ txlm-RoBERTa** - Hindi-optimized sentiment analysis
|
| 505 |
- **π΅ Prosodic Analysis** - Voice tone, pitch, energy detection
|
| 506 |
- **π Mixed Emotion Detection** - Handles complex feelings
|
| 507 |
- **π Hinglish Support** - Works with Hindi + English mix
|