Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -74,7 +74,7 @@ async def chat(request: ChatRequest):
|
|
| 74 |
return {"response": response_text}
|
| 75 |
|
| 76 |
# Endpoint voice chat + TTS
|
| 77 |
-
@app.post("/voice_chat")
|
| 78 |
async def voice_chat(file: UploadFile = File(...)):
|
| 79 |
file_location = f"temp_{file.filename}"
|
| 80 |
with open(file_location, "wb") as f:
|
|
@@ -82,9 +82,29 @@ async def voice_chat(file: UploadFile = File(...)):
|
|
| 82 |
|
| 83 |
result = whisper_model.transcribe(file_location, language="vi")
|
| 84 |
user_text = result["text"]
|
| 85 |
-
os.remove(file_location)
|
| 86 |
-
|
| 87 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
if any(kw in user_text.lower() for kw in ["nghe nhạc", "mở bài hát", "bài hát", "bài"]):
|
| 89 |
song_name = extract_song_name(user_text)
|
| 90 |
if song_name:
|
|
|
|
| 74 |
return {"response": response_text}
|
| 75 |
|
| 76 |
# Endpoint voice chat + TTS
|
| 77 |
+
"""@app.post("/voice_chat")
|
| 78 |
async def voice_chat(file: UploadFile = File(...)):
|
| 79 |
file_location = f"temp_{file.filename}"
|
| 80 |
with open(file_location, "wb") as f:
|
|
|
|
| 82 |
|
| 83 |
result = whisper_model.transcribe(file_location, language="vi")
|
| 84 |
user_text = result["text"]
|
| 85 |
+
os.remove(file_location)"""
|
| 86 |
+
import io
|
| 87 |
+
import numpy as np
|
| 88 |
+
import scipy.io.wavfile as wav
|
| 89 |
+
from fastapi import FastAPI, Request
|
| 90 |
+
from fastapi.responses import JSONResponse app = FastAPI()
|
| 91 |
+
@app.post("/voice_chat") async def voice_chat(request: Request):
|
| 92 |
+
# Đọc dữ liệu âm thanh thô từ ESP32 gửi lên
|
| 93 |
+
raw_audio = await request.body()
|
| 94 |
+
# Giả sử âm thanh là PCM 16-bit mono, sample rate 16000 Hz
|
| 95 |
+
sample_rate = 16000
|
| 96 |
+
audio_np = np.frombuffer(raw_audio, dtype=np.int16)
|
| 97 |
+
# Chuyển thành file WAV trong bộ nhớ
|
| 98 |
+
wav_io = io.BytesIO()
|
| 99 |
+
wav.write(wav_io, sample_rate, audio_np) wav_io.seek(0)
|
| 100 |
+
# Lưu file WAV tạm để dùng với Whisper
|
| 101 |
+
with open("temp_audio.wav", "wb") as f: f.write(wav_io.read())
|
| 102 |
+
# Gọi Whisper để chuyển âm thanh thành văn bản
|
| 103 |
+
import whisper model = whisper.load_model("base")
|
| 104 |
+
result = model.transcribe("temp_audio.wav", language="vi")
|
| 105 |
+
user_text = result["text"]
|
| 106 |
+
return JSONResponse(content={"text": user_text})
|
| 107 |
+
# Kiểm tra yêu cầu mở nhạc
|
| 108 |
if any(kw in user_text.lower() for kw in ["nghe nhạc", "mở bài hát", "bài hát", "bài"]):
|
| 109 |
song_name = extract_song_name(user_text)
|
| 110 |
if song_name:
|