Spaces:
Runtime error
Runtime error
Update app.py
Browse files1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374import os, tempfile, uuidfrom fastapi import FastAPIimport gradio as grimport soundfile as sfimport torchimport numpy as npimport nemo.collections.asr as nemo_asrfrom speechbrain.pretrained import EncoderClassifierfrom transformers import AutoTokenizer, AutoModelForCausalLM# Initialize FastAPI and modelsapp = FastAPI()conversation_history = {}# Model loadingasr_model = nemo_asr.models.ASRModel.from_pretrained("nvidia/parakeet-tdt-0.6b-v2") # ASR [2]emotion_model = EncoderClassifier.from_hparams(source="speechbrain/emotion-recognition-wav2vec2-IEMOCAP",savedir="emotion_cache") # Emotion [3]llm_name = "microsoft/DialoGPT-medium"llm_tokenizer = AutoTokenizer.from_pretrained(llm_name)llm_model = AutoModelForCausalLM.from_pretrained(llm_name).to("cuda" if torch.cuda.is_available() else "cpu") # LLM [4]def transcribe_and_emote(audio_path):text = asr_model.transcribe([audio_path])[0].textemotion = emotion_model.classify_file(audio_path)[0]return text, emotiondef generate_reply(user_text, emotion, uid):# Track and trim historyhist = conversation_history.setdefault(uid, [])ctx = f"[Feeling:{emotion}] {user_text}"hist.append(ctx)hist = hist[-6:]conversation_history[uid] = histprompt = " ".join(hist)inputs = llm_tokenizer.encode(prompt, return_tensors="pt").to(llm_model.device)out = llm_model.generate(inputs, max_new_tokens=100, pad_token_id=llm_tokenizer.eos_token_id)reply = llm_tokenizer.decode(out[0], skip_special_tokens=True)[len(prompt):].strip()hist.append(reply)return reply or "Iβm here to help!"def process(audio, uid):if not audio:return "", "", "", uid# Save temp filetmp = tempfile.NamedTemporaryFile(suffix=".wav", delete=False)data, sr = audiosf.write(tmp.name, data, sr)# ASR + Emotiontext, emo = transcribe_and_emote(tmp.name)# LLM responsereply = generate_reply(text, emo, uid)# Clean upos.unlink(tmp.name)return text, emo, reply, uid# Gradio interfacewith gr.Blocks() as demo:uid_state = gr.State(value=str(uuid.uuid4()))audio_in = gr.Audio(source="microphone", type="numpy")txt_out = gr.Textbox(label="Transcription")emo_out = gr.Textbox(label="Emotion")rep_out = gr.Textbox(label="AI Reply")btn = gr.Button("Process")btn.click(process, inputs=[audio_in, uid_state], outputs=[txt_out, emo_out, rep_out, uid_state])app = gr.mount_gradio_app(app, demo, path="/")if __name__ == "__main__":import uvicornuvicorn.run(app, host="0.0.0.0", port=7860)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508import gradio as grimport torchimport numpy as npimport librosaimport soundfile as sfimport threadingimport timeimport queueimport warningsfrom typing import Optional, List, Dict, Tuplefrom dataclasses import dataclassfrom collections import dequeimport psutilimport gc# Import modelsfrom dia.model import Diafrom transformers import pipelineimport webrtcvadwarnings.filterwarnings("ignore", category=FutureWarning)warnings.filterwarnings("ignore", category=UserWarning)@dataclassclass ConversationTurn:user_audio: np.ndarrayuser_text: strai_response_text: strai_response_audio: np.ndarraytimestamp: floatemotion: strspeaker_id: strclass EmotionRecognizer:def __init__(self):self.emotion_pipeline = pipeline("audio-classification",model="ehcalabres/wav2vec2-lg-xlsr-en-speech-emotion-recognition",device=0 if torch.cuda.is_available() else -1)def detect_emotion(self, audio: np.ndarray, sample_rate: int = 16000) -> str:try:result = self.emotion_pipeline({"array": audio, "sampling_rate": sample_rate})return result[0]["label"] if result else "neutral"except Exception as e:print(f"Emotion detection error: {e}")return "neutral"class VADProcessor:def __init__(self, aggressiveness: int = 2):self.vad = webrtcvad.Vad(aggressiveness)self.sample_rate = 16000self.frame_duration = 30 # msself.frame_size = int(self.sample_rate * self.frame_duration / 1000)def is_speech(self, audio: np.ndarray) -> bool:try:# Convert to 16-bit PCMaudio_int16 = (audio * 32767).astype(np.int16)# Process in framesframes = []for i in range(0, len(audio_int16) - self.frame_size, self.frame_size):frame = audio_int16[i:i + self.frame_size].tobytes()frames.append(self.vad.is_speech(frame, self.sample_rate))# Return True if majority of frames contain speechreturn sum(frames) > len(frames) * 0.3except Exception:return True # Default to treating as speechclass ConversationManager:def __init__(self, max_exchanges: int = 50):self.conversations: Dict[str, deque] = {}self.max_exchanges = max_exchangesself.lock = threading.RLock()def add_turn(self, session_id: str, turn: ConversationTurn):with self.lock:if session_id not in self.conversations:self.conversations[session_id] = deque(maxlen=self.max_exchanges)self.conversations[session_id].append(turn)def get_context(self, session_id: str, last_n: int = 5) -> List[ConversationTurn]:with self.lock:if session_id not in self.conversations:return []return list(self.conversations[session_id])[-last_n:]def clear_session(self, session_id: str):with self.lock:if session_id in self.conversations:del self.conversations[session_id]class SupernaturalAI:def __init__(self):self.device = "cuda" if torch.cuda.is_available() else "cpu"self.models_loaded = Falseself.processing_queue = queue.Queue()self.conversation_manager = ConversationManager()self.emotion_recognizer = Noneself.vad_processor = VADProcessor()# Modelsself.ultravox_model = Noneself.dia_model = None# Performance trackingself.active_sessions = set()self.processing_times = deque(maxlen=100)print("Initializing Supernatural AI...")self._initialize_models()def _initialize_models(self):try:print("Loading Ultravox model...")self.ultravox_model = pipeline('automatic-speech-recognition',model='fixie-ai/ultravox-v0_2',trust_remote_code=True,device=0 if torch.cuda.is_available() else -1,torch_dtype=torch.float16)print("Loading Dia TTS model...")self.dia_model = Dia.from_pretrained("nari-labs/Dia-1.6B",compute_dtype="float16")print("Loading emotion recognition...")self.emotion_recognizer = EmotionRecognizer()self.models_loaded = Trueprint("β All models loaded successfully!")# Memory cleanupif torch.cuda.is_available():torch.cuda.empty_cache()except Exception as e:print(f"β Error loading models: {e}")self.models_loaded = Falsedef _get_memory_usage(self) -> Dict[str, float]:"""Get current memory usage statistics"""memory = psutil.virtual_memory()gpu_memory = {}if torch.cuda.is_available():for i in range(torch.cuda.device_count()):gpu_memory[f"GPU_{i}"] = {"allocated": torch.cuda.memory_allocated(i) / 1024**3,"cached": torch.cuda.memory_reserved(i) / 1024**3}return {"RAM": memory.percent,"GPU": gpu_memory}def _generate_contextual_prompt(self,user_text: str,emotion: str,context: List[ConversationTurn]) -> str:"""Generate contextual prompt with emotion and conversation history"""# Build context from previous turnscontext_text = ""if context:for turn in context[-3:]: # Last 3 exchangescontext_text += f"[S1] {turn.user_text} [S2] {turn.ai_response_text} "# Emotion-aware response generationemotion_modifiers = {"happy": "(cheerful)","sad": "(sympathetic)","angry": "(calming)","fear": "(reassuring)","surprise": "(excited)","neutral": ""}modifier = emotion_modifiers.get(emotion.lower(), "")# Create supernatural AI personalityprompt = f"{context_text}[S1] {user_text} [S2] {modifier} As a supernatural AI with deep emotional understanding, I sense your {emotion} energy. "return promptdef process_audio_input(self,audio_data: Tuple[int, np.ndarray],session_id: str) -> Tuple[Optional[Tuple[int, np.ndarray]], str, str]:"""Main processing pipeline for audio input"""if not self.models_loaded:return None, "β Models not loaded", "Please wait for initialization"if audio_data is None:return None, "β No audio received", "Please record some audio"start_time = time.time()try:sample_rate, audio = audio_data# Ensure audio is mono and proper formatif len(audio.shape) > 1:audio = np.mean(audio, axis=1)# Normalize audioaudio = audio.astype(np.float32)if np.max(np.abs(audio)) > 0:audio = audio / np.max(np.abs(audio)) * 0.95# Voice Activity Detectionif not self.vad_processor.is_speech(audio):return None, "π No speech detected", "Please speak clearly"# Resample if neededif sample_rate != 16000:audio = librosa.resample(audio, orig_sr=sample_rate, target_sr=16000)sample_rate = 16000# Speech Recognition with Ultravoxtry:speech_result = self.ultravox_model({'array': audio,'sampling_rate': sample_rate})user_text = speech_result.get('text', '').strip()if not user_text:return None, "β Could not understand speech", "Please speak more clearly"except Exception as e:print(f"ASR Error: {e}")return None, f"β Speech recognition failed: {str(e)}", "Please try again"# Emotion Recognitionemotion = self.emotion_recognizer.detect_emotion(audio, sample_rate)# Get conversation contextcontext = self.conversation_manager.get_context(session_id)# Generate contextual responseprompt = self._generate_contextual_prompt(user_text, emotion, context)# Generate speech with Dia TTStry:with torch.no_grad():audio_output = self.dia_model.generate(prompt,use_torch_compile=False, # Better stabilityverbose=False)# Ensure audio output is proper formatif isinstance(audio_output, torch.Tensor):audio_output = audio_output.cpu().numpy()# Normalize outputif len(audio_output) > 0:max_val = np.max(np.abs(audio_output))if max_val > 1.0:audio_output = audio_output / max_val * 0.95except Exception as e:print(f"TTS Error: {e}")return None, f"β Speech generation failed: {str(e)}", "Please try again"# Extract AI response text (remove speaker tags and modifiers)ai_response = prompt.split('[S2]')[-1].strip()ai_response = ai_response.replace('(cheerful)', '').replace('(sympathetic)', '')ai_response = ai_response.replace('(calming)', '').replace('(reassuring)', '')ai_response = ai_response.replace('(excited)', '').strip()# Store conversation turnturn = ConversationTurn(user_audio=audio,user_text=user_text,ai_response_text=ai_response,ai_response_audio=audio_output,timestamp=time.time(),emotion=emotion,speaker_id=session_id)self.conversation_manager.add_turn(session_id, turn)# Track performanceprocessing_time = time.time() - start_timeself.processing_times.append(processing_time)# Memory cleanupif torch.cuda.is_available():torch.cuda.empty_cache()gc.collect()status = f"β Processed in {processing_time:.2f}s | Emotion: {emotion} | Users: {len(self.active_sessions)}"return (44100, audio_output), status, f"**You said:** {user_text}\n\n**AI Response:** {ai_response}"except Exception as e:print(f"Processing error: {e}")return None, f"β Processing failed: {str(e)}", "Please try again"def get_conversation_history(self, session_id: str) -> str:"""Get formatted conversation history"""context = self.conversation_manager.get_context(session_id, last_n=10)if not context:return "No conversation history yet."history = "## Conversation History\n\n"for i, turn in enumerate(context, 1):history += f"**Turn {i}:**\n"history += f"- **You:** {turn.user_text}\n"history += f"- **AI:** {turn.ai_response_text}\n"history += f"- **Emotion Detected:** {turn.emotion}\n\n"return historydef clear_conversation(self, session_id: str) -> str:"""Clear conversation history for session"""self.conversation_manager.clear_session(session_id)return "Conversation history cleared."def get_system_status(self) -> str:"""Get system status information"""memory = self._get_memory_usage()avg_processing = np.mean(self.processing_times) if self.processing_times else 0status = f"""## System Status**Performance:**- Average Processing Time: {avg_processing:.2f}s- Active Sessions: {len(self.active_sessions)}- Total Conversations: {len(self.conversation_manager.conversations)}**Memory Usage:**- RAM: {memory['RAM']:.1f}%- GPU Memory: {memory.get('GPU', {})}**Models Status:**- Models Loaded: {"β " if self.models_loaded else "β"}- Device: {self.device}"""return status# Initialize the AI systemprint("Starting Supernatural AI system...")ai_system = SupernaturalAI()# Gradio Interfacedef process_audio_interface(audio, session_id):"""Interface function for Gradio"""if not session_id:session_id = f"user_{int(time.time())}"ai_system.active_sessions.add(session_id)result = ai_system.process_audio_input(audio, session_id)return result + (session_id,)def get_history_interface(session_id):"""Get conversation history interface"""if not session_id:return "No session ID provided"return ai_system.get_conversation_history(session_id)def clear_history_interface(session_id):"""Clear history interface"""if not session_id:return "No session ID provided"return ai_system.clear_conversation(session_id)# Create Gradio interfacewith gr.Blocks(title="Supernatural Conversational AI", theme=gr.themes.Soft()) as demo:gr.HTML("""<div style="text-align: center; padding: 20px;"><h1>π§ββοΈ Supernatural Conversational AI</h1><p style="font-size: 18px; color: #666;">Advanced Speech-to-Speech AI with Emotional Intelligence</p><p style="color: #888;">Powered by Ultravox + Dia TTS | Optimized for 4x L4 GPUs</p></div>""")with gr.Row():with gr.Column(scale=2):# Audio input/outputaudio_input = gr.Audio(label="π€ Speak to the AI",sources=["microphone"],type="numpy",streaming=False)audio_output = gr.Audio(label="π AI Response",type="numpy",autoplay=True)# Session managementsession_id = gr.Textbox(label="Session ID",placeholder="Auto-generated if empty",value="",interactive=True)# Process buttonprocess_btn = gr.Button("π― Process Audio", variant="primary", size="lg")with gr.Column(scale=1):# Status and conversationstatus_display = gr.Textbox(label="π Status",interactive=False,lines=3)conversation_display = gr.Markdown(label="π¬ Conversation",value="Start speaking to begin...")# History managementwith gr.Row():history_btn = gr.Button("π Show History", size="sm")clear_btn = gr.Button("ποΈ Clear History", size="sm")status_btn = gr.Button("β‘ System Status", size="sm")# History and status displayhistory_display = gr.Markdown(label="π Conversation History",value="No history yet.")# Event handlersprocess_btn.click(fn=process_audio_interface,inputs=[audio_input, session_id],outputs=[audio_output, status_display, conversation_display, session_id])history_btn.click(fn=get_history_interface,inputs=[session_id],outputs=[history_display])clear_btn.click(fn=clear_history_interface,inputs=[session_id],outputs=[history_display])status_btn.click(fn=lambda: ai_system.get_system_status(),outputs=[history_display])# Auto-process on audio inputaudio_input.change(fn=process_audio_interface,inputs=[audio_input, session_id],outputs=[audio_output, status_display, conversation_display, session_id])# Usage instructionsgr.HTML("""<div style="margin-top: 20px; padding: 15px; background: #f0f8ff; border-radius: 8px;"><h3>π‘ Usage Instructions:</h3><ul><li><strong>Record Audio:</strong> Click the microphone and speak naturally</li><li><strong>Emotional AI:</strong> The AI detects and responds to your emotions</li><li><strong>Conversation Memory:</strong> Up to 50 exchanges are remembered</li><li><strong>Session Management:</strong> Use Session ID to maintain separate conversations</li><li><strong>Performance:</strong> Optimized for sub-500ms latency</li></ul><p><strong>Supported Features:</strong> Emotion recognition, voice activity detection,contextual responses, conversation history, concurrent users (15-20), memory management</p></div>""")# Configure for optimal performancedemo.queue(concurrency_count=20, # Support 20 concurrent usersmax_size=100,api_open=False)if __name__ == "__main__":demo.launch(server_name="0.0.0.0",server_port=7860,share=False,show_error=True,quiet=False,enable_queue=True,max_threads=40)
app.py+503-69