This repository has been archived by the owner on Nov 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
78 lines (51 loc) · 2.14 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
# ---------------------------- IMPORTING DEPENDENCIES ---------------------------- #
from fastapi import FastAPI, Form
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
import Scripts.predictor as predictor
import time
# ----------------------------- CONFIGURING FASTAPI ------------------------------- #
# initializing fastapi app
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# ---------------------------------- MAIN CODE ----------------------------------- #
# root endpoint for server status check
@app.get("/")
def read_root():
return {"status": "Server is up and running"}
# Pydantic classes for processing incoming requests
class Filedata(BaseModel):
filedata: str = Form(...)
# endpoint for audio upload and conversion to text using base64 data from frontend
@app.post("/predict-base64/")
def predict_base64_default(filedata: Filedata):
return time_compute(predictor.predict_base64, filedata.filedata)
# return predictor.predict_base64(filedata.filedata)
# endpoint for audio upload and conversion to text using base64 data from frontend
@app.post("/predict-base64/{model_id}/")
def predict_base64(model_id: str, filedata: Filedata):
return time_compute(predictor.predict_base64, filedata.filedata, model_id)
# return predictor.predict_base64(filedata.filedata, model_id)
# endpoint for audio upload and conversion to text using array data from frontend
@app.post("/predict-array/{model_id}/")
def predict_array(filedata: str = Form(...)):
audio_array = eval(filedata)
assert isinstance(audio_array, list)
return {"status": "please use base64 format to use this API"}
# endpoint for live transcription
@app.post("/live-transcribe/{model_id}/")
def predict_live(filedata: str = Form(...)):
return {"status": "please use base64 format to use this API"}
# making time computation function
def time_compute(func, *args):
start_time = time.time()
response = func(*args)
end_time = time.time()
response["time_elapsed"] = end_time - start_time
return response