Update services/sambanova_voice_service.py
Browse files
services/sambanova_voice_service.py
CHANGED
|
@@ -11,15 +11,11 @@ from fastrtc import (
|
|
| 11 |
Stream,
|
| 12 |
get_stt_model,
|
| 13 |
)
|
| 14 |
-
from gradio.utils import get_space
|
| 15 |
-
import asyncio
|
| 16 |
|
| 17 |
class SambanovaVoiceService:
|
| 18 |
-
"""Dịch vụ Voice AI với Sambanova API - Fixed
|
| 19 |
|
| 20 |
def __init__(self):
|
| 21 |
-
self.curr_dir = Path(__file__).parent
|
| 22 |
-
|
| 23 |
# Khởi tạo client Sambanova
|
| 24 |
self.client = openai.OpenAI(
|
| 25 |
api_key=os.environ.get("SAMBANOVA_API_KEY"),
|
|
@@ -29,25 +25,28 @@ class SambanovaVoiceService:
|
|
| 29 |
# STT model
|
| 30 |
self.model = get_stt_model()
|
| 31 |
|
| 32 |
-
# RTC configuration - DISABLE TURN
|
| 33 |
self.rtc_configuration = None
|
| 34 |
|
| 35 |
-
# Stream instance - tạo một lần duy nhất
|
| 36 |
-
self._stream = None
|
| 37 |
-
|
| 38 |
print("✅ Sambanova Voice Service initialized")
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
"""Lazy initialization của stream"""
|
| 43 |
-
if self._stream is None:
|
| 44 |
-
self._stream = self._create_stream()
|
| 45 |
-
return self._stream
|
| 46 |
-
|
| 47 |
-
def _create_stream(self):
|
| 48 |
-
"""Tạo FastRTC stream - internal method"""
|
| 49 |
response_handler = self._create_response_handler()
|
| 50 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
return Stream(
|
| 52 |
ReplyOnStopWords(
|
| 53 |
response_handler,
|
|
@@ -56,27 +55,11 @@ class SambanovaVoiceService:
|
|
| 56 |
),
|
| 57 |
mode="send",
|
| 58 |
modality="audio",
|
| 59 |
-
additional_inputs=[
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
height=400,
|
| 65 |
-
elem_id="sambanova_chatbot" # Thêm ID duy nhất
|
| 66 |
-
),
|
| 67 |
-
gr.State(value=[], elem_id="sambanova_state")
|
| 68 |
-
],
|
| 69 |
-
additional_outputs=[
|
| 70 |
-
gr.Chatbot(
|
| 71 |
-
type="messages",
|
| 72 |
-
value=[],
|
| 73 |
-
elem_id="sambanova_chatbot_output"
|
| 74 |
-
),
|
| 75 |
-
gr.State(value=[], elem_id="sambanova_state_output")
|
| 76 |
-
],
|
| 77 |
-
additional_outputs_handler=lambda chatbot, state, new_chatbot, new_state: (new_chatbot, new_state),
|
| 78 |
-
concurrency_limit=3,
|
| 79 |
-
time_limit=120,
|
| 80 |
rtc_configuration=self.rtc_configuration,
|
| 81 |
)
|
| 82 |
|
|
@@ -132,8 +115,4 @@ class SambanovaVoiceService:
|
|
| 132 |
conversation_state.append(error_msg)
|
| 133 |
yield AdditionalOutputs(gradio_chatbot, conversation_state)
|
| 134 |
|
| 135 |
-
return response
|
| 136 |
-
|
| 137 |
-
def get_ui_component(self):
|
| 138 |
-
"""Lấy UI component của stream - đảm bảo chỉ gọi một lần"""
|
| 139 |
-
return self.stream.ui()
|
|
|
|
| 11 |
Stream,
|
| 12 |
get_stt_model,
|
| 13 |
)
|
|
|
|
|
|
|
| 14 |
|
| 15 |
class SambanovaVoiceService:
|
| 16 |
+
"""Dịch vụ Voice AI với Sambanova API - Fixed State elem_id error"""
|
| 17 |
|
| 18 |
def __init__(self):
|
|
|
|
|
|
|
| 19 |
# Khởi tạo client Sambanova
|
| 20 |
self.client = openai.OpenAI(
|
| 21 |
api_key=os.environ.get("SAMBANOVA_API_KEY"),
|
|
|
|
| 25 |
# STT model
|
| 26 |
self.model = get_stt_model()
|
| 27 |
|
| 28 |
+
# RTC configuration - DISABLE TURN
|
| 29 |
self.rtc_configuration = None
|
| 30 |
|
|
|
|
|
|
|
|
|
|
| 31 |
print("✅ Sambanova Voice Service initialized")
|
| 32 |
|
| 33 |
+
def create_stream(self):
|
| 34 |
+
"""Tạo FastRTC stream với components đơn giản"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
response_handler = self._create_response_handler()
|
| 36 |
|
| 37 |
+
# Tạo components không dùng elem_id cho State
|
| 38 |
+
chatbot_input = gr.Chatbot(
|
| 39 |
+
type="messages",
|
| 40 |
+
value=[],
|
| 41 |
+
label="💬 Voice Conversation",
|
| 42 |
+
height=300
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
state_input = gr.State(value=[])
|
| 46 |
+
|
| 47 |
+
chatbot_output = gr.Chatbot(type="messages", value=[])
|
| 48 |
+
state_output = gr.State(value=[])
|
| 49 |
+
|
| 50 |
return Stream(
|
| 51 |
ReplyOnStopWords(
|
| 52 |
response_handler,
|
|
|
|
| 55 |
),
|
| 56 |
mode="send",
|
| 57 |
modality="audio",
|
| 58 |
+
additional_inputs=[chatbot_input, state_input],
|
| 59 |
+
additional_outputs=[chatbot_output, state_output],
|
| 60 |
+
additional_outputs_handler=lambda *args: (args[2], args[3]),
|
| 61 |
+
concurrency_limit=2,
|
| 62 |
+
time_limit=90,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
rtc_configuration=self.rtc_configuration,
|
| 64 |
)
|
| 65 |
|
|
|
|
| 115 |
conversation_state.append(error_msg)
|
| 116 |
yield AdditionalOutputs(gradio_chatbot, conversation_state)
|
| 117 |
|
| 118 |
+
return response
|
|
|
|
|
|
|
|
|
|
|
|