Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,55 +2,59 @@ import gradio as gr
|
|
2 |
import azure.cognitiveservices.speech as speechsdk
|
3 |
|
4 |
def assess_pronunciation(audio_file, reference_text):
|
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 |
# Create Gradio interface
|
56 |
interface = gr.Interface(
|
|
|
2 |
import azure.cognitiveservices.speech as speechsdk
|
3 |
|
4 |
def assess_pronunciation(audio_file, reference_text):
|
5 |
+
try:
|
6 |
+
# Configure Azure Speech Service
|
7 |
+
speech_key = "12afe22c558a4f8d8bd28d6a67cdb9b0"
|
8 |
+
service_region = "westus"
|
9 |
+
speech_config = speechsdk.SpeechConfig(subscription=speech_key, region=service_region)
|
10 |
+
|
11 |
+
# Set up the audio configuration
|
12 |
+
audio_config = speechsdk.audio.AudioConfig(filename=audio_file)
|
13 |
+
|
14 |
+
# Create pronunciation assessment config
|
15 |
+
pronunciation_config = speechsdk.PronunciationAssessmentConfig(
|
16 |
+
reference_text=reference_text,
|
17 |
+
grading_system=speechsdk.PronunciationAssessmentGradingSystem.HundredMark,
|
18 |
+
granularity=speechsdk.PronunciationAssessmentGranularity.Phoneme
|
19 |
+
)
|
20 |
+
pronunciation_config.enable_prosody_assessment()
|
21 |
|
22 |
+
# Create the recognizer
|
23 |
+
recognizer = speechsdk.SpeechRecognizer(speech_config=speech_config, audio_config=audio_config)
|
24 |
+
pronunciation_config.apply_to(recognizer)
|
25 |
|
26 |
+
# Recognize speech and assess pronunciation
|
27 |
+
result = recognizer.recognize_once()
|
28 |
|
29 |
+
# Debug information
|
30 |
+
print(f"Recognition result reason: {result.reason}")
|
31 |
|
32 |
+
if result.reason == speechsdk.ResultReason.RecognizedSpeech:
|
33 |
+
pronunciation_result = speechsdk.PronunciationAssessmentResult(result)
|
34 |
+
|
35 |
+
# Extract and format the results
|
36 |
+
accuracy_score = pronunciation_result.accuracy_score
|
37 |
+
fluency_score = pronunciation_result.fluency_score
|
38 |
+
completeness_score = pronunciation_result.completeness_score
|
39 |
+
prosody_score = pronunciation_result.prosody_score
|
40 |
|
41 |
+
return {
|
42 |
+
"Accuracy": accuracy_score,
|
43 |
+
"Fluency": fluency_score,
|
44 |
+
"Completeness": completeness_score,
|
45 |
+
"Prosody": prosody_score
|
46 |
+
}
|
47 |
+
elif result.reason == speechsdk.ResultReason.NoMatch:
|
48 |
+
print("NOMATCH: Speech could not be recognized.")
|
49 |
+
return {"Error": "Speech could not be recognized. Please try again with a clearer audio."}
|
50 |
+
elif result.reason == speechsdk.ResultReason.Canceled:
|
51 |
+
cancellation_details = speechsdk.CancellationDetails(result)
|
52 |
+
print(f"CANCELED: Reason={cancellation_details.reason}")
|
53 |
+
print(f"CANCELED: ErrorDetails={cancellation_details.error_details}")
|
54 |
+
return {"Error": f"Speech recognition canceled: {cancellation_details.error_details}"}
|
55 |
+
except Exception as e:
|
56 |
+
print(f"An error occurred: {str(e)}")
|
57 |
+
return {"Error": f"An unexpected error occurred: {str(e)}"}
|
58 |
|
59 |
# Create Gradio interface
|
60 |
interface = gr.Interface(
|