Commit 19e4a883 authored by Prabuddha Gimhan's avatar Prabuddha Gimhan

main app module developed

parent a132874f
import numpy as np
import tempfile
import soundfile as sf
import shutil
import os
import cv2
import tempfile
import shutil
import uvicorn
from fastapi import FastAPI, File, UploadFile
from fastapi.responses import FileResponse , JSONResponse
from fastapi.middleware.cors import CORSMiddleware
from service01.self_improvement import function01_main
from service01.text_to_time_and_number import word_to_time,word_to_number,extract_time_from_text,extract_numbers_from_text
from service02.Handwriting_recognition import Handwriting_recognition
from service03.lj_functiono3 import speech_to_text , scoring , text_to_speech,scoring_letter
app = FastAPI(title="Learn_Joy",
description="FastAPI for Learn_Joy",
version="0.104.1")
# Allow all origins (replace * with specific origins if needed)
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.get("/")
def read_root():
return {"Hello": "World"}
#####FUNCTION01############################
@app.post("/function01/self_improvement/")
async def function1_self_improvement(player_name:str):
out_put=function01_main(player_name=player_name)
return out_put
@app.post("/function01/STnumber/")
async def ST_number(audio_file: UploadFile = File(...)):
# Create a temporary directory
temp_dir = tempfile.mkdtemp()
# Create a temporary file path
temp_file_path = os.path.join(temp_dir, audio_file.filename)
# Write the uploaded file content to the temporary file
with open(temp_file_path, "wb") as temp_file:
shutil.copyfileobj(audio_file.file, temp_file)
#auto speech recognition
transcriptions=speech_to_text(temp_file_path)
#print(transcriptions)
# Clean up: Remove the temporary directory and its contents
shutil.rmtree(temp_dir)
#convert text to number
try:
number_sent=word_to_number(transcriptions)
number=extract_numbers_from_text( number_sent)
return {"ID":1,"Number":number}
except Exception as e:
return{"ID": 0,"Number":"Rate limit reached for gpt-3.5-turbo in organization org-ZZwojjJF2JZq3FiZnZ2tOhmn on requests per min (RPM): Limit 3, Used 3"}
@app.post("/function01/STtime/")
async def ST_time(audio_file: UploadFile = File(...)):
# Create a temporary directory
temp_dir = tempfile.mkdtemp()
# Create a temporary file path
temp_file_path = os.path.join(temp_dir, audio_file.filename)
# Write the uploaded file content to the temporary file
with open(temp_file_path, "wb") as temp_file:
shutil.copyfileobj(audio_file.file, temp_file)
#auto speech recognition
transcriptions=speech_to_text(temp_file_path)
#print(transcriptions)
# Clean up: Remove the temporary directory and its contents
shutil.rmtree(temp_dir)
#convert text to time
try:
time_sent=word_to_time(transcriptions)
time=extract_time_from_text(time_sent)
return {"ID":1,"Time":time}
except Exception as e:
return{"ID": 0,"Time":"Rate limit reached for gpt-3.5-turbo in organization org-ZZwojjJF2JZq3FiZnZ2tOhmn on requests per min (RPM): Limit 3, Used 3"}
#############Function02#########################
#blackboard white letter
@app.post("/function02/recognition_BB/")
async def function2_recognition(BB_image:UploadFile =File(...)):
out_put={}
temp_dir = tempfile.mkdtemp()
# Create a temporary file path foe doner file
BB_image_path = os.path.join(temp_dir,BB_image.filename)
# Write the uploaded file content to the temporary file
with open(BB_image_path, "wb") as temp_file1:
shutil.copyfileobj(BB_image.file, temp_file1)
out_put= Handwriting_recognition(BB_image=BB_image_path,WB_image=None)
# Clean up: Remove the temporary directory and its contents
shutil.rmtree(temp_dir)
return out_put
#whiteboard blackletter
@app.post("/function02/recognition_WB/")
async def function2_recognition(WB_image:UploadFile =File(...)):
out_put={}
# Create a temporary directory
temp_dir = tempfile.mkdtemp()
WB_image_path = os.path.join(temp_dir,WB_image.filename)
# Write the uploaded file content to the temporary file
with open(WB_image_path, "wb") as temp_file2:
shutil.copyfileobj(WB_image.file, temp_file2)
out_put= Handwriting_recognition(BB_image=None,WB_image=WB_image_path)
# Clean up: Remove the temporary directory and its contents
shutil.rmtree(temp_dir)
return out_put
##############FUNCTION3#####################
@app.post("/function3/STT_words/")
async def main_fun03_words(words:str, audio_file: UploadFile = File(...)):
# Create a temporary directory
temp_dir = tempfile.mkdtemp()
# Create a temporary file path
temp_file_path = os.path.join(temp_dir, audio_file.filename)
# Write the uploaded file content to the temporary file
with open(temp_file_path, "wb") as temp_file:
shutil.copyfileobj(audio_file.file, temp_file)
#auto speech recognition
transcriptions=speech_to_text(temp_file_path)
#get the prediction score
final_sent_score,final_word_score,missing_voice2=scoring(words,transcriptions)
#return score
voice_score=''
voice_sent=''
if int(final_sent_score) >= int(final_word_score):
voice_score=np.round(final_sent_score)
else :
voice_score=np.round(final_word_score)
if int(voice_score)==100:
voice_sent=f"WOW !!!! YOU WIN !!!, You got hundred percent score"
else:
voice_sent=f"You got less than hundred percent score, Please try these words {missing_voice2}"
#text to speech
speech_array=text_to_speech(voice_sent)
# Create a temporary file to store the audio data
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as temp2_file:
temp_filename = temp2_file.name
sf.write(temp_filename,speech_array, samplerate=16000, format='WAV')
# Clean up: Remove the temporary directory and its contents
shutil.rmtree(temp_dir)
json_data={"final_sent_score":np.round(final_sent_score), "final_word_score":np.round(final_word_score), "missing_voice2":missing_voice2}
# Return the audio file as a response & jasonrespons
#return [{"final_sent_score":np.round(final_sent_score), "final_word_score":np.round(final_word_score), "missing_voice2":missing_voice2},FileResponse(temp_filename, media_type="audio/wav", filename="speech.wav")]
return {"Scoring":json_data,"audio_file":FileResponse(temp_filename, media_type="audio/wav", filename="speech.wav")}
#return [JSONResponse(content=json_data, status_code=200, media_type="application/json", headers={"Scoring": "final_sent_score,final_word_score,missing_voice2"}),
# FileResponse(temp_filename, media_type="audio/wav", filename="speech.wav")]
@app.post("/function3/STT_letter/")
async def main_fun03_letter(letter:str, audio_file: UploadFile = File(...)):
# Create a temporary directory
temp_dir = tempfile.mkdtemp()
# Create a temporary file path
temp_file_path = os.path.join(temp_dir, audio_file.filename)
# Write the uploaded file content to the temporary file
with open(temp_file_path, "wb") as temp_file:
shutil.copyfileobj(audio_file.file, temp_file)
#auto speech recognition
transcriptions=speech_to_text(temp_file_path)
#print(transcriptions)
#get the prediction score
score=scoring_letter(letter,transcriptions)
#return score
missing_letter=[]
voice_sent=''
if int(score) == 100:
pass
else :
missing_letter.append(letter)
if int(score)==100:
voice_sent=f"WOW !!!! YOU WIN !!!, You got hundred percent score"
else:
voice_sent=f"You got less than hundred percent score, Please try letter {missing_letter}"
#text to speech
speech_array=text_to_speech(voice_sent)
# Create a temporary file to store the audio data
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as temp2_file:
temp_filename = temp2_file.name
sf.write(temp_filename,speech_array, samplerate=16000, format='WAV')
# Clean up: Remove the temporary directory and its contents
shutil.rmtree(temp_dir)
json_data={"final_sent_score":int(score), "missing_Letter":missing_letter}
# Return the audio file as a response & jasonrespons
#return [{"final_sent_score":np.round(final_sent_score), "final_word_score":np.round(final_word_score), "missing_voice2":missing_voice2},FileResponse(temp_filename, media_type="audio/wav", filename="speech.wav")]
return {"Scoring":json_data,"audio_file":FileResponse(temp_filename, media_type="audio/wav", filename="speech.wav")}
#return [JSONResponse(content=json_data, status_code=200, media_type="application/json", headers={"Scoring": "final_sent_score,final_word_score,missing_voice2"}),
# FileResponse(temp_filename, media_type="audio/wav", filename="speech.wav")]
#uvicorn main:app --reload
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment