Spaces:
Build error
Build error
- server/src/main.py +17 -5
server/src/main.py
CHANGED
@@ -4,6 +4,7 @@ from typing import Union
|
|
4 |
from fastapi import Request
|
5 |
import torch
|
6 |
from transformers import pipeline
|
|
|
7 |
|
8 |
from fastapi import FastAPI
|
9 |
from contextlib import asynccontextmanager
|
@@ -14,14 +15,25 @@ ATTN_IMPLEMENTATION = os.getenv('ATTN_IMPLEMENTATION', "sdpa")
|
|
14 |
|
15 |
@asynccontextmanager
|
16 |
async def lifespan(app: FastAPI):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
app.state.transcribe_pipeline = pipeline(
|
18 |
"automatic-speech-recognition",
|
19 |
-
model=
|
20 |
-
|
21 |
-
|
22 |
-
|
|
|
23 |
)
|
24 |
-
app.state.transcribe_pipeline.model.to('cuda')
|
25 |
yield
|
26 |
|
27 |
app = FastAPI(lifespan=lifespan)
|
|
|
4 |
from fastapi import Request
|
5 |
import torch
|
6 |
from transformers import pipeline
|
7 |
+
from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor, pipeline
|
8 |
|
9 |
from fastapi import FastAPI
|
10 |
from contextlib import asynccontextmanager
|
|
|
15 |
|
16 |
@asynccontextmanager
|
17 |
async def lifespan(app: FastAPI):
|
18 |
+
torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
|
19 |
+
model_id = "openai/whisper-large-v3"
|
20 |
+
device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
21 |
+
|
22 |
+
model = AutoModelForSpeechSeq2Seq.from_pretrained(
|
23 |
+
model_id, torch_dtype=torch_dtype, low_cpu_mem_usage=True, use_safetensors=True
|
24 |
+
)
|
25 |
+
model.to(device)
|
26 |
+
|
27 |
+
processor = AutoProcessor.from_pretrained(model_id)
|
28 |
+
|
29 |
app.state.transcribe_pipeline = pipeline(
|
30 |
"automatic-speech-recognition",
|
31 |
+
model=model,
|
32 |
+
tokenizer=processor.tokenizer,
|
33 |
+
feature_extractor=processor.feature_extractor,
|
34 |
+
torch_dtype=torch_dtype,
|
35 |
+
device=device,
|
36 |
)
|
|
|
37 |
yield
|
38 |
|
39 |
app = FastAPI(lifespan=lifespan)
|