-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.py
59 lines (47 loc) · 1.74 KB
/
index.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
from fastapi import FastAPI, File, UploadFile
import scipy.io.wavfile as wav
import sounddevice as sd
from fastapi.middleware.cors import CORSMiddleware
from structure.BEZIK import BEZIK
from structure.SpeechRecognizer import SpeechRecognizer
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
model = BEZIK(False)
speech_recognizer = SpeechRecognizer()
@app.get("/command/{req}")
def execute_command(req: str):
try:
operation, cache = model(req.replace('%', ' '))
return { "status": 200, "operation": operation, "message": "Operation executed succesfully", "cache": cache }
except:
return { "status": 500, "message": "Operation executing error" }
@app.get("/upload")
async def upload_file():
try:
duration = 5
fs=16000
print("Start speaking...")
recording = sd.rec(int(duration * fs), samplerate=fs, channels=1, dtype='int16')
sd.wait()
print("Recording finished.")
wav.write("recording.wav", fs, recording)
return { "status": 200, "message": "File uploaded succesfully" }
except:
return { "status": 500, "message": "Uploading file error" }
@app.get("/read")
def read_item():
try:
transcription = speech_recognizer.transcribe_audio("recording.wav")
operation = model(transcription.replace('%', ' '))
return { "status": 200, "operation": operation, "transcription": transcription, "message": "Operation executed succesfully" }
except:
return { "status": 500, "message": "Transcription / Operation executing error" }
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000)