Commit
·
5712999
1
Parent(s):
de93e48
ct2
Browse files- Dockerfile +3 -2
- app.py +28 -29
- requirements.txt +4 -2
Dockerfile
CHANGED
@@ -3,8 +3,9 @@ FROM python:3.10-slim
|
|
3 |
WORKDIR /code
|
4 |
|
5 |
COPY requirements.txt .
|
6 |
-
RUN
|
|
|
7 |
|
8 |
COPY app.py .
|
9 |
|
10 |
-
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
|
|
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 |
COPY app.py .
|
10 |
|
11 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
app.py
CHANGED
@@ -1,39 +1,38 @@
|
|
1 |
-
from faster_whisper import WhisperModel
|
2 |
-
from fastapi import FastAPI, UploadFile, File
|
3 |
-
import uvicorn
|
4 |
import os
|
5 |
-
|
|
|
|
|
|
|
6 |
|
|
|
7 |
app = FastAPI()
|
8 |
|
9 |
-
#
|
10 |
-
|
|
|
11 |
|
12 |
@app.get("/")
|
13 |
def root():
|
14 |
-
return {
|
15 |
-
|
16 |
-
|
|
|
17 |
|
18 |
@app.post("/transcribe")
|
19 |
async def transcribe(file: UploadFile = File(...)):
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
if __name__ == "__main__":
|
39 |
-
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
|
|
|
|
|
1 |
import os
|
2 |
+
from fastapi import FastAPI, UploadFile, File
|
3 |
+
import soundfile as sf
|
4 |
+
import ctranslate2
|
5 |
+
from transformers import WhisperProcessor
|
6 |
|
7 |
+
# 初始化 FastAPI 应用
|
8 |
app = FastAPI()
|
9 |
|
10 |
+
# 加载 Whisper 处理器和 CTranslate2 模型
|
11 |
+
processor = WhisperProcessor.from_pretrained("openai/whisper-small")
|
12 |
+
model = ctranslate2.Whisper("ct2_model", compute_type="int8", device="cpu")
|
13 |
|
14 |
@app.get("/")
|
15 |
def root():
|
16 |
+
return {
|
17 |
+
"message": "CTranslate2 Whisper API is running.",
|
18 |
+
"usage": "POST /transcribe with an audio file (.wav, .mp3, etc.)"
|
19 |
+
}
|
20 |
|
21 |
@app.post("/transcribe")
|
22 |
async def transcribe(file: UploadFile = File(...)):
|
23 |
+
# 保存上传音频
|
24 |
+
temp_path = f"/tmp/{file.filename}"
|
25 |
+
with open(temp_path, "wb") as f:
|
26 |
+
f.write(await file.read())
|
27 |
+
|
28 |
+
# 加载音频并提取特征
|
29 |
+
audio_input, sample_rate = sf.read(temp_path)
|
30 |
+
inputs = processor(audio_input, sampling_rate=sample_rate, return_tensors="np")
|
31 |
+
features = inputs.input_features[0]
|
32 |
+
|
33 |
+
# 运行 CTranslate2 推理
|
34 |
+
results = model.generate(features)
|
35 |
+
tokens = results[0]["tokens"]
|
36 |
+
text = processor.decode(tokens, skip_special_tokens=True)
|
37 |
+
|
38 |
+
return {"text": text}
|
|
|
|
|
|
|
|
requirements.txt
CHANGED
@@ -1,3 +1,5 @@
|
|
1 |
-
faster-whisper==0.9.0
|
2 |
fastapi
|
3 |
-
uvicorn
|
|
|
|
|
|
|
|
|
|
1 |
fastapi
|
2 |
+
uvicorn
|
3 |
+
ctranslate2
|
4 |
+
transformers
|
5 |
+
soundfile
|