Spaces:
Sleeping
Sleeping
Update app.py
Browse files13 unmodified lines141516171819202122232425262728293031323334353637383940414243444546474849505113 unmodified linesos.system(f"start {temp_file.name}") # Windowsreturn "✅ Text is being read out. Please listen and read it yourself."# Function to transcribe user's audiodef transcribe_audio(audio, original_text):recognizer = sr.Recognizer()with sr.AudioFile(audio) as source:audio_data = recognizer.record(source)try:start_time = time.time()# Using Google Speech Recognition (supports Hindi)transcription = recognizer.recognize_google(audio_data, language="hi-IN")end_time = time.time()# Calculate Accuracyoriginal_words = original_text.strip().split()transcribed_words = transcription.strip().split()matcher = difflib.SequenceMatcher(None, original_words, transcribed_words)accuracy = round(matcher.ratio() * 100, 2)# Calculate speedduration = end_time - start_time # time to process (not speaking time)# Better: estimate speaking time from audio length if needed (advanced)speed = round(len(transcribed_words) / duration, 2) # words per secondresult = {"📝 Transcribed Text": transcription,"🎯 Accuracy (%)": accuracy,"⏱️ Speaking Speed (words/sec)": speed}return resultexcept Exception as e:return {"error": str(e)}# Gradio Appwith gr.Blocks() as app:gr.Markdown("## 🗣️ Hindi Reading & Pronunciation Practice App")13 unmodified lines14151617181920212223242526272829303132333435363738394041424344454647484950515213 unmodified linesos.system(f"start {temp_file.name}") # Windowsreturn "✅ Text is being read out. Please listen and read it yourself."def transcribe_audio(audio, original_text):recognizer = sr.Recognizer()try:with sr.AudioFile(audio) as source:audio_data = recognizer.record(source)# Try chunking if neededtranscription = recognizer.recognize_google(audio_data, language="hi-IN")# Clean and split the text better (remove punctuations etc.)import reoriginal_words = re.findall(r'\w+', original_text.strip())transcribed_words = re.findall(r'\w+', transcription.strip())matcher = difflib.SequenceMatcher(None, original_words, transcribed_words)accuracy = round(matcher.ratio() * 100, 2)speed = round(len(transcribed_words) / (len(audio_data.frame_data) / audio_data.sample_rate), 2)result = {"📝 Transcribed Text": transcription,"🎯 Accuracy (%)": accuracy,"⏱️ Speaking Speed (words/sec)": speed}return resultexcept sr.UnknownValueError:return {"error": "Could not understand audio"}except sr.RequestError as e:return {"error": f"Request error: {e}"}except Exception as e:return {"error": str(e)}# Gradio Appwith gr.Blocks() as app:gr.Markdown("## 🗣️ Hindi Reading & Pronunciation Practice App")
app.py+16-15