charlie0608 commited on
Commit
08572fb
1 Parent(s): c22314e

Update app/main.py

Browse files
Files changed (1) hide show
  1. app/main.py +36 -1
app/main.py CHANGED
@@ -1,8 +1,43 @@
1
  from fastapi import FastAPI
2
- from whisper import speech_to_text
 
3
 
4
  app = FastAPI()
5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
  @app.get("/")
7
  def root():
8
  return {"Hello": "World"}
 
1
  from fastapi import FastAPI
2
+ import torch
3
+ from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor, pipeline
4
 
5
  app = FastAPI()
6
 
7
+ device = "cuda:0" if torch.cuda.is_available() else "cpu"
8
+ torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
9
+
10
+ model_id = "openai/whisper-large-v3"
11
+
12
+ model = AutoModelForSpeechSeq2Seq.from_pretrained(
13
+ model_id, torch_dtype=torch_dtype, low_cpu_mem_usage=True, use_safetensors=True
14
+ )
15
+ model.to(device)
16
+
17
+ processor = AutoProcessor.from_pretrained(model_id)
18
+
19
+ pipe = pipeline(
20
+ "automatic-speech-recognition",
21
+ model=model,
22
+ tokenizer=processor.tokenizer,
23
+ feature_extractor=processor.feature_extractor,
24
+ max_new_tokens=128,
25
+ chunk_length_s=30,
26
+ batch_size=16,
27
+ return_timestamps=True,
28
+ torch_dtype=torch_dtype,
29
+ device=device,
30
+ )
31
+
32
+ def speech_to_text(path_to_file: str) -> str:
33
+ """
34
+ Given the path to the .wav file, it returns the transcription.
35
+ """
36
+ result = pipe(path_to_file)
37
+
38
+ return result["text"]
39
+
40
+
41
  @app.get("/")
42
  def root():
43
  return {"Hello": "World"}