Commit
·
90f5178
1
Parent(s):
41a953b
submit
Browse files- Dockerfile +14 -6
- app.py +20 -22
- requirements.txt +2 -3
Dockerfile
CHANGED
@@ -1,14 +1,22 @@
|
|
1 |
FROM python:3.10-slim
|
2 |
-
|
|
|
3 |
WORKDIR /code
|
4 |
|
|
|
|
|
|
|
|
|
|
|
5 |
COPY requirements.txt .
|
6 |
-
RUN
|
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 |
-
|
|
|
|
|
|
|
|
|
|
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
|
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
|
13 |
print("✅ THIS IS NEW APP.PY VERSION")
|
14 |
-
|
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 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
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 |
-
|
4 |
-
|
5 |
-
soundfile
|
|
|
1 |
fastapi
|
2 |
uvicorn
|
3 |
+
soundfile
|
4 |
+
faster-whisper
|
|