File size: 1,252 Bytes
79b7942
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import csv
import os
from concurrent.futures import ThreadPoolExecutor

from loguru import logger

from app.utils.common import convert_mp3_to_wav, get_doc, transcribe_audio
from app.utils.comprehension import evaluate_comprehension
from app.utils.grammar import evaluate_grammar
from app.utils.intonation import evaluate_intonation


def get_content_score(audio_file):
    if audio_file.endswith("mp3"):
        wav_file = convert_mp3_to_wav(audio_file)
    else:
        wav_file = audio_file

    # Step 2: Transcribe the WAV to text
    transcript = transcribe_audio(wav_file)

    doc = get_doc(transcript)

    with ThreadPoolExecutor() as executor:
        futures = [
            executor.submit(evaluate_intonation, wav_file),
            executor.submit(evaluate_grammar, transcript, doc),
            executor.submit(evaluate_comprehension, transcript, doc),
        ]

    return {
        "intonation_score": round((float(futures[0].result().get("intonation_score")) * 10), 2),
        "grammar_score": round((float(futures[1].result().get("grammar_score")) * 10), 2),
        "grammar_errors": futures[1].result().get("errors"),
        "comprehension_score": round((float(futures[2].result().get("comprehension_score")) * 10), 2),
    }