whisper/v2w_service.py
zzhou 14e5568890 2025/10/11
加了一个网络服务部署的功能
2025-10-11 14:18:27 +08:00

95 lines
2.7 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import whisper
import time
import datetime
from flask import Flask, request, jsonify
import threading
app = Flask(__name__)
app.config['JSON_AS_ASCII'] = False # 确保中文正常显示
# 全局变量存储模型
model = None
def load_model():
global model
print("开始加载 Whisper 模型...")
start_time = time.time()
# 手动指定模型存储路径
model_path = "./models" # 您可以修改为任意路径
# 根据实际情况选择使用CPU还是GPU
model = whisper.load_model("medium", download_root=model_path, device="cpu")
load_time = time.time() - start_time
print(f"模型加载完成,耗时: {str(datetime.timedelta(seconds=load_time))}")
print(f"模型存储路径: {model_path}")
# 在应用启动时加载模型
@app.before_request
def before_first_request():
global model
if model is None:
print("首次请求,加载模型中...")
load_model()
@app.route('/transcribe', methods=['POST'])
def transcribe():
if model is None:
return jsonify({"error": "模型尚未加载完成"}), 503
if 'audio' not in request.files:
return jsonify({"error": "未提供音频文件"}), 400
audio_file = request.files['audio']
audio_path = f"/{audio_file.filename}"
audio_file.save(audio_path)
# 开始转录 - 使用 FP32 避免 NaN
start_time = time.time()
result = model.transcribe(audio_path, language="zh", fp16=False) # 主动降低精度,使用 FP32 避免 NaN
transcription_time = time.time() - start_time
return jsonify({
"text": result["text"],
"processing_time": transcription_time
})
@app.route('/transcribe_text', methods=['POST'])
def transcribe_text():
"""返回纯文本格式的转录结果,方便命令行查看"""
if model is None:
return "模型尚未加载完成", 503
if 'audio' not in request.files:
return "未提供音频文件", 400
audio_file = request.files['audio']
audio_path = f"/{audio_file.filename}"
audio_file.save(audio_path)
# 开始转录 - 使用 FP32 避免 NaN
start_time = time.time()
result = model.transcribe(audio_path, language="zh", fp16=False)
transcription_time = time.time() - start_time
# 返回纯文本格式
return f"{result['text']}\r\n处理时间: {transcription_time:.2f}"
@app.route('/health', methods=['GET'])
def health_check():
return jsonify({
"status": "ok",
"model_loaded": model is not None
})
if __name__ == '__main__':
# 在启动应用前预先加载模型
print("启动服务前预先加载模型...")
load_model()
app.run(host='0.0.0.0', port=5000, threaded=True)