|
|
import os |
|
|
import wave |
|
|
import json |
|
|
import numpy as np |
|
|
from flask import Flask, request, jsonify |
|
|
from flask_cors import CORS |
|
|
from vosk import Model, KaldiRecognizer |
|
|
|
|
|
|
|
|
app = Flask(__name__) |
|
|
CORS(app) |
|
|
|
|
|
|
|
|
MODEL_PATH = "model" |
|
|
if not os.path.exists(MODEL_PATH): |
|
|
raise Exception("Model Vosk không tìm thấy!") |
|
|
|
|
|
model = Model(MODEL_PATH) |
|
|
|
|
|
@app.route("/") |
|
|
def home(): |
|
|
return "Vosk STT API đang chạy!" |
|
|
|
|
|
@app.route("/stt", methods=["POST"]) |
|
|
def stt(): |
|
|
if "audio" not in request.files: |
|
|
return jsonify({"error": "Không tìm thấy file audio!"}), 400 |
|
|
|
|
|
audio_file = request.files["audio"] |
|
|
file_path = "temp.wav" |
|
|
audio_file.save(file_path) |
|
|
|
|
|
|
|
|
wf = wave.open(file_path, "rb") |
|
|
rec = KaldiRecognizer(model, wf.getframerate()) |
|
|
|
|
|
result_text = "" |
|
|
while True: |
|
|
data = wf.readframes(4000) |
|
|
if len(data) == 0: |
|
|
break |
|
|
if rec.AcceptWaveform(data): |
|
|
result_text += json.loads(rec.Result())["text"] + " " |
|
|
|
|
|
|
|
|
return jsonify({"text": result_text.strip()}) |
|
|
|
|
|
if __name__ == "__main__": |
|
|
app.run(host="0.0.0.0", port=7860, debug=True) |
|
|
|