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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -30
app.py CHANGED
@@ -6,25 +6,45 @@ from flask import Flask, request, jsonify
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)
30
  if len(data) == 0:
@@ -33,27 +53,9 @@ def process_audio(file_path):
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)
 
6
  from flask_cors import CORS
7
  from vosk import Model, KaldiRecognizer
8
 
9
+ # Thư mục chứa model
10
+ MODEL_PATH = "model/vosk-model"
11
+
12
+ # Kiểm tra model đã tải chưa
13
+ if not os.path.exists(MODEL_PATH):
14
+ raise Exception(f"❌ Model Vosk không tìm thấy tại {MODEL_PATH}! Kiểm tra lại.")
15
+
16
+ # Load model
17
+ print("✅ Đang tải model Vosk...")
18
+ model = Model(MODEL_PATH)
19
+
20
  # Khởi tạo Flask app
21
  app = Flask(__name__)
22
  CORS(app)
23
 
24
+ @app.route("/")
25
+ def home():
26
+ return "✅ Vosk STT API đang chạy!"
 
27
 
28
+ @app.route("/stt", methods=["POST"])
29
+ def stt():
30
+ if "audio" not in request.files:
31
+ return jsonify({"error": "Không tìm thấy file audio!"}), 400
32
 
33
+ audio_file = request.files["audio"]
34
+ file_path = "temp.wav"
35
+ audio_file.save(file_path)
36
+
37
+ # Mở file âm thanh
38
  wf = wave.open(file_path, "rb")
39
+
40
+ # Kiểm tra file đúng định dạng WAV mono không
41
+ if wf.getnchannels() != 1 or wf.getsampwidth() != 2 or wf.getcomptype() != "NONE":
42
+ os.remove(file_path)
43
+ return jsonify({"error": "File audio phải là WAV mono PCM!"}), 400
44
 
45
  rec = KaldiRecognizer(model, wf.getframerate())
46
  result_text = ""
47
+
48
  while True:
49
  data = wf.readframes(4000)
50
  if len(data) == 0:
 
53
  result_text += json.loads(rec.Result())["text"] + " "
54
 
55
  wf.close()
56
+ os.remove(file_path) # Xóa file tạm
 
 
 
 
 
57
 
58
+ return jsonify({"text": result_text.strip()})
 
 
 
 
 
 
 
 
 
 
 
 
 
59
 
60
  if __name__ == "__main__":
61
+ app.run(host="0.0.0.0", port=7860, debug=True)