-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
111 lines (90 loc) · 3.82 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
from fastapi import FastAPI, Form, UploadFile, File
from fastapi import HTTPException, status
import os
import shutil
from functools import lru_cache
from pathlib import Path
from typing import Any, List, Union, Optional
from datetime import timedelta
import numpy as np
import mlx_whisper
app = FastAPI()
# @lru_cache(maxsize=1)
# def get_whisper_model(whisper_model: str):
# """Get a whisper model from the cache or download it if it doesn't exist"""
# model = mlx_whisper.load_model(whisper_model)
# return model
def transcribe(audio_path: str, whisper_model: str, **whisper_args):
"""Transcribe the audio file using whisper"""
transcript = mlx_whisper.transcribe(audio_path)
return transcript
WHISPER_DEFAULT_SETTINGS = {
"whisper_model": "large-v2",
}
UPLOAD_DIR="/tmp"
@app.post('/v1/audio/transcriptions')
async def transcriptions(model: str = Form(...),
file: UploadFile = File(...),
response_format: Optional[str] = Form(None),
prompt: Optional[str] = Form(None),
temperature: Optional[float] = Form(None),
language: Optional[str] = Form(None)):
assert model == "whisper-1"
if file is None:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=f"Bad Request, bad file"
)
if response_format is None:
response_format = 'json'
if response_format not in ['json',
'text',
'srt',
'verbose_json',
'vtt']:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=f"Bad Request, bad response_format"
)
if temperature is None:
temperature = 0.0
if temperature < 0.0 or temperature > 1.0:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=f"Bad Request, bad temperature"
)
filename = file.filename
fileobj = file.file
upload_name = os.path.join(UPLOAD_DIR, filename)
upload_file = open(upload_name, 'wb+')
shutil.copyfileobj(fileobj, upload_file)
upload_file.close()
transcript = transcribe(audio_path=upload_name, **WHISPER_DEFAULT_SETTINGS)
if response_format in ['text']:
return transcript['text']
if response_format in ['srt']:
ret = ""
for seg in transcript['segments']:
td_s = timedelta(milliseconds=seg["start"]*1000)
td_e = timedelta(milliseconds=seg["end"]*1000)
t_s = f'{td_s.seconds//3600:02}:{(td_s.seconds//60)%60:02}:{td_s.seconds%60:02}.{td_s.microseconds//1000:03}'
t_e = f'{td_e.seconds//3600:02}:{(td_e.seconds//60)%60:02}:{td_e.seconds%60:02}.{td_e.microseconds//1000:03}'
ret += '{}\n{} --> {}\n{}\n\n'.format(seg["id"], t_s, t_e, seg["text"])
ret += '\n'
return ret
if response_format in ['vtt']:
ret = "WEBVTT\n\n"
for seg in transcript['segments']:
td_s = timedelta(milliseconds=seg["start"]*1000)
td_e = timedelta(milliseconds=seg["end"]*1000)
t_s = f'{td_s.seconds//3600:02}:{(td_s.seconds//60)%60:02}:{td_s.seconds%60:02}.{td_s.microseconds//1000:03}'
t_e = f'{td_e.seconds//3600:02}:{(td_e.seconds//60)%60:02}:{td_e.seconds%60:02}.{td_e.microseconds//1000:03}'
ret += "{} --> {}\n{}\n\n".format(t_s, t_e, seg["text"])
return ret
if response_format in ['verbose_json']:
transcript.setdefault('task', 'transcribe')
transcript.setdefault('duration', transcript['segments'][-1]['end'])
if transcript['language'] == 'ja':
transcript['language'] = 'japanese'
return transcript
return {'text': transcript['text']}