Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -78,42 +78,54 @@ async def chat(request: ChatRequest):
|
|
| 78 |
|
| 79 |
# Endpoint voice chat + TTS
|
| 80 |
@app.post("/voice_chat")
|
| 81 |
-
async def voice_chat(
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
"
|
| 113 |
-
|
| 114 |
-
"
|
| 115 |
-
|
| 116 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
# Endpoint trả về file âm thanh
|
| 118 |
@app.get("/get_audio")
|
| 119 |
async def get_audio():
|
|
|
|
| 78 |
|
| 79 |
# Endpoint voice chat + TTS
|
| 80 |
@app.post("/voice_chat")
|
| 81 |
+
async def voice_chat(request: Request):
|
| 82 |
+
try:
|
| 83 |
+
raw_audio = await request.body()
|
| 84 |
+
sample_rate = 16000
|
| 85 |
+
audio_np = np.frombuffer(raw_audio, dtype=np.int16)
|
| 86 |
+
|
| 87 |
+
# Chuyển thành WAV
|
| 88 |
+
wav_io = io.BytesIO()
|
| 89 |
+
wav.write(wav_io, sample_rate, audio_np)
|
| 90 |
+
wav_io.seek(0)
|
| 91 |
+
|
| 92 |
+
with open("temp_audio.wav", "wb") as f:
|
| 93 |
+
f.write(wav_io.read())
|
| 94 |
+
|
| 95 |
+
# Whisper nhận dạng
|
| 96 |
+
result = whisper_model.transcribe("temp_audio.wav", language="vi")
|
| 97 |
+
user_text = result["text"]
|
| 98 |
+
|
| 99 |
+
# Kiểm tra yêu cầu mở nhạc
|
| 100 |
+
if any(kw in user_text.lower() for kw in ["nghe nhạc", "mở bài hát", "bài hát", "bài"]):
|
| 101 |
+
song_name = extract_song_name(user_text)
|
| 102 |
+
if song_name:
|
| 103 |
+
wav_path = download_youtube_as_wav(song_name)
|
| 104 |
+
if wav_path:
|
| 105 |
+
return FileResponse(wav_path, media_type="audio/wav", filename="song.wav")
|
| 106 |
+
else:
|
| 107 |
+
return JSONResponse({"error": "Không tìm thấy hoặc tải được bài hát."}, status_code=404)
|
| 108 |
+
|
| 109 |
+
# Xử lý hội thoại
|
| 110 |
+
conversation.append({"role": "user", "content": user_text})
|
| 111 |
+
text = tokenizer.apply_chat_template(conversation, tokenize=False, add_generation_prompt=True)
|
| 112 |
+
model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
|
| 113 |
+
response_text = generate_full_response(model_inputs)
|
| 114 |
+
conversation.append({"role": "assistant", "content": response_text})
|
| 115 |
+
|
| 116 |
+
# TTS
|
| 117 |
+
tts = gTTS(response_text, lang="vi")
|
| 118 |
+
audio_file = "response.mp3"
|
| 119 |
+
tts.save(audio_file)
|
| 120 |
+
|
| 121 |
+
return {
|
| 122 |
+
"user_text": user_text,
|
| 123 |
+
"response": response_text,
|
| 124 |
+
"audio_url": "/get_audio"
|
| 125 |
+
}
|
| 126 |
+
|
| 127 |
+
except Exception as e:
|
| 128 |
+
return JSONResponse({"error": str(e)}, status_code=500)
|
| 129 |
# Endpoint trả về file âm thanh
|
| 130 |
@app.get("/get_audio")
|
| 131 |
async def get_audio():
|