datbkpro commited on
Commit
1e3a9b7
·
verified ·
1 Parent(s): 02feac3

Update ui/tabs.py

Browse files
Files changed (1) hide show
  1. ui/tabs.py +47 -53
ui/tabs.py CHANGED
@@ -53,24 +53,20 @@ 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 - Fixed duplicate block error"""
57
 
58
  with gr.Blocks() as sambanova_tab:
59
  gr.Markdown("## 🤖 Sambanova Voice AI - Llama 3.2 3B")
60
- gr.Markdown("Trò chuyện voice-to-voice với model Llama 3.2 3B thông qua Sambanova API")
61
 
62
- # State variables với ID duy nhất
63
  chatbot = gr.Chatbot(
64
  type="messages",
65
  value=[],
66
  label="💬 Hội thoại Voice",
67
- height=400,
68
- elem_id="sambanova_main_chatbot"
69
- )
70
- conversation_state = gr.State(
71
- value=[],
72
- elem_id="sambanova_main_state"
73
  )
 
74
 
75
  with gr.Row():
76
  with gr.Column(scale=1):
@@ -78,7 +74,7 @@ def create_sambanova_voice_tab(sambanova_service):
78
  gr.Markdown("""
79
  **Cách sử dụng:**
80
  1. Nhấn **Bắt đầu Voice Chat**
81
- 2. Nói "Computer", "Hey", "Hello" hoặc "Xin chào" để kích hoạt
82
  3. Trò chuyện tự nhiên bằng giọng nói
83
  4. Mỗi phiên giới hạn 90 giây
84
 
@@ -86,30 +82,35 @@ def create_sambanova_voice_tab(sambanova_service):
86
  ✅ Voice-to-Text conversation
87
  ✅ Llama 3.2 3B model
88
  ✅ Stop-word activation
89
- ✅ Multi-language support
90
  """)
91
 
92
- # WebRTC component - chỉ gọi một lần
93
- webrtc = sambanova_service.get_ui_component()
 
 
 
 
 
 
 
 
94
 
95
- # Manual text input fallback
96
- with gr.Accordion("📝 Text Input (Fallback)", open=False):
97
  text_input = gr.Textbox(
98
  label="Nhập tin nhắn",
99
- placeholder="Nhập tin nhắn text nếu voice không hoạt động...",
100
- lines=2,
101
- elem_id="sambanova_text_input"
102
  )
103
- text_send_btn = gr.Button("📤 Gửi tin nhắn", elem_id="sambanova_send_btn")
104
 
105
  # Controls
106
  with gr.Row():
107
- clear_btn = gr.Button("🗑️ Xóa hội thoại", variant="secondary", elem_id="sambanova_clear_btn")
108
  status_display = gr.Textbox(
109
  label="Trạng thái",
110
- value="✅ Sẵn sàng kết nối...",
111
- interactive=False,
112
- elem_id="sambanova_status"
113
  )
114
 
115
  with gr.Column(scale=2):
@@ -120,28 +121,11 @@ def create_sambanova_voice_tab(sambanova_service):
120
  - **Provider**: Sambanova AI
121
  - **Context**: 128K tokens
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
- - Text-based responses
130
  """)
131
-
132
- # Conversation display
133
- gr.Markdown("### 💬 Cuộc trò chuyện")
134
-
135
- # Additional outputs handling
136
- def handle_additional_outputs(new_chatbot, new_state):
137
- """Xử lý additional outputs từ streaming"""
138
- return new_chatbot, new_state, "✅ Đang trò chuyện..."
139
-
140
- # Event handlers
141
- def clear_conversation():
142
- """Xóa hội thoại"""
143
- return [], [], "🔄 Đã xóa hội thoại"
144
 
 
145
  def send_text_message(text, current_chatbot, current_state):
146
  """Gửi tin nhắn text"""
147
  if not text.strip():
@@ -172,17 +156,11 @@ def create_sambanova_voice_tab(sambanova_service):
172
  error_msg = f"❌ Lỗi: {str(e)}"
173
  return current_chatbot, current_state, error_msg
174
 
