Update ui/tabs.py
Browse files- ui/tabs.py +154 -1
ui/tabs.py
CHANGED
|
@@ -9,6 +9,7 @@ from services.image_service import ImageService
|
|
| 9 |
from services.streaming_voice_service import StreamingVoiceService
|
| 10 |
from services.openai_realtime_service import HybridStreamingService
|
| 11 |
from services.stream_object_detection_service import StreamObjectDetection
|
|
|
|
| 12 |
from core.rag_system import EnhancedRAGSystem
|
| 13 |
from core.tts_service import EnhancedTTSService
|
| 14 |
from core.wikipedia_processor import WikipediaProcessor
|
|
@@ -18,13 +19,16 @@ def create_all_tabs(audio_service: AudioService, chat_service: ChatService,
|
|
| 18 |
image_service: ImageService, rag_system: EnhancedRAGSystem,
|
| 19 |
tts_service: EnhancedTTSService, wikipedia_processor: WikipediaProcessor,
|
| 20 |
streaming_voice_service: StreamingVoiceService,
|
| 21 |
-
hybrid_service: HybridStreamingService
|
|
|
|
| 22 |
):
|
| 23 |
|
| 24 |
with gr.Tab("🎙️ Streaming Voice "):
|
| 25 |
create_streaming_voice_tab(streaming_voice_service)
|
| 26 |
with gr.Tab("OpenAI Realtime"):
|
| 27 |
create_openai_realtime_tab(hybrid_service)
|
|
|
|
|
|
|
| 28 |
with gr.Tab("🎙️ Audio"):
|
| 29 |
create_audio_tab(audio_service)
|
| 30 |
|
|
@@ -44,7 +48,156 @@ def create_all_tabs(audio_service: AudioService, chat_service: ChatService,
|
|
| 44 |
create_language_info_tab(rag_system.multilingual_manager)
|
| 45 |
with gr.Tab("Stream Object Detection"):
|
| 46 |
create_streaming_object_detection()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
def create_openai_realtime_tab(hybrid_service: HybridStreamingService):
|
| 49 |
"""Tạo tab cho OpenAI Realtime API"""
|
| 50 |
|
|
|
|
| 9 |
from services.streaming_voice_service import StreamingVoiceService
|
| 10 |
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 core.rag_system import EnhancedRAGSystem
|
| 14 |
from core.tts_service import EnhancedTTSService
|
| 15 |
from core.wikipedia_processor import WikipediaProcessor
|
|
|
|
| 19 |
image_service: ImageService, rag_system: EnhancedRAGSystem,
|
| 20 |
tts_service: EnhancedTTSService, wikipedia_processor: WikipediaProcessor,
|
| 21 |
streaming_voice_service: StreamingVoiceService,
|
| 22 |
+
hybrid_service: HybridStreamingService,
|
| 23 |
+
voice_coding_service: VoiceCodingService
|
| 24 |
):
|
| 25 |
|
| 26 |
with gr.Tab("🎙️ Streaming Voice "):
|
| 27 |
create_streaming_voice_tab(streaming_voice_service)
|
| 28 |
with gr.Tab("OpenAI Realtime"):
|
| 29 |
create_openai_realtime_tab(hybrid_service)
|
| 30 |
+
with gr.Tab("Voice Coding"):
|
| 31 |
+
create_voice_coding_tab(voice_coding_service)
|
| 32 |
with gr.Tab("🎙️ Audio"):
|
| 33 |
create_audio_tab(audio_service)
|
| 34 |
|
|
|
|
| 48 |
create_language_info_tab(rag_system.multilingual_manager)
|
| 49 |
with gr.Tab("Stream Object Detection"):
|
| 50 |
create_streaming_object_detection()
|
| 51 |
+
def create_voice_coding_tab(voice_coding_service):
|
| 52 |
+
"""Tạo tab Voice Coding"""
|
| 53 |
+
|
| 54 |
+
with gr.Blocks() as coding_tab:
|
| 55 |
+
gr.Markdown("## 🦙 Voice Coding - Lập trình bằng giọng nói")
|
| 56 |
+
gr.Markdown("Tạo và chỉnh sửa ứng dụng HTML single-file chỉ bằng giọng nói!")
|
| 57 |
+
|
| 58 |
+
# State variables
|
| 59 |
+
history = gr.State([{"role": "system", "content": voice_coding_service.system_prompt}])
|
| 60 |
+
current_code = gr.State("")
|
| 61 |
+
|
| 62 |
+
with gr.Row():
|
| 63 |
+
with gr.Column(scale=1):
|
| 64 |
+
gr.Markdown("### 🎤 Hướng dẫn sử dụng:")
|
| 65 |
+
gr.Markdown("""
|
| 66 |
+
1. Nhấn **Bắt đầu Recording**
|
| 67 |
+
2. Nói "Hello Llama" và chờ xác nhận
|
| 68 |
+
3. Đưa ra yêu cầu lập trình của bạn
|
| 69 |
+
4. Mỗi phiên giới hạn 90 giây
|
| 70 |
+
|
| 71 |
+
**Ví dụ:**
|
| 72 |
+
- "Tạo trang web hello world"
|
| 73 |
+
- "Tạo calculator bằng HTML"
|
| 74 |
+
- "Tạo đồng hồ digital"
|
| 75 |
+
""")
|
| 76 |
+
|
| 77 |
+
# WebRTC component
|
| 78 |
+
webrtc = WebRTC(
|
| 79 |
+
rtc_configuration=voice_coding_service.rtc_configuration,
|
| 80 |
+
mode="send",
|
| 81 |
+
modality="audio",
|
| 82 |
+
label="🎙️ Microphone"
|
| 83 |
+
)
|
| 84 |
+
|
| 85 |
+
# Manual input fallback
|
| 86 |
+
with gr.Accordion("📝 Nhập text thủ công", open=False):
|
| 87 |
+
text_input = gr.Textbox(
|
| 88 |
+
label="Yêu cầu lập trình",
|
| 89 |
+
placeholder="Nhập yêu cầu code của bạn ở đây...",
|
| 90 |
+
lines=3
|
| 91 |
+
)
|
| 92 |
+
text_submit_btn = gr.Button("🚀 Generate Code")
|
| 93 |
+
|
| 94 |
+
# Controls
|
| 95 |
+
with gr.Row():
|
| 96 |
+
clear_btn = gr.Button("🗑️ Xóa hội thoại")
|
| 97 |
+
reset_btn = gr.Button("🔄 Reset Code")
|
| 98 |
+
|
| 99 |
+
with gr.Column(scale=2):
|
| 100 |
+
with gr.Tabs():
|
| 101 |
+
with gr.Tab("🎮 Sandbox Preview"):
|
| 102 |
+
sandbox = gr.HTML(
|
| 103 |
+
value=voice_coding_service.sandbox_html,
|
| 104 |
+
label="Live Preview"
|
| 105 |
+
)
|
| 106 |
+
|
| 107 |
+
with gr.Tab("📄 Code Editor"):
|
| 108 |
+
code_display = gr.Code(
|
| 109 |
+
language="html",
|
| 110 |
+
label="Generated HTML Code",
|
| 111 |
+
lines=25,
|
| 112 |
+
interactive=True, # Cho phép chỉnh sửa
|
| 113 |
+
elem_classes="code-component"
|
| 114 |
+
)
|
| 115 |
+
|
| 116 |
+
with gr.Tab("💬 Chat History"):
|
| 117 |
+
chat_display = gr.Chatbot(
|
| 118 |
+
type="messages",
|
| 119 |
+
label="Lịch sử hội thoại",
|
| 120 |
+
height=400
|
| 121 |
+
)
|
| 122 |
+
|
| 123 |
+
# WebRTC streaming
|
| 124 |
+
webrtc.stream(
|
| 125 |
+
ReplyOnStopWords(
|
| 126 |
+
voice_coding_service.generate_code,
|
| 127 |
+
input_sample_rate=16000,
|
| 128 |
+
stop_words=["hello llama", "hello lama", "hello lamma", "hello llamma", "xin chào llama"],
|
| 129 |
+
),
|
| 130 |
+
inputs=[webrtc, history, code_display],
|
| 131 |
+
outputs=[webrtc],
|
| 132 |
+
time_limit=90,
|
| 133 |
+
concurrency_limit=5,
|
| 134 |
+
)
|
| 135 |
+
|
| 136 |
+
# Event handlers
|
| 137 |
+
def handle_additional_outputs(history, code):
|
| 138 |
+
"""Xử lý additional outputs từ streaming"""
|
| 139 |
+
return history, code, history
|
| 140 |
+
|
| 141 |
+
def update_sandbox(code):
|
| 142 |
+
"""Cập nhật sandbox khi code thay đổi"""
|
| 143 |
+
return voice_coding_service.display_in_sandbox(code)
|
| 144 |
|
| 145 |
+
def clear_conversation():
|
| 146 |
+
"""Xóa hội thoại"""
|
| 147 |
+
return [{"role": "system", "content": voice_coding_service.system_prompt}], "", voice_coding_service.sandbox_html
|
| 148 |
+
|
| 149 |
+
def reset_code():
|
| 150 |
+
"""Reset code về trạng thái ban đầu"""
|
| 151 |
+
return "", voice_coding_service.sandbox_html
|
| 152 |
+
|
| 153 |
+
def generate_from_text(text, history, current_code):
|
| 154 |
+
"""Generate code từ text input"""
|
| 155 |
+
if not text.strip():
|
| 156 |
+
return history, current_code, history, "Vui lòng nhập yêu cầu"
|
| 157 |
+
|
| 158 |
+
# Simulate audio input format
|
| 159 |
+
fake_audio = (16000, np.zeros(16000, dtype=np.float32))
|
| 160 |
+
|
| 161 |
+
# Use the same generation logic
|
| 162 |
+
result = None
|
| 163 |
+
for output in voice_coding_service.generate_code(fake_audio, history, current_code):
|
| 164 |
+
result = output
|
| 165 |
+
|
| 166 |
+
if result:
|
| 167 |
+
new_history, new_code = result.args
|
| 168 |
+
return new_history, new_code, new_history, "✅ Đã generate code từ text"
|
| 169 |
+
|
| 170 |
+
return history, current_code, history, "❌ Lỗi generate code"
|
| 171 |
+
|
| 172 |
+
# Connect events
|
| 173 |
+
webrtc.on_additional_outputs(
|
| 174 |
+
handle_additional_outputs,
|
| 175 |
+
outputs=[history, code_display, chat_display]
|
| 176 |
+
)
|
| 177 |
+
|
| 178 |
+
code_display.change(
|
| 179 |
+
update_sandbox,
|
| 180 |
+
inputs=[code_display],
|
| 181 |
+
outputs=[sandbox]
|
| 182 |
+
)
|
| 183 |
+
|
| 184 |
+
clear_btn.click(
|
| 185 |
+
clear_conversation,
|
| 186 |
+
outputs=[history, code_display, sandbox]
|
| 187 |
+
)
|
| 188 |
+
|
| 189 |
+
reset_btn.click(
|
| 190 |
+
reset_code,
|
| 191 |
+
outputs=[code_display, sandbox]
|
| 192 |
+
)
|
| 193 |
+
|
| 194 |
+
text_submit_btn.click(
|
| 195 |
+
generate_from_text,
|
| 196 |
+
inputs=[text_input, history, code_display],
|
| 197 |
+
outputs=[history, code_display, chat_display, text_input]
|
| 198 |
+
)
|
| 199 |
+
|
| 200 |
+
return coding_tab
|
| 201 |
def create_openai_realtime_tab(hybrid_service: HybridStreamingService):
|
| 202 |
"""Tạo tab cho OpenAI Realtime API"""
|
| 203 |
|