Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,8 +7,11 @@ import torch
|
|
| 7 |
from gtts import gTTS
|
| 8 |
import os
|
| 9 |
import yt_dlp
|
| 10 |
-
import re
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
hf_token = os.getenv("HF_TOKEN")
|
| 14 |
app = FastAPI()
|
|
@@ -75,19 +78,15 @@ async def chat(request: ChatRequest):
|
|
| 75 |
|
| 76 |
# Endpoint voice chat + TTS
|
| 77 |
"""@app.post("/voice_chat")
|
| 78 |
-
|
| 79 |
file_location = f"temp_{file.filename}"
|
| 80 |
with open(file_location, "wb") as f:
|
| 81 |
f.write(await file.read())
|
| 82 |
|
| 83 |
result = whisper_model.transcribe(file_location, language="vi")
|
| 84 |
user_text = result["text"]
|
| 85 |
-
os.remove(file_location)
|
| 86 |
-
|
| 87 |
-
import numpy as np
|
| 88 |
-
import scipy.io.wavfile as wav
|
| 89 |
-
from fastapi import FastAPI, Request
|
| 90 |
-
from fastapi.responses import JSONResponse
|
| 91 |
@app.post("/voice_chat")
|
| 92 |
async def voice_chat(request: Request):
|
| 93 |
# Đọc dữ liệu âm thanh thô từ ESP32 gửi lên
|
|
@@ -97,14 +96,15 @@ async def voice_chat(request: Request):
|
|
| 97 |
audio_np = np.frombuffer(raw_audio, dtype=np.int16)
|
| 98 |
# Chuyển thành file WAV trong bộ nhớ
|
| 99 |
wav_io = io.BytesIO()
|
| 100 |
-
wav.write(wav_io, sample_rate, audio_np)
|
|
|
|
| 101 |
# Lưu file WAV tạm để dùng với Whisper
|
| 102 |
with open("temp_audio.wav", "wb") as f: f.write(wav_io.read())
|
| 103 |
# Gọi Whisper để chuyển âm thanh thành văn bản
|
| 104 |
import whisper model = whisper.load_model("base")
|
| 105 |
result = model.transcribe("temp_audio.wav", language="vi")
|
| 106 |
user_text = result["text"]
|
| 107 |
-
return JSONResponse(content={"text": user_text})
|
| 108 |
# Kiểm tra yêu cầu mở nhạc
|
| 109 |
if any(kw in user_text.lower() for kw in ["nghe nhạc", "mở bài hát", "bài hát", "bài"]):
|
| 110 |
song_name = extract_song_name(user_text)
|
|
@@ -129,7 +129,7 @@ async def voice_chat(request: Request):
|
|
| 129 |
return {
|
| 130 |
"user_text": user_text,
|
| 131 |
"response": response_text,
|
| 132 |
-
"audio_url":
|
| 133 |
}
|
| 134 |
|
| 135 |
# Endpoint trả về file âm thanh
|
|
|
|
| 7 |
from gtts import gTTS
|
| 8 |
import os
|
| 9 |
import yt_dlp
|
| 10 |
+
#import re
|
| 11 |
+
import io
|
| 12 |
+
import numpy as np
|
| 13 |
+
import scipy.io.wavfile as wav
|
| 14 |
+
#from fastapi.responses import JSONResponse
|
| 15 |
|
| 16 |
hf_token = os.getenv("HF_TOKEN")
|
| 17 |
app = FastAPI()
|
|
|
|
| 78 |
|
| 79 |
# Endpoint voice chat + TTS
|
| 80 |
"""@app.post("/voice_chat")
|
| 81 |
+
async def voice_chat(file: UploadFile = File(...)):
|
| 82 |
file_location = f"temp_{file.filename}"
|
| 83 |
with open(file_location, "wb") as f:
|
| 84 |
f.write(await file.read())
|
| 85 |
|
| 86 |
result = whisper_model.transcribe(file_location, language="vi")
|
| 87 |
user_text = result["text"]
|
| 88 |
+
os.remove(file_location)
|
| 89 |
+
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
@app.post("/voice_chat")
|
| 91 |
async def voice_chat(request: Request):
|
| 92 |
# Đọc dữ liệu âm thanh thô từ ESP32 gửi lên
|
|
|
|
| 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)
|
| 100 |
+
wav_io.seek(0)
|
| 101 |
# Lưu file WAV tạm để dùng với Whisper
|
| 102 |
with open("temp_audio.wav", "wb") as f: f.write(wav_io.read())
|
| 103 |
# Gọi Whisper để chuyển âm thanh thành văn bản
|
| 104 |
import whisper model = whisper.load_model("base")
|
| 105 |
result = model.transcribe("temp_audio.wav", language="vi")
|
| 106 |
user_text = result["text"]
|
| 107 |
+
#return JSONResponse(content={"text": user_text})
|
| 108 |
# Kiểm tra yêu cầu mở nhạc
|
| 109 |
if any(kw in user_text.lower() for kw in ["nghe nhạc", "mở bài hát", "bài hát", "bài"]):
|
| 110 |
song_name = extract_song_name(user_text)
|
|
|
|
| 129 |
return {
|
| 130 |
"user_text": user_text,
|
| 131 |
"response": response_text,
|
| 132 |
+
"audio_url": "/get_audio"
|
| 133 |
}
|
| 134 |
|
| 135 |
# Endpoint trả về file âm thanh
|