Graduation / Logic /SpeachToText.py
Mohammedallyl's picture
Upload 27 files
c4dc0b3 verified
raw
history blame
899 Bytes
from fastapi import APIRouter,UploadFile
from io import BytesIO
import speech_recognition as sr
from scipy.io import wavfile
from os import environ
Recognizer=sr.Recognizer()
SpeachRouter=APIRouter()
@SpeachRouter.post("/SpeachTotext")
def SpeachToTextEndPoint(Audio:UploadFile):
try:
with sr.AudioFile(BytesIO(Audio.file.read())) as File:
audio=Recognizer.listen(File)
environ["GOOGLE_APPLICATION_CREDENTIALS"]="smms"
Text:str=Recognizer.recognize_google(audio,language="en-US")
if ("on" in Text and "light" in Text) or ("off" not in Text and "light" in Text):
return {"Status":True,"Message":"l"} # l ::LightOn
elif "light" in Text:
return {"Status":True,"Message":"o"} # o ::LightOff
except LookupError as e:
return {"Status":False,"Message":e}