speechfeedback / app.py
jinggujiwoo7's picture
Update app.py
645c2e9 verified
raw
history blame
No virus
2.65 kB
import gradio as gr
import speech_recognition as sr
from Levenshtein import ratio
import tempfile
import numpy as np
import soundfile as sf
import language_tool_python
# Initialize LanguageTool for English grammar checking
tool = language_tool_python.LanguageTool('en-US')
def transcribe_audio(file_info):
r = sr.Recognizer()
with tempfile.NamedTemporaryFile(delete=True, suffix=".wav") as tmpfile:
sf.write(tmpfile.name, data=file_info[1], samplerate=44100, format='WAV')
tmpfile.seek(0)
with sr.AudioFile(tmpfile.name) as source:
audio_data = r.record(source)
try:
text = r.recognize_google(audio_data)
return text
except sr.UnknownValueError:
return "Could not understand audio"
except sr.RequestError as e:
return f"Could not request results; {e}"
def pronunciation_correction(expected_text, file_info):
user_spoken_text = transcribe_audio(file_info)
similarity = ratio(expected_text.lower(), user_spoken_text.lower())
description = f"{similarity:.2f}"
if similarity >= 0.9:
feedback = "Excellent pronunciation!"
elif similarity >= 0.7:
feedback = "Good pronunciation!"
elif similarity >= 0.5:
feedback = "Needs improvement."
else:
feedback = "Poor pronunciation, try to focus more on clarity."
return feedback, description
def grammar_check(text):
matches = tool.check(text)
corrected_text = tool.correct(text)
feedback = "\n".join(f"Line {match.contextOffset}: {match.message}" for match in matches)
return corrected_text, feedback
with gr.Blocks() as app:
with gr.Row():
sentence_input = gr.Textbox(label="Enter a Sentence", lines=2)
check_grammar_button = gr.Button("Check Grammar")
corrected_sentence_output = gr.Textbox(label="Corrected Sentence", interactive=False)
grammar_feedback = gr.Textbox(label="Grammar Feedback", interactive=False)
audio_input = gr.Audio(label="Upload Audio File", type="numpy")
check_pronunciation_button = gr.Button("Check Pronunciation")
pronunciation_feedback = gr.Textbox(label="Pronunciation Feedback")
pronunciation_score = gr.Number(label="Pronunciation Accuracy Score: 0 (No Match) ~ 1 (Perfect)")
check_grammar_button.click(
grammar_check,
inputs=sentence_input,
outputs=[corrected_sentence_output, grammar_feedback]
)
check_pronunciation_button.click(
pronunciation_correction,
inputs=[corrected_sentence_output, audio_input],
outputs=[pronunciation_feedback, pronunciation_score]
)
app.launch(debug=True)