Update ui/tabs.py
Browse files- ui/tabs.py +97 -66
ui/tabs.py
CHANGED
|
@@ -49,22 +49,26 @@ def create_all_tabs(audio_service: AudioService, chat_service: ChatService,
|
|
| 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":
|
| 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"
|
| 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 |
|
|
@@ -74,22 +78,24 @@ def create_voice_coding_tab(voice_coding_service):
|
|
| 74 |
- "Tạo đồng hồ digital"
|
| 75 |
""")
|
| 76 |
|
| 77 |
-
#
|
| 78 |
-
|
| 79 |
-
rtc_configuration=voice_coding_service.rtc_configuration,
|
| 80 |
-
mode="send",
|
| 81 |
-
modality="audio",
|
| 82 |
-
label="🎙️ Microphone"
|
| 83 |
-
)
|
| 84 |
|
| 85 |
-
# Manual input
|
| 86 |
-
with gr.Accordion("📝 Nhập text thủ công", open=
|
| 87 |
text_input = gr.Textbox(
|
| 88 |
label="Yêu cầu lập trình",
|
| 89 |
-
placeholder="
|
| 90 |
lines=3
|
| 91 |
)
|
| 92 |
-
text_submit_btn = gr.Button("🚀 Generate Code")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
|
| 94 |
# Controls
|
| 95 |
with gr.Row():
|
|
@@ -109,8 +115,8 @@ def create_voice_coding_tab(voice_coding_service):
|
|
| 109 |
language="html",
|
| 110 |
label="Generated HTML Code",
|
| 111 |
lines=25,
|
| 112 |
-
interactive=True,
|
| 113 |
-
|
| 114 |
)
|
| 115 |
|
| 116 |
with gr.Tab("💬 Chat History"):
|
|
@@ -120,23 +126,48 @@ def create_voice_coding_tab(voice_coding_service):
|
|
| 120 |
height=400
|
| 121 |
)
|
| 122 |
|
| 123 |
-
#
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
|
| 141 |
def update_sandbox(code):
|
| 142 |
"""Cập nhật sandbox khi code thay đổi"""
|
|
@@ -144,35 +175,20 @@ def create_voice_coding_tab(voice_coding_service):
|
|
| 144 |
|
| 145 |
def clear_conversation():
|
| 146 |
"""Xóa hội thoại"""
|
| 147 |
-
return [{"role": "system", "content":
|
| 148 |
|
| 149 |
def reset_code():
|
| 150 |
-
"""Reset code
|
| 151 |
-
return "", voice_coding_service.sandbox_html
|
| 152 |
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 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(
|
|
@@ -183,21 +199,36 @@ def create_voice_coding_tab(voice_coding_service):
|
|
| 183 |
|
| 184 |
clear_btn.click(
|
| 185 |
clear_conversation,
|
| 186 |
-
outputs=[history,
|
| 187 |
)
|
| 188 |
|
| 189 |
reset_btn.click(
|
| 190 |
reset_code,
|
| 191 |
-
outputs=[
|
| 192 |
)
|
| 193 |
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 198 |
)
|
| 199 |
|
| 200 |
-
return coding_tab
|
| 201 |
def create_openai_realtime_tab(hybrid_service: HybridStreamingService):
|
| 202 |
"""Tạo tab cho OpenAI Realtime API"""
|
| 203 |
|
|
|
|
| 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 với FastRTC"""
|
| 53 |
+
|
| 54 |
+
# Tạo stream
|
| 55 |
+
stream = voice_coding_service.create_stream()
|
| 56 |
|
| 57 |
with gr.Blocks() as coding_tab:
|
| 58 |
gr.Markdown("## 🦙 Voice Coding - Lập trình bằng giọng nói")
|
| 59 |
gr.Markdown("Tạo và chỉnh sửa ứng dụng HTML single-file chỉ bằng giọng nói!")
|
| 60 |
|
| 61 |
# State variables
|
| 62 |
+
history = gr.State([{"role": "system", "content": "You are an AI coding assistant."}])
|
| 63 |
current_code = gr.State("")
|
| 64 |
+
status_message = gr.State("Sẵn sàng...")
|
| 65 |
|
| 66 |
with gr.Row():
|
| 67 |
with gr.Column(scale=1):
|
| 68 |
gr.Markdown("### 🎤 Hướng dẫn sử dụng:")
|
| 69 |
gr.Markdown("""
|
| 70 |
1. Nhấn **Bắt đầu Recording**
|
| 71 |
+
2. Nói "Hello Llama" hoặc "Code"
|
| 72 |
3. Đưa ra yêu cầu lập trình của bạn
|
| 73 |
4. Mỗi phiên giới hạn 90 giây
|
| 74 |
|
|
|
|
| 78 |
- "Tạo đồng hồ digital"
|
| 79 |
""")
|
| 80 |
|
| 81 |
+
# FastRTC component
|
| 82 |
+
fastrtc_audio = stream.ui()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
|
| 84 |
+
# Manual input
|
| 85 |
+
with gr.Accordion("📝 Nhập text thủ công", open=True):
|
| 86 |
text_input = gr.Textbox(
|
| 87 |
label="Yêu cầu lập trình",
|
| 88 |
+
placeholder="Ví dụ: Tạo trang web hello world với màu nền xanh...",
|
| 89 |
lines=3
|
| 90 |
)
|
| 91 |
+
text_submit_btn = gr.Button("🚀 Generate Code", variant="primary")
|
| 92 |
+
|
| 93 |
+
# Status display
|
| 94 |
+
status_display = gr.Textbox(
|
| 95 |
+
label="Trạng thái",
|
| 96 |
+
value="Sẵn sàng...",
|
| 97 |
+
interactive=False
|
| 98 |
+
)
|
| 99 |
|
| 100 |
# Controls
|
| 101 |
with gr.Row():
|
|
|
|
| 115 |
language="html",
|
| 116 |
label="Generated HTML Code",
|
| 117 |
lines=25,
|
| 118 |
+
interactive=True,
|
| 119 |
+
value=""
|
| 120 |
)
|
| 121 |
|
| 122 |
with gr.Tab("💬 Chat History"):
|
|
|
|
| 126 |
height=400
|
| 127 |
)
|
| 128 |
|
| 129 |
+
# Event handlers cho manual input
|
| 130 |
+
def generate_from_text(text, current_history, current_code_value):
|
| 131 |
+
"""Generate code từ text input"""
|
| 132 |
+
if not text.strip():
|
| 133 |
+
return current_history, current_code_value, current_history, "Vui lòng nhập yêu cầu", voice_coding_service.sandbox_html
|
| 134 |
+
|
| 135 |
+
try:
|
| 136 |
+
# Tạo prompt
|
| 137 |
+
user_prompt = f"Please write a single-file HTML application to fulfill the following request.\nThe message:{text}\nCurrent code you have written:{current_code_value}"
|
| 138 |
+
|
| 139 |
+
# Update history
|
| 140 |
+
new_history = current_history + [
|
| 141 |
+
{"role": "user", "content": user_prompt}
|
| 142 |
+
]
|
| 143 |
+
|
| 144 |
+
# Generate code với Groq
|
| 145 |
+
response = voice_coding_service.groq_client.chat.completions.create(
|
| 146 |
+
model="llama-3.3-70b-versatile",
|
| 147 |
+
messages=new_history,
|
| 148 |
+
temperature=1,
|
| 149 |
+
max_tokens=2048,
|
| 150 |
+
top_p=1,
|
| 151 |
+
stream=False,
|
| 152 |
+
)
|
| 153 |
+
|
| 154 |
+
output = response.choices[0].message.content
|
| 155 |
+
|
| 156 |
+
# Extract HTML
|
| 157 |
+
html_code = voice_coding_service.extract_html_content(output)
|
| 158 |
+
if not html_code:
|
| 159 |
+
html_code = f"<!-- Generated Code -->\n{output}"
|
| 160 |
+
|
| 161 |
+
# Update history
|
| 162 |
+
new_history.append({"role": "assistant", "content": output})
|
| 163 |
+
|
| 164 |
+
# Update sandbox
|
| 165 |
+
sandbox_html = voice_coding_service.display_in_sandbox(html_code)
|
| 166 |
+
|
| 167 |
+
return new_history, html_code, new_history, "✅ Đã generate code!", sandbox_html
|
| 168 |
+
|
| 169 |
+
except Exception as e:
|
| 170 |
+
return current_history, current_code_value, current_history, f"❌ Lỗi: {str(e)}", voice_coding_service.sandbox_html
|
| 171 |
|
| 172 |
def update_sandbox(code):
|
| 173 |
"""Cập nhật sandbox khi code thay đổi"""
|
|
|
|
| 175 |
|
| 176 |
def clear_conversation():
|
| 177 |
"""Xóa hội thoại"""
|
| 178 |
+
return [{"role": "system", "content": "You are an AI coding assistant."}], "", [], "Đã xóa hội thoại", voice_coding_service.sandbox_html
|
| 179 |
|
| 180 |
def reset_code():
|
| 181 |
+
"""Reset code"""
|
| 182 |
+
return "", voice_coding_service.sandbox_html, "Đã reset code"
|
| 183 |
|
| 184 |
+
# Kết nối events
|
| 185 |
+
text_submit_btn.click(
|
| 186 |
+
generate_from_text,
|
| 187 |
+
inputs=[text_input, history, current_code],
|
| 188 |
+
outputs=[history, current_code, chat_display, status_display, sandbox]
|
| 189 |
+
).then(
|
| 190 |
+
lambda: "", # Clear text input
|
| 191 |
+
outputs=[text_input]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 192 |
)
|
| 193 |
|
| 194 |
code_display.change(
|
|
|
|
| 199 |
|
| 200 |
clear_btn.click(
|
| 201 |
clear_conversation,
|
| 202 |
+
outputs=[history, current_code, chat_display, status_display, sandbox]
|
| 203 |
)
|
| 204 |
|
| 205 |
reset_btn.click(
|
| 206 |
reset_code,
|
| 207 |
+
outputs=[current_code, sandbox, status_display]
|
| 208 |
)
|
| 209 |
|
| 210 |
+
# FastRTC additional outputs handling
|
| 211 |
+
def handle_fastrtc_outputs(output_data):
|
| 212 |
+
"""Xử lý outputs từ FastRTC"""
|
| 213 |
+
if hasattr(output_data, 'args'):
|
| 214 |
+
data = output_data.args[0] if output_data.args else {}
|
| 215 |
+
|
| 216 |
+
if data.get('type') == 'code_generated':
|
| 217 |
+
return data.get('history', []), data.get('code', ''), data.get('history', []), data.get('message', ''), voice_coding_service.display_in_sandbox(data.get('code', ''))
|
| 218 |
+
elif data.get('type') == 'loading':
|
| 219 |
+
return data.get('history', []), data.get('code', ''), data.get('history', []), data.get('message', ''), voice_coding_service.loading_html
|
| 220 |
+
elif data.get('type') == 'error':
|
| 221 |
+
return data.get('history', []), data.get('code', ''), data.get('history', []), data.get('message', ''), voice_coding_service.sandbox_html
|
| 222 |
+
|
| 223 |
+
return history.value, current_code.value, chat_display.value, status_display.value, sandbox.value
|
| 224 |
+
|
| 225 |
+
# Kết nối FastRTC outputs
|
| 226 |
+
fastrtc_audio.on_additional_outputs(
|
| 227 |
+
handle_fastrtc_outputs,
|
| 228 |
+
outputs=[history, current_code, chat_display, status_display, sandbox]
|
| 229 |
)
|
| 230 |
|
| 231 |
+
return coding_tab
|
| 232 |
def create_openai_realtime_tab(hybrid_service: HybridStreamingService):
|
| 233 |
"""Tạo tab cho OpenAI Realtime API"""
|
| 234 |
|