-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser_interface.py
More file actions
168 lines (149 loc) · 6.71 KB
/
user_interface.py
File metadata and controls
168 lines (149 loc) · 6.71 KB
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
import gradio as gr
import whisper
import keras
import pickle
from keras.preprocessing.sequence import pad_sequences
import numpy as np
import re, emoji, contractions
from datetime import datetime
import os
import logging
import soundfile as sf
# Set up logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# Load models and tokenizer
try:
logger.info("Loading sentiment model...")
sentiment_model = keras.models.load_model('imdb_gru.keras')
logger.info("Loading emotion model...")
emotion_model = keras.models.load_model('emotion_model_transformer.keras')
logger.info("Loading tokenizer...")
with open("tokenizer1.pkl", "rb") as f:
tokenizer = pickle.load(f)
max_len = 100
logger.info("Loading Whisper model...")
whisper_model = whisper.load_model("tiny")
logger.info("All models loaded successfully")
except Exception as e:
logger.error(f"Error loading models: {str(e)}")
raise
# Define emotion labels
emotion_labels = [
'admiration', 'amusement', 'anger', 'annoyance', 'approval', 'caring', 'confusion',
'curiosity', 'desire', 'disappointment', 'disapproval', 'disgust', 'embarrassment',
'excitement', 'fear', 'gratitude', 'grief', 'joy', 'love', 'nervousness', 'optimism',
'pride', 'realization', 'relief', 'remorse', 'sadness', 'surprise', 'neutral'
]
def preprocess_text(text):
if not isinstance(text, str): return ""
text = emoji.demojize(text, delimiters=(" ", " "))
text = contractions.fix(text)
text = re.sub(r'\b(Cuz|coz)\b', 'because', text, flags=re.IGNORECASE)
text = re.sub(r'\b(Ikr)\b', 'I know right', text, flags=re.IGNORECASE)
text = re.sub(r'\b(Faux pas)\b', 'mistake', text, flags=re.IGNORECASE)
text = text.lower()
text = re.sub(r'(.)\1{2,}', r'\1\1', text)
text = re.sub(r'\s+', ' ', text).strip()
return text
def predict_emotion(text):
try:
logger.info(f"Predicting emotion for text: {text[:50]}...")
processed = preprocess_text(text)
sequence = tokenizer.texts_to_sequences([processed])
padded = pad_sequences(sequence, maxlen=max_len, padding='post', truncating='post')
probs = emotion_model.predict(padded, verbose=0)[0]
top3 = probs.argsort()[-3:][::-1]
result = [(emotion_labels[i], float(probs[i])) for i in top3]
logger.info(f"Emotion prediction result: {result}")
return result
except Exception as e:
logger.error(f"Error in emotion prediction: {str(e)}")
return [("error", 1.0)]
def prediction_pipeline(text):
try:
logger.info(f"Predicting sentiment for text: {text[:50]}...")
processed = preprocess_text(text)
sequence = tokenizer.texts_to_sequences([processed])
padded = pad_sequences(sequence, maxlen=max_len, padding='post', truncating='post')
pred = sentiment_model.predict(padded, verbose=0)
result = "positive" if pred[0][0] > 0.5 else "negative"
logger.info(f"Sentiment prediction result: {result}")
return result
except Exception as e:
logger.error(f"Error in sentiment prediction: {str(e)}")
return "error"
def get_sentiment_and_emotion(text):
# Fake stub — replace with real LLM logic or LangChain prompt
return "neutral", "curiosity"
def analyze_audio(audio):
try:
logger.info(f"Received audio input: {audio}")
logger.info(f"Audio input type: {type(audio)}")
if audio is None:
logger.warning("No audio provided")
logger.info(f"Type of emotions: {type([])}, value: {[]}")
return "No audio provided", "", {}, "", ""
# Handle the audio input which could be a tuple of (sample_rate, audio_data)
if isinstance(audio, tuple):
sample_rate, audio_data = audio
# Save the audio data to a temporary file
temp_file = "temp_audio.wav"
sf.write(temp_file, audio_data, sample_rate)
audio = temp_file
elif not isinstance(audio, str):
logger.error(f"Unexpected audio input type: {type(audio)}")
logger.info(f"Type of emotions: {type([])}, value: {[]}")
return "Invalid audio input", "", {}, "", ""
# Check if audio file exists and is readable
if not os.path.exists(audio):
logger.error(f"Audio file not found: {audio}")
logger.info(f"Type of emotions: {type([])}, value: {[]}")
return "Audio file not found", "", {}, "", ""
# Get audio file info
try:
audio_info = sf.info(audio)
logger.info(f"Audio file info: {audio_info}")
logger.info(f"Audio file duration: {audio_info.duration} seconds")
logger.info(f"Audio file sample rate: {audio_info.samplerate} Hz")
logger.info(f"Audio file channels: {audio_info.channels}")
except Exception as e:
logger.error(f"Error reading audio file info: {str(e)}")
logger.info("Starting audio transcription...")
result = whisper_model.transcribe(audio)
transcription = result["text"]
logger.info(f"Transcription result: {transcription}")
if not transcription.strip():
logger.warning("No speech detected in audio")
logger.info(f"Type of emotions: {type([])}, value: {[]}")
return "No speech detected", "", {}, "", ""
logger.info("Starting sentiment analysis...")
sentiment = prediction_pipeline(transcription)
logger.info("Starting emotion analysis...")
emotions = predict_emotion(transcription)
logger.info(f"Type of emotions: {type(emotions)}, value: {emotions}")
sentiment_llm, emotion_llm = get_sentiment_and_emotion(transcription)
# Clean up temporary file if it was created
if isinstance(audio, tuple) and os.path.exists("temp_audio.wav"):
os.remove("temp_audio.wav")
return transcription, sentiment, dict(emotions), sentiment_llm, emotion_llm
except Exception as e:
logger.error(f"Error in audio analysis: {str(e)}", exc_info=True)
logger.info(f"Type of emotions: {type([])}, value: {[]}")
return f"Error: {str(e)}", "", {}, "", ""
demo = gr.Interface(
fn=analyze_audio,
inputs=gr.Audio(type="filepath"),
outputs=[
gr.Text(label="Transcription"),
gr.Text(label="Sentiment (Keras)"),
gr.Label(label="Top 3 Emotions (Keras)"),
gr.Text(label="Sentiment (LLM)"),
gr.Text(label="Emotion (LLM)")
],
live=False,
title="🎤 Emotion & Sentiment Analyzer",
description="Speak into the mic. Whisper will transcribe and two models will analyze your speech."
)
if __name__ == "__main__":
demo.launch(share=False)