Update ui/tabs.py
Browse files- ui/tabs.py +133 -99
ui/tabs.py
CHANGED
|
@@ -23,7 +23,7 @@ def create_all_tabs(audio_service: AudioService, chat_service: ChatService,
|
|
| 23 |
streaming_voice_service: StreamingVoiceService,
|
| 24 |
hybrid_service: HybridStreamingService,
|
| 25 |
voice_coding_service: VoiceCodingService,
|
| 26 |
-
sambanova_voice_service
|
| 27 |
):
|
| 28 |
|
| 29 |
with gr.Tab("🎙️ Streaming Voice "):
|
|
@@ -31,9 +31,9 @@ def create_all_tabs(audio_service: AudioService, chat_service: ChatService,
|
|
| 31 |
with gr.Tab("OpenAI Realtime"):
|
| 32 |
create_openai_realtime_tab(hybrid_service)
|
| 33 |
with gr.Tab("GemeniAI RealTime"):
|
| 34 |
-
create_gemini_realtime_tab()
|
| 35 |
with gr.Tab("SambonovaAI Realtime"):
|
| 36 |
-
create_sambanova_voice_tab()
|
| 37 |
with gr.Tab("Generation Code"):
|
| 38 |
create_voice_coding_tab(voice_coding_service)
|
| 39 |
with gr.Tab("🎙️ Audio"):
|
|
@@ -51,16 +51,13 @@ def create_all_tabs(audio_service: AudioService, chat_service: ChatService,
|
|
| 51 |
with gr.Tab("🔊 Text-to-Speech"):
|
| 52 |
create_tts_tab(tts_service)
|
| 53 |
|
| 54 |
-
with gr.Tab("🌐 Language Info"):
|
| 55 |
create_language_info_tab(rag_system.multilingual_manager)
|
| 56 |
with gr.Tab("Stream Object Detection"):
|
| 57 |
create_streaming_object_detection()
|
| 58 |
def create_gemini_realtime_tab():
|
| 59 |
"""Tạo tab cho Gemini Realtime API"""
|
| 60 |
|
| 61 |
-
# Khởi tạo service
|
| 62 |
-
gemini_service = GeminiRealtimeService()
|
| 63 |
-
|
| 64 |
with gr.Blocks() as gemini_tab:
|
| 65 |
gr.Markdown("""
|
| 66 |
# 🎯 Gemini Realtime API
|
|
@@ -117,130 +114,188 @@ def create_gemini_realtime_tab():
|
|
| 117 |
label="Gemini Chat",
|
| 118 |
type="messages",
|
| 119 |
height=400,
|
| 120 |
-
show_copy_button=True
|
|
|
|
| 121 |
)
|
| 122 |
|
| 123 |
-
# Audio interface
|
| 124 |
with gr.Group():
|
| 125 |
-
gr.Markdown("### 🎤 Audio Streaming")
|
| 126 |
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 130 |
|
| 131 |
transcription_display = gr.Textbox(
|
| 132 |
label="Bạn nói",
|
| 133 |
interactive=False,
|
| 134 |
lines=2,
|
| 135 |
-
placeholder="
|
|
|
|
| 136 |
)
|
| 137 |
|
| 138 |
audio_output = gr.Audio(
|
| 139 |
label="Gemini trả lời",
|
| 140 |
interactive=False,
|
| 141 |
-
autoplay=True
|
|
|
|
| 142 |
)
|
| 143 |
|
| 144 |
# State management
|
| 145 |
connection_state = gr.State(value=False)
|
| 146 |
-
audio_stream_state = gr.State(value=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
|
| 148 |
-
# Callback function để xử lý events từ Gemini
|
| 149 |
async def gemini_callback(data: dict):
|
| 150 |
"""Callback function để xử lý events từ Gemini service"""
|
| 151 |
if data['type'] == 'status':
|
| 152 |
return data['message'], data['message']
|
| 153 |
elif data['type'] == 'text':
|
| 154 |
-
# Cập nhật chatbot với response từ Gemini
|
| 155 |
-
current_messages = chatbot.value if hasattr(chatbot, 'value') else []
|
| 156 |
-
current_messages.append({"role": "assistant", "content": data['content']})
|
| 157 |
return f"📝 Gemini: {data['content']}", data['content']
|
| 158 |
elif data['type'] == 'error':
|
| 159 |
return f"❌ Lỗi: {data['message']}", data['message']
|
| 160 |
return "Unknown event", "Unknown event"
|
| 161 |
|
| 162 |
# Event handlers for connection
|
| 163 |
-
async def connect_gemini(api_key: str, voice_name: str):
|
| 164 |
"""Kết nối đến Gemini Realtime API"""
|
| 165 |
try:
|
| 166 |
if not api_key:
|
| 167 |
-
return False, "❌ Vui lòng nhập API Key", "Chưa kết nối"
|
| 168 |
|
| 169 |
-
# Khởi tạo
|
| 170 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 171 |
success = await gemini_service.start_session(
|
| 172 |
voice_name=voice_name,
|
| 173 |
callback=gemini_callback
|
| 174 |
)
|
| 175 |
|
| 176 |
if success:
|
| 177 |
-
|
|
|
|
|
|
|
| 178 |
else:
|
| 179 |
-
return False, "❌ Không thể kết nối", "Lỗi kết nối"
|
| 180 |
|
| 181 |
except Exception as e:
|
| 182 |
-
return False, f"❌ Lỗi kết nối: {str(e)}", "Lỗi kết nối"
|
| 183 |
|
| 184 |
-
async def disconnect_gemini():
|
| 185 |
"""Ngắt kết nối Gemini"""
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 191 |
try:
|
| 192 |
-
#
|
| 193 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 194 |
except Exception as e:
|
| 195 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 196 |
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 200 |
|
| 201 |
# Connection events
|
| 202 |
connect_btn.click(
|
| 203 |
connect_gemini,
|
| 204 |
-
inputs=[api_key, voice_select],
|
| 205 |
-
outputs=[connection_state, status_display, connection_info]
|
| 206 |
)
|
| 207 |
|
| 208 |
disconnect_btn.click(
|
| 209 |
disconnect_gemini,
|
| 210 |
-
|
|
|
|
| 211 |
)
|
| 212 |
|
| 213 |
-
#
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
|
|
|
| 217 |
)
|
| 218 |
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 222 |
)
|
| 223 |
|
| 224 |
-
# Đơn giản hóa JavaScript -
|
| 225 |
gemini_tab.load(
|
| 226 |
fn=None,
|
| 227 |
inputs=[],
|
| 228 |
outputs=[],
|
| 229 |
-
js="""
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
setTimeout(() => {
|
| 234 |
-
const statusElement = document.querySelector('[data-testid="status-display"] textarea');
|
| 235 |
-
if (statusElement) {
|
| 236 |
-
statusElement.value = 'Tab Gemini đã sẵn sàng';
|
| 237 |
-
}
|
| 238 |
-
}, 1000);
|
| 239 |
-
}
|
| 240 |
-
"""
|
| 241 |
)
|
| 242 |
|
| 243 |
-
#
|
| 244 |
with gr.Accordion("📖 Hướng dẫn sử dụng", open=False):
|
| 245 |
gr.Markdown("""
|
| 246 |
### Cách sử dụng Gemini Realtime:
|
|
@@ -252,45 +307,24 @@ def create_gemini_realtime_tab():
|
|
| 252 |
|
| 253 |
2. **Kết nối**:
|
| 254 |
- Chọn giọng nói yêu thích
|
| 255 |
-
- Nhấn "Kết nối" để thiết lập session
|
| 256 |
|
| 257 |
-
3. **
|
| 258 |
-
-
|
| 259 |
-
-
|
| 260 |
-
-
|
| 261 |
-
|
| 262 |
-
### Tính năng:
|
| 263 |
-
- 🎯 Hội thoại thời gian thực
|
| 264 |
-
- 🎙️ Nhận diện giọng nói
|
| 265 |
-
- 🔊 Phản hồi audio chất lượng cao
|
| 266 |
-
- ⚡ Độ trễ thấp
|
| 267 |
-
""")
|
| 268 |
-
|
| 269 |
-
# Demo functionality với text input
|
| 270 |
-
with gr.Accordion("🧪 Demo với Text Input", open=True):
|
| 271 |
-
demo_input = gr.Textbox(
|
| 272 |
-
label="Nhập tin nhắn demo",
|
| 273 |
-
placeholder="Nhập tin nhắn để test kết nối Gemini..."
|
| 274 |
-
)
|
| 275 |
-
demo_btn = gr.Button("Gửi tin nhắn demo")
|
| 276 |
-
demo_output = gr.Textbox(label="Kết quả")
|
| 277 |
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
try:
|
| 284 |
-
# Ở đây có thể tích hợp với text chat của Gemini
|
| 285 |
-
return f"✅ Đã gửi: {message} (Demo - chức năng text chat sẽ được tích hợp đầy đủ)"
|
| 286 |
-
except Exception as e:
|
| 287 |
-
return f"❌ Lỗi: {str(e)}"
|
| 288 |
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
)
|
| 294 |
|
| 295 |
return gemini_tab
|
| 296 |
|
|
|
|
| 23 |
streaming_voice_service: StreamingVoiceService,
|
| 24 |
hybrid_service: HybridStreamingService,
|
| 25 |
voice_coding_service: VoiceCodingService,
|
| 26 |
+
sambanova_voice_service: SambanovaVoiceService
|
| 27 |
):
|
| 28 |
|
| 29 |
with gr.Tab("🎙️ Streaming Voice "):
|
|
|
|
| 31 |
with gr.Tab("OpenAI Realtime"):
|
| 32 |
create_openai_realtime_tab(hybrid_service)
|
| 33 |
with gr.Tab("GemeniAI RealTime"):
|
| 34 |
+
create_gemini_realtime_tab() # Đã sửa - không cần tham số
|
| 35 |
with gr.Tab("SambonovaAI Realtime"):
|
| 36 |
+
create_sambanova_voice_tab(sambanova_voice_service) # Thêm tham số nếu cần
|
| 37 |
with gr.Tab("Generation Code"):
|
| 38 |
create_voice_coding_tab(voice_coding_service)
|
| 39 |
with gr.Tab("🎙️ Audio"):
|
|
|
|
| 51 |
with gr.Tab("🔊 Text-to-Speech"):
|
| 52 |
create_tts_tab(tts_service)
|
| 53 |
|
| 54 |
+
with gr.Tab("🌐 Language Info"):
|
| 55 |
create_language_info_tab(rag_system.multilingual_manager)
|
| 56 |
with gr.Tab("Stream Object Detection"):
|
| 57 |
create_streaming_object_detection()
|
| 58 |
def create_gemini_realtime_tab():
|
| 59 |
"""Tạo tab cho Gemini Realtime API"""
|
| 60 |
|
|
|
|
|
|
|
|
|
|
| 61 |
with gr.Blocks() as gemini_tab:
|
| 62 |
gr.Markdown("""
|
| 63 |
# 🎯 Gemini Realtime API
|
|
|
|
| 114 |
label="Gemini Chat",
|
| 115 |
type="messages",
|
| 116 |
height=400,
|
| 117 |
+
show_copy_button=True,
|
| 118 |
+
value=[] # Khởi tạo giá trị mặc định
|
| 119 |
)
|
| 120 |
|
| 121 |
+
# Audio interface - Simplified for now
|
| 122 |
with gr.Group():
|
| 123 |
+
gr.Markdown("### 🎤 Audio Streaming (Coming Soon)")
|
| 124 |
|
| 125 |
+
gr.Markdown("""
|
| 126 |
+
**Tính năng audio realtime đang được phát triển**
|
| 127 |
+
- 🎙️ Real-time voice recognition
|
| 128 |
+
- 🔊 Real-time audio response
|
| 129 |
+
- ⚡ Low latency streaming
|
| 130 |
+
""")
|
| 131 |
+
|
| 132 |
+
# Tạm thời ẩn các nút audio
|
| 133 |
+
# with gr.Row():
|
| 134 |
+
# start_audio_btn = gr.Button("🎙️ Bắt đầu nói", variant="primary", visible=False)
|
| 135 |
+
# stop_audio_btn = gr.Button("⏹️ Dừng nói", variant="secondary", visible=False)
|
| 136 |
|
| 137 |
transcription_display = gr.Textbox(
|
| 138 |
label="Bạn nói",
|
| 139 |
interactive=False,
|
| 140 |
lines=2,
|
| 141 |
+
placeholder="Tính năng audio đang được phát triển...",
|
| 142 |
+
visible=False
|
| 143 |
)
|
| 144 |
|
| 145 |
audio_output = gr.Audio(
|
| 146 |
label="Gemini trả lời",
|
| 147 |
interactive=False,
|
| 148 |
+
autoplay=True,
|
| 149 |
+
visible=False
|
| 150 |
)
|
| 151 |
|
| 152 |
# State management
|
| 153 |
connection_state = gr.State(value=False)
|
| 154 |
+
# audio_stream_state = gr.State(value=False)
|
| 155 |
+
|
| 156 |
+
# Khởi tạo service (không khởi tạo global để tránh lỗi)
|
| 157 |
+
gemini_service = None
|
| 158 |
+
|
| 159 |
+
async def initialize_gemini_service(api_key: str):
|
| 160 |
+
"""Khởi tạo Gemini service"""
|
| 161 |
+
nonlocal gemini_service
|
| 162 |
+
try:
|
| 163 |
+
from services.gemini_realtime_service import GeminiRealtimeService
|
| 164 |
+
gemini_service = GeminiRealtimeService(api_key)
|
| 165 |
+
await gemini_service.initialize()
|
| 166 |
+
return True, "✅ Đã khởi tạo Gemini service"
|
| 167 |
+
except Exception as e:
|
| 168 |
+
return False, f"❌ Lỗi khởi tạo: {str(e)}"
|
| 169 |
|
|
|
|
| 170 |
async def gemini_callback(data: dict):
|
| 171 |
"""Callback function để xử lý events từ Gemini service"""
|
| 172 |
if data['type'] == 'status':
|
| 173 |
return data['message'], data['message']
|
| 174 |
elif data['type'] == 'text':
|
|
|
|
|
|
|
|
|
|
| 175 |
return f"📝 Gemini: {data['content']}", data['content']
|
| 176 |
elif data['type'] == 'error':
|
| 177 |
return f"❌ Lỗi: {data['message']}", data['message']
|
| 178 |
return "Unknown event", "Unknown event"
|
| 179 |
|
| 180 |
# Event handlers for connection
|
| 181 |
+
async def connect_gemini(api_key: str, voice_name: str, current_chat):
|
| 182 |
"""Kết nối đến Gemini Realtime API"""
|
| 183 |
try:
|
| 184 |
if not api_key:
|
| 185 |
+
return False, "❌ Vui lòng nhập API Key", "Chưa kết nối", current_chat
|
| 186 |
|
| 187 |
+
# Khởi tạo service
|
| 188 |
+
success, message = await initialize_gemini_service(api_key)
|
| 189 |
+
if not success:
|
| 190 |
+
return False, message, "Lỗi khởi tạo", current_chat
|
| 191 |
+
|
| 192 |
+
# Kết nối session
|
| 193 |
success = await gemini_service.start_session(
|
| 194 |
voice_name=voice_name,
|
| 195 |
callback=gemini_callback
|
| 196 |
)
|
| 197 |
|
| 198 |
if success:
|
| 199 |
+
# Thêm message chào mừng
|
| 200 |
+
new_chat = current_chat + [{"role": "assistant", "content": f"Xin chào! Tôi là Gemini AI với giọng {voice_name}. Hãy bắt đầu trò chuyện!"}]
|
| 201 |
+
return True, "✅ Đã kết nối Gemini", f"Đang sử dụng giọng: {voice_name}", new_chat
|
| 202 |
else:
|
| 203 |
+
return False, "❌ Không thể kết nối Gemini", "Lỗi kết nối", current_chat
|
| 204 |
|
| 205 |
except Exception as e:
|
| 206 |
+
return False, f"❌ Lỗi kết nối: {str(e)}", "Lỗi kết nối", current_chat
|
| 207 |
|
| 208 |
+
async def disconnect_gemini(current_chat):
|
| 209 |
"""Ngắt kết nối Gemini"""
|
| 210 |
+
nonlocal gemini_service
|
| 211 |
+
if gemini_service:
|
| 212 |
+
await gemini_service.close()
|
| 213 |
+
gemini_service = None
|
| 214 |
+
|
| 215 |
+
# Thêm message thông báo ngắt kết nối
|
| 216 |
+
new_chat = current_chat + [{"role": "assistant", "content": "Đã ngắt kết nối với Gemini."}]
|
| 217 |
+
return False, "🔌 Đã ngắt kết nối", "Ngắt kết nối", new_chat
|
| 218 |
+
|
| 219 |
+
# Text chat functionality
|
| 220 |
+
async def send_text_message(message: str, history):
|
| 221 |
+
"""Gửi tin nhắn text đến Gemini"""
|
| 222 |
+
if not gemini_service or not gemini_service.is_active:
|
| 223 |
+
return history, "❌ Chưa kết nối Gemini. Vui lòng kết nối trước."
|
| 224 |
+
|
| 225 |
try:
|
| 226 |
+
# Thêm tin nhắn user vào history
|
| 227 |
+
new_history = history + [{"role": "user", "content": message}]
|
| 228 |
+
|
| 229 |
+
# Gửi đến Gemini (sử dụng text fallback)
|
| 230 |
+
response_text = await gemini_service.send_text(message)
|
| 231 |
+
|
| 232 |
+
if response_text:
|
| 233 |
+
# Thêm response vào history
|
| 234 |
+
new_history.append({"role": "assistant", "content": response_text})
|
| 235 |
+
return new_history, f"✅ Đã nhận phản hồi ({len(response_text)} ký tự)"
|
| 236 |
+
else:
|
| 237 |
+
error_msg = "❌ Không nhận được phản hồi từ Gemini"
|
| 238 |
+
new_history.append({"role": "assistant", "content": error_msg})
|
| 239 |
+
return new_history, error_msg
|
| 240 |
+
|
| 241 |
except Exception as e:
|
| 242 |
+
error_msg = f"❌ Lỗi: {str(e)}"
|
| 243 |
+
new_history = history + [
|
| 244 |
+
{"role": "user", "content": message},
|
| 245 |
+
{"role": "assistant", "content": error_msg}
|
| 246 |
+
]
|
| 247 |
+
return new_history, error_msg
|
| 248 |
|
| 249 |
+
# Thêm text input cho chat
|
| 250 |
+
with gr.Row():
|
| 251 |
+
text_input = gr.Textbox(
|
| 252 |
+
label="Tin nhắn của bạn",
|
| 253 |
+
placeholder="Nhập tin nhắn và nhấn Enter...",
|
| 254 |
+
scale=4
|
| 255 |
+
)
|
| 256 |
+
send_text_btn = gr.Button("📤 Gửi", scale=1)
|
| 257 |
|
| 258 |
# Connection events
|
| 259 |
connect_btn.click(
|
| 260 |
connect_gemini,
|
| 261 |
+
inputs=[api_key, voice_select, chatbot],
|
| 262 |
+
outputs=[connection_state, status_display, connection_info, chatbot]
|
| 263 |
)
|
| 264 |
|
| 265 |
disconnect_btn.click(
|
| 266 |
disconnect_gemini,
|
| 267 |
+
inputs=[chatbot],
|
| 268 |
+
outputs=[connection_state, status_display, connection_info, chatbot]
|
| 269 |
)
|
| 270 |
|
| 271 |
+
# Text message events
|
| 272 |
+
send_text_btn.click(
|
| 273 |
+
send_text_message,
|
| 274 |
+
inputs=[text_input, chatbot],
|
| 275 |
+
outputs=[chatbot, connection_info]
|
| 276 |
)
|
| 277 |
|
| 278 |
+
text_input.submit(
|
| 279 |
+
send_text_message,
|
| 280 |
+
inputs=[text_input, chatbot],
|
| 281 |
+
outputs=[chatbot, connection_info]
|
| 282 |
+
).then(
|
| 283 |
+
lambda: "", # Clear input
|
| 284 |
+
outputs=[text_input]
|
| 285 |
)
|
| 286 |
|
| 287 |
+
# Đơn giản hóa JavaScript - sửa lỗi syntax
|
| 288 |
gemini_tab.load(
|
| 289 |
fn=None,
|
| 290 |
inputs=[],
|
| 291 |
outputs=[],
|
| 292 |
+
js="""function() {
|
| 293 |
+
console.log('Gemini Realtime Tab Loaded Successfully');
|
| 294 |
+
return [];
|
| 295 |
+
}"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 296 |
)
|
| 297 |
|
| 298 |
+
# Hướng dẫn sử dụng
|
| 299 |
with gr.Accordion("📖 Hướng dẫn sử dụng", open=False):
|
| 300 |
gr.Markdown("""
|
| 301 |
### Cách sử dụng Gemini Realtime:
|
|
|
|
| 307 |
|
| 308 |
2. **Kết nối**:
|
| 309 |
- Chọn giọng nói yêu thích
|
| 310 |
+
- Nhấn **"Kết nối"** để thiết lập session
|
| 311 |
|
| 312 |
+
3. **Trò chuyện**:
|
| 313 |
+
- Nhập tin nhắn vào ô text
|
| 314 |
+
- Nhấn **Enter** hoặc **"Gửi"** để gửi
|
| 315 |
+
- Gemini sẽ trả lời ngay lập tức
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 316 |
|
| 317 |
+
### Tính năng hiện có:
|
| 318 |
+
- 💬 Text chat với Gemini
|
| 319 |
+
- 🔗 Kết nối realtime
|
| 320 |
+
- 🎯 Multiple voice options
|
| 321 |
+
- 📊 Status monitoring
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 322 |
|
| 323 |
+
### Tính năng sắp có:
|
| 324 |
+
- 🎙️ Real-time voice recognition
|
| 325 |
+
- 🔊 Real-time audio response
|
| 326 |
+
- ⚡ Low latency streaming
|
| 327 |
+
""")
|
| 328 |
|
| 329 |
return gemini_tab
|
| 330 |
|