datbkpro commited on
Commit
3dde460
·
verified ·
1 Parent(s): 8df3db4

Update services/voice_coding_service.py

Browse files
Files changed (1) hide show
  1. services/voice_coding_service.py +47 -37
services/voice_coding_service.py CHANGED
@@ -9,7 +9,7 @@ from fastrtc import (
9
  AsyncStreamHandler,
10
  AdditionalOutputs,
11
  wait_for_item,
12
- get_twilio_turn_credentials,
13
  )
14
  from gradio.utils import get_space
15
 
@@ -39,8 +39,10 @@ class VoiceCodingHandler(AsyncStreamHandler):
39
 
40
  def extract_html_content(self, text):
41
  """Extract content including HTML tags."""
 
 
42
  match = re.search(r"<!DOCTYPE html>.*?</html>", text, re.DOTALL)
43
- return match.group(0) if match else None
44
 
45
  async def start_up(self):
46
  """Khởi động handler"""
@@ -63,12 +65,12 @@ class VoiceCodingHandler(AsyncStreamHandler):
63
  try:
64
  print("🎤 Processing audio for voice coding...")
65
 
66
- # đây chúng ta cần chuyển audio sang text
67
- # Tạm thời sử dụng transcription service sẵn
68
- transcription = await self._transcribe_audio(audio_data, sample_rate)
69
 
70
- if transcription and self._is_trigger_phrase(transcription):
71
- print(f"🎯 Trigger phrase detected: {transcription}")
72
 
73
  # Generate loading state
