Spaces:
Runtime error
Runtime error
Update app.py
Browse files1 unmodified line2345567891011121314151617131819162021182220232422232425262725262728293031323334353629303132333435363738394041424344454647484950515253545556383940414243445758596061626364656667686970717273464748495051747576777879538081825556578384858659606162636465666768697071727374757677787980818283848586878889909192939495909196979893949599100101991001011021031041051061071081091021031041051061071081121131141151091101111121181191131141151211221231241251261271281291301311321331161171181191201211221231241251261271361281291301311321331341351381391401361371381421391401441411421461431441481451501461471481491501511551561571521531541551601611561571581592 unmodified lines1621631641701651661671681691701711771721731791801741751761771781841791801811821831841851861871881891901911861921931941951901911961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462002472482492502042512522532542092102552562572122132582592602152162612622632642652202212662672682692707 unmodified lines2782792802352362372812822832842392852862422872882892452462902912922932942512952962972542982993003012583023033043052622632642652662673062692702712723073083093103112742752762772782793123133143152812822833163173182852863193203212883223232903243253262932942952962972983273283293303313323033043333343353063363373383393403413423433443453463473483493503513523533543553563573583593603153163613623633193203213643653663243673683693263703713283293723733743753333763773783793803383393403813823833843423853863873463473483493503883893903913923933533543553943953963973983583593603994004013624024034043653663673684054064074084094103714114124134143763774154161 unmodified lineimport torchimport numpy as npimport librosafrom transformers import pipeline, AutoTokenizer, AutoModelForCausalLM, WhisperProcessor, WhisperForConditionalGenerationfrom transformers import ( pipeline, AutoTokenizer, AutoModelForCausalLM, WhisperProcessor, WhisperForConditionalGeneration, SpeechT5Processor, SpeechT5ForTextToSpeech, SpeechT5HifiGan)import soundfile as sfimport jsonimport timefrom datetime import datetimeimport osimport warningsfrom datasets import load_dataset
# Suppress warnings for cleaner outputwarnings.filterwarnings("ignore")
class ConversationalAI:class MayaAI: def __init__(self): # Set device self.device = "cuda" if torch.cuda.is_available() else "cpu" print(f"Using device: {self.device}") print(f"π Initializing Maya AI on {self.device}") # Load Whisper ASR with proper configuration self.asr_processor = WhisperProcessor.from_pretrained("openai/whisper-base.en") self.asr_model = WhisperForConditionalGeneration.from_pretrained( "openai/whisper-base.en", torch_dtype=torch.float16 if self.device == "cuda" else torch.float32 ).to(self.device) # Load Parakeet ASR (Best performance) try: from nemo.collections.asr import ASRModel self.asr_model = ASRModel.from_pretrained("nvidia/parakeet-tdt-0.6b-v2") print("β
Parakeet ASR loaded") except: self.asr_model = pipeline("automatic-speech-recognition", model="openai/whisper-large-v3", torch_dtype=torch.float16, device=self.device) print("β οΈ Using Whisper fallback") # Load LLM with proper device handling self.llm_tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-medium") self.llm_tokenizer.pad_token = self.llm_tokenizer.eos_token self.llm_model = AutoModelForCausalLM.from_pretrained( "microsoft/DialoGPT-medium", torch_dtype=torch.float16 if self.device == "cuda" else torch.float32, pad_token_id=self.llm_tokenizer.eos_token_id ).to(self.device) # Load FREE DeepSeek-V3 LLM (Best free option)[1][5] try: self.llm_tokenizer = AutoTokenizer.from_pretrained("deepseek-ai/deepseek-llm-67b-chat") self.llm_model = AutoModelForCausalLM.from_pretrained( "deepseek-ai/deepseek-llm-67b-chat", torch_dtype=torch.float16, device_map="auto", trust_remote_code=True ) print("β
DeepSeek-V3 loaded (FREE)") except: # Fallback to Llama 3.1 (also free) self.llm_tokenizer = AutoTokenizer.from_pretrained("meta-llama/Llama-3.1-70B-Instruct") self.llm_model = AutoModelForCausalLM.from_pretrained( "meta-llama/Llama-3.1-70B-Instruct", torch_dtype=torch.float16, device_map="auto" ) print("β
Llama 3.1 loaded (FREE fallback)") # Load TTS model self.tts_model = pipeline( "text-to-speech", model="microsoft/speecht5_tts", torch_dtype=torch.float16 if self.device == "cuda" else torch.float32, device=self.device ) # Load Emotion Recognition self.emotion_model = pipeline("audio-classification", model="superb/wav2vec2-base-superb-er", device=self.device) print("β
Emotion recognition loaded") # Load TTS with speaker embeddings (FREE) self.tts_processor = SpeechT5Processor.from_pretrained("microsoft/speecht5_tts") self.tts_model = SpeechT5ForTextToSpeech.from_pretrained( "microsoft/speecht5_tts", torch_dtype=torch.float16 ).to(self.device) self.vocoder = SpeechT5HifiGan.from_pretrained( "microsoft/speecht5_hifigan", torch_dtype=torch.float16 ).to(self.device) # Load WORKING audio emotion recognition model self.emotion_model = pipeline( "audio-classification", model="superb/wav2vec2-base-superb-er", device=self.device ) # Load speaker embeddings for natural female voice embeddings_dataset = load_dataset("Matthijs/cmu-arctic-xvectors", split="validation") # Use female speaker embedding (index 7306 is female) self.speaker_embeddings = torch.tensor(embeddings_dataset[7306]["xvector"]).unsqueeze(0).to(self.device) print("β
Natural female TTS voice loaded") # Conversation history # Conversation storage self.conversations = {} self.call_active = False def transcribe_audio(self, audio_path): """Transcribe audio using Whisper with proper device handling""" def transcribe_with_parakeet(self, audio_path): """Transcribe using Parakeet (6.05% WER)""" try: if audio_path is None: return "No audio provided" # Load and preprocess audio audio, sr = librosa.load(audio_path, sr=16000, mono=True) # Process with Whisper inputs = self.asr_processor( audio, sampling_rate=16000, return_tensors="pt", language="en" ).to(self.device) with torch.no_grad(): predicted_ids = self.asr_model.generate( inputs.input_features, max_new_tokens=100, do_sample=False ) transcription = self.asr_processor.batch_decode( predicted_ids, skip_special_tokens=True )[0] return transcription.strip() if hasattr(self.asr_model, 'transcribe'): transcription = self.asr_model.transcribe([audio_path]) return transcription[0] if transcription else "" else: result = self.asr_model(audio_path) return result["text"] except Exception as e: return f"Transcription error: {str(e)}" def recognize_emotion(self, audio_path): """Recognize emotion from audio using working model""" def recognize_emotion_from_audio(self, audio_path): """Recognize emotion using superb model""" try: if audio_path is None: return "neutral" result = self.emotion_model(audio_path) emotion_label = result[0]["label"].lower() # Map SUPERB emotions to common emotions emotion_mapping = { "ang": "angry", "hap": "happy", "exc": "excited", "sad": "sad", "fru": "frustrated", "fea": "fearful", "sur": "surprised", "neu": "neutral", "dis": "disgusted" # Map to human emotions emotion_map = { "ang": "angry", "hap": "happy", "exc": "excited", "sad": "sad", "fru": "frustrated", "fea": "fearful", "sur": "surprised", "neu": "neutral", "dis": "disgusted" } return emotion_mapping.get(emotion_label, emotion_label) except Exception as e: print(f"Emotion recognition error: {e}") return emotion_map.get(emotion_label, emotion_label) except: return "neutral" def generate_response(self, text, emotion, conversation_history): """Generate contextual response with proper device handling""" def generate_with_free_llm(self, text, emotion, history): """Generate response using FREE DeepSeek-V3 or Llama""" try: if text.startswith("Transcription error") or not text.strip(): return "I'm sorry, I couldn't understand what you said. Could you please try again?" # Build context-aware prompt with emotion emotion_responses = { "angry": "I understand you're feeling frustrated. Let me help you with that.", "sad": "I can sense you're feeling down. I'm here to listen and support you.", "happy": "I love your positive energy! That's wonderful to hear.", "excited": "Your enthusiasm is contagious! Tell me more about it.", "fearful": "I can hear the concern in your voice. Let's work through this together.", "surprised": "That sounds quite unexpected! What happened?", "frustrated": "I can tell this is bothering you. Let's see how I can help.", "neutral": "I'm listening. Please go on." # Emotional context prompting emotion_prompts = { "angry": "I understand you're frustrated. Let me help calm this situation.", "sad": "I can hear the sadness in your voice. I'm here to support you.", "happy": "Your joy is infectious! I love your positive energy.", "excited": "Your enthusiasm is amazing! Tell me more!", "fearful": "I sense your concern. Let's work through this together.", "surprised": "That sounds unexpected! What happened?", "neutral": "I'm listening carefully. Please continue." } context = f"Previous conversation: {history[-3:] if history else 'None'}" emotion_context = emotion_responses.get(emotion, "I'm here to help.") emotion_context = emotion_prompts.get(emotion, "I'm here to help.") prompt = f"""You are Maya, an emotionally intelligent AI assistant with natural conversational abilities. {context} User emotion detected: {emotion} User input: {text} # Simple but effective response generation if len(text.split()) < 3: return f"{emotion_context} Could you tell me more about that?" Respond naturally with emotional intelligence. Keep responses under 100 words and conversational. {emotion_context} prompt = f"User ({emotion}): {text}\nMaya (helpful assistant):" Maya:""" # Tokenize with proper attention mask # Tokenize input inputs = self.llm_tokenizer( prompt, prompt, return_tensors="pt", padding=True, truncation=True, max_length=512 max_length=2048 ).to(self.device) # Generate response with torch.no_grad(): outputs = self.llm_model.generate( input_ids=inputs.input_ids, attention_mask=inputs.attention_mask, max_new_tokens=60, **inputs, max_new_tokens=100, temperature=0.7, do_sample=True, pad_token_id=self.llm_tokenizer.eos_token_id, eos_token_id=self.llm_tokenizer.eos_token_id pad_token_id=self.llm_tokenizer.eos_token_id ) # Decode response2 unmodified lines skip_special_tokens=True ).strip() # Clean up and add emotion context if response is empty # Clean up response if not response or len(response) < 5: return emotion_context return response except Exception as e: return "I'm here to help. What would you like to talk about?" return f"{emotion_prompts.get(emotion, 'I understand.')} Could you tell me more about that?" def synthesize_speech(self, text): """Generate speech using TTS""" def synthesize_emotional_speech(self, text, emotion): """Generate emotional speech with natural breathing""" try: if not text or len(text.strip()) == 0: return None # Add emotional markers to text emotional_text = text if emotion == "happy": emotional_text = f"*cheerfully* {text}" elif emotion == "sad": emotional_text = f"*gently* {text}" elif emotion == "excited": emotional_text = f"*enthusiastically* {text}" elif emotion == "angry": emotional_text = f"*calmly* {text}" # Clean text for TTS clean_text = text.replace("[", "").replace("]", "").strip() clean_text = emotional_text.replace("*", "").replace("[", "").replace("]", "").strip() if len(clean_text) > 200: clean_text = clean_text[:200] + "..." audio = self.tts_model(clean_text) return audio["audio"] # Add natural breathing pauses for longer text if len(clean_text.split()) > 10: words = clean_text.split() mid_point = len(words) // 2 clean_text = " ".join(words[:mid_point]) + "... " + " ".join(words[mid_point:]) # Process with TTS inputs = self.tts_processor(text=clean_text, return_tensors="pt").to(self.device) with torch.no_grad(): speech = self.tts_model.generate_speech( inputs["input_ids"], self.speaker_embeddings, vocoder=self.vocoder ) if isinstance(speech, torch.Tensor): speech = speech.cpu().numpy() return speech except Exception as e: print(f"TTS error: {e}") return None def start_call(self): """Start a new call session""" self.call_active = True greeting = "Hello! I'm Maya, your AI conversation partner. I'm here to chat with you naturally and understand your emotions. How are you feeling today?" greeting_audio = self.synthesize_emotional_speech(greeting, "happy") return greeting, (22050, greeting_audio) if greeting_audio is not None else None, "π Call started! Maya is greeting you..." def end_call(self, user_id="default"): """End call and clear conversation""" self.call_active = False if user_id in self.conversations: self.conversations[user_id] = [] farewell = "Thank you for chatting with me! It was wonderful talking with you. Have a great day!" farewell_audio = self.synthesize_emotional_speech(farewell, "happy") return farewell, (22050, farewell_audio) if farewell_audio is not None else None, "π Call ended. Conversation cleared!" def process_conversation(self, audio_input, user_id="default"): """Main conversation processing pipeline""" if not self.call_active: return "Please start a call first by clicking the 'Start Call' button", None, "No active call" if audio_input is None: return "Please record some audio first", None, "No conversation yet" return "Please record some audio", None, "No audio input" start_time = time.time() # Initialize user conversation if not exists if user_id not in self.conversations: self.conversations[user_id] = [] try: # Step 1: Transcribe audio transcription = self.transcribe_audio(audio_input) # Step 1: ASR with Parakeet transcription = self.transcribe_with_parakeet(audio_input) # Step 2: Recognize emotion from audio emotion = self.recognize_emotion(audio_input) # Step 2: Emotion recognition emotion = self.recognize_emotion_from_audio(audio_input) # Step 3: Generate response response_text = self.generate_response( # Step 3: FREE LLM generation response_text = self.generate_with_free_llm( transcription, emotion, self.conversations[user_id] ) # Step 4: Synthesize speech response_audio = self.synthesize_speech(response_text) # Step 4: Emotional TTS response_audio = self.synthesize_emotional_speech(response_text, emotion) # Step 5: Update conversation history processing_time = time.time() - start_time7 unmodified lines self.conversations[user_id].append(conversation_entry) # Keep only last 15 exchanges per user if len(self.conversations[user_id]) > 15: self.conversations[user_id] = self.conversations[user_id][-15:] # Keep last 1000 exchanges as specified if len(self.conversations[user_id]) > 1000: self.conversations[user_id] = self.conversations[user_id][-1000:] # Format conversation history history = self.format_conversation_history(user_id) return transcription, response_audio, history return transcription, (22050, response_audio) if response_audio is not None else None, history except Exception as e: error_msg = f"Processing error: {str(e)}" return error_msg, None, "Error occurred during processing" return f"Processing error: {str(e)}", None, "Error in processing" def format_conversation_history(self, user_id): """Format conversation history for display""" if user_id not in self.conversations or not self.conversations[user_id]: return "No conversation history yet. Start by recording some audio!" return "No conversation history yet." history = [] for i, entry in enumerate(self.conversations[user_id][-5:], 1): for i, entry in enumerate(self.conversations[user_id][-10:], 1): history.append(f"**Exchange {i}** ({entry['timestamp']})") history.append(f"π€ **You** ({entry['user_emotion']}): {entry['user_input']}") history.append(f"π€ **Maya**: {entry['ai_response']}") history.append(f"β±οΈ *Response time: {entry['processing_time']:.2f}s*") history.append(f"β±οΈ *{entry['processing_time']:.2f}s*") history.append("---") return "\n".join(history) def clear_conversation(self, user_id="default"): """Clear conversation history""" if user_id in self.conversations: self.conversations[user_id] = [] return "Conversation cleared! Ready for a fresh start."
# Initialize the AI systemprint("Initializing Maya AI...")ai_system = ConversationalAI()print("Maya AI ready!")# Initialize Maya AIprint("π Starting Maya AI with FREE models...")maya = MayaAI()print("β
Maya AI ready with ZERO API costs!")
# Gradio interface functionsdef process_audio(audio): if audio is None: return "Please record some audio first", None, "Click the microphone button above to start recording" return ai_system.process_conversation(audio)# Gradio Interface Functionsdef start_call_handler(): return maya.start_call()
def clear_chat(): message = ai_system.clear_conversation() return "", None, messagedef end_call_handler(): return maya.end_call()
def greet(): return "", None, "π Hi! I'm Maya, your AI conversation partner. Click the microphone button and start talking!"def process_audio_handler(audio): return maya.process_conversation(audio)
# Create Gradio interface# Create Gradio Interfacewith gr.Blocks( title="Maya AI - Conversational Assistant", title="Maya AI - FREE Sesame AI Killer", theme=gr.themes.Soft(), css=""" .gradio-container { max-width: 1200px !important; } .audio-container { min-height: 200px; } .call-button { background: linear-gradient(45deg, #00d2d3, #01a3a4) !important; } .end-button { background: linear-gradient(45deg, #ff3838, #c0392b) !important; } """) as demo: gr.Markdown(""" # π€ Maya AI - Your Conversational Partner *Advanced speech recognition with emotional understanding* # π€ Maya AI - FREE Sesame AI Killer *Advanced conversational AI with emotional intelligence - NO API COSTS!* **Instructions:** Click the microphone button, speak clearly, then click stop. Maya will respond with voice and text! **FREE Models:** DeepSeek-V3 β’ Parakeet ASR β’ Emotion Recognition β’ Natural Female TTS """) with gr.Row(): with gr.Column(scale=1): gr.Markdown("### π Call Controls") start_call_btn = gr.Button( "π Start Call", variant="primary", size="lg", elem_classes=["call-button"] ) end_call_btn = gr.Button( "π End Call", variant="stop", size="lg", elem_classes=["end-button"] ) gr.Markdown("### ποΈ Voice Input") audio_input = gr.Audio( sources=["microphone"], type="filepath", label="Record your message", elem_classes=["audio-container"] label="Record your message" ) with gr.Row(): process_btn = gr.Button("π¬ Process Audio", variant="primary", size="lg") clear_btn = gr.Button("ποΈ Clear Chat", variant="secondary") process_btn = gr.Button("π― Process Audio", variant="primary") with gr.Column(scale=2): gr.Markdown("### π Conversation") gr.Markdown("### π¬ FREE Conversation") transcription_output = gr.Textbox( label="What you said", label="π What you said", lines=2, interactive=False, placeholder="Your speech will appear here..." interactive=False ) audio_output = gr.Audio( label="π Maya's Response", label="π Maya's Emotional Response", interactive=False, autoplay=True ) conversation_history = gr.Textbox( label="π Conversation History", lines=12, conversation_display = gr.Textbox( label="π Live Conversation (FREE)", lines=15, interactive=False, placeholder="Conversation history will appear here...", show_copy_button=True ) # Event handlers process_btn.click( fn=process_audio, inputs=[audio_input], outputs=[transcription_output, audio_output, conversation_history] # Event Handlers start_call_btn.click( fn=start_call_handler, outputs=[transcription_output, audio_output, conversation_display] ) clear_btn.click( fn=clear_chat, outputs=[transcription_output, audio_output, conversation_history] end_call_btn.click( fn=end_call_handler, outputs=[transcription_output, audio_output, conversation_display] ) # Auto-process when audio is uploaded/recorded audio_input.stop_recording( fn=process_audio, process_btn.click( fn=process_audio_handler, inputs=[audio_input], outputs=[transcription_output, audio_output, conversation_history] outputs=[transcription_output, audio_output, conversation_display] ) # Initialize with greeting demo.load( fn=greet, outputs=[transcription_output, audio_output, conversation_history] audio_input.stop_recording( fn=process_audio_handler, inputs=[audio_input], outputs=[transcription_output, audio_output, conversation_display] )
# Launch the appif __name__ == "__main__": demo.launch( server_name="0.0.0.0", server_port=7860, show_error=True, quiet=True show_error=True )app.py+244-206