datbkpro commited on
Commit
7bd2666
·
verified ·
1 Parent(s): 7d0b18d

Update ui/tabs.py

Browse files
Files changed (1) hide show
  1. ui/tabs.py +135 -2
ui/tabs.py CHANGED
@@ -10,6 +10,7 @@ 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
@@ -20,14 +21,17 @@ def create_all_tabs(audio_service: AudioService, chat_service: ChatService,
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)
@@ -48,6 +52,135 @@ def create_all_tabs(audio_service: AudioService, chat_service: ChatService,
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 đơn giản - Text-based trước"""
53
 
 
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 services.sambanova_voice_service import SambanovaVoiceService
14
  from core.rag_system import EnhancedRAGSystem
15
  from core.tts_service import EnhancedTTSService
16
  from core.wikipedia_processor import WikipediaProcessor
 
21
  tts_service: EnhancedTTSService, wikipedia_processor: WikipediaProcessor,
22
  streaming_voice_service: StreamingVoiceService,
23
  hybrid_service: HybridStreamingService,
24
+ voice_coding_service: VoiceCodingService,
25
+ sambanova_voice_service : SambanovaVoiceService
26
  ):
27
 
28
  with gr.Tab("🎙️ Streaming Voice "):
29
  create_streaming_voice_tab(streaming_voice_service)
30
  with gr.Tab("OpenAI Realtime"):
31
  create_openai_realtime_tab(hybrid_service)
32
+ with gr.Tab("SambonovaAI Realtime"):
33
+ create_sambanova_voice_tab(sambanova_voice_service)
34
+ with gr.Tab("Generation Code"):
35
  create_voice_coding_tab(voice_coding_service)
36
  with gr.Tab("🎙️ Audio"):
37
  create_audio_tab(audio_service)
 
52
  create_language_info_tab(rag_system.multilingual_manager)
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")
63
+ gr.Markdown("Trò chuyện voice-to-voice với model Llama 3.2 3B thông qua Sambanova API")
64
+
65
+ # State variables
66
+ chatbot = gr.Chatbot(
67
+ type="messages",
68
+ value=[],
69
+ label="💬 Hội thoại Voice",
70
+ height=400
71
+ )
72
+ conversation_state = gr.State(value=[])
73
+
74
+ with gr.Row():
75
+ with gr.Column(scale=1):
76
+ gr.Markdown("### 🎤 Hướng dẫn sử dụng:")
77
+ gr.Markdown("""
78
+ **Cách sử dụng:**
79
+ 1. Nhấn **Bắt đầu Voice Chat**
80
+ 2. Nói "Computer", "Hey", "Hello" hoặc "Xin chào" để kích hoạt
81
+ 3. Trò chuyện tự nhiên bằng giọng nói
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 có 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(
168
+ handle_additional_outputs,
169
+ outputs=[chatbot, conversation_state, status_display]
170
+ )
171
+
172
+ clear_btn.click(
173
+ clear_conversation,
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
184
  def create_voice_coding_tab(voice_coding_service):
185
  """Tạo tab Voice Coding đơn giản - Text-based trước"""
186