Update ui/tabs.py
Browse files- ui/tabs.py +55 -0
ui/tabs.py
CHANGED
|
@@ -199,6 +199,61 @@ def create_audio_tab(audio_service: AudioService):
|
|
| 199 |
inputs=audio_input,
|
| 200 |
outputs=[transcription_output, response_output, tts_audio_output, language_display] # UPDATED
|
| 201 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 202 |
def create_image_tab(image_service: ImageService):
|
| 203 |
gr.Markdown("## Phân tích hình ảnh")
|
| 204 |
with gr.Row():
|
|
|
|
| 199 |
inputs=audio_input,
|
| 200 |
outputs=[transcription_output, response_output, tts_audio_output, language_display] # UPDATED
|
| 201 |
)
|
| 202 |
+
def create_streaming_voice_tab(streaming_service: StreamingVoiceService):
|
| 203 |
+
"""Tạo tab streaming voice sử dụng Gradio microphone"""
|
| 204 |
+
|
| 205 |
+
# Create components
|
| 206 |
+
(microphone, clear_btn, state_display,
|
| 207 |
+
transcription, ai_response, tts_output, conversation_state) = create_streaming_voice_components()
|
| 208 |
+
|
| 209 |
+
def process_audio_stream(audio_data):
|
| 210 |
+
"""Xử lý audio stream từ microphone"""
|
| 211 |
+
if audio_data is None:
|
| 212 |
+
return {
|
| 213 |
+
'transcription': "❌ Không có âm thanh",
|
| 214 |
+
'response': "Vui lòng nói lại",
|
| 215 |
+
'tts_audio': None
|
| 216 |
+
}, {}, {}
|
| 217 |
+
|
| 218 |
+
try:
|
| 219 |
+
# Xử lý audio
|
| 220 |
+
result = streaming_service.process_streaming_audio(audio_data)
|
| 221 |
+
|
| 222 |
+
# Cập nhật state
|
| 223 |
+
state = streaming_service.get_conversation_state()
|
| 224 |
+
|
| 225 |
+
return result['transcription'], result['response'], result['tts_audio'], state
|
| 226 |
+
|
| 227 |
+
except Exception as e:
|
| 228 |
+
error_msg = f"❌ Lỗi xử lý: {str(e)}"
|
| 229 |
+
return error_msg, "Xin lỗi, có lỗi xảy ra", None, {}
|
| 230 |
+
|
| 231 |
+
def clear_conversation():
|
| 232 |
+
"""Xóa hội thoại"""
|
| 233 |
+
streaming_service.clear_conversation()
|
| 234 |
+
state = streaming_service.get_conversation_state()
|
| 235 |
+
return "", "", None, state
|
| 236 |
+
|
| 237 |
+
# Event handlers
|
| 238 |
+
microphone.stream(
|
| 239 |
+
process_audio_stream,
|
| 240 |
+
inputs=[microphone],
|
| 241 |
+
outputs=[transcription, ai_response, tts_output, state_display]
|
| 242 |
+
)
|
| 243 |
+
|
| 244 |
+
clear_btn.click(
|
| 245 |
+
clear_conversation,
|
| 246 |
+
outputs=[transcription, ai_response, tts_output, state_display]
|
| 247 |
+
)
|
| 248 |
+
|
| 249 |
+
# Initial state
|
| 250 |
+
def get_initial_state():
|
| 251 |
+
return streaming_service.get_conversation_state()
|
| 252 |
+
|
| 253 |
+
state_display.change(
|
| 254 |
+
get_initial_state,
|
| 255 |
+
outputs=[state_display]
|
| 256 |
+
)
|
| 257 |
def create_image_tab(image_service: ImageService):
|
| 258 |
gr.Markdown("## Phân tích hình ảnh")
|
| 259 |
with gr.Row():
|