Update app.py
Browse files
app.py
CHANGED
@@ -1,291 +1,488 @@
|
|
1 |
-
import
|
2 |
-
|
3 |
-
import
|
4 |
-
|
5 |
-
|
6 |
-
from
|
7 |
-
import
|
8 |
-
import
|
|
|
|
|
|
|
|
|
9 |
|
10 |
-
|
11 |
-
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
12 |
-
SEED = 42; SAMPLE_RATE = 22050; TEMPERATURE = 0.7
|
13 |
-
torch.manual_seed(SEED); np.random.seed(SEED)
|
14 |
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
|
22 |
-
class
|
23 |
-
def __init__(self):
|
24 |
-
|
25 |
-
self.
|
26 |
-
|
27 |
-
|
28 |
-
def setup_models(self):
|
29 |
-
# 1. ASR: Parakeet RNNT
|
30 |
-
print("π’ Loading ASR model...")
|
31 |
-
try:
|
32 |
-
self.asr_model = nemo_asr.models.EncDecRNNTBPEModel.from_pretrained(
|
33 |
-
"nvidia/parakeet-rnnt-1.1b"
|
34 |
-
).to(DEVICE).eval()
|
35 |
-
print("β
Parakeet ASR loaded")
|
36 |
-
except Exception as e:
|
37 |
-
print(f"β οΈ Parakeet failed: {e}")
|
38 |
-
print("π Loading Whisper fallback...")
|
39 |
-
self.asr_pipeline = pipeline(
|
40 |
-
"automatic-speech-recognition",
|
41 |
-
model="openai/whisper-base.en",
|
42 |
-
device=0 if DEVICE == "cuda" else -1
|
43 |
-
)
|
44 |
-
print("β
Whisper ASR loaded")
|
45 |
-
|
46 |
-
# 2. SER: Emotion classifier (simplified for demo)
|
47 |
-
print("π Setting up emotion recognition...")
|
48 |
-
X_demo = np.random.rand(100, 128)
|
49 |
-
y_demo = np.random.randint(0, 5, 100) # 5 emotions: neutral, happy, sad, angry, surprised
|
50 |
-
self.ser_clf = LogisticRegression().fit(X_demo, y_demo)
|
51 |
-
self.emotion_labels = ["neutral", "happy", "sad", "angry", "surprised"]
|
52 |
-
print("β
SER model ready")
|
53 |
-
|
54 |
-
# 3. LLM: Conversational model
|
55 |
-
print("π§ Loading LLM...")
|
56 |
-
bnb_cfg = BitsAndBytesConfig(
|
57 |
-
load_in_4bit=True,
|
58 |
-
bnb_4bit_compute_dtype=torch.float16,
|
59 |
-
bnb_4bit_use_double_quant=True,
|
60 |
-
bnb_4bit_quant_type="nf4"
|
61 |
-
)
|
62 |
|
63 |
-
|
64 |
-
self.
|
65 |
-
|
|
|
|
|
|
|
|
|
66 |
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
71 |
torch_dtype=torch.float16,
|
72 |
-
|
73 |
)
|
74 |
-
print("β
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
try:
|
79 |
-
self.tts = TTS("tts_models/en/ljspeech/tacotron2-DDC").to(DEVICE)
|
80 |
-
print("β
TTS loaded")
|
81 |
-
except Exception as e:
|
82 |
-
print(f"β οΈ TTS error: {e}")
|
83 |
-
self.tts = None
|
84 |
-
|
85 |
-
# Memory cleanup
|
86 |
-
if DEVICE == "cuda":
|
87 |
-
torch.cuda.empty_cache()
|
88 |
-
gc.collect()
|
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 |
-
wav = np.array(wav, dtype=np.float32)
|
146 |
-
# Normalize audio
|
147 |
-
wav = wav / np.max(np.abs(wav)) if np.max(np.abs(wav)) > 0 else wav
|
148 |
-
return (SAMPLE_RATE, (wav * 32767).astype(np.int16))
|
149 |
-
else:
|
150 |
-
# Return silence if TTS fails
|
151 |
-
return (SAMPLE_RATE, np.zeros(SAMPLE_RATE, dtype=np.int16))
|
152 |
-
except Exception as e:
|
153 |
-
print(f"TTS Error: {e}")
|
154 |
-
return (SAMPLE_RATE, np.zeros(SAMPLE_RATE, dtype=np.int16))
|
155 |
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
return chat_history, None, ""
|
160 |
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
# Step 4: Text to Speech
|
174 |
-
audio_response = self.synthesize(ai_response)
|
175 |
-
|
176 |
-
# Update chat history
|
177 |
-
chat_history.append([user_text, ai_response])
|
178 |
-
|
179 |
-
# Memory cleanup
|
180 |
-
if DEVICE == "cuda":
|
181 |
-
torch.cuda.empty_cache()
|
182 |
-
gc.collect()
|
183 |
-
|
184 |
-
return chat_history, audio_response, f"You said: {user_text} (detected emotion: {emotion})"
|
185 |
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
190 |
|
191 |
-
|
192 |
-
|
193 |
-
|
|
|
|
|
|
|
|
|
194 |
|
195 |
-
# Gradio Interface
|
196 |
def create_interface():
|
197 |
with gr.Blocks(
|
198 |
-
title="
|
199 |
-
theme=gr.themes.Soft()
|
|
|
|
|
|
|
|
|
|
|
200 |
) as demo:
|
201 |
|
202 |
gr.HTML("""
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
</
|
|
|
207 |
""")
|
208 |
|
209 |
with gr.Row():
|
210 |
-
with gr.Column(scale=
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
|
215 |
-
)
|
216 |
|
|
|
|
|
217 |
audio_input = gr.Audio(
|
218 |
-
label="
|
219 |
sources=["microphone"],
|
220 |
type="numpy",
|
221 |
-
|
222 |
)
|
223 |
|
224 |
-
|
225 |
-
submit_btn = gr.Button("π¬ Process Speech", variant="primary", scale=2)
|
226 |
-
clear_btn = gr.Button("ποΈ Clear Chat", variant="secondary", scale=1)
|
227 |
-
|
228 |
-
with gr.Column(scale=1):
|
229 |
-
audio_output = gr.Audio(
|
230 |
-
label="π AI Response",
|
231 |
-
type="numpy",
|
232 |
-
autoplay=True
|
233 |
-
)
|
234 |
|
|
|
235 |
status_display = gr.Textbox(
|
236 |
label="π Status",
|
237 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
238 |
interactive=False
|
239 |
)
|
240 |
|
241 |
-
|
242 |
-
|
243 |
-
|
244 |
-
|
245 |
-
|
246 |
-
|
247 |
-
|
248 |
-
|
249 |
-
|
250 |
-
|
251 |
-
|
252 |
-
|
253 |
-
|
254 |
-
|
255 |
-
|
256 |
-
|
257 |
-
|
258 |
-
|
259 |
-
|
260 |
-
|
261 |
-
|
262 |
-
|
263 |
-
|
264 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
265 |
)
|
266 |
|
267 |
-
|
268 |
-
fn=
|
269 |
-
|
|
|
270 |
)
|
271 |
|
272 |
-
|
273 |
-
fn=
|
274 |
-
|
275 |
-
outputs=[chatbot, audio_output, status_display]
|
276 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
277 |
|
278 |
return demo
|
279 |
|
280 |
-
# Launch application
|
281 |
if __name__ == "__main__":
|
282 |
-
print("
|
283 |
-
demo = create_interface()
|
284 |
|
285 |
-
|
286 |
-
|
287 |
-
|
288 |
-
|
289 |
-
|
290 |
-
|
291 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import torch
|
3 |
+
import numpy as np
|
4 |
+
import librosa
|
5 |
+
import soundfile as sf
|
6 |
+
from transformers import pipeline, AutoTokenizer, AutoModelForCausalLM
|
7 |
+
from dia.model import Dia
|
8 |
+
import warnings
|
9 |
+
import json
|
10 |
+
import time
|
11 |
+
from datetime import datetime
|
12 |
+
import os
|
13 |
|
14 |
+
warnings.filterwarnings("ignore")
|
|
|
|
|
|
|
15 |
|
16 |
+
# Global models
|
17 |
+
ultravox_pipe = None
|
18 |
+
qwen_model = None
|
19 |
+
qwen_tokenizer = None
|
20 |
+
dia_model = None
|
21 |
+
conversation_history = []
|
22 |
|
23 |
+
class ConversationManager:
|
24 |
+
def __init__(self, max_exchanges=5):
|
25 |
+
self.history = []
|
26 |
+
self.max_exchanges = max_exchanges
|
27 |
+
self.current_emotion = "neutral"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
|
29 |
+
def add_exchange(self, user_input, ai_response, emotion="neutral"):
|
30 |
+
self.history.append({
|
31 |
+
"timestamp": datetime.now().isoformat(),
|
32 |
+
"user": user_input,
|
33 |
+
"ai": ai_response,
|
34 |
+
"emotion": emotion
|
35 |
+
})
|
36 |
|
37 |
+
# Keep only last max_exchanges
|
38 |
+
if len(self.history) > self.max_exchanges:
|
39 |
+
self.history = self.history[-self.max_exchanges:]
|
40 |
+
|
41 |
+
def get_context(self):
|
42 |
+
context = ""
|
43 |
+
for exchange in self.history[-3:]: # Last 3 exchanges for context
|
44 |
+
context += f"User: {exchange['user']}\nAI: {exchange['ai']}\n"
|
45 |
+
return context
|
46 |
+
|
47 |
+
def clear(self):
|
48 |
+
self.history = []
|
49 |
+
self.current_emotion = "neutral"
|
50 |
+
|
51 |
+
def load_models():
|
52 |
+
"""Load all models with optimized memory usage"""
|
53 |
+
global ultravox_pipe, qwen_model, qwen_tokenizer, dia_model
|
54 |
+
|
55 |
+
print("π Loading Ultravox for ASR + Emotion Recognition...")
|
56 |
+
try:
|
57 |
+
ultravox_pipe = pipeline(
|
58 |
+
model='fixie-ai/ultravox-v0_4',
|
59 |
+
trust_remote_code=True,
|
60 |
torch_dtype=torch.float16,
|
61 |
+
device_map="auto"
|
62 |
)
|
63 |
+
print("β
Ultravox loaded successfully!")
|
64 |
+
except Exception as e:
|
65 |
+
print(f"β Error loading Ultravox: {e}")
|
66 |
+
return False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
|
68 |
+
print("π§ Loading Qwen2.5-1.5B for conversation...")
|
69 |
+
try:
|
70 |
+
qwen_tokenizer = AutoTokenizer.from_pretrained(
|
71 |
+
"Qwen/Qwen2.5-1.5B-Instruct",
|
72 |
+
trust_remote_code=True
|
73 |
+
)
|
74 |
+
qwen_model = AutoModelForCausalLM.from_pretrained(
|
75 |
+
"Qwen/Qwen2.5-1.5B-Instruct",
|
76 |
+
torch_dtype=torch.float16,
|
77 |
+
device_map="auto",
|
78 |
+
trust_remote_code=True
|
79 |
+
)
|
80 |
+
print("β
Qwen loaded successfully!")
|
81 |
+
except Exception as e:
|
82 |
+
print(f"β Error loading Qwen: {e}")
|
83 |
+
return False
|
84 |
|
85 |
+
print("ποΈ Loading Enhanced Dia TTS...")
|
86 |
+
try:
|
87 |
+
dia_model = Dia.from_pretrained(
|
88 |
+
"nari-labs/Dia-1.6B",
|
89 |
+
compute_dtype="float16"
|
90 |
+
)
|
91 |
+
print("β
Dia TTS loaded successfully!")
|
92 |
+
except Exception as e:
|
93 |
+
print(f"β Error loading Dia: {e}")
|
94 |
+
return False
|
95 |
|
96 |
+
return True
|
97 |
+
|
98 |
+
def detect_emotion_from_speech(audio_input):
|
99 |
+
"""Extract emotion from speech using Ultravox understanding"""
|
100 |
+
try:
|
101 |
+
# Emotional keywords mapping
|
102 |
+
emotion_keywords = {
|
103 |
+
"happy": ["laugh", "excited", "joy", "great", "awesome", "wonderful"],
|
104 |
+
"sad": ["cry", "upset", "disappointed", "sorry", "terrible"],
|
105 |
+
"angry": ["mad", "furious", "annoyed", "frustrated"],
|
106 |
+
"surprised": ["wow", "amazing", "incredible", "unbelievable"],
|
107 |
+
"neutral": []
|
108 |
+
}
|
109 |
+
|
110 |
+
# Use Ultravox to understand speech context
|
111 |
+
turns = [
|
112 |
+
{"role": "system", "content": "Analyze the emotional tone of the user's speech. Respond with just the emotion: happy, sad, angry, surprised, or neutral."},
|
113 |
+
]
|
114 |
+
|
115 |
+
result = ultravox_pipe({
|
116 |
+
'audio': audio_input,
|
117 |
+
'turns': turns,
|
118 |
+
'sampling_rate': 16000
|
119 |
+
}, max_new_tokens=10)
|
120 |
+
|
121 |
+
detected_emotion = result[0]['generated_text'].lower().strip()
|
122 |
+
|
123 |
+
# Validate emotion
|
124 |
+
valid_emotions = ["happy", "sad", "angry", "surprised", "neutral"]
|
125 |
+
if detected_emotion not in valid_emotions:
|
126 |
+
detected_emotion = "neutral"
|
127 |
|
128 |
+
return detected_emotion
|
129 |
+
except:
|
130 |
+
return "neutral"
|
131 |
+
|
132 |
+
def speech_to_text_with_emotion(audio_input):
|
133 |
+
"""Convert speech to text and detect emotion"""
|
134 |
+
try:
|
135 |
+
if audio_input is None:
|
136 |
+
return "", "neutral"
|
137 |
+
|
138 |
+
# Convert audio format if needed
|
139 |
+
if isinstance(audio_input, tuple):
|
140 |
+
sample_rate, audio_data = audio_input
|
141 |
+
audio_data = audio_data.astype(np.float32)
|
142 |
+
if len(audio_data.shape) > 1:
|
143 |
+
audio_data = audio_data.mean(axis=1)
|
144 |
+
else:
|
145 |
+
audio_data = audio_input
|
146 |
+
sample_rate = 16000
|
147 |
+
|
148 |
+
# Resample to 16kHz if needed
|
149 |
+
if sample_rate != 16000:
|
150 |
+
audio_data = librosa.resample(audio_data, orig_sr=sample_rate, target_sr=16000)
|
151 |
+
|
152 |
+
# Speech to text using Ultravox
|
153 |
+
turns = [
|
154 |
+
{"role": "system", "content": "Transcribe the user's speech accurately. Only provide the transcription."},
|
155 |
+
]
|
156 |
+
|
157 |
+
result = ultravox_pipe({
|
158 |
+
'audio': audio_data,
|
159 |
+
'turns': turns,
|
160 |
+
'sampling_rate': 16000
|
161 |
+
}, max_new_tokens=100)
|
162 |
+
|
163 |
+
transcription = result[0]['generated_text'].strip()
|
164 |
+
|
165 |
+
# Detect emotion
|
166 |
+
emotion = detect_emotion_from_speech(audio_data)
|
167 |
+
|
168 |
+
return transcription, emotion
|
169 |
+
|
170 |
+
except Exception as e:
|
171 |
+
print(f"Error in STT: {e}")
|
172 |
+
return "Sorry, I couldn't understand that.", "neutral"
|
173 |
+
|
174 |
+
def generate_contextual_response(user_input, emotion, conversation_manager):
|
175 |
+
"""Generate contextual response using Qwen"""
|
176 |
+
try:
|
177 |
+
context = conversation_manager.get_context()
|
178 |
+
|
179 |
+
# Emotional system prompt
|
180 |
+
emotional_prompts = {
|
181 |
+
"happy": "Respond with enthusiasm and joy. Use exclamations and positive language.",
|
182 |
+
"sad": "Respond with empathy and comfort. Be gentle and understanding.",
|
183 |
+
"angry": "Respond calmly and try to de-escalate. Be patient and helpful.",
|
184 |
+
"surprised": "Share in the surprise and excitement. Be engaging and curious.",
|
185 |
+
"neutral": "Respond naturally and conversationally."
|
186 |
+
}
|
187 |
+
|
188 |
+
system_prompt = f"""You are Maya, a friendly and emotionally intelligent AI assistant.
|
189 |
+
{emotional_prompts.get(emotion, emotional_prompts['neutral'])}
|
190 |
+
|
191 |
+
Previous conversation context:
|
192 |
+
{context}
|
193 |
+
|
194 |
+
Current user emotion: {emotion}
|
195 |
+
|
196 |
+
Guidelines:
|
197 |
+
- Keep responses concise (1-2 sentences)
|
198 |
+
- Match the user's emotional tone
|
199 |
+
- Be natural and conversational
|
200 |
+
- Include emotional expressions when appropriate like (laughs), (sighs), etc.
|
201 |
+
"""
|
202 |
+
|
203 |
+
messages = [
|
204 |
+
{"role": "system", "content": system_prompt},
|
205 |
+
{"role": "user", "content": user_input}
|
206 |
+
]
|
207 |
+
|
208 |
+
# Generate response
|
209 |
+
text = qwen_tokenizer.apply_chat_template(
|
210 |
+
messages,
|
211 |
+
tokenize=False,
|
212 |
+
add_generation_prompt=True
|
213 |
+
)
|
214 |
+
|
215 |
+
model_inputs = qwen_tokenizer([text], return_tensors="pt").to(qwen_model.device)
|
216 |
+
|
217 |
+
with torch.no_grad():
|
218 |
+
generated_ids = qwen_model.generate(
|
219 |
+
model_inputs.input_ids,
|
220 |
+
max_new_tokens=100,
|
221 |
+
do_sample=True,
|
222 |
+
temperature=0.7,
|
223 |
+
pad_token_id=qwen_tokenizer.eos_token_id
|
224 |
+
)
|
225 |
+
|
226 |
+
generated_ids = [
|
227 |
+
output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)
|
228 |
+
]
|
229 |
+
|
230 |
+
response = qwen_tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
231 |
+
|
232 |
+
return response.strip()
|
233 |
+
|
234 |
+
except Exception as e:
|
235 |
+
print(f"Error in response generation: {e}")
|
236 |
+
return "I'm sorry, I'm having trouble processing that right now."
|
237 |
+
|
238 |
+
def text_to_speech_emotional(text, emotion="neutral", speaker="S1"):
|
239 |
+
"""Convert text to emotional speech using enhanced Dia"""
|
240 |
+
try:
|
241 |
+
# Clear GPU cache
|
242 |
+
if torch.cuda.is_available():
|
243 |
+
torch.cuda.empty_cache()
|
244 |
+
|
245 |
+
# Emotional markers for Dia
|
246 |
+
emotional_markers = {
|
247 |
+
"happy": "(excited) ",
|
248 |
+
"sad": "(sad) ",
|
249 |
+
"angry": "(frustrated) ",
|
250 |
+
"surprised": "(surprised) ",
|
251 |
+
"neutral": ""
|
252 |
+
}
|
253 |
+
|
254 |
+
# Add emotional context and natural pauses
|
255 |
+
enhanced_text = f"[{speaker}] {emotional_markers.get(emotion, '')}{text}"
|
256 |
+
|
257 |
+
# Add natural breathing pauses for longer text
|
258 |
+
if len(text) > 50:
|
259 |
+
enhanced_text = enhanced_text.replace(". ", ". (pause) ")
|
260 |
+
enhanced_text = enhanced_text.replace("! ", "! (pause) ")
|
261 |
+
enhanced_text = enhanced_text.replace("? ", "? (pause) ")
|
262 |
+
|
263 |
+
print(f"Generating TTS for: {enhanced_text[:100]}...")
|
264 |
+
|
265 |
+
# Generate audio
|
266 |
+
with torch.no_grad():
|
267 |
+
audio_output = dia_model.generate(
|
268 |
+
enhanced_text,
|
269 |
+
use_torch_compile=False,
|
270 |
+
verbose=False
|
271 |
+
)
|
272 |
+
|
273 |
+
# Process audio output
|
274 |
+
if isinstance(audio_output, torch.Tensor):
|
275 |
+
audio_output = audio_output.cpu().numpy()
|
276 |
+
|
277 |
+
# Normalize audio
|
278 |
+
if len(audio_output) > 0:
|
279 |
+
max_val = np.max(np.abs(audio_output))
|
280 |
+
if max_val > 1.0:
|
281 |
+
audio_output = audio_output / max_val * 0.95
|
282 |
+
|
283 |
+
return (44100, audio_output)
|
284 |
+
|
285 |
+
except Exception as e:
|
286 |
+
print(f"Error in TTS: {e}")
|
287 |
+
return None
|
288 |
+
|
289 |
+
# Initialize conversation manager
|
290 |
+
conv_manager = ConversationManager()
|
291 |
+
|
292 |
+
def start_call():
|
293 |
+
"""Initialize call and return greeting"""
|
294 |
+
conv_manager.clear()
|
295 |
+
greeting_text = "Hello! I'm Maya, your AI assistant. How can I help you today?"
|
296 |
+
greeting_audio = text_to_speech_emotional(greeting_text, "happy")
|
297 |
|
298 |
+
return greeting_audio, greeting_text, "Call started! π"
|
299 |
+
|
300 |
+
def process_conversation(audio_input):
|
301 |
+
"""Main conversation processing pipeline"""
|
302 |
+
if audio_input is None:
|
303 |
+
return None, "Please record some audio first.", "", "No audio input received."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
304 |
|
305 |
+
try:
|
306 |
+
# Step 1: Speech to Text + Emotion Detection
|
307 |
+
user_text, emotion = speech_to_text_with_emotion(audio_input)
|
|
|
308 |
|
309 |
+
if not user_text or user_text.strip() == "":
|
310 |
+
return None, "I didn't catch that. Could you please repeat?", "", "No speech detected."
|
311 |
+
|
312 |
+
# Step 2: Generate contextual response
|
313 |
+
ai_response = generate_contextual_response(user_text, emotion, conv_manager)
|
314 |
+
|
315 |
+
# Step 3: Convert to speech
|
316 |
+
response_audio = text_to_speech_emotional(ai_response, emotion)
|
317 |
+
|
318 |
+
# Step 4: Update conversation history
|
319 |
+
conv_manager.add_exchange(user_text, ai_response, emotion)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
320 |
|
321 |
+
status = f"β
Processed | Emotion: {emotion} | Exchange: {len(conv_manager.history)}/5"
|
322 |
+
|
323 |
+
return response_audio, ai_response, user_text, status
|
324 |
+
|
325 |
+
except Exception as e:
|
326 |
+
error_msg = f"β Error processing conversation: {str(e)}"
|
327 |
+
return None, "I'm sorry, I encountered an error. Please try again.", "", error_msg
|
328 |
+
|
329 |
+
def get_conversation_history():
|
330 |
+
"""Return formatted conversation history"""
|
331 |
+
if not conv_manager.history:
|
332 |
+
return "No conversation history yet."
|
333 |
+
|
334 |
+
history_text = "π **Conversation History:**\n\n"
|
335 |
+
for i, exchange in enumerate(conv_manager.history, 1):
|
336 |
+
timestamp = exchange['timestamp'][:19].replace('T', ' ')
|
337 |
+
history_text += f"**Exchange {i}** ({timestamp}) - Emotion: {exchange['emotion']}\n"
|
338 |
+
history_text += f"π€ **You:** {exchange['user']}\n"
|
339 |
+
history_text += f"π€ **Maya:** {exchange['ai']}\n\n"
|
340 |
+
|
341 |
+
return history_text
|
342 |
|
343 |
+
def end_call():
|
344 |
+
"""End call and clear conversation"""
|
345 |
+
farewell_text = "Thank you for talking with me! Have a great day!"
|
346 |
+
farewell_audio = text_to_speech_emotional(farewell_text, "happy")
|
347 |
+
conv_manager.clear()
|
348 |
+
|
349 |
+
return farewell_audio, farewell_text, "Call ended. πβ"
|
350 |
|
351 |
+
# Create Gradio Interface
|
352 |
def create_interface():
|
353 |
with gr.Blocks(
|
354 |
+
title="Maya AI - Advanced Speech-to-Speech Assistant",
|
355 |
+
theme=gr.themes.Soft(),
|
356 |
+
css="""
|
357 |
+
.call-button { background: linear-gradient(45deg, #FF6B6B, #4ECDC4) !important; }
|
358 |
+
.record-button { background: linear-gradient(45deg, #45B7D1, #96CEB4) !important; }
|
359 |
+
.end-button { background: linear-gradient(45deg, #FFA07A, #FF6347) !important; }
|
360 |
+
"""
|
361 |
) as demo:
|
362 |
|
363 |
gr.HTML("""
|
364 |
+
<div style="text-align: center; padding: 20px; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); border-radius: 15px; margin-bottom: 20px;">
|
365 |
+
<h1 style="color: white; margin: 0; font-size: 2.5em;">ποΈ Maya AI</h1>
|
366 |
+
<p style="color: white; margin: 10px 0; font-size: 1.2em;">Advanced Speech-to-Speech Conversational AI</p>
|
367 |
+
<p style="color: #E8E8E8; margin: 0;">Natural β’ Emotional β’ Contextual</p>
|
368 |
+
</div>
|
369 |
""")
|
370 |
|
371 |
with gr.Row():
|
372 |
+
with gr.Column(scale=1):
|
373 |
+
# Call Controls
|
374 |
+
gr.HTML("<h3>π Call Controls</h3>")
|
375 |
+
start_btn = gr.Button("π Start Call", elem_classes="call-button", size="lg")
|
376 |
+
end_btn = gr.Button("πβ End Call", elem_classes="end-button", size="lg")
|
|
|
377 |
|
378 |
+
# Audio Input
|
379 |
+
gr.HTML("<h3>π€ Voice Input</h3>")
|
380 |
audio_input = gr.Audio(
|
381 |
+
label="Record Your Message",
|
382 |
sources=["microphone"],
|
383 |
type="numpy",
|
384 |
+
elem_classes="record-button"
|
385 |
)
|
386 |
|
387 |
+
process_btn = gr.Button("π― Process Message", variant="primary", size="lg")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
388 |
|
389 |
+
# Status
|
390 |
status_display = gr.Textbox(
|
391 |
label="π Status",
|
392 |
+
interactive=False,
|
393 |
+
lines=2
|
394 |
+
)
|
395 |
+
|
396 |
+
with gr.Column(scale=2):
|
397 |
+
# AI Response Audio
|
398 |
+
gr.HTML("<h3>π Maya's Response</h3>")
|
399 |
+
response_audio = gr.Audio(
|
400 |
+
label="Maya's Voice Response",
|
401 |
+
type="numpy",
|
402 |
interactive=False
|
403 |
)
|
404 |
|
405 |
+
# Text Displays
|
406 |
+
with gr.Row():
|
407 |
+
with gr.Column():
|
408 |
+
user_text_display = gr.Textbox(
|
409 |
+
label="π€ What You Said",
|
410 |
+
interactive=False,
|
411 |
+
lines=3
|
412 |
+
)
|
413 |
+
|
414 |
+
with gr.Column():
|
415 |
+
ai_text_display = gr.Textbox(
|
416 |
+
label="π€ Maya's Response",
|
417 |
+
interactive=False,
|
418 |
+
lines=3
|
419 |
+
)
|
420 |
+
|
421 |
+
# Conversation History
|
422 |
+
with gr.Row():
|
423 |
+
with gr.Column():
|
424 |
+
gr.HTML("<h3>π Conversation History</h3>")
|
425 |
+
history_btn = gr.Button("π Show History", variant="secondary")
|
426 |
+
history_display = gr.Markdown(
|
427 |
+
value="No conversation history yet.",
|
428 |
+
label="Conversation Log"
|
429 |
+
)
|
430 |
+
|
431 |
+
# Event Handlers
|
432 |
+
start_btn.click(
|
433 |
+
fn=start_call,
|
434 |
+
outputs=[response_audio, ai_text_display, status_display]
|
435 |
)
|
436 |
|
437 |
+
process_btn.click(
|
438 |
+
fn=process_conversation,
|
439 |
+
inputs=[audio_input],
|
440 |
+
outputs=[response_audio, ai_text_display, user_text_display, status_display]
|
441 |
)
|
442 |
|
443 |
+
end_btn.click(
|
444 |
+
fn=end_call,
|
445 |
+
outputs=[response_audio, ai_text_display, status_display]
|
|
|
446 |
)
|
447 |
+
|
448 |
+
history_btn.click(
|
449 |
+
fn=get_conversation_history,
|
450 |
+
outputs=[history_display]
|
451 |
+
)
|
452 |
+
|
453 |
+
# Usage Instructions
|
454 |
+
gr.HTML("""
|
455 |
+
<div style="margin-top: 20px; padding: 20px; background: #f8f9fa; border-radius: 10px; border-left: 5px solid #007bff;">
|
456 |
+
<h3>π‘ How to Use Maya AI:</h3>
|
457 |
+
<ol>
|
458 |
+
<li><strong>Start Call:</strong> Click "π Start Call" to begin your conversation</li>
|
459 |
+
<li><strong>Record:</strong> Use the microphone to record your message</li>
|
460 |
+
<li><strong>Process:</strong> Click "π― Process Message" to get Maya's response</li>
|
461 |
+
<li><strong>Listen:</strong> Maya will respond with natural, emotional speech</li>
|
462 |
+
<li><strong>Continue:</strong> Keep the conversation going (up to 5 exchanges)</li>
|
463 |
+
<li><strong>End:</strong> Click "πβ End Call" when finished</li>
|
464 |
+
</ol>
|
465 |
+
|
466 |
+
<h4>π Emotional Features:</h4>
|
467 |
+
<p>Maya automatically detects your emotions and responds accordingly with natural expressions, breathing pauses, and contextual understanding!</p>
|
468 |
+
</div>
|
469 |
+
""")
|
470 |
|
471 |
return demo
|
472 |
|
|
|
473 |
if __name__ == "__main__":
|
474 |
+
print("π Initializing Maya AI System...")
|
|
|
475 |
|
476 |
+
if load_models():
|
477 |
+
print("β
All models loaded successfully!")
|
478 |
+
print("π Launching Maya AI Interface...")
|
479 |
+
|
480 |
+
demo = create_interface()
|
481 |
+
demo.launch(
|
482 |
+
server_name="0.0.0.0",
|
483 |
+
server_port=7860,
|
484 |
+
share=True,
|
485 |
+
show_error=True
|
486 |
+
)
|
487 |
+
else:
|
488 |
+
print("β Failed to load models. Please check your setup.")
|