bori0824 commited on
Commit
a0c3c1e
1 Parent(s): b55e3bf

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +85 -0
app.py ADDED
@@ -0,0 +1,85 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import speech_recognition as sr
3
+ from Levenshtein import ratio
4
+ import tempfile
5
+ import numpy as np
6
+ import soundfile as sf
7
+ import os
8
+
9
+ def transcribe_audio(file_info):
10
+ r = sr.Recognizer()
11
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmpfile:
12
+ sf.write(tmpfile.name, data=file_info[1], samplerate=44100, format='WAV')
13
+ tmpfile.seek(0)
14
+ with sr.AudioFile(tmpfile.name) as source:
15
+ audio_data = r.record(source)
16
+ os.remove(tmpfile.name) # Cleanup temporary file
17
+ try:
18
+ text = r.recognize_google(audio_data)
19
+ return text
20
+ except sr.UnknownValueError:
21
+ return "Could not understand audio"
22
+ except sr.RequestError as e:
23
+ return f"Could not request results; {e}"
24
+
25
+ def pronunciation_correction(expected_text, file_info):
26
+ print(f"Expected text: {expected_text}")
27
+ user_spoken_text = transcribe_audio(file_info)
28
+ print(f"User spoken text: {user_spoken_text}")
29
+
30
+ similarity = ratio(expected_text.lower(), user_spoken_text.lower())
31
+ description = f"{similarity:.2f}"
32
+
33
+ if similarity >= 0.9:
34
+ feedback = "Excellent pronunciation!"
35
+ elif similarity >= 0.7:
36
+ feedback = "Good pronunciation!"
37
+ elif similarity >= 0.5:
38
+ feedback = "Needs improvement."
39
+ else:
40
+ feedback = "Poor pronunciation, try to focus more on clarity."
41
+
42
+ print(f"Similarity: {similarity}, Feedback: {feedback}")
43
+ return feedback, description
44
+
45
+ def validate_sentence(sentence):
46
+ if not sentence.strip():
47
+ return "Please enter a sentence."
48
+ return sentence
49
+
50
+ def download_audio(file_info):
51
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmpfile:
52
+ sf.write(tmpfile.name, data=file_info[1], samplerate=44100, format='WAV')
53
+ tmpfile.seek(0)
54
+ return tmpfile.name # Return file path for download
55
+
56
+ with gr.Blocks() as app:
57
+ with gr.Row():
58
+ sentence_input = gr.Textbox(label="Enter Your Sentence Here")
59
+ validated_sentence = gr.Textbox(label="Valid Sentence", interactive=False)
60
+ audio_input = gr.Audio(label="Upload or Record Audio File", type="numpy")
61
+ check_pronunciation_button = gr.Button("Check Pronunciation")
62
+ pronunciation_feedback = gr.Textbox(label="Pronunciation Feedback")
63
+ pronunciation_score = gr.Number(label="Pronunciation Accuracy Score: 0 (No Match) ~ 1 (Perfect)")
64
+ download_audio_button = gr.Button("Download Recording")
65
+ download_output = gr.File(label="Download Your Recording")
66
+
67
+ sentence_input.change(
68
+ validate_sentence,
69
+ inputs=sentence_input,
70
+ outputs=validated_sentence
71
+ )
72
+
73
+ check_pronunciation_button.click(
74
+ pronunciation_correction,
75
+ inputs=[validated_sentence, audio_input],
76
+ outputs=[pronunciation_feedback, pronunciation_score]
77
+ )
78
+
79
+ download_audio_button.click(
80
+ download_audio,
81
+ inputs=[audio_input],
82
+ outputs=download_output
83
+ )
84
+
85
+ app.launch(debug=True)