Spaces:
Runtime error
Runtime error
Update main.py
Browse files
main.py
CHANGED
@@ -1,7 +1,34 @@
|
|
1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
app = FastAPI()
|
4 |
|
|
|
|
|
|
|
|
|
|
|
5 |
@app.get("/")
|
6 |
def read_root():
|
7 |
-
return {"
|
|
|
1 |
+
import torch
|
2 |
+
from fastapi import BackgroundTasks, FastAPI
|
3 |
+
from transformers import pipeline
|
4 |
+
|
5 |
+
MODEL_NAME = "openai/whisper-large-v2"
|
6 |
+
|
7 |
+
device = 0 if torch.cuda.is_available() else "cpu"
|
8 |
+
|
9 |
+
pipe = pipeline(
|
10 |
+
task="automatic-speech-recognition",
|
11 |
+
model=MODEL_NAME,
|
12 |
+
chunk_length_s=30,
|
13 |
+
device=device,
|
14 |
+
)
|
15 |
+
|
16 |
+
all_special_ids = pipe.tokenizer.all_special_ids
|
17 |
+
transcribe_token_id = all_special_ids[-5]
|
18 |
+
translate_token_id = all_special_ids[-6]
|
19 |
+
|
20 |
+
def transcribe(file):
|
21 |
+
pipe.model.config.forced_decoder_ids = [[2, transcribe_token_id]]
|
22 |
+
text = pipe(file, return_timestamps=True)
|
23 |
+
return text
|
24 |
|
25 |
app = FastAPI()
|
26 |
|
27 |
+
@app.get("/transcribe")
|
28 |
+
def transcribe(file: UploadFile = File(...), background_tasks: BackgroundTasks):
|
29 |
+
background_tasks.add_task(transcribe, file)
|
30 |
+
return {"text": "Processing file..."}
|
31 |
+
|
32 |
@app.get("/")
|
33 |
def read_root():
|
34 |
+
return {"text": "World!"}
|