Update ui/tabs.py
Browse files- ui/tabs.py +61 -52
ui/tabs.py
CHANGED
|
@@ -53,10 +53,7 @@ def create_all_tabs(audio_service: AudioService, chat_service: ChatService,
|
|
| 53 |
with gr.Tab("Stream Object Detection"):
|
| 54 |
create_streaming_object_detection()
|
| 55 |
def create_sambanova_voice_tab(sambanova_service):
|
| 56 |
-
"""Tạo tab Sambanova Voice AI"""
|
| 57 |
-
|
| 58 |
-
# Tạo stream
|
| 59 |
-
stream = sambanova_service.create_stream()
|
| 60 |
|
| 61 |
with gr.Blocks() as sambanova_tab:
|
| 62 |
gr.Markdown("## 🤖 Sambanova Voice AI - Llama 3.2 3B")
|
|
@@ -82,86 +79,95 @@ def create_sambanova_voice_tab(sambanova_service):
|
|
| 82 |
4. Mỗi phiên giới hạn 90 giây
|
| 83 |
|
| 84 |
**Tính năng:**
|
| 85 |
-
✅ Voice-to-
|
| 86 |
-
✅ Real-time streaming
|
| 87 |
✅ Llama 3.2 3B model
|
| 88 |
-
✅
|
| 89 |
✅ Multi-language support
|
|
|
|
|
|
|
| 90 |
""")
|
| 91 |
|
| 92 |
-
#
|
|
|
|
| 93 |
webrtc = stream.ui()
|
| 94 |
|
| 95 |
-
# Manual
|
| 96 |
-
with gr.Accordion("
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
label="Temperature"
|
| 102 |
-
)
|
| 103 |
-
top_p = gr.Slider(
|
| 104 |
-
minimum=0.1,
|
| 105 |
-
maximum=1.0,
|
| 106 |
-
value=0.1,
|
| 107 |
-
label="Top-P"
|
| 108 |
)
|
|
|
|
| 109 |
|
|
|
|
| 110 |
with gr.Row():
|
| 111 |
clear_btn = gr.Button("🗑️ Xóa hội thoại", variant="secondary")
|
| 112 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
|
| 114 |
with gr.Column(scale=2):
|
| 115 |
-
#
|
| 116 |
-
with gr.Accordion("📊 Model
|
| 117 |
gr.Markdown("""
|
| 118 |
-
**Llama 3.2 3B Instruct**
|
| 119 |
- **Provider**: Sambanova AI
|
| 120 |
-
- **Context
|
| 121 |
-
- **Languages**: Multi-language
|
| 122 |
- **Specialization**: Instruction following
|
| 123 |
- **API**: OpenAI-compatible
|
| 124 |
|
| 125 |
**Voice Features:**
|
| 126 |
- Real-time speech recognition
|
| 127 |
-
- Voice activity detection
|
| 128 |
- Stop-word activation
|
| 129 |
-
-
|
| 130 |
""")
|
| 131 |
|
| 132 |
-
#
|
| 133 |
-
|
| 134 |
-
label="🔄 Trạng thái hệ thống",
|
| 135 |
-
value="Sẵn sàng kết nối...",
|
| 136 |
-
interactive=False,
|
| 137 |
-
lines=2
|
| 138 |
-
)
|
| 139 |
|
| 140 |
# Additional outputs handling
|
| 141 |
-
def handle_additional_outputs(
|
| 142 |
"""Xử lý additional outputs từ streaming"""
|
| 143 |
-
return
|
| 144 |
|
| 145 |
# Event handlers
|
| 146 |
def clear_conversation():
|
| 147 |
"""Xóa hội thoại"""
|
| 148 |
return [], [], "🔄 Đã xóa hội thoại"
|
| 149 |
|
| 150 |
-
def
|
| 151 |
-
"""
|
| 152 |
-
if not
|
| 153 |
-
return "❌
|
| 154 |
|
| 155 |
try:
|
| 156 |
-
|
| 157 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 158 |
|
| 159 |
-
|
| 160 |
-
json.dump(chat_history, f, ensure_ascii=False, indent=2)
|
| 161 |
|
| 162 |
-
return f"✅ Đã export chat thành {filename}"
|
| 163 |
except Exception as e:
|
| 164 |
-
|
|
|
|
| 165 |
|
| 166 |
# Kết nối events
|
| 167 |
webrtc.on_additional_outputs(
|
|
@@ -174,10 +180,13 @@ def create_sambanova_voice_tab(sambanova_service):
|
|
| 174 |
outputs=[chatbot, conversation_state, status_display]
|
| 175 |
)
|
| 176 |
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
inputs=[chatbot],
|
| 180 |
-
outputs=[status_display]
|
|
|
|
|
|
|
|
|
|
| 181 |
)
|
| 182 |
|
| 183 |
return sambanova_tab
|
|
|
|
| 53 |
with gr.Tab("Stream Object Detection"):
|
| 54 |
create_streaming_object_detection()
|
| 55 |
def create_sambanova_voice_tab(sambanova_service):
|
| 56 |
+
"""Tạo tab Sambanova Voice AI - Simplified version"""
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
with gr.Blocks() as sambanova_tab:
|
| 59 |
gr.Markdown("## 🤖 Sambanova Voice AI - Llama 3.2 3B")
|
|
|
|
| 79 |
4. Mỗi phiên giới hạn 90 giây
|
| 80 |
|
| 81 |
**Tính năng:**
|
| 82 |
+
✅ Voice-to-Text conversation
|
|
|
|
| 83 |
✅ Llama 3.2 3B model
|
| 84 |
+
✅ Stop-word activation
|
| 85 |
✅ Multi-language support
|
| 86 |
+
|
| 87 |
+
**Lưu ý:** Chức năng voice output đang được phát triển
|
| 88 |
""")
|
| 89 |
|
| 90 |
+
# Tạo stream
|
| 91 |
+
stream = sambanova_service.create_stream()
|
| 92 |
webrtc = stream.ui()
|
| 93 |
|
| 94 |
+
# Manual text input fallback
|
| 95 |
+
with gr.Accordion("📝 Text Input (Fallback)", open=False):
|
| 96 |
+
text_input = gr.Textbox(
|
| 97 |
+
label="Nhập tin nhắn",
|
| 98 |
+
placeholder="Nhập tin nhắn text nếu voice không hoạt động...",
|
| 99 |
+
lines=2
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
)
|
| 101 |
+
text_send_btn = gr.Button("📤 Gửi tin nhắn")
|
| 102 |
|
| 103 |
+
# Controls
|
| 104 |
with gr.Row():
|
| 105 |
clear_btn = gr.Button("🗑️ Xóa hội thoại", variant="secondary")
|
| 106 |
+
status_display = gr.Textbox(
|
| 107 |
+
label="Trạng thái",
|
| 108 |
+
value="✅ Sẵn sàng kết nối...",
|
| 109 |
+
interactive=False
|
| 110 |
+
)
|
| 111 |
|
| 112 |
with gr.Column(scale=2):
|
| 113 |
+
# Model information
|
| 114 |
+
with gr.Accordion("📊 Thông tin Model", open=True):
|
| 115 |
gr.Markdown("""
|
| 116 |
+
**Llama 3.2 3B Instruct - Sambanova**
|
| 117 |
- **Provider**: Sambanova AI
|
| 118 |
+
- **Context**: 128K tokens
|
|
|
|
| 119 |
- **Specialization**: Instruction following
|
| 120 |
- **API**: OpenAI-compatible
|
| 121 |
|
| 122 |
**Voice Features:**
|
| 123 |
- Real-time speech recognition
|
| 124 |
+
- Voice activity detection
|
| 125 |
- Stop-word activation
|
| 126 |
+
- Text-based responses
|
| 127 |
""")
|
| 128 |
|
| 129 |
+
# Conversation display
|
| 130 |
+
gr.Markdown("### 💬 Cuộc trò chuyện")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
|
| 132 |
# Additional outputs handling
|
| 133 |
+
def handle_additional_outputs(new_chatbot, new_state):
|
| 134 |
"""Xử lý additional outputs từ streaming"""
|
| 135 |
+
return new_chatbot, new_state, "✅ Đang trò chuyện..."
|
| 136 |
|
| 137 |
# Event handlers
|
| 138 |
def clear_conversation():
|
| 139 |
"""Xóa hội thoại"""
|
| 140 |
return [], [], "🔄 Đã xóa hội thoại"
|
| 141 |
|
| 142 |
+
def send_text_message(text, current_chatbot, current_state):
|
| 143 |
+
"""Gửi tin nhắn text"""
|
| 144 |
+
if not text.strip():
|
| 145 |
+
return current_chatbot, current_state, "❌ Vui lòng nhập tin nhắn"
|
| 146 |
|
| 147 |
try:
|
| 148 |
+
# Thêm user message
|
| 149 |
+
new_chatbot = current_chatbot + [{"role": "user", "content": text}]
|
| 150 |
+
new_state = current_state + [{"role": "user", "content": text}]
|
| 151 |
+
|
| 152 |
+
# Gọi Sambanova API
|
| 153 |
+
response = sambanova_service.client.chat.completions.create(
|
| 154 |
+
model="Meta-Llama-3.2-3B-Instruct",
|
| 155 |
+
messages=new_state,
|
| 156 |
+
temperature=0.1,
|
| 157 |
+
top_p=0.1,
|
| 158 |
+
)
|
| 159 |
+
|
| 160 |
+
ai_response = response.choices[0].message.content
|
| 161 |
+
|
| 162 |
+
# Thêm AI response
|
| 163 |
+
new_chatbot = new_chatbot + [{"role": "assistant", "content": ai_response}]
|
| 164 |
+
new_state = new_state + [{"role": "assistant", "content": ai_response}]
|
| 165 |
|
| 166 |
+
return new_chatbot, new_state, "✅ Đã gửi tin nhắn"
|
|
|
|
| 167 |
|
|
|
|
| 168 |
except Exception as e:
|
| 169 |
+
error_msg = f"❌ Lỗi: {str(e)}"
|
| 170 |
+
return current_chatbot, current_state, error_msg
|
| 171 |
|
| 172 |
# Kết nối events
|
| 173 |
webrtc.on_additional_outputs(
|
|
|
|
| 180 |
outputs=[chatbot, conversation_state, status_display]
|
| 181 |
)
|
| 182 |
|
| 183 |
+
text_send_btn.click(
|
| 184 |
+
send_text_message,
|
| 185 |
+
inputs=[text_input, chatbot, conversation_state],
|
| 186 |
+
outputs=[chatbot, conversation_state, status_display]
|
| 187 |
+
).then(
|
| 188 |
+
lambda: "", # Clear text input
|
| 189 |
+
outputs=[text_input]
|
| 190 |
)
|
| 191 |
|
| 192 |
return sambanova_tab
|