datbkpro commited on
Commit
2029cd9
·
verified ·
1 Parent(s): cc93a2a

Update ui/tabs.py

Browse files
Files changed (1) hide show
  1. 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-Voice conversation
86
- ✅ Real-time streaming
87
  ✅ Llama 3.2 3B model
88
- Low latency
89
  ✅ Multi-language support
 
 
90
  """)
91
 
92
- # WebRTC component
 
93
  webrtc = stream.ui()
94
 
95
- # Manual controls
96
- with gr.Accordion("⚙️ Cài đặt nâng cao", open=False):
97
- temperature = gr.Slider(
98
- minimum=0.1,
99
- maximum=1.0,
100
- value=0.1,
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
- export_btn = gr.Button("💾 Export Chat", variant="secondary")
 
 
 
 
113
 
114
  with gr.Column(scale=2):
115
- # Hiển thị thông tin model
116
- with gr.Accordion("📊 Model Information", open=True):
117
  gr.Markdown("""
118
- **Llama 3.2 3B Instruct**
119
  - **Provider**: Sambanova AI
120
- - **Context Window**: 128K tokens
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
- - Low-latency streaming
130
  """)
131
 
132
- # Hiển thị trạng thái
133
- status_display = gr.Textbox(
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(chatbot_output, state_output):
142
  """Xử lý additional outputs từ streaming"""
143
- return chatbot_output, state_output, "✅ Đang trò chuyện..."
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 export_chat(chat_history):
151
- """Export chat history"""
152
- if not chat_history:
153
- return "❌ Không dữ liệu để export"
154
 
155
  try:
156
- timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
157
- filename = f"sambanova_chat_{timestamp}.json"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
158
 
159
- with open(filename, 'w', encoding='utf-8') as f:
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
- return f"❌ Lỗi export: {str(e)}"
 
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
- export_btn.click(
178
- export_chat,
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