File size: 2,635 Bytes
fe79a8f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b82152e
 
 
 
 
 
fe79a8f
b82152e
 
 
 
 
 
 
fe79a8f
b82152e
fe79a8f
b82152e
 
 
fe79a8f
 
 
 
 
 
 
 
 
 
 
 
 
b82152e
fe79a8f
 
 
 
 
 
 
 
 
b82152e
fe79a8f
 
 
 
 
 
 
 
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
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.",
        )