tudeplom commited on
Commit
de1cb80
·
verified ·
1 Parent(s): d57a541

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -22
app.py CHANGED
@@ -6,34 +6,24 @@ from flask import Flask, request, jsonify
6
  from flask_cors import CORS
7
  from vosk import Model, KaldiRecognizer
8
 
9
- # Tạo Flask app
10
  app = Flask(__name__)
11
  CORS(app)
12
 
13
- # Tải model Vosk (dùng model nhỏ để tiết kiệm tài nguyên)
14
- MODEL_PATH = "model" # Thư mục chứa model Vosk
15
  if not os.path.exists(MODEL_PATH):
16
- raise Exception("Model Vosk không tìm thấy!")
17
 
18
  model = Model(MODEL_PATH)
19
 
20
- @app.route("/")
21
- def home():
22
- return "Vosk STT API đang chạy!"
23
-
24
- @app.route("/stt", methods=["POST"])
25
- def stt():
26
- if "audio" not in request.files:
27
- return jsonify({"error": "Không tìm thấy file audio!"}), 400
28
-
29
- audio_file = request.files["audio"]
30
- file_path = "temp.wav"
31
- audio_file.save(file_path)
32
-
33
- # Xử lý file audio
34
  wf = wave.open(file_path, "rb")
35
- rec = KaldiRecognizer(model, wf.getframerate())
 
36
 
 
37
  result_text = ""
38
  while True:
39
  data = wf.readframes(4000)
@@ -42,8 +32,28 @@ def stt():
42
  if rec.AcceptWaveform(data):
43
  result_text += json.loads(rec.Result())["text"] + " "
44
 
45
- # Trả về kết quả STT
46
- return jsonify({"text": result_text.strip()})
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47
 
48
  if __name__ == "__main__":
49
- app.run(host="0.0.0.0", port=7860, debug=True)
 
6
  from flask_cors import CORS
7
  from vosk import Model, KaldiRecognizer
8
 
9
+ # Khởi tạo Flask app
10
  app = Flask(__name__)
11
  CORS(app)
12
 
13
+ # Đường dẫn model Vosk
14
+ MODEL_PATH = "model"
15
  if not os.path.exists(MODEL_PATH):
16
+ raise Exception("Model Vosk không tìm thấy! Đảm bảo đã tải về model đúng thư mục.")
17
 
18
  model = Model(MODEL_PATH)
19
 
20
+ def process_audio(file_path):
21
+ """ Xử lý file audio và trả về văn bản nhận dạng """
 
 
 
 
 
 
 
 
 
 
 
 
22
  wf = wave.open(file_path, "rb")
23
+ if wf.getnchannels() != 1:
24
+ return {"error": "Chỉ hỗ trợ file WAV mono (1 kênh)"}
25
 
26
+ rec = KaldiRecognizer(model, wf.getframerate())
27
  result_text = ""
28
  while True:
29
  data = wf.readframes(4000)
 
32
  if rec.AcceptWaveform(data):
33
  result_text += json.loads(rec.Result())["text"] + " "
34
 
35
+ wf.close()
36
+ os.remove(file_path) # Xóa file sau khi xử lý
37
+ return {"text": result_text.strip()}
38
+
39
+ @app.route("/")
40
+ def home():
41
+ return "✅ Vosk STT API đang chạy! Gửi file âm thanh đến /stt để nhận dạng."
42
+
43
+ @app.route("/stt", methods=["POST"])
44
+ def stt():
45
+ if "audio" not in request.files:
46
+ return jsonify({"error": "Không tìm thấy file audio!"}), 400
47
+
48
+ audio_file = request.files["audio"]
49
+ if not audio_file.filename.endswith(".wav"):
50
+ return jsonify({"error": "Chỉ hỗ trợ file WAV!"}), 400
51
+
52
+ file_path = "temp.wav"
53
+ audio_file.save(file_path)
54
+
55
+ result = process_audio(file_path)
56
+ return jsonify(result)
57
 
58
  if __name__ == "__main__":
59
+ app.run(host="0.0.0.0", port=7860, debug=True)