littlebird13 commited on
Commit
a4f3d87
·
verified ·
1 Parent(s): 7ace409

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -99
app.py CHANGED
@@ -87,10 +87,6 @@ class LiveTranslateHandler(AsyncStreamHandler):
87
  self.video_capture = None # 视频捕获设备
88
  self.last_capture_time = 0 # 上次视频帧捕获时间戳
89
  self.enable_video = False
90
- self.output_queue = asyncio.Queue()
91
- self.awaiting_new_message = True
92
- self.stable_text = "" # 黑色部分
93
- self.temp_text = "" # 灰色部分
94
 
95
  def setup_video(self):
96
  """设置视频捕获设备"""
@@ -194,82 +190,21 @@ class LiveTranslateHandler(AsyncStreamHandler):
194
  continue
195
  event_type = event["type"]
196
 
197
- # if event_type == "response.audio_transcript.delta":
198
- # # 增量字幕
199
- # text = event.get("transcript", "")
200
- # if text:
201
- # await self.output_queue.put(
202
- # AdditionalOutputs({"role": "assistant", "content": text, "update": True, "new_message": self.awaiting_new_message
203
- # })
204
- # )
205
- # self.awaiting_new_message = False
206
-
207
- # # 中间文本内容
208
- # if event_type in ("response.text.text", "response.audio_transcript.text"):
209
- # # 中间结果 + stash(stash通常是句子完整缓存)
210
- # stash_text = event.get("stash", "")
211
- # text_field = event.get("text", "")
212
- # if stash_text or text_field:
213
- # await self.output_queue.put(
214
- # AdditionalOutputs({"role": "assistant", "content": stash_text or text_field, "update": True, "new_message": self.awaiting_new_message})
215
- # )
216
- # self.awaiting_new_message = False
217
-
218
- # elif event_type == "response.audio_transcript.done":
219
- # # 最终完整句子
220
- # transcript = event.get("transcript", "")
221
- # if transcript:
222
- # await self.output_queue.put(
223
- # AdditionalOutputs({"role": "assistant", "content": transcript, "update": True, "new_message": self.awaiting_new_message})
224
- # )
225
- # self.awaiting_new_message = True
226
-
227
  if event_type == "response.audio_transcript.delta":
228
- self.temp_text = event.get("transcript", "")
229
- if self.temp_text:
230
- await self.output_queue.put(
231
- AdditionalOutputs({
232
- "role": "assistant",
233
- "content": (self.stable_text, self.temp_text),
234
- "update": True,
235
- "new_message": self.awaiting_new_message
236
- })
237
- )
238
- self.awaiting_new_message = False
239
-
240
- elif event_type in ("response.text.text", "response.audio_transcript.text"):
241
- # 更新稳定部分(stash / text 认为是已确认的)
242
- new_stable = event.get("stash") or event.get("text") or ""
243
- if new_stable:
244
- self.stable_text = f"{self.stable_text}{new_stable}"
245
- self.temp_text = "" # 临时部分清空
246
  await self.output_queue.put(
247
- AdditionalOutputs({
248
- "role": "assistant",
249
- "content": (self.stable_text, self.temp_text),
250
- "update": True,
251
- "new_message": self.awaiting_new_message
252
- })
253
  )
254
- self.awaiting_new_message = False
255
 
256
  elif event_type == "response.audio_transcript.done":
 
257
  transcript = event.get("transcript", "")
258
  if transcript:
259
- self.stable_text = transcript
260
- self.temp_text = ""
261
  await self.output_queue.put(
262
- AdditionalOutputs({
263
- "role": "assistant",
264
- "content": (self.stable_text, self.temp_text),
265
- "update": True,
266
- "new_message": self.awaiting_new_message
267
- })
268
  )
269
- # 开启新气泡
270
- self.awaiting_new_message = True
271
- self.stable_text = ""
272
- self.temp_text = ""
273
 
274
  elif event_type == "response.audio.delta":
275
  audio_b64 = event.get("delta", "")
@@ -326,32 +261,7 @@ class LiveTranslateHandler(AsyncStreamHandler):
326
 
327
 
328
  def update_chatbot(chatbot: list[dict], response: dict):
329
- is_update = response.pop("update", False)
330
- new_message_flag = response.pop("new_message", False)
331
- content_tuple = response["content"]
332
-
333
- # 组 HTML:黑色稳定文本 + 灰色临时文本
334
- stable_html = f"<span style='color:black'>{content_tuple[0]}</span>"
335
- temp_html = f"<span style='color:gray'>{content_tuple[1]}</span>"
336
- html_content = stable_html + temp_html
337
-
338
- if is_update:
339
- if new_message_flag or not chatbot:
340
- chatbot.append({
341
- "role": "assistant",
342
- "content": html_content
343
- })
344
- else:
345
- if chatbot[-1]["role"] == "assistant":
346
- chatbot[-1]["content"] = html_content
347
- else:
348
- chatbot.append({
349
- "role": "assistant",
350
- "content": html_content
351
- })
352
- else:
353
- chatbot.append(response)
354
-
355
  return chatbot
356
 
357
 
@@ -386,7 +296,7 @@ stream = Stream(
386
  LiveTranslateHandler(),
387
  mode="send-receive",
388
  modality="audio",
389
- additional_inputs=[src_language, language, voice, video_flag, chatbot],
390
  additional_outputs=[chatbot],
391
  additional_outputs_handler=update_chatbot,
392
  rtc_configuration=rtc_config,
@@ -439,4 +349,4 @@ if __name__ == "__main__":
439
  else:
440
  import uvicorn
441
 
442
- uvicorn.run(app, host="0.0.0.0", port=7862)
 
87
  self.video_capture = None # 视频捕获设备
88
  self.last_capture_time = 0 # 上次视频帧捕获时间戳
89
  self.enable_video = False
 
 
 
 
90
 
91
  def setup_video(self):
92
  """设置视频捕获设备"""
 
190
  continue
191
  event_type = event["type"]
192
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
193
  if event_type == "response.audio_transcript.delta":
194
+ # 增量字幕
195
+ text = event.get("transcript", "")
196
+ if text:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
197
  await self.output_queue.put(
198
+ AdditionalOutputs({"role": "assistant", "content": text})
 
 
 
 
 
199
  )
 
200
 
201
  elif event_type == "response.audio_transcript.done":
202
+ # 最终完整句子
203
  transcript = event.get("transcript", "")
204
  if transcript:
 
 
205
  await self.output_queue.put(
206
+ AdditionalOutputs({"role": "assistant", "content": transcript})
 
 
 
 
 
207
  )
 
 
 
 
208
 
209
  elif event_type == "response.audio.delta":
210
  audio_b64 = event.get("delta", "")
 
261
 
262
 
263
  def update_chatbot(chatbot: list[dict], response: dict):
264
+ chatbot.append(response)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
265
  return chatbot
266
 
267
 
 
296
  LiveTranslateHandler(),
297
  mode="send-receive",
298
  modality="audio",
299
+ additional_inputs=[src_language, language, voice, video_flag,chatbot],
300
  additional_outputs=[chatbot],
301
  additional_outputs_handler=update_chatbot,
302
  rtc_configuration=rtc_config,
 
349
  else:
350
  import uvicorn
351
 
352
+ uvicorn.run(app, host="0.0.0.0", port=7860)