JustNikunj commited on
Commit
a61c0d3
Β·
verified Β·
1 Parent(s): ddc433e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -18
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 XLM-RoBERTa Hindi Sentiment Model (Better accuracy)
19
- print("πŸ“š Loading XLM-RoBERTa sentiment analysis model...")
20
  try:
21
- sentiment_model_name = "cardiffnlp/twitter-xlm-roberta-base-sentiment"
22
- sentiment_tokenizer = AutoTokenizer.from_pretrained(sentiment_model_name)
23
- sentiment_model = AutoModelForSequenceClassification.from_pretrained(sentiment_model_name)
24
  sentiment_pipeline = pipeline(
25
  "text-classification",
26
- model=sentiment_model,
27
- tokenizer=sentiment_tokenizer,
28
  top_k=None
29
  )
30
- print("βœ… XLM-RoBERTa sentiment model loaded successfully")
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'].lower()
281
- mapped_label = label_mapping.get(label, label_mapping.get(result['label'], 'Neutral'))
282
- sentiment_scores[mapped_label] = result['score']
 
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
- - **🧠 XLM-RoBERTa** - Multilingual sentiment analysis
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