Update services/voice_coding_service.py
Browse files1 unmodified line234567891011121314151617181920212223242514 unmodified lines40414243444546474849505152535455565758596061626364656667686 unmodified lines757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331 unmodified lineimport numpy as npimport base64import refrom groq import Groqfrom gradio_webrtc import (WebRTC,ReplyOnStopWords,AdditionalOutputs,audio_to_bytes,get_twilio_turn_credentials,)class VoiceCodingService:"""Dịch vụ Voice Coding sử dụng Groq + WebRTC"""def __init__(self, groq_client: Groq):self.groq_client = groq_clientself.rtc_configuration = get_twilio_turn_credentials()# HTML templatesself.spinner_html = """<div style="text-align: center; padding: 20px;"><div class="spinner"></div><p>🦙 Llama đang code...</p>14 unmodified lines}</style>"""self.sandbox_html = """<div style="text-align: center; padding: 20px;"><h3>🎮 Sandbox Preview</h3><p>Code sẽ được hiển thị ở đây sau khi generate</p></div>"""self.something_happened_html = """<div style="text-align: center; padding: 20px; color: #e74c3c;"><h3>❌ Có lỗi xảy ra</h3><p>Không thể generate code. Vui lòng thử lại.</p></div>"""# Promptsself.system_prompt = "You are an AI coding assistant. Your task is to write single-file HTML applications based on a user's request. Only return the necessary code. Include all necessary imports and styles. You may also be asked to edit your original response. Respond in Vietnamese when appropriate."self.user_prompt = "Please write a single-file HTML application to fulfill the following request.\nThe message:{user_message}\nCurrent code you have written:{code}"def extract_html_content(self, text):"""Extract content including HTML tags."""match = re.search(r"<!DOCTYPE html>.*?</html>", text, re.DOTALL)return match.group(0) if match else Nonedef display_in_sandbox(self, code):"""Hiển thị code trong sandbox iframe"""6 unmodified linesreturn f'<iframe src="{data_uri}" width="100%" height="600px" style="border: 1px solid #ccc;"></iframe>'except Exception as e:print(f"❌ Lỗi display sandbox: {e}")return self.something_happened_htmldef generate_code(self, user_message: tuple[int, np.ndarray], history: list[dict], code: str):"""Generate code từ voice input"""yield AdditionalOutputs(history, self.spinner_html)try:# Chuyển audio sang textsr, audio = user_messageaudio = audio.squeeze()print("🎤 Converting speech to text...")text = self.groq_client.audio.transcriptions.create(file=("audio-file.mp3", audio_to_bytes((sr, audio))),model="whisper-large-v3-turbo",response_format="verbose_json",).textprint(f"📝 Transcription: {text}")# Format user messageuser_msg_formatted = self.user_prompt.format(user_message=text, code=code)history.append({"role": "user", "content": user_msg_formatted})# Generate code với Groqprint("🦙 Generating code with Llama...")response = self.groq_client.chat.completions.create(model="llama-3.1-8b-instant",messages=history,temperature=1,max_tokens=2048,top_p=1,stream=False,)output = response.choices[0].message.contentprint("✅ Code generated successfully")# Extract HTML codetry:html_code = self.extract_html_content(output)if not html_code:html_code = f"<!-- Generated Code -->\n{output}"except Exception as e:print(f"⚠️ Could not extract HTML: {e}")html_code = self.something_happened_html# Update historyhistory.append({"role": "assistant", "content": output})yield AdditionalOutputs(history, html_code)except Exception as e:print(f"❌ Lỗi generate code: {e}")history.append({"role": "assistant", "content": f"Error: {str(e)}"})yield AdditionalOutputs(history, self.something_happened_html)1 unmodified line2345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518614 unmodified lines2012022032042052062072082092102112122132142152162176 unmodified lines2242252262271 unmodified lineimport numpy as npimport base64import reimport asynciofrom groq import Groqfrom fastrtc import (Stream,AsyncStreamHandler,AdditionalOutputs,wait_for_item,get_twilio_turn_credentials,)from gradio.utils import get_spaceclass VoiceCodingHandler(AsyncStreamHandler):"""FastRTC Handler cho Voice Coding"""def __init__(self, groq_client: Groq):super().__init__(expected_layout="mono",output_sample_rate=24000,input_sample_rate=16000,)self.groq_client = groq_clientself.input_queue = asyncio.Queue()self.output_queue = asyncio.Queue()self.is_active = False# Promptsself.system_prompt = "You are an AI coding assistant. Your task is to write single-file HTML applications based on a user's request. Only return the necessary code. Include all necessary imports and styles. You may also be asked to edit your original response. Respond in Vietnamese when appropriate."self.user_prompt = "Please write a single-file HTML application to fulfill the following request.\nThe message:{user_message}\nCurrent code you have written:{code}"self.current_history = [{"role": "system", "content": self.system_prompt}]self.current_code = ""def copy(self):return VoiceCodingHandler(self.groq_client)def extract_html_content(self, text):"""Extract content including HTML tags."""match = re.search(r"<!DOCTYPE html>.*?</html>", text, re.DOTALL)return match.group(0) if match else Noneasync def start_up(self):"""Khởi động handler"""self.is_active = Trueprint("✅ Voice Coding Handler started")async def receive(self, frame: tuple[int, np.ndarray]) -> None:"""Nhận audio frame"""if not self.is_active:returnsample_rate, array = framearray = array.squeeze()# Xử lý audio trong backgroundasyncio.create_task(self._process_audio(array, sample_rate))async def _process_audio(self, audio_data: np.ndarray, sample_rate: int):"""Xử lý audio và generate code"""try:print("🎤 Processing audio for voice coding...")# Ở đây chúng ta cần chuyển audio sang text# Tạm thời sử dụng transcription service có sẵntranscription = await self._transcribe_audio(audio_data, sample_rate)if transcription and self._is_trigger_phrase(transcription):print(f"🎯 Trigger phrase detected: {transcription}")# Generate loading stateawait self.output_queue.put(AdditionalOutputs({"type": "loading","message": "🦙 Llama đang code...","history": self.current_history,"code": self.current_code}))# Generate codeawait self._generate_code(transcription)except Exception as e:print(f"❌ Lỗi xử lý audio: {e}")async def _transcribe_audio(self, audio_data: np.ndarray, sample_rate: int) -> str:"""Chuyển audio sang text - simplified version"""try:# Sử dụng transcription service có sẵn từ hệ thống của bạn# Hoặc implement Whisper localreturn "Tạo trang web hello world" # Tạm thời return test textexcept Exception as e:print(f"❌ Lỗi transcription: {e}")return ""def _is_trigger_phrase(self, text: str) -> bool:"""Kiểm tra trigger phrase"""trigger_phrases = ["hello llama", "xin chào llama", "llama", "code"]text_lower = text.lower()return any(phrase in text_lower for phrase in trigger_phrases)async def _generate_code(self, user_message: str):"""Generate code từ text input"""try:# Format user messageuser_msg_formatted = self.user_prompt.format(user_message=user_message,code=self.current_code)# Update historyself.current_history.append({"role": "user", "content": user_msg_formatted})# Generate code với Groqprint("🦙 Generating code with Llama...")response = self.groq_client.chat.completions.create(model="llama-3.3-70b-versatile",messages=self.current_history,temperature=1,max_tokens=2048,top_p=1,stream=False,)output = response.choices[0].message.contentprint("✅ Code generated successfully")# Extract HTML codehtml_code = self.extract_html_content(output)if not html_code:html_code = f"<!-- Generated Code -->\n{output}"# Update stateself.current_history.append({"role": "assistant", "content": output})self.current_code = html_code# Send resultawait self.output_queue.put(AdditionalOutputs({"type": "code_generated","history": self.current_history,"code": html_code,"message": "✅ Code đã được generate!"}))except Exception as e:print(f"❌ Lỗi generate code: {e}")await self.output_queue.put(AdditionalOutputs({"type": "error","message": f"❌ Lỗi: {str(e)}","history": self.current_history,"code": self.current_code}))async def emit(self):"""Emit outputs"""try:return await wait_for_item(self.output_queue)except Exception as e:print(f"❌ Lỗi emit: {e}")return Noneasync def shutdown(self):"""Dừng handler"""self.is_active = Falseprint("🛑 Voice Coding Handler stopped")class VoiceCodingService:"""Dịch vụ Voice Coding sử dụng FastRTC"""def __init__(self, groq_client: Groq):self.groq_client = groq_clientself.rtc_configuration = get_twilio_turn_credentials() if get_space() else None# HTML templatesself.sandbox_html = """<div style="text-align: center; padding: 20px;"><h3>🎮 Sandbox Preview</h3><p>Code sẽ được hiển thị ở đây sau khi generate</p></div>"""self.loading_html = """<div style="text-align: center; padding: 20px;"><div class="spinner"></div><p>🦙 Llama đang code...</p>14 unmodified lines}</style>"""def create_stream(self):"""Tạo FastRTC stream"""return Stream(VoiceCodingHandler(self.groq_client),modality="audio",mode="send-receive",rtc_configuration=self.rtc_configuration,concurrency_limit=5 if get_space() else None,time_limit=90 if get_space() else None,)def display_in_sandbox(self, code):"""Hiển thị code trong sandbox iframe"""6 unmodified linesreturn f'<iframe src="{data_uri}" width="100%" height="600px" style="border: 1px solid #ccc;"></iframe>'except Exception as e:print(f"❌ Lỗi display sandbox: {e}")return f'<div style="color: red;">Lỗi hiển thị sandbox: {str(e)}</div>'
services/voice_coding_service.py+179-85