Update ui/tabs.py
Browse files- ui/tabs.py +264 -0
ui/tabs.py
CHANGED
|
@@ -11,6 +11,8 @@ from services.openai_realtime_service import HybridStreamingService
|
|
| 11 |
from services.stream_object_detection_service import StreamObjectDetection
|
| 12 |
from services.voice_coding_service import VoiceCodingService
|
| 13 |
from services.sambanova_voice_service import SambanovaVoiceService
|
|
|
|
|
|
|
| 14 |
from core.rag_system import EnhancedRAGSystem
|
| 15 |
from core.tts_service import EnhancedTTSService
|
| 16 |
from core.wikipedia_processor import WikipediaProcessor
|
|
@@ -29,6 +31,8 @@ def create_all_tabs(audio_service: AudioService, chat_service: ChatService,
|
|
| 29 |
create_streaming_voice_tab(streaming_voice_service)
|
| 30 |
with gr.Tab("OpenAI Realtime"):
|
| 31 |
create_openai_realtime_tab(hybrid_service)
|
|
|
|
|
|
|
| 32 |
with gr.Tab("SambonovaAI Realtime"):
|
| 33 |
create_sambanova_voice_tab()
|
| 34 |
with gr.Tab("Generation Code"):
|
|
@@ -52,6 +56,266 @@ def create_all_tabs(audio_service: AudioService, chat_service: ChatService,
|
|
| 52 |
create_language_info_tab(rag_system.multilingual_manager)
|
| 53 |
with gr.Tab("Stream Object Detection"):
|
| 54 |
create_streaming_object_detection()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
def create_sambanova_voice_tab():
|
| 56 |
"""Tạo tab Sambanova AI với Voice Input/Output"""
|
| 57 |
|
|
|
|
| 11 |
from services.stream_object_detection_service import StreamObjectDetection
|
| 12 |
from services.voice_coding_service import VoiceCodingService
|
| 13 |
from services.sambanova_voice_service import SambanovaVoiceService
|
| 14 |
+
from services.gemini_realtime_service import GeminiRealtimeService, GeminiStreamHandler
|
| 15 |
+
|
| 16 |
from core.rag_system import EnhancedRAGSystem
|
| 17 |
from core.tts_service import EnhancedTTSService
|
| 18 |
from core.wikipedia_processor import WikipediaProcessor
|
|
|
|
| 31 |
create_streaming_voice_tab(streaming_voice_service)
|
| 32 |
with gr.Tab("OpenAI Realtime"):
|
| 33 |
create_openai_realtime_tab(hybrid_service)
|
| 34 |
+
with gr.tab("GemeniAI RealTime"):
|
| 35 |
+
create_gemini_realtime_tab()
|
| 36 |
with gr.Tab("SambonovaAI Realtime"):
|
| 37 |
create_sambanova_voice_tab()
|
| 38 |
with gr.Tab("Generation Code"):
|
|
|
|
| 56 |
create_language_info_tab(rag_system.multilingual_manager)
|
| 57 |
with gr.Tab("Stream Object Detection"):
|
| 58 |
create_streaming_object_detection()
|
| 59 |
+
def create_gemini_realtime_tab():
|
| 60 |
+
"""Tạo tab cho Gemini Realtime API"""
|
| 61 |
+
|
| 62 |
+
# Khởi tạo service
|
| 63 |
+
gemini_service = GeminiRealtimeService()
|
| 64 |
+
|
| 65 |
+
with gr.Blocks() as gemini_tab:
|
| 66 |
+
gr.Markdown("""
|
| 67 |
+
# 🎯 Gemini Realtime API
|
| 68 |
+
**Streaming hội thoại thời gian thực với Google Gemini**
|
| 69 |
+
""")
|
| 70 |
+
|
| 71 |
+
with gr.Row():
|
| 72 |
+
with gr.Column(scale=1):
|
| 73 |
+
# Connection controls
|
| 74 |
+
with gr.Group():
|
| 75 |
+
gr.Markdown("### 🔗 Kết nối")
|
| 76 |
+
|
| 77 |
+
api_key = gr.Textbox(
|
| 78 |
+
label="Gemini API Key",
|
| 79 |
+
type="password",
|
| 80 |
+
placeholder="Nhập API key của bạn...",
|
| 81 |
+
value=os.getenv("GEMINI_API_KEY", ""),
|
| 82 |
+
info="Lấy từ https://aistudio.google.com/"
|
| 83 |
+
)
|
| 84 |
+
|
| 85 |
+
voice_select = gr.Dropdown(
|
| 86 |
+
choices=["Puck", "Charon", "Kore", "Fenrir", "Aoede"],
|
| 87 |
+
value="Puck",
|
| 88 |
+
label="Giọng nói AI",
|
| 89 |
+
info="Chọn giọng nói cho Gemini"
|
| 90 |
+
)
|
| 91 |
+
|
| 92 |
+
with gr.Row():
|
| 93 |
+
connect_btn = gr.Button("🔗 Kết nối", variant="primary")
|
| 94 |
+
disconnect_btn = gr.Button("🔌 Ngắt kết nối", variant="secondary")
|
| 95 |
+
|
| 96 |
+
# Status panel
|
| 97 |
+
with gr.Group():
|
| 98 |
+
gr.Markdown("### 📊 Trạng thái")
|
| 99 |
+
|
| 100 |
+
status_display = gr.Textbox(
|
| 101 |
+
label="Trạng thái kết nối",
|
| 102 |
+
value="Chưa kết nối",
|
| 103 |
+
interactive=False
|
| 104 |
+
)
|
| 105 |
+
|
| 106 |
+
connection_info = gr.Textbox(
|
| 107 |
+
label="Thông tin",
|
| 108 |
+
interactive=False,
|
| 109 |
+
lines=2
|
| 110 |
+
)
|
| 111 |
+
|
| 112 |
+
with gr.Column(scale=2):
|
| 113 |
+
# Chat interface
|
| 114 |
+
with gr.Group():
|
| 115 |
+
gr.Markdown("### 💬 Hội thoại")
|
| 116 |
+
|
| 117 |
+
chatbot = gr.Chatbot(
|
| 118 |
+
label="Gemini Chat",
|
| 119 |
+
type="messages",
|
| 120 |
+
height=400,
|
| 121 |
+
show_copy_button=True
|
| 122 |
+
)
|
| 123 |
+
|
| 124 |
+
# Audio interface
|
| 125 |
+
with gr.Group():
|
| 126 |
+
gr.Markdown("### 🎤 Audio Streaming")
|
| 127 |
+
|
| 128 |
+
with gr.Row():
|
| 129 |
+
start_audio_btn = gr.Button("🎙️ Bắt đầu nói", variant="primary")
|
| 130 |
+
stop_audio_btn = gr.Button("⏹️ Dừng nói", variant="secondary")
|
| 131 |
+
|
| 132 |
+
transcription_display = gr.Textbox(
|
| 133 |
+
label="Bạn nói",
|
| 134 |
+
interactive=False,
|
| 135 |
+
lines=2,
|
| 136 |
+
placeholder="Văn bản nhận diện sẽ hiển thị ở đây..."
|
| 137 |
+
)
|
| 138 |
+
|
| 139 |
+
audio_output = gr.Audio(
|
| 140 |
+
label="Gemini trả lời",
|
| 141 |
+
interactive=False,
|
| 142 |
+
autoplay=True
|
| 143 |
+
)
|
| 144 |
+
|
| 145 |
+
# State management
|
| 146 |
+
connection_state = gr.State(value=False)
|
| 147 |
+
audio_stream_state = gr.State(value=False)
|
| 148 |
+
|
| 149 |
+
# Event handlers for connection
|
| 150 |
+
def connect_gemini(api_key: str, voice_name: str):
|
| 151 |
+
"""Kết nối đến Gemini Realtime API"""
|
| 152 |
+
try:
|
| 153 |
+
if not api_key:
|
| 154 |
+
return False, "❌ Vui lòng nhập API Key", "Chưa kết nối"
|
| 155 |
+
|
| 156 |
+
# Lưu API key
|
| 157 |
+
gemini_service.api_key = api_key
|
| 158 |
+
|
| 159 |
+
return True, "✅ Đang kết nối...", f"Đang kết nối với giọng: {voice_name}"
|
| 160 |
+
|
| 161 |
+
except Exception as e:
|
| 162 |
+
return False, f"❌ Lỗi kết nối: {str(e)}", "Lỗi kết nối"
|
| 163 |
+
|
| 164 |
+
def disconnect_gemini():
|
| 165 |
+
"""Ngắt kết nối Gemini"""
|
| 166 |
+
asyncio.create_task(gemini_service.close())
|
| 167 |
+
return False, "🔌 Đã ngắt kết nối", "Ngắt kết nối"
|
| 168 |
+
|
| 169 |
+
def handle_gemini_callback(data: dict):
|
| 170 |
+
"""Xử lý callback từ Gemini service"""
|
| 171 |
+
if data['type'] == 'status':
|
| 172 |
+
return data['message']
|
| 173 |
+
elif data['type'] == 'text':
|
| 174 |
+
return f"📝 Gemini: {data['content']}"
|
| 175 |
+
elif data['type'] == 'error':
|
| 176 |
+
return f"❌ Lỗi: {data['message']}"
|
| 177 |
+
return "Unknown event"
|
| 178 |
+
|
| 179 |
+
def start_audio_stream():
|
| 180 |
+
"""Bắt đầu stream audio"""
|
| 181 |
+
# Setup callback handler
|
| 182 |
+
gemini_service.callback = handle_gemini_callback
|
| 183 |
+
return True, "🎙️ Đang nghe..."
|
| 184 |
+
|
| 185 |
+
def stop_audio_stream():
|
| 186 |
+
"""Dừng stream audio"""
|
| 187 |
+
return False, "⏹️ Đã dừng"
|
| 188 |
+
|
| 189 |
+
def update_chat(history, message: str, role: str = "user"):
|
| 190 |
+
"""Cập nhật chat history"""
|
| 191 |
+
if not message:
|
| 192 |
+
return history
|
| 193 |
+
|
| 194 |
+
if role == "user":
|
| 195 |
+
history.append([message, None])
|
| 196 |
+
else:
|
| 197 |
+
if history and history[-1][1] is None:
|
| 198 |
+
history[-1][1] = message
|
| 199 |
+
else:
|
| 200 |
+
history.append([None, message])
|
| 201 |
+
return history
|
| 202 |
+
|
| 203 |
+
# Connection events
|
| 204 |
+
connect_btn.click(
|
| 205 |
+
connect_gemini,
|
| 206 |
+
inputs=[api_key, voice_select],
|
| 207 |
+
outputs=[connection_state, status_display, connection_info]
|
| 208 |
+
)
|
| 209 |
+
|
| 210 |
+
disconnect_btn.click(
|
| 211 |
+
disconnect_gemini,
|
| 212 |
+
outputs=[connection_state, status_display, connection_info]
|
| 213 |
+
)
|
| 214 |
+
|
| 215 |
+
# Audio events
|
| 216 |
+
start_audio_btn.click(
|
| 217 |
+
start_audio_stream,
|
| 218 |
+
outputs=[audio_stream_state, transcription_display]
|
| 219 |
+
)
|
| 220 |
+
|
| 221 |
+
stop_audio_btn.click(
|
| 222 |
+
stop_audio_stream,
|
| 223 |
+
outputs=[audio_stream_state, transcription_display]
|
| 224 |
+
)
|
| 225 |
+
|
| 226 |
+
# JavaScript for real-time updates
|
| 227 |
+
gemini_tab.load(
|
| 228 |
+
fn=None,
|
| 229 |
+
inputs=[],
|
| 230 |
+
outputs=[],
|
| 231 |
+
js="""
|
| 232 |
+
function setupGeminiRealtime() {
|
| 233 |
+
console.log('Setting up Gemini Realtime interface...');
|
| 234 |
+
|
| 235 |
+
// Setup WebRTC or other real-time communication
|
| 236 |
+
const audioContext = new (window.AudioContext || window.webkitAudioContext)();
|
| 237 |
+
|
| 238 |
+
// Event listeners for real-time updates
|
| 239 |
+
document.addEventListener('gemini_event', function(e) {
|
| 240 |
+
const data = e.detail;
|
| 241 |
+
handleGeminiEvent(data);
|
| 242 |
+
});
|
| 243 |
+
|
| 244 |
+
function handleGeminiEvent(data) {
|
| 245 |
+
switch(data.type) {
|
| 246 |
+
case 'transcription':
|
| 247 |
+
updateTranscription(data.text);
|
| 248 |
+
break;
|
| 249 |
+
case 'response':
|
| 250 |
+
updateChatbot(data.text, 'assistant');
|
| 251 |
+
break;
|
| 252 |
+
case 'audio':
|
| 253 |
+
playAudioData(data.audioData, data.sampleRate);
|
| 254 |
+
break;
|
| 255 |
+
case 'status':
|
| 256 |
+
updateStatus(data.message);
|
| 257 |
+
break;
|
| 258 |
+
}
|
| 259 |
+
}
|
| 260 |
+
|
| 261 |
+
function updateTranscription(text) {
|
| 262 |
+
const elem = document.querySelector('[data-testid="transcription-display"] textarea');
|
| 263 |
+
if (elem) elem.value = text;
|
| 264 |
+
}
|
| 265 |
+
|
| 266 |
+
function updateStatus(message) {
|
| 267 |
+
const elem = document.querySelector('[data-testid="status-display"] textarea');
|
| 268 |
+
if (elem) elem.value = message;
|
| 269 |
+
}
|
| 270 |
+
|
| 271 |
+
function playAudioData(audioData, sampleRate) {
|
| 272 |
+
// Implementation for playing audio data
|
| 273 |
+
console.log('Playing audio from Gemini...');
|
| 274 |
+
}
|
| 275 |
+
}
|
| 276 |
+
setupGeminiRealtime();
|
| 277 |
+
"""
|
| 278 |
+
)
|
| 279 |
+
|
| 280 |
+
# Additional examples and instructions
|
| 281 |
+
with gr.Accordion("📖 Hướng dẫn sử dụng", open=False):
|
| 282 |
+
gr.Markdown("""
|
| 283 |
+
### Cách sử dụng Gemini Realtime:
|
| 284 |
+
|
| 285 |
+
1. **Lấy API Key**:
|
| 286 |
+
- Truy cập [Google AI Studio](https://aistudio.google.com/)
|
| 287 |
+
- Tạo API key mới trong mục API Keys
|
| 288 |
+
- Sao chép và dán vào ô API Key
|
| 289 |
+
|
| 290 |
+
2. **Kết nối**:
|
| 291 |
+
- Chọn giọng nói yêu thích
|
| 292 |
+
- Nhấn "Kết nối" để thiết lập session
|
| 293 |
+
|
| 294 |
+
3. **Hội thoại**:
|
| 295 |
+
- Nhấn "Bắt đầu nói" để bắt đầu thu âm
|
| 296 |
+
- Gemini sẽ trả lời trong thời gian thực
|
| 297 |
+
- Nhấn "Dừng nói" để kết thúc
|
| 298 |
+
|
| 299 |
+
### Tính năng:
|
| 300 |
+
- 🎯 Hội thoại thời gian thực
|
| 301 |
+
- 🎙️ Nhận diện giọng nói
|
| 302 |
+
- 🔊 Phản hồi audio chất lượng cao
|
| 303 |
+
- ⚡ Độ trễ thấp
|
| 304 |
+
""")
|
| 305 |
+
|
| 306 |
+
return gemini_tab
|
| 307 |
+
|
| 308 |
+
# FastAPI routes cho Gemini (tùy chọn)
|
| 309 |
+
def setup_gemini_routes(app):
|
| 310 |
+
"""Thiết lập routes FastAPI cho Gemini"""
|
| 311 |
+
|
| 312 |
+
@app.get("/gemini/status")
|
| 313 |
+
async def get_gemini_status():
|
| 314 |
+
return {"status": "active", "service": "gemini_realtime"}
|
| 315 |
+
|
| 316 |
+
@app.post("/gemini/connect")
|
| 317 |
+
async def connect_gemini():
|
| 318 |
+
return {"message": "Gemini connection endpoint"}
|
| 319 |
def create_sambanova_voice_tab():
|
| 320 |
"""Tạo tab Sambanova AI với Voice Input/Output"""
|
| 321 |
|