175
- # Kết nối events - chỉ kết nối một lần
176
- webrtc.on_additional_outputs(
177
- handle_additional_outputs,
178
- outputs=[chatbot, conversation_state, status_display]
179
- )
180
-
181
- clear_btn.click(
182
- clear_conversation,
183
- outputs=[chatbot, conversation_state, status_display]
184
- )
185
 
 
186
  text_send_btn.click(
187
  send_text_message,
188
  inputs=[text_input, chatbot, conversation_state],
@@ -191,6 +169,22 @@ def create_sambanova_voice_tab(sambanova_service):
191
  lambda: "", # Clear text input
192
  outputs=[text_input]
193
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
194
 
195
  return sambanova_tab
196
  def create_voice_coding_tab(voice_coding_service):
 
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")
60
+ gr.Markdown("Trò chuyện voice-to-voice với model Llama 3.2 3B")
61
 
62
+ # State variables - KHÔNG dùng elem_id cho State
63
  chatbot = gr.Chatbot(
64
  type="messages",
65
  value=[],
66
  label="💬 Hội thoại Voice",
67
+ height=400
 
 
 
 
 
68
  )
69
+ conversation_state = gr.State(value=[])
70
 
71
  with gr.Row():
72
  with gr.Column(scale=1):
 
74
  gr.Markdown("""
75
  **Cách sử dụng:**
76
  1. Nhấn **Bắt đầu Voice Chat**
77
+ 2. Nói "Computer", "Hey", "Hello" hoặc "Xin chào"
78
  3. Trò chuyện tự nhiên bằng giọng nói
79
  4. Mỗi phiên giới hạn 90 giây
80
 
 
82
  ✅ Voice-to-Text conversation
83
  ✅ Llama 3.2 3B model
84
  ✅ Stop-word activation
 
85
  """)
86
 
87
+ # Tạo stream
88
+ try:
89
+ stream = sambanova_service.create_stream()
90
+ webrtc = stream.ui()
91
+ webrtc_available = True
92
+ except Exception as e:
93
+ print(f"⚠️ Cannot create WebRTC stream: {e}")
94
+ webrtc_available = False
95
+ gr.Markdown("### ❌ Voice Input Not Available")
96
+ gr.Markdown("Voice features are currently unavailable. Please use text input below.")
97
 
98
+ # Text input fallback
99
+ with gr.Accordion("📝 Text Input", open=True):
100
  text_input = gr.Textbox(
101
  label="Nhập tin nhắn",
102
+ placeholder="Nhập tin nhắn text...",
103
+ lines=3
 
104
  )
105
+ text_send_btn = gr.Button("📤 Gửi tin nhắn", variant="primary")
106
 
107
  # Controls
108
  with gr.Row():
109
+ clear_btn = gr.Button("🗑️ Xóa hội thoại")
110
  status_display = gr.Textbox(
111
  label="Trạng thái",
112
+ value="✅ Sẵn sàng" if webrtc_available else "⚠️ Text mode only",
113
+ interactive=False
 
114
  )
115
 
116
  with gr.Column(scale=2):
 
121
  - **Provider**: Sambanova AI
122
  - **Context**: 128K tokens
123
  - **Specialization**: Instruction following
124
+ - **Temperature**: 0.1
125
+ - **Top-P**: 0.1
 
 
 
 
 
126
  """)
 
 
 
 
 
 
 
 
 
 
 
 
 
127
 
128
+ # Event handlers cho text input
129
  def send_text_message(text, current_chatbot, current_state):
130
  """Gửi tin nhắn text"""
131
  if not text.strip():
 
156
  error_msg = f"❌ Lỗi: {str(e)}"
157
  return current_chatbot, current_state, error_msg
158
 
159
+ def clear_conversation():
160
+ """Xóa hội thoại"""
161
+ return [], [], "🔄 Đã xóa hội thoại"
 
 
 
 
 
 
 
162
 
163
+ # Kết nối events cho text input
164
  text_send_btn.click(
165
  send_text_message,
166
  inputs=[text_input, chatbot, conversation_state],
 
169
  lambda: "", # Clear text input
170
  outputs=[text_input]
171
  )
172
+
173
+ clear_btn.click(
174
+ clear_conversation,
175
+ outputs=[chatbot, conversation_state, status_display]
176
+ )
177
+
178
+ # Kết nối events cho WebRTC nếu available
179
+ if webrtc_available:
180
+ def handle_webrtc_outputs(new_chatbot, new_state):
181
+ """Xử lý outputs từ WebRTC"""
182
+ return new_chatbot, new_state, "✅ Đang trò chuyện..."
183
+
184
+ webrtc.on_additional_outputs(
185
+ handle_webrtc_outputs,
186
+ outputs=[chatbot, conversation_state, status_display]
187
+ )
188
 
189
  return sambanova_tab
190
  def create_voice_coding_tab(voice_coding_service):