|
import gradio as gr |
|
import azure.cognitiveservices.speech as speechsdk |
|
|
|
def assess_pronunciation(audio_file, reference_text): |
|
try: |
|
|
|
speech_key = "12afe22c558a4f8d8bd28d6a67cdb9b0" |
|
service_region = "westus" |
|
speech_config = speechsdk.SpeechConfig(subscription=speech_key, region=service_region) |
|
|
|
|
|
audio_config = speechsdk.audio.AudioConfig(filename=audio_file) |
|
|
|
|
|
pronunciation_config = speechsdk.PronunciationAssessmentConfig( |
|
reference_text=reference_text, |
|
grading_system=speechsdk.PronunciationAssessmentGradingSystem.HundredMark, |
|
granularity=speechsdk.PronunciationAssessmentGranularity.Phoneme |
|
) |
|
|
|
|
|
recognizer = speechsdk.SpeechRecognizer(speech_config=speech_config, audio_config=audio_config) |
|
pronunciation_config.apply_to(recognizer) |
|
|
|
|
|
result = recognizer.recognize_once() |
|
|
|
|
|
print(f"Recognition result reason: {result.reason}") |
|
|
|
if result.reason == speechsdk.ResultReason.RecognizedSpeech: |
|
pronunciation_result = speechsdk.PronunciationAssessmentResult(result) |
|
|
|
|
|
accuracy_score = pronunciation_result.accuracy_score |
|
fluency_score = pronunciation_result.fluency_score |
|
completeness_score = pronunciation_result.completeness_score |
|
prosody_score = pronunciation_result.prosody_score |
|
|
|
return { |
|
"Accuracy": accuracy_score, |
|
"Fluency": fluency_score, |
|
"Completeness": completeness_score, |
|
"Prosody": prosody_score |
|
} |
|
elif result.reason == speechsdk.ResultReason.NoMatch: |
|
print("NOMATCH: Speech could not be recognized.") |
|
return {"Error": "Speech could not be recognized. Please try again with a clearer audio."} |
|
elif result.reason == speechsdk.ResultReason.Canceled: |
|
cancellation_details = speechsdk.CancellationDetails(result) |
|
print(f"CANCELED: Reason={cancellation_details.reason}") |
|
print(f"CANCELED: ErrorDetails={cancellation_details.error_details}") |
|
return {"Error": f"Speech recognition canceled: {cancellation_details.error_details}"} |
|
except Exception as e: |
|
print(f"An error occurred: {str(e)}") |
|
return {"Error": f"An unexpected error occurred: {str(e)}"} |
|
|
|
|
|
interface = gr.Interface( |
|
fn=assess_pronunciation, |
|
inputs=[ |
|
gr.Audio(type="filepath"), |
|
gr.Textbox(label="Reference Text", placeholder="Enter the reference text you are pronouncing") |
|
], |
|
outputs="json", |
|
title="Chinese Pronunciation Checker" |
|
) |
|
|
|
if __name__ == "__main__": |
|
interface.launch() |
|
|