Update services/sambanova_voice_service.py
Browse files
services/sambanova_voice_service.py
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
import base64
|
| 2 |
import json
|
| 3 |
import os
|
|
@@ -10,30 +11,114 @@ from fastrtc import (
|
|
| 10 |
ReplyOnStopWords,
|
| 11 |
Stream,
|
| 12 |
get_stt_model,
|
|
|
|
| 13 |
)
|
| 14 |
|
| 15 |
class SambanovaVoiceService:
|
| 16 |
-
"""Dịch vụ Sambanova AI
|
| 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"),
|
| 22 |
base_url="https://api.sambanova.ai/v1",
|
| 23 |
)
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
"""Generate response từ Sambanova API"""
|
| 28 |
try:
|
| 29 |
response = self.client.chat.completions.create(
|
| 30 |
-
model=
|
| 31 |
messages=messages,
|
| 32 |
-
temperature=
|
| 33 |
-
top_p=
|
| 34 |
max_tokens=1024,
|
| 35 |
)
|
| 36 |
return response.choices[0].message.content
|
| 37 |
except Exception as e:
|
| 38 |
print(f"❌ Sambanova API Error: {e}")
|
| 39 |
-
return f"Xin lỗi, có lỗi xảy ra: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import io
|
| 2 |
import base64
|
| 3 |
import json
|
| 4 |
import os
|
|
|
|
| 11 |
ReplyOnStopWords,
|
| 12 |
Stream,
|
| 13 |
get_stt_model,
|
| 14 |
+
get_twilio_turn_credentials,
|
| 15 |
)
|
| 16 |
|
| 17 |
class SambanovaVoiceService:
|
| 18 |
+
"""Dịch vụ Sambanova AI với Voice Streaming hoàn chỉnh"""
|
| 19 |
|
| 20 |
+
def __init__(self, tts_service=None):
|
| 21 |
# Khởi tạo client Sambanova
|
| 22 |
self.client = openai.OpenAI(
|
| 23 |
api_key=os.environ.get("SAMBANOVA_API_KEY"),
|
| 24 |
base_url="https://api.sambanova.ai/v1",
|
| 25 |
)
|
| 26 |
+
# Khởi tạo STT model
|
| 27 |
+
self.stt_model = get_stt_model()
|
| 28 |
+
# Tích hợp TTS service
|
| 29 |
+
self.tts_service = tts_service
|
| 30 |
+
print("✅ Sambanova Voice Service initialized với TTS")
|
| 31 |
|
| 32 |
+
def get_available_models(self):
|
| 33 |
+
"""Lấy danh sách model có sẵn"""
|
| 34 |
+
return [
|
| 35 |
+
"Meta-Llama-3.1-8B-Instruct"
|
| 36 |
+
]
|
| 37 |
+
|
| 38 |
+
def generate_response(self, messages, model="Meta-Llama-3.1-8B-Instruct", temperature=0.1, top_p=0.1):
|
| 39 |
"""Generate response từ Sambanova API"""
|
| 40 |
try:
|
| 41 |
response = self.client.chat.completions.create(
|
| 42 |
+
model=model,
|
| 43 |
messages=messages,
|
| 44 |
+
temperature=temperature,
|
| 45 |
+
top_p=top_p,
|
| 46 |
max_tokens=1024,
|
| 47 |
)
|
| 48 |
return response.choices[0].message.content
|
| 49 |
except Exception as e:
|
| 50 |
print(f"❌ Sambanova API Error: {e}")
|
| 51 |
+
return f"Xin lỗi, có lỗi xảy ra: {str(e)}"
|
| 52 |
+
|
| 53 |
+
def stream_generate_response(self, messages, model="Meta-Llama-3.1-8B-Instruct", temperature=0.1, top_p=0.1):
|
| 54 |
+
"""Stream response từ Sambanova API"""
|
| 55 |
+
try:
|
| 56 |
+
response = self.client.chat.completions.create(
|
| 57 |
+
model=model,
|
| 58 |
+
messages=messages,
|
| 59 |
+
temperature=temperature,
|
| 60 |
+
top_p=top_p,
|
| 61 |
+
max_tokens=1024,
|
| 62 |
+
stream=True
|
| 63 |
+
)
|
| 64 |
+
|
| 65 |
+
full_response = ""
|
| 66 |
+
for chunk in response:
|
| 67 |
+
if chunk.choices[0].delta.content:
|
| 68 |
+
text_chunk = chunk.choices[0].delta.content
|
| 69 |
+
full_response += text_chunk
|
| 70 |
+
yield text_chunk, full_response
|
| 71 |
+
|
| 72 |
+
except Exception as e:
|
| 73 |
+
error_msg = f"❌ Lỗi: {str(e)}"
|
| 74 |
+
yield error_msg, error_msg
|
| 75 |
+
|
| 76 |
+
def speech_to_text(self, audio):
|
| 77 |
+
"""Chuyển speech thành text"""
|
| 78 |
+
try:
|
| 79 |
+
text = self.stt_model.stt(audio)
|
| 80 |
+
print(f"🎤 STT Result: {text}")
|
| 81 |
+
return text
|
| 82 |
+
except Exception as e:
|
| 83 |
+
print(f"❌ STT Error: {e}")
|
| 84 |
+
return ""
|
| 85 |
+
|
| 86 |
+
def text_to_speech(self, text, language='vi'):
|
| 87 |
+
"""Chuyển text thành speech sử dụng TTS service"""
|
| 88 |
+
if self.tts_service is None:
|
| 89 |
+
print("❌ TTS service chưa được khởi tạo")
|
| 90 |
+
return None
|
| 91 |
+
|
| 92 |
+
try:
|
| 93 |
+
audio_bytes = self.tts_service.text_to_speech(text, language)
|
| 94 |
+
if audio_bytes:
|
| 95 |
+
# Lưu file tạm và trả về filepath
|
| 96 |
+
filename = f"tts_{int(time.time())}.mp3"
|
| 97 |
+
filepath = self.tts_service.save_tts_audio(audio_bytes, filename)
|
| 98 |
+
return filepath
|
| 99 |
+
return None
|
| 100 |
+
except Exception as e:
|
| 101 |
+
print(f"❌ TTS Error: {e}")
|
| 102 |
+
return None
|
| 103 |
+
|
| 104 |
+
def generate_response_with_voice(self, messages, model="Meta-Llama-3.1-8B-Instruct", language='vi'):
|
| 105 |
+
"""Generate response và chuyển thành voice"""
|
| 106 |
+
try:
|
| 107 |
+
# Lấy text response
|
| 108 |
+
text_response = self.generate_response(messages, model)
|
| 109 |
+
|
| 110 |
+
# Chuyển thành voice
|
| 111 |
+
audio_filepath = self.text_to_speech(text_response, language)
|
| 112 |
+
|
| 113 |
+
return {
|
| 114 |
+
"text": text_response,
|
| 115 |
+
"audio": audio_filepath,
|
| 116 |
+
"audio_bytes": self.tts_service.text_to_speech(text_response, language) if audio_filepath else None
|
| 117 |
+
}
|
| 118 |
+
except Exception as e:
|
| 119 |
+
print(f"❌ Error in generate_response_with_voice: {e}")
|
| 120 |
+
return {
|
| 121 |
+
"text": f"Xin lỗi, có lỗi xảy ra: {str(e)}",
|
| 122 |
+
"audio": None,
|
| 123 |
+
"audio_bytes": None
|
| 124 |
+
}
|