-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
78 lines (61 loc) · 2.27 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
from fastapi import FastAPI, UploadFile
from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor, pipeline
import torch
import os
app = FastAPI()
model = None
processor = None
pipe = None
@app.on_event("startup")
async def startup_event():
global model, processor, pipe
device = "cuda:0" if torch.cuda.is_available() else "cpu"
torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
model_id = "openai/whisper-medium"
print("Starting model initialization...")
try:
model = AutoModelForSpeechSeq2Seq.from_pretrained(
model_id, torch_dtype=torch_dtype, low_cpu_mem_usage=True, use_safetensors=True
)
print("Model loaded.")
processor = AutoProcessor.from_pretrained(model_id)
print("Processor loaded.")
pipe = pipeline(
"automatic-speech-recognition",
model=model,
tokenizer=processor.tokenizer,
feature_extractor=processor.feature_extractor,
torch_dtype=torch_dtype,
device=device,
)
print("Pipeline created.")
except Exception as e:
print(f"Error during startup: {e}")
print("Startup complete.")
@app.post('/talk')
async def post_audio(file: UploadFile):
global pipe
if pipe is None:
raise RuntimeError("Model pipeline is not initialized.")
audio_file = await file.read() # Read file content as bytes
result = pipe(audio_file)
transcription = result["text"]
return {'message': 'transcription done', 'transcription': transcription}
@app.get("/process_local_audio")
async def process_local_audio():
global pipe
if pipe is None:
raise RuntimeError("Model pipeline is not initialized.")
# Absolute path to the audio file
file_path = os.path.join(os.getcwd(), "learning-bot/test-1.mp3")
# Ensure the file exists
if not os.path.isfile(file_path):
return {"error": "File not found", "file_path": file_path}
with open(file_path, "rb") as f:
audio_file = f.read() # Read the file as bytes
result = pipe(audio_file)
transcription = result["text"]
return {'message': 'transcription done', 'transcription': transcription}
@app.get("/")
async def root():
return {"message": "Hello World"}