tianyaogavin commited on
Commit
90f5178
·
1 Parent(s): 41a953b
Files changed (3) hide show
  1. Dockerfile +14 -6
  2. app.py +20 -22
  3. requirements.txt +2 -3
Dockerfile CHANGED
@@ -1,14 +1,22 @@
1
  FROM python:3.10-slim
2
- # Trigger rebuild 2025-03-14-16:57
 
3
  WORKDIR /code
4
 
 
 
 
 
 
5
  COPY requirements.txt .
6
- RUN apt-get update && apt-get install -y libsndfile1 && \
7
- pip install --no-cache-dir -r requirements.txt
8
 
9
- # ✅ 把相关目录和文件都复制进去
10
  COPY app.py .
11
  COPY ct2_model/ ct2_model/
12
- COPY whisper_processor/ whisper_processor/
13
 
14
- CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
 
 
 
 
 
1
  FROM python:3.10-slim
2
+
3
+ # 设置工作目录
4
  WORKDIR /code
5
 
6
+ # 安装系统依赖
7
+ RUN apt-get update && apt-get install -y --no-install-recommends libsndfile1 && \
8
+ rm -rf /var/lib/apt/lists/*
9
+
10
+ # 复制依赖文件并安装 Python 依赖
11
  COPY requirements.txt .
12
+ RUN pip install --no-cache-dir -r requirements.txt
 
13
 
14
+ # 复制应用代码和模型
15
  COPY app.py .
16
  COPY ct2_model/ ct2_model/
 
17
 
18
+ # 暴露端口
19
+ EXPOSE 7860
20
+
21
+ # 启动 FastAPI 应用
22
+ CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
app.py CHANGED
@@ -1,18 +1,18 @@
1
-
2
  from fastapi import FastAPI, UploadFile, File
3
  import soundfile as sf
4
- import ctranslate2
5
- from transformers import WhisperProcessor
6
  import os
 
 
 
7
  os.environ["HF_HOME"] = "/tmp/hf_cache"
8
 
9
  # 初始化 FastAPI 应用
10
  app = FastAPI()
11
 
12
- # 加载 Whisper 处理器和 CTranslate2 模型
13
  print("✅ THIS IS NEW APP.PY VERSION")
14
- processor = WhisperProcessor.from_pretrained("./whisper_processor")
15
- model = ctranslate2.Whisper("ct2_model", compute_type="int8", device="cpu")
16
 
17
  @app.get("/")
18
  def root():
@@ -23,19 +23,17 @@ def root():
23
 
24
  @app.post("/transcribe")
25
  async def transcribe(file: UploadFile = File(...)):
26
- # 保存上传音频
27
- temp_path = f"/tmp/{file.filename}"
28
- with open(temp_path, "wb") as f:
29
- f.write(await file.read())
30
-
31
- # 加载音频并提取特征
32
- audio_input, sample_rate = sf.read(temp_path)
33
- inputs = processor(audio_input, sampling_rate=sample_rate, return_tensors="np")
34
- features = inputs.input_features[0]
35
-
36
- # 运行 CTranslate2 推理
37
- results = model.generate(features)
38
- tokens = results[0]["tokens"]
39
- text = processor.decode(tokens, skip_special_tokens=True)
40
-
41
- return {"text": text}
 
 
1
  from fastapi import FastAPI, UploadFile, File
2
  import soundfile as sf
3
+ from faster_whisper import WhisperModel
 
4
  import os
5
+ import tempfile
6
+
7
+ # 设置 Hugging Face 缓存目录
8
  os.environ["HF_HOME"] = "/tmp/hf_cache"
9
 
10
  # 初始化 FastAPI 应用
11
  app = FastAPI()
12
 
13
+ # 加载 Faster-Whisper 模型
14
  print("✅ THIS IS NEW APP.PY VERSION")
15
+ model = WhisperModel("ct2_model", compute_type="int8", device="cpu")
 
16
 
17
  @app.get("/")
18
  def root():
 
23
 
24
  @app.post("/transcribe")
25
  async def transcribe(file: UploadFile = File(...)):
26
+ # 保存上传音频到临时文件
27
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as temp_file:
28
+ temp_path = temp_file.name
29
+ temp_file.write(await file.read())
30
+
31
+ try:
32
+ # 使用 Faster-Whisper 进行推理
33
+ segments, info = model.transcribe(temp_path)
34
+ transcription = " ".join([segment.text for segment in segments])
35
+
36
+ return {"text": transcription}
37
+ finally:
38
+ # 删除临时文件
39
+ os.remove(temp_path)
 
 
requirements.txt CHANGED
@@ -1,5 +1,4 @@
1
  fastapi
2
  uvicorn
3
- ctranslate2==3.20.0
4
- transformers
5
- soundfile
 
1
  fastapi
2
  uvicorn
3
+ soundfile
4
+ faster-whisper