Update services/sambanova_voice_service.py
Browse files
services/sambanova_voice_service.py
CHANGED
|
@@ -5,21 +5,17 @@ from pathlib import Path
|
|
| 5 |
import gradio as gr
|
| 6 |
import numpy as np
|
| 7 |
import openai
|
| 8 |
-
from fastapi import FastAPI
|
| 9 |
-
from fastapi.responses import HTMLResponse, StreamingResponse
|
| 10 |
from fastrtc import (
|
| 11 |
AdditionalOutputs,
|
| 12 |
ReplyOnStopWords,
|
| 13 |
Stream,
|
| 14 |
get_stt_model,
|
| 15 |
-
get_cloudflare_turn_credentials_async, # Sử dụng Cloudflare free
|
| 16 |
)
|
| 17 |
from gradio.utils import get_space
|
| 18 |
-
from pydantic import BaseModel
|
| 19 |
import asyncio
|
| 20 |
|
| 21 |
class SambanovaVoiceService:
|
| 22 |
-
"""Dịch vụ Voice AI với Sambanova API - Fixed
|
| 23 |
|
| 24 |
def __init__(self):
|
| 25 |
self.curr_dir = Path(__file__).parent
|
|
@@ -33,22 +29,58 @@ class SambanovaVoiceService:
|
|
| 33 |
# STT model
|
| 34 |
self.model = get_stt_model()
|
| 35 |
|
| 36 |
-
# RTC configuration -
|
| 37 |
-
self.rtc_configuration =
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
print("✅ Sambanova Voice Service initialized")
|
| 40 |
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
-
def
|
| 52 |
"""Tạo response handler cho voice streaming"""
|
| 53 |
|
| 54 |
def response(
|
|
@@ -68,15 +100,12 @@ class SambanovaVoiceService:
|
|
| 68 |
yield AdditionalOutputs(gradio_chatbot, conversation_state)
|
| 69 |
return
|
| 70 |
|
| 71 |
-
# Thêm
|
| 72 |
-
|
| 73 |
-
gradio_chatbot.append(
|
| 74 |
-
{"role": "user", "content": f"🎤: {text}"} # Simplified - chỉ hiển thị text
|
| 75 |
-
)
|
| 76 |
-
yield AdditionalOutputs(gradio_chatbot, conversation_state)
|
| 77 |
-
|
| 78 |
-
# Thêm text vào conversation state
|
| 79 |
conversation_state.append({"role": "user", "content": text})
|
|
|
|
|
|
|
| 80 |
|
| 81 |
# Gọi Sambanova API
|
| 82 |
print("🤖 Calling Sambanova API...")
|
|
@@ -105,34 +134,6 @@ class SambanovaVoiceService:
|
|
| 105 |
|
| 106 |
return response
|
| 107 |
|
| 108 |
-
def
|
| 109 |
-
"""
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
return Stream(
|
| 113 |
-
ReplyOnStopWords(
|
| 114 |
-
response_handler,
|
| 115 |
-
stop_words=["computer", "hey", "hello", "xin chào", "llama"],
|
| 116 |
-
input_sample_rate=16000,
|
| 117 |
-
),
|
| 118 |
-
mode="send",
|
| 119 |
-
modality="audio",
|
| 120 |
-
additional_inputs=[
|
| 121 |
-
gr.Chatbot(
|
| 122 |
-
type="messages",
|
| 123 |
-
value=[],
|
| 124 |
-
label="�� Voice Conversation",
|
| 125 |
-
height=400
|
| 126 |
-
),
|
| 127 |
-
gr.State(value=[])
|
| 128 |
-
],
|
| 129 |
-
additional_outputs=[
|
| 130 |
-
gr.Chatbot(type="messages", value=[]),
|
| 131 |
-
gr.State(value=[])
|
| 132 |
-
],
|
| 133 |
-
|
| 134 |
-
additional_outputs_handler=lambda chatbot, state, new_chatbot, new_state: (new_chatbot, new_state),
|
| 135 |
-
concurrency_limit=3,
|
| 136 |
-
time_limit=120,
|
| 137 |
-
rtc_configuration=self.rtc_configuration,
|
| 138 |
-
)
|
|
|
|
| 5 |
import gradio as gr
|
| 6 |
import numpy as np
|
| 7 |
import openai
|
|
|
|
|
|
|
| 8 |
from fastrtc import (
|
| 9 |
AdditionalOutputs,
|
| 10 |
ReplyOnStopWords,
|
| 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 Duplicate Block Error"""
|
| 19 |
|
| 20 |
def __init__(self):
|
| 21 |
self.curr_dir = Path(__file__).parent
|
|
|
|
| 29 |
# STT model
|
| 30 |
self.model = get_stt_model()
|
| 31 |
|
| 32 |
+
# RTC configuration - DISABLE TURN để tránh lỗi
|
| 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 |
+
@property
|
| 41 |
+
def stream(self):
|
| 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,
|
| 54 |
+
stop_words=["computer", "hey", "hello", "xin chào", "llama"],
|
| 55 |
+
input_sample_rate=16000,
|
| 56 |
+
),
|
| 57 |
+
mode="send",
|
| 58 |
+
modality="audio",
|
| 59 |
+
additional_inputs=[
|
| 60 |
+
gr.Chatbot(
|
| 61 |
+
type="messages",
|
| 62 |
+
value=[],
|
| 63 |
+
label="💬 Voice Conversation",
|
| 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 |
|
| 83 |
+
def _create_response_handler(self):
|
| 84 |
"""Tạo response handler cho voice streaming"""
|
| 85 |
|
| 86 |
def response(
|
|
|
|
| 100 |
yield AdditionalOutputs(gradio_chatbot, conversation_state)
|
| 101 |
return
|
| 102 |
|
| 103 |
+
# Thêm user message
|
| 104 |
+
user_msg = {"role": "user", "content": f"🎤: {text}"}
|
| 105 |
+
gradio_chatbot.append(user_msg)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
conversation_state.append({"role": "user", "content": text})
|
| 107 |
+
|
| 108 |
+
yield AdditionalOutputs(gradio_chatbot, conversation_state)
|
| 109 |
|
| 110 |
# Gọi Sambanova API
|
| 111 |
print("🤖 Calling Sambanova API...")
|
|
|
|
| 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()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|