import gradio as gr import difflib import random # Function to evaluate the pronunciation and get a new sentence def pronunciation_evaluator(user_audio, sentence): # Mock text (assuming perfect recognition for the example) recognized_text = sentence.lower() # In a real application, this would be the result of speech-to-text # Compare the user's pronunciation with the original sentence similarity_ratio = difflib.SequenceMatcher(None, recognized_text, sentence.lower()).ratio() # Generate feedback based on similarity if similarity_ratio > 0.9: feedback = "Excellent! Your pronunciation is very accurate." elif similarity_ratio > 0.7: feedback = "Good job! But there's some room for improvement." else: feedback = "Keep practicing! Try to match the pronunciation more closely." # Get a new sentence for the next round new_sentence = get_sentence() # Return the feedback and the new sentence return f"Recognized Text: {recognized_text}\n\nFeedback: {feedback}", new_sentence # Function to get a random sentence def get_sentence(): sentences = [ "The quick brown fox jumps over the lazy dog.", "She sells seashells by the seashore.", "How much wood would a woodchuck chuck if a woodchuck could chuck wood?", "Peter Piper picked a peck of pickled peppers.", "I scream, you scream, we all scream for ice cream." ] return random.choice(sentences) # Initial sentence initial_sentence = get_sentence() # Gradio Interface interface = gr.Interface( fn=pronunciation_evaluator, inputs=[ gr.Audio(type="filepath", label="Record your pronunciation"), gr.Textbox(lines=2, label="Sentence to Pronounce", value=initial_sentence, interactive=False), # Display the sentence gr.State(value=initial_sentence) # Holds the current sentence ], outputs=[ "text", gr.Textbox(label="New Sentence to Pronounce", interactive=False), # Display the new sentence gr.State() # The new sentence will be stored as the state for the next input ], title="Pronunciation Evaluator", description="Record yourself pronouncing the given sentence and receive feedback. A new sentence will be provided after each evaluation." ) interface.launch()