Update app.py
Browse files
app.py
CHANGED
|
@@ -1,12 +1,10 @@
|
|
| 1 |
import os
|
| 2 |
import wave
|
| 3 |
import json
|
|
|
|
|
|
|
| 4 |
import uuid
|
| 5 |
-
import numpy as np
|
| 6 |
-
from flask import Flask, request, jsonify
|
| 7 |
-
from flask_cors import CORS
|
| 8 |
from vosk import Model, KaldiRecognizer
|
| 9 |
-
from flasgger import Swagger
|
| 10 |
|
| 11 |
# Thư mục chứa model
|
| 12 |
MODEL_PATH = "model/vosk-model"
|
|
@@ -19,78 +17,29 @@ if not os.path.exists(MODEL_PATH):
|
|
| 19 |
print("\u2705 Đang tải model Vosk...")
|
| 20 |
model = Model(MODEL_PATH)
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
@app.route("/")
|
| 28 |
-
def home():
|
| 29 |
-
"""API Home
|
| 30 |
-
---
|
| 31 |
-
responses:
|
| 32 |
-
200:
|
| 33 |
-
description: API đang chạy
|
| 34 |
-
"""
|
| 35 |
-
return "\u2705 Vosk STT API đang chạy!"
|
| 36 |
-
|
| 37 |
-
@app.route("/stt", methods=["POST"])
|
| 38 |
-
def stt():
|
| 39 |
-
"""Chuyển đổi giọng nói thành văn bản (Speech-to-Text)
|
| 40 |
-
---
|
| 41 |
-
consumes:
|
| 42 |
-
- multipart/form-data
|
| 43 |
-
parameters:
|
| 44 |
-
- in: formData
|
| 45 |
-
name: audio
|
| 46 |
-
type: file
|
| 47 |
-
required: true
|
| 48 |
-
description: File âm thanh WAV mono PCM
|
| 49 |
-
responses:
|
| 50 |
-
200:
|
| 51 |
-
description: Kết quả chuyển đổi văn bản
|
| 52 |
-
schema:
|
| 53 |
-
type: object
|
| 54 |
-
properties:
|
| 55 |
-
text:
|
| 56 |
-
type: string
|
| 57 |
-
example: "Xin chào thế giới"
|
| 58 |
-
400:
|
| 59 |
-
description: Lỗi nếu file âm thanh không hợp lệ hoặc không tìm thấy
|
| 60 |
-
"""
|
| 61 |
-
if "audio" not in request.files:
|
| 62 |
-
return jsonify({"error": "Không tìm thấy file audio!"}), 400
|
| 63 |
-
|
| 64 |
-
audio_file = request.files["audio"]
|
| 65 |
-
file_path = f"/tmp/{uuid.uuid4()}.wav" # Lưu vào thư mục tạm để tránh lỗi quyền hạn
|
| 66 |
-
audio_file.save(file_path)
|
| 67 |
-
|
| 68 |
-
try:
|
| 69 |
-
# Mở file âm thanh
|
| 70 |
-
wf = wave.open(file_path, "rb")
|
| 71 |
-
|
| 72 |
-
# Kiểm tra file có đúng định dạng WAV mono không
|
| 73 |
-
if wf.getnchannels() != 1 or wf.getsampwidth() != 2 or wf.getcomptype() != "NONE":
|
| 74 |
-
return jsonify({"error": "File audio phải là WAV mono PCM!"}), 400
|
| 75 |
-
|
| 76 |
-
rec = KaldiRecognizer(model, wf.getframerate())
|
| 77 |
-
result_text = ""
|
| 78 |
-
|
| 79 |
-
while True:
|
| 80 |
-
data = wf.readframes(4000)
|
| 81 |
-
if len(data) == 0:
|
| 82 |
-
break
|
| 83 |
-
if rec.AcceptWaveform(data):
|
| 84 |
-
result_text += json.loads(rec.Result())["text"] + " "
|
| 85 |
-
|
| 86 |
-
return jsonify({"text": result_text.strip()})
|
| 87 |
-
|
| 88 |
-
except Exception as e:
|
| 89 |
-
return jsonify({"error": str(e)}), 500
|
| 90 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
finally:
|
| 92 |
-
|
| 93 |
-
|
|
|
|
|
|
|
|
|
|
| 94 |
|
| 95 |
-
|
| 96 |
-
|
|
|
|
| 1 |
import os
|
| 2 |
import wave
|
| 3 |
import json
|
| 4 |
+
import asyncio
|
| 5 |
+
import websockets
|
| 6 |
import uuid
|
|
|
|
|
|
|
|
|
|
| 7 |
from vosk import Model, KaldiRecognizer
|
|
|
|
| 8 |
|
| 9 |
# Thư mục chứa model
|
| 10 |
MODEL_PATH = "model/vosk-model"
|
|
|
|
| 17 |
print("\u2705 Đang tải model Vosk...")
|
| 18 |
model = Model(MODEL_PATH)
|
| 19 |
|
| 20 |
+
async def recognize_audio(websocket, path):
|
| 21 |
+
print("🔵 Kết nối WebSocket mới...")
|
| 22 |
+
rec = KaldiRecognizer(model, 16000)
|
| 23 |
+
result_text = ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
+
try:
|
| 26 |
+
async for message in websocket:
|
| 27 |
+
if isinstance(message, bytes): # Kiểm tra nếu dữ liệu là bytes
|
| 28 |
+
if rec.AcceptWaveform(message):
|
| 29 |
+
result = json.loads(rec.Result())
|
| 30 |
+
text = result.get("text", "")
|
| 31 |
+
result_text += text + " "
|
| 32 |
+
await websocket.send(json.dumps({"text": text}))
|
| 33 |
+
else:
|
| 34 |
+
await websocket.send(json.dumps({"error": "Dữ liệu không hợp lệ"}))
|
| 35 |
+
except websockets.exceptions.ConnectionClosed:
|
| 36 |
+
print("🔴 Kết nối WebSocket đã đóng")
|
| 37 |
finally:
|
| 38 |
+
print(f"✅ Văn bản cuối cùng: {result_text.strip()}")
|
| 39 |
+
|
| 40 |
+
# Chạy server WebSocket
|
| 41 |
+
start_server = websockets.serve(recognize_audio, "0.0.0.0", 7860)
|
| 42 |
+
print("🚀 WebSocket STT server đang chạy trên ws://0.0.0.0:7860")
|
| 43 |
|
| 44 |
+
asyncio.get_event_loop().run_until_complete(start_server)
|
| 45 |
+
asyncio.get_event_loop().run_forever()
|