74
  await self.output_queue.put(AdditionalOutputs({
@@ -84,21 +86,11 @@ class VoiceCodingHandler(AsyncStreamHandler):
84
  except Exception as e:
85
  print(f"❌ Lỗi xử lý audio: {e}")
86
 
87
- async def _transcribe_audio(self, audio_data: np.ndarray, sample_rate: int) -> str:
88
- """Chuyển audio sang text - simplified version"""
89
- try:
90
- # Sử dụng transcription service sẵn từ hệ thống của bạn
91
- # Hoặc implement Whisper local
92
- return "Tạo trang web hello world" # Tạm thời return test text
93
- except Exception as e:
94
- print(f"❌ Lỗi transcription: {e}")
95
- return ""
96
-
97
- def _is_trigger_phrase(self, text: str) -> bool:
98
- """Kiểm tra trigger phrase"""
99
- trigger_phrases = ["hello llama", "xin chào llama", "llama", "code"]
100
- text_lower = text.lower()
101
- return any(phrase in text_lower for phrase in trigger_phrases)
102
 
103
  async def _generate_code(self, user_message: str):
104
  """Generate code từ text input"""
@@ -115,11 +107,11 @@ class VoiceCodingHandler(AsyncStreamHandler):
115
  # Generate code với Groq
116
  print("🦙 Generating code with Llama...")
117
  response = self.groq_client.chat.completions.create(
118
- model="llama-3.3-70b-versatile",
119
  messages=self.current_history,
120
- temperature=1,
121
- max_tokens=2048,
122
- top_p=1,
123
  stream=False,
124
  )
125
 
@@ -128,8 +120,6 @@ class VoiceCodingHandler(AsyncStreamHandler):
128
 
129
  # Extract HTML code
130
  html_code = self.extract_html_content(output)
131
- if not html_code:
132
- html_code = f"<!-- Generated Code -->\n{output}"
133
 
134
  # Update state
135
  self.current_history.append({"role": "assistant", "content": output})
@@ -170,13 +160,21 @@ class VoiceCodingService:
170
 
171
  def __init__(self, groq_client: Groq):
172
  self.groq_client = groq_client
173
- self.rtc_configuration = get_twilio_turn_credentials() if get_space() else None
 
 
 
 
 
 
 
174
 
175
  # HTML templates
176
  self.sandbox_html = """
177
- <div style="text-align: center; padding: 20px;">
178
  <h3>🎮 Sandbox Preview</h3>
179
  <p>Code sẽ được hiển thị ở đây sau khi generate</p>
 
180
  </div>
181
  """
182
 
@@ -202,6 +200,13 @@ class VoiceCodingService:
202
  </style>
203
  """
204
 
 
 
 
 
 
 
 
205
  def create_stream(self):
206
  """Tạo FastRTC stream"""
207
  return Stream(
@@ -209,19 +214,24 @@ class VoiceCodingService:
209
  modality="audio",
210
  mode="send-receive",
211
  rtc_configuration=self.rtc_configuration,
212
- concurrency_limit=5 if get_space() else None,
213
- time_limit=90 if get_space() else None,
214
  )
215
 
216
  def display_in_sandbox(self, code):
217
  """Hiển thị code trong sandbox iframe"""
218
- if not code:
219
  return self.sandbox_html
220
 
221
  try:
222
- encoded_html = base64.b64encode(code.encode("utf-8")).decode("utf-8")
223
- data_uri = f"data:text/html;charset=utf-8;base64,{encoded_html}"
224
- return f'<iframe src="{data_uri}" width="100%" height="600px" style="border: 1px solid #ccc;"></iframe>'
 
 
 
 
 
225
  except Exception as e:
226
  print(f"❌ Lỗi display sandbox: {e}")
227
- return f'<div style="color: red;">Lỗi hiển thị sandbox: {str(e)}</div>'
 
9
  AsyncStreamHandler,
10
  AdditionalOutputs,
11
  wait_for_item,
12
+ get_cloudflare_turn_credentials_async, # Sử dụng Cloudflare free
13
  )
14
  from gradio.utils import get_space
15
 
 
39
 
40
  def extract_html_content(self, text):
41
  """Extract content including HTML tags."""
42
+ if not text:
43
+ return None
44
  match = re.search(r"<!DOCTYPE html>.*?</html>", text, re.DOTALL)
45
+ return match.group(0) if match else text # Return full text if no HTML found
46
 
47
  async def start_up(self):
48
  """Khởi động handler"""
 
65
  try:
66
  print("🎤 Processing audio for voice coding...")
67
 
68
+ # Tạm thời sử dụng text input thay vì audio transcription
69
+ # Trong thực tế, bạn sẽ tích hợp với VOSK/Whisper
70
+ transcription = await self._mock_transcribe_audio()
71
 
72
+ if transcription:
73
+ print(f"🎯 Received request: {transcription}")
74
 
75
  # Generate loading state
76
  await self.output_queue.put(AdditionalOutputs({
 
86
  except Exception as e:
87
  print(f"❌ Lỗi xử lý audio: {e}")
88
 
89
+ async def _mock_transcribe_audio(self) -> str:
90
+ """Mock transcription - trong thực tế sẽ tích hợp với ASR"""
91
+ # Tạm thời return test text
92
+ # Bạnthể tích hợp với VOSK/Whisper sau
93
+ return "Tạo trang web hello world với màu nền xanh và chữ màu trắng"
 
 
 
 
 
 
 
 
 
 
94
 
95
  async def _generate_code(self, user_message: str):
96
  """Generate code từ text input"""
 
107
  # Generate code với Groq
108
  print("🦙 Generating code with Llama...")
109
  response = self.groq_client.chat.completions.create(
110
+ model="llama-3.1-8b-instant", # Sử dụng model có sẵn
111
  messages=self.current_history,
112
+ temperature=0.7,
113
+ max_tokens=1024,
114
+ top_p=0.9,
115
  stream=False,
116
  )
117
 
 
120
 
121
  # Extract HTML code
122
  html_code = self.extract_html_content(output)
 
 
123
 
124
  # Update state
125
  self.current_history.append({"role": "assistant", "content": output})
 
160
 
161
  def __init__(self, groq_client: Groq):
162
  self.groq_client = groq_client
163
+
164
+ # Sử dụng Cloudflare TURN miễn phí hoặc None cho local development
165
+ try:
166
+ self.rtc_configuration = asyncio.run(get_cloudflare_turn_credentials_async())
167
+ print("✅ Using Cloudflare TURN servers")
168
+ except Exception as e:
169
+ print(f"⚠️ Cannot get TURN credentials, using None: {e}")
170
+ self.rtc_configuration = None # Sẽ hoạt động trên local network
171
 
172
  # HTML templates
173
  self.sandbox_html = """
174
+ <div style="text-align: center; padding: 20px; border: 2px dashed #ccc; border-radius: 10px;">
175
  <h3>🎮 Sandbox Preview</h3>
176
  <p>Code sẽ được hiển thị ở đây sau khi generate</p>
177
+ <p><small>Chức năng voice đang được phát triển. Vui lòng sử dụng text input.</small></p>
178
  </div>
179
  """
180
 
 
200
  </style>
201
  """
202
 
203
+ def extract_html_content(self, text):
204
+ """Extract content including HTML tags."""
205
+ if not text:
206
+ return "<!-- No code generated -->"
207
+ match = re.search(r"<!DOCTYPE html>.*?</html>", text, re.DOTALL)
208
+ return match.group(0) if match else f"<!-- Generated Code -->\n<pre>{text}</pre>"
209
+
210
  def create_stream(self):
211
  """Tạo FastRTC stream"""
212
  return Stream(
 
214
  modality="audio",
215
  mode="send-receive",
216
  rtc_configuration=self.rtc_configuration,
217
+ concurrency_limit=3,
218
+ time_limit=120,
219
  )
220
 
221
  def display_in_sandbox(self, code):
222
  """Hiển thị code trong sandbox iframe"""
223
+ if not code or "No code" in code:
224
  return self.sandbox_html
225
 
226
  try:
227
+ # Kiểm tra xem code có phải HTML không
228
+ if any(tag in code.lower() for tag in ['<html', '<!doctype', '<body', '<head']):
229
+ encoded_html = base64.b64encode(code.encode("utf-8")).decode("utf-8")
230
+ data_uri = f"data:text/html;charset=utf-8;base64,{encoded_html}"
231
+ return f'<iframe src="{data_uri}" width="100%" height="600px" style="border: 1px solid #ccc; border-radius: 5px;"></iframe>'
232
+ else:
233
+ # Nếu không phải HTML, hiển thị dưới dạng text
234
+ return f'<div style="padding: 20px; background: #f5f5f5; border-radius: 5px;"><h4>Generated Content:</h4><pre style="white-space: pre-wrap;">{code}</pre></div>'
235
  except Exception as e:
236
  print(f"❌ Lỗi display sandbox: {e}")
237
+ return f'<div style="color: red; padding: 20px;">Lỗi hiển thị sandbox: {str(e)}</div>'