Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
import os
|
| 2 |
import json
|
| 3 |
import asyncio
|
| 4 |
-
from fastapi import FastAPI, WebSocket
|
| 5 |
from fastapi.middleware.cors import CORSMiddleware
|
| 6 |
from vosk import Model, KaldiRecognizer
|
| 7 |
import uvicorn
|
|
@@ -9,9 +9,9 @@ import uvicorn
|
|
| 9 |
# Kiểm tra model
|
| 10 |
MODEL_PATH = "model/vosk-model"
|
| 11 |
if not os.path.exists(MODEL_PATH):
|
| 12 |
-
raise Exception("
|
| 13 |
|
| 14 |
-
print("
|
| 15 |
model = Model(MODEL_PATH)
|
| 16 |
|
| 17 |
# Tạo app FastAPI
|
|
@@ -26,30 +26,30 @@ app.add_middleware(
|
|
| 26 |
allow_headers=["*"],
|
| 27 |
)
|
| 28 |
|
| 29 |
-
@app.websocket("/ws") #
|
| 30 |
async def websocket_endpoint(websocket: WebSocket):
|
| 31 |
await websocket.accept()
|
| 32 |
rec = KaldiRecognizer(model, 16000)
|
| 33 |
result_text = ""
|
| 34 |
|
| 35 |
-
print("
|
| 36 |
-
|
| 37 |
try:
|
| 38 |
while True:
|
| 39 |
-
message = await websocket.receive_bytes() # Nhận
|
| 40 |
-
print(f"📩 Nhận dữ liệu ({len(message)} bytes)")
|
| 41 |
-
|
| 42 |
if rec.AcceptWaveform(message):
|
| 43 |
result = json.loads(rec.Result())
|
| 44 |
text = result.get("text", "")
|
| 45 |
result_text += text + " "
|
| 46 |
-
await websocket.send_json({"text": text}) # Gửi kết quả
|
| 47 |
-
print(f"
|
|
|
|
|
|
|
| 48 |
except Exception as e:
|
| 49 |
-
print(f"
|
| 50 |
await websocket.close(code=1011, reason="Lỗi xử lý dữ liệu")
|
| 51 |
finally:
|
| 52 |
-
print(f"
|
| 53 |
|
| 54 |
if __name__ == "__main__":
|
| 55 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
| 1 |
import os
|
| 2 |
import json
|
| 3 |
import asyncio
|
| 4 |
+
from fastapi import FastAPI, WebSocket, WebSocketDisconnect
|
| 5 |
from fastapi.middleware.cors import CORSMiddleware
|
| 6 |
from vosk import Model, KaldiRecognizer
|
| 7 |
import uvicorn
|
|
|
|
| 9 |
# Kiểm tra model
|
| 10 |
MODEL_PATH = "model/vosk-model"
|
| 11 |
if not os.path.exists(MODEL_PATH):
|
| 12 |
+
raise Exception("\u274c Model Vosk không tìm thấy!")
|
| 13 |
|
| 14 |
+
print("\u2705 Đang tải model Vosk...")
|
| 15 |
model = Model(MODEL_PATH)
|
| 16 |
|
| 17 |
# Tạo app FastAPI
|
|
|
|
| 26 |
allow_headers=["*"],
|
| 27 |
)
|
| 28 |
|
| 29 |
+
@app.websocket("/ws") # Đường dẫn WebSocket
|
| 30 |
async def websocket_endpoint(websocket: WebSocket):
|
| 31 |
await websocket.accept()
|
| 32 |
rec = KaldiRecognizer(model, 16000)
|
| 33 |
result_text = ""
|
| 34 |
|
| 35 |
+
print("\U0001F535 Client đã kết nối WebSocket!")
|
| 36 |
+
|
| 37 |
try:
|
| 38 |
while True:
|
| 39 |
+
message = await websocket.receive_bytes() # Nhận dữ liệu
|
|
|
|
|
|
|
| 40 |
if rec.AcceptWaveform(message):
|
| 41 |
result = json.loads(rec.Result())
|
| 42 |
text = result.get("text", "")
|
| 43 |
result_text += text + " "
|
| 44 |
+
await websocket.send_json({"text": text}) # Gửi kết quả
|
| 45 |
+
print(f"\U0001F4E4 Gửi kết quả: {text}")
|
| 46 |
+
except WebSocketDisconnect:
|
| 47 |
+
print("\U0001F534 Client đã ngắt kết nối!")
|
| 48 |
except Exception as e:
|
| 49 |
+
print(f"\u274c Lỗi WebSocket: {e}")
|
| 50 |
await websocket.close(code=1011, reason="Lỗi xử lý dữ liệu")
|
| 51 |
finally:
|
| 52 |
+
print(f"\u2705 Văn bản cuối cùng: {result_text.strip()}")
|
| 53 |
|
| 54 |
if __name__ == "__main__":
|
| 55 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|