Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
from fastapi import FastAPI, File, UploadFile
|
| 2 |
from fastapi.responses import FileResponse
|
| 3 |
from pydantic import BaseModel
|
| 4 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
|
@@ -60,12 +60,12 @@ class ChatRequest(BaseModel):
|
|
| 60 |
message: str
|
| 61 |
|
| 62 |
@app.get("/")
|
| 63 |
-
def read_root():
|
| 64 |
return {"message": "Ứng dụng đang chạy!"}
|
| 65 |
|
| 66 |
# Endpoint chat text
|
| 67 |
@app.post("/chat")
|
| 68 |
-
async def chat(request: ChatRequest):
|
| 69 |
conversation.append({"role": "user", "content": request.message})
|
| 70 |
text = tokenizer.apply_chat_template(conversation, tokenize=False, add_generation_prompt=True)
|
| 71 |
model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
|
|
@@ -75,7 +75,7 @@ async def chat(request: ChatRequest):
|
|
| 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:
|
| 81 |
f.write(await file.read())
|
|
@@ -88,7 +88,8 @@ 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 |
# Đọ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
|
|
@@ -133,7 +134,7 @@ from fastapi.responses import JSONResponse
|
|
| 133 |
|
| 134 |
# Endpoint trả về file âm thanh
|
| 135 |
@app.get("/get_audio")
|
| 136 |
-
async def get_audio():
|
| 137 |
return FileResponse("response.mp3", media_type="audio/mpeg")
|
| 138 |
|
| 139 |
# Hàm sinh phản hồi
|
|
|
|
| 1 |
+
from fastapi import FastAPI, File, UploadFile, Request
|
| 2 |
from fastapi.responses import FileResponse
|
| 3 |
from pydantic import BaseModel
|
| 4 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
|
|
|
| 60 |
message: str
|
| 61 |
|
| 62 |
@app.get("/")
|
| 63 |
+
def read_root():
|
| 64 |
return {"message": "Ứng dụng đang chạy!"}
|
| 65 |
|
| 66 |
# Endpoint chat text
|
| 67 |
@app.post("/chat")
|
| 68 |
+
async def chat(request: ChatRequest):
|
| 69 |
conversation.append({"role": "user", "content": request.message})
|
| 70 |
text = tokenizer.apply_chat_template(conversation, tokenize=False, add_generation_prompt=True)
|
| 71 |
model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
|
|
|
|
| 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:
|
| 81 |
f.write(await file.read())
|
|
|
|
| 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
|
| 94 |
raw_audio = await request.body()
|
| 95 |
# Giả sử âm thanh là PCM 16-bit mono, sample rate 16000 Hz
|
|
|
|
| 134 |
|
| 135 |
# Endpoint trả về file âm thanh
|
| 136 |
@app.get("/get_audio")
|
| 137 |
+
async def get_audio():
|
| 138 |
return FileResponse("response.mp3", media_type="audio/mpeg")
|
| 139 |
|
| 140 |
# Hàm sinh phản hồi
|