from fastapi import APIRouter, UploadFile, File, Body, HTTPException, status from fastapi.responses import JSONResponse from typing import Annotated import time import os from app.transcriber import get_transcription from app.matcher import match from app.mfcc import mfcc_similarty_check from app.string_processor import clean_transcription from app.passing import calculate_passing import requests """ initialize the router """ router = APIRouter(prefix="/voice", tags=["Voice"]) @router.post("/transcribe") async def transcribe_audio( original_url: Annotated[str, Body()], recorded: Annotated[UploadFile, File()], matcher_text: Annotated[str, Body()], ): try: # # Validate URL # if not original_url.endswith(".wav"): # raise HTTPException( # status_code=status.HTTP_400_BAD_REQUEST, # detail="Invalid URL. Please provide a URL pointing to a wav file.", # ) # # Download the audio file from the URL # response = requests.get(original_url) # if response.status_code != 200: # raise HTTPException( # status_code=status.HTTP_400_BAD_REQUEST, # detail="Unable to download the audio file from the URL.", # ) # filename_original = f"audio_{int(time.time())}_original.wav" # # Save the downloaded file temporarily # with open(filename_original, "wb") as buffer: # buffer.write(response.content) # Read file bytes recorded_bytes = await recorded.read() filename_recorded = f"audio_{int(time.time())}_recorded.wav" # Save the file temporarily with open(filename_recorded, "wb") as buffer: buffer.write(recorded_bytes) try: text = get_transcription(filename_recorded) text = clean_transcription(text) sequence, phonetic = match(matcher_text, text) weighted_score, is_passing = calculate_passing(sequence, phonetic) return JSONResponse( { "transcription": text, "score": round(weighted_score), "passing": str(is_passing) } ) finally: # Clean up the temporary file # os.remove(filename_original) os.remove(filename_recorded) except Exception as e: print(e) raise HTTPException( status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail="Unable to process the audio. Please try again later.", )