1een commited on
Commit
c87bc33
·
1 Parent(s): 3972a4f
Files changed (2) hide show
  1. Dockerfile +3 -4
  2. fixed_app.py +39 -14
Dockerfile CHANGED
@@ -8,7 +8,6 @@ RUN mkdir -p /app/.cache && chmod 777 /app/.cache
8
 
9
  # 设置环境变量
10
  ENV XDG_CACHE_HOME=/app/.cache
11
- # ENV TORCH_HOME=/app/.cache/torch
12
 
13
  # 安装系统依赖
14
  RUN apt-get update && apt-get install -y \
@@ -37,11 +36,11 @@ COPY . .
37
  # 暴露端口
38
  EXPOSE 7860
39
 
40
- # 检查whisper.cpp二进制文件位置
41
- # RUN find /app -name "main" -type f -executable 2>/dev/null || echo "main not found in /app"
42
-
43
  # 添加执行权限
44
  RUN chmod +x startup.sh
45
 
 
 
 
46
  # 使用启动脚本
47
  CMD ["./startup.sh"]
 
8
 
9
  # 设置环境变量
10
  ENV XDG_CACHE_HOME=/app/.cache
 
11
 
12
  # 安装系统依赖
13
  RUN apt-get update && apt-get install -y \
 
36
  # 暴露端口
37
  EXPOSE 7860
38
 
 
 
 
39
  # 添加执行权限
40
  RUN chmod +x startup.sh
41
 
42
+ # free -h
43
+ RUN free -h && df -h
44
+
45
  # 使用启动脚本
46
  CMD ["./startup.sh"]
fixed_app.py CHANGED
@@ -24,13 +24,14 @@ app = FastAPI(title="Whisper API", version="1.0.0")
24
 
25
  class AudioRequest(BaseModel):
26
  audio: str # base64 编码的音频数据
27
- model: str = "base" # 默认使用base模型
28
  language: Optional[str] = "zh" # 默认中文
29
  task: Optional[str] = "transcribe"
30
  temperature: Optional[float] = 0.0
31
  word_timestamps: Optional[bool] = False
32
  beam_size: Optional[int] = 1
33
  condition_on_previous_text: Optional[bool] = False
 
34
 
35
  def load_model(model_name: str):
36
  """确保模型文件存在,返回模型路径"""
@@ -206,20 +207,44 @@ async def transcribe_audio(request: AudioRequest):
206
  temp_dir = tempfile.mkdtemp()
207
  output_file = os.path.join(temp_dir, "output")
208
 
209
- # 构建命令
210
- cmd = [
211
- whisper_binary,
212
- "-m", model_path,
213
- "-f", audio_file,
214
- "-l", request.language or "zh",
215
- "-oj", # --output-json: 输出JSON格式
216
- "-of", output_file, # 指定输出文件
217
- "-t", str(os.cpu_count() or 1),
218
- ]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
219
 
220
- # 添加可选参数
221
- if request.beam_size:
222
- cmd += ["-bs", str(request.beam_size)] # --beam-size 的简写
 
 
 
 
223
  if request.temperature:
224
  cmd += ["-tp", str(request.temperature)] # --temperature 的简写
225
 
 
24
 
25
  class AudioRequest(BaseModel):
26
  audio: str # base64 编码的音频数据
27
+ model: str = "tiny" # 默认使用tiny模型以提高速度
28
  language: Optional[str] = "zh" # 默认中文
29
  task: Optional[str] = "transcribe"
30
  temperature: Optional[float] = 0.0
31
  word_timestamps: Optional[bool] = False
32
  beam_size: Optional[int] = 1
33
  condition_on_previous_text: Optional[bool] = False
34
+ fast_mode: Optional[bool] = True # 快速模式
35
 
36
  def load_model(model_name: str):
37
  """确保模型文件存在,返回模型路径"""
 
207
  temp_dir = tempfile.mkdtemp()
208
  output_file = os.path.join(temp_dir, "output")
209
 
210
+ # 构建命令 - 根据fast_mode调整参数
211
+ if request.fast_mode:
212
+ # 快速模式:牺牲一些精度换取速度
213
+ cmd = [
214
+ whisper_binary,
215
+ "-m", model_path,
216
+ "-f", audio_file,
217
+ "-l", request.language or "zh",
218
+ "-oj", # --output-json: 输出JSON格式
219
+ "-of", output_file, # 指定输出文件
220
+ "-t", str(os.cpu_count()), # 使用所有CPU核心
221
+ "-bs", "1", # beam size = 1 (最快)
222
+ "-bo", "1", # best of = 1 (最快)
223
+ "-ac", "0", # 音频上下文 = 0 (最快)
224
+ "-nf", # --no-fallback: 禁用温度回退
225
+ "-tp", "0.0", # 温度 = 0 (确定性输出)
226
+ ]
227
+ else:
228
+ # 标准模式:平衡速度和精度
229
+ cmd = [
230
+ whisper_binary,
231
+ "-m", model_path,
232
+ "-f", audio_file,
233
+ "-l", request.language or "zh",
234
+ "-oj", # --output-json: 输出JSON格式
235
+ "-of", output_file, # 指定输出文件
236
+ "-t", str(os.cpu_count()), # 使用所有CPU核心
237
+ "-bs", "5", # beam size = 5 (默认)
238
+ "-bo", "5", # best of = 5 (默认)
239
+ ]
240
 
241
+ # 添加可选参数(覆盖默认值)
242
+ if request.beam_size and request.beam_size != 1:
243
+ # 移除默认的-bs 1,添加用户指定的值
244
+ if "-bs" in cmd and "1" in cmd:
245
+ bs_index = cmd.index("-bs")
246
+ if bs_index + 1 < len(cmd) and cmd[bs_index + 1] == "1":
247
+ cmd[bs_index + 1] = str(request.beam_size)
248
  if request.temperature:
249
  cmd += ["-tp", str(request.temperature)] # --temperature 的简写
250