tudeplom commited on
Commit
e23ad72
·
verified ·
1 Parent(s): d146d4e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -7
app.py CHANGED
@@ -16,8 +16,8 @@ model = Model(MODEL_PATH)
16
 
17
  # Khởi tạo Flask app
18
  app = Flask(__name__)
19
- CORS(app) # Cho phép CORS để React app truy cập
20
- Swagger(app) # Khởi tạo Swagger cho tài liệu API
21
 
22
  @app.route("/")
23
  def home():
@@ -55,7 +55,6 @@ def stt():
55
  500:
56
  description: Lỗi server nội bộ
57
  """
58
- # Kiểm tra trường 'file' để khớp với React
59
  if "file" not in request.files:
60
  return jsonify({"error": "Không tìm thấy file âm thanh! Vui lòng gửi trường 'file'."}), 400
61
 
@@ -72,7 +71,13 @@ def stt():
72
  wav_path = None
73
  wf = None
74
  try:
75
- # Chuyển đổi WebM sang WAV mono PCM bằng ffmpeg
 
 
 
 
 
 
76
  wav_path = tempfile.mktemp(suffix=".wav")
77
  stream = ffmpeg.input(webm_path)
78
  stream = ffmpeg.output(
@@ -80,7 +85,8 @@ def stt():
80
  wav_path,
81
  acodec="pcm_s16le", # PCM 16-bit signed little-endian
82
  ac=1, # Mono
83
- ar=16000 # Tần số mẫu 16kHz, phù hợp với Vosk
 
84
  )
85
  ffmpeg.run(stream, overwrite_output=True, quiet=True)
86
 
@@ -104,7 +110,6 @@ def stt():
104
  result = json.loads(rec.Result())
105
  result_text += result.get("text", "") + " "
106
  else:
107
- # Thêm kết quả từng phần để cải thiện độ chính xác
108
  partial_result = json.loads(rec.PartialResult())
109
  if partial_result.get("partial", ""):
110
  result_text += partial_result["partial"] + " "
@@ -116,7 +121,8 @@ def stt():
116
  return jsonify({"text": final_text})
117
 
118
  except ffmpeg.Error as e:
119
- return jsonify({"error": f"Lỗi chuyển đổi âm thanh từ WebM sang WAV: {str(e.stderr.decode())}"}), 500
 
120
  except Exception as e:
121
  return jsonify({"error": f"Lỗi xử lý âm thanh: {str(e)}"}), 500
122
 
 
16
 
17
  # Khởi tạo Flask app
18
  app = Flask(__name__)
19
+ CORS(app)
20
+ Swagger(app)
21
 
22
  @app.route("/")
23
  def home():
 
55
  500:
56
  description: Lỗi server nội bộ
57
  """
 
58
  if "file" not in request.files:
59
  return jsonify({"error": "Không tìm thấy file âm thanh! Vui lòng gửi trường 'file'."}), 400
60
 
 
71
  wav_path = None
72
  wf = None
73
  try:
74
+ # Kiểm tra xem FFmpeg sẵn không
75
+ try:
76
+ ffmpeg.probe(webm_path)
77
+ except Exception as e:
78
+ return jsonify({"error": f"FFmpeg không khả dụng: {str(e)}"}), 500
79
+
80
+ # Chuyển đổi WebM sang WAV mono PCM
81
  wav_path = tempfile.mktemp(suffix=".wav")
82
  stream = ffmpeg.input(webm_path)
83
  stream = ffmpeg.output(
 
85
  wav_path,
86
  acodec="pcm_s16le", # PCM 16-bit signed little-endian
87
  ac=1, # Mono
88
+ ar=16000, # Tần số mẫu 16kHz, phù hợp với Vosk
89
+ format="wav"
90
  )
91
  ffmpeg.run(stream, overwrite_output=True, quiet=True)
92
 
 
110
  result = json.loads(rec.Result())
111
  result_text += result.get("text", "") + " "
112
  else:
 
113
  partial_result = json.loads(rec.PartialResult())
114
  if partial_result.get("partial", ""):
115
  result_text += partial_result["partial"] + " "
 
121
  return jsonify({"text": final_text})
122
 
123
  except ffmpeg.Error as e:
124
+ error_message = e.stderr.decode('utf-8') if e.stderr else str(e)
125
+ return jsonify({"error": f"Lỗi chuyển đổi âm thanh từ WebM sang WAV: {error_message}"}), 500
126
  except Exception as e:
127
  return jsonify({"error": f"Lỗi xử lý âm thanh: {str(e)}"}), 500
128