aikitty commited on
Commit
8269875
·
verified ·
1 Parent(s): 798a71b

Update app.py

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