jinggujiwoo7 commited on
Commit
9d4a77e
1 Parent(s): 0dca604

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +113 -15
app.py CHANGED
@@ -1,13 +1,16 @@
 
 
1
  import gradio as gr
2
  import speech_recognition as sr
3
  from Levenshtein import ratio
4
  import tempfile
 
5
  import soundfile as sf
6
 
7
  def transcribe_audio(file_info):
8
  r = sr.Recognizer()
9
  with tempfile.NamedTemporaryFile(delete=True, suffix=".wav") as tmpfile:
10
- sf.write(tmpfile.name, data=file_info[1], samplerate=44100, format='WAV')
11
  tmpfile.seek(0)
12
  with sr.AudioFile(tmpfile.name) as source:
13
  audio_data = r.record(source)
@@ -35,18 +38,113 @@ def pronunciation_correction(expected_text, file_info):
35
 
36
  return feedback, description
37
 
38
- def start_speech_practice(user_text, file_info):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
  user_spoken_text = transcribe_audio(file_info)
40
- return f"You said: {user_spoken_text}"
41
-
42
- iface = gr.Interface(
43
- start_speech_practice,
44
- [
45
- gr.inputs.Textbox(label="Type your sentence here", lines=5, default="Type here"),
46
- gr.inputs.Audio(label="Upload Audio File", type="numpy")
47
- ],
48
- gr.outputs.Textbox(label="Your Pronunciation"),
49
- capture_session=True,
50
- )
51
-
52
- iface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # app.py
2
+
3
  import gradio as gr
4
  import speech_recognition as sr
5
  from Levenshtein import ratio
6
  import tempfile
7
+ import numpy as np
8
  import soundfile as sf
9
 
10
  def transcribe_audio(file_info):
11
  r = sr.Recognizer()
12
  with tempfile.NamedTemporaryFile(delete=True, suffix=".wav") as tmpfile:
13
+ sf.write(tmpfile.name, data=file_info, samplerate=44100, format='WAV')
14
  tmpfile.seek(0)
15
  with sr.AudioFile(tmpfile.name) as source:
16
  audio_data = r.record(source)
 
38
 
39
  return feedback, description
40
 
41
+ def grammar_check(sentence):
42
+ # Example implementation of a grammar checker function
43
+ # Ideally, you would use a grammar checking API or tool
44
+ # This is a placeholder to show the structure
45
+ if not sentence.endswith("."):
46
+ return "Sentence should end with a period."
47
+ if len(sentence.split()) < 3:
48
+ return "Sentence is too short. Please provide more details."
49
+ return "No grammar issues found."
50
+
51
+ with gr.Blocks() as app:
52
+ with gr.Column():
53
+ text_input = gr.Textbox(label="Type your sentence here", lines=5, placeholder="Type here...")
54
+ grammar_check_button = gr.Button("Check Grammar")
55
+ grammar_feedback = gr.Textbox(label="Grammar Feedback", interactive=False)
56
+
57
+ audio_input = gr.Audio(label="Upload Audio File", type="numpy")
58
+ check_pronunciation_button = gr.Button("Check Pronunciation")
59
+
60
+ pronunciation_feedback = gr.Textbox(label="Pronunciation Feedback")
61
+ pronunciation_score = gr.Number(label="Pronunciation Accuracy Score: 0 (No Match) ~ 1 (Perfect)")
62
+
63
+ grammar_check_button.click(
64
+ grammar_check,
65
+ inputs=text_input,
66
+ outputs=grammar_feedback
67
+ )
68
+
69
+ check_pronunciation_button.click(
70
+ pronunciation_correction,
71
+ inputs=[text_input, audio_input],
72
+ outputs=[pronunciation_feedback, pronunciation_score]
73
+ )
74
+
75
+ app.launch(debug=True)
76
+ # app.py
77
+
78
+ import gradio as gr
79
+ import speech_recognition as sr
80
+ from Levenshtein import ratio
81
+ import tempfile
82
+ import numpy as np
83
+ import soundfile as sf
84
+
85
+ def transcribe_audio(file_info):
86
+ r = sr.Recognizer()
87
+ with tempfile.NamedTemporaryFile(delete=True, suffix=".wav") as tmpfile:
88
+ sf.write(tmpfile.name, data=file_info, samplerate=44100, format='WAV')
89
+ tmpfile.seek(0)
90
+ with sr.AudioFile(tmpfile.name) as source:
91
+ audio_data = r.record(source)
92
+ try:
93
+ text = r.recognize_google(audio_data)
94
+ return text
95
+ except sr.UnknownValueError:
96
+ return "Could not understand audio"
97
+ except sr.RequestError as e:
98
+ return f"Could not request results; {e}"
99
+
100
+ def pronunciation_correction(expected_text, file_info):
101
  user_spoken_text = transcribe_audio(file_info)
102
+ similarity = ratio(expected_text.lower(), user_spoken_text.lower())
103
+ description = f"{similarity:.2f}"
104
+
105
+ if similarity >= 0.9:
106
+ feedback = "Excellent pronunciation!"
107
+ elif similarity >= 0.7:
108
+ feedback = "Good pronunciation!"
109
+ elif similarity >= 0.5:
110
+ feedback = "Needs improvement."
111
+ else:
112
+ feedback = "Poor pronunciation, try to focus more on clarity."
113
+
114
+ return feedback, description
115
+
116
+ def grammar_check(sentence):
117
+ # Example implementation of a grammar checker function
118
+ # Ideally, you would use a grammar checking API or tool
119
+ # This is a placeholder to show the structure
120
+ if not sentence.endswith("."):
121
+ return "Sentence should end with a period."
122
+ if len(sentence.split()) < 3:
123
+ return "Sentence is too short. Please provide more details."
124
+ return "No grammar issues found."
125
+
126
+ with gr.Blocks() as app:
127
+ with gr.Column():
128
+ text_input = gr.Textbox(label="Type your sentence here", lines=5, placeholder="Type here...")
129
+ grammar_check_button = gr.Button("Check Grammar")
130
+ grammar_feedback = gr.Textbox(label="Grammar Feedback", interactive=False)
131
+
132
+ audio_input = gr.Audio(label="Upload Audio File", type="numpy")
133
+ check_pronunciation_button = gr.Button("Check Pronunciation")
134
+
135
+ pronunciation_feedback = gr.Textbox(label="Pronunciation Feedback")
136
+ pronunciation_score = gr.Number(label="Pronunciation Accuracy Score: 0 (No Match) ~ 1 (Perfect)")
137
+
138
+ grammar_check_button.click(
139
+ grammar_check,
140
+ inputs=text_input,
141
+ outputs=grammar_feedback
142
+ )
143
+
144
+ check_pronunciation_button.click(
145
+ pronunciation_correction,
146
+ inputs=[text_input, audio_input],
147
+ outputs=[pronunciation_feedback, pronunciation_score]
148
+ )
149
+
150
+ app.launch(debug=True)