ReadabilityTest / app.py
NLPV's picture
Update app.py
2cf982a verified
raw
history blame
2.52 kB
import gradio as gr
from gtts import gTTS
import time
import difflib
import tempfile
import os
import speech_recognition as sr
# Function to play the text (optional)
def play_text(text):
tts = gTTS(text=text, lang='hi', slow=False)
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.mp3')
tts.save(temp_file.name)
os.system(f"start {temp_file.name}") # Windows
return "✅ 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 needed
transcription = recognizer.recognize_google(audio_data, language="hi-IN")
# Clean and split the text better (remove punctuations etc.)
import re
original_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 result
except 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 App
with gr.Blocks() as app:
gr.Markdown("## 🗣️ Hindi Reading & Pronunciation Practice App")
with gr.Row():
input_text = gr.Textbox(label="Paste Hindi Text Here", placeholder="यहाँ हिंदी टेक्स्ट लिखें...")
play_button = gr.Button("🔊 Listen to Text")
play_button.click(play_text, inputs=[input_text], outputs=[])
gr.Markdown("### 🎤 Now upload or record yourself reading the text aloud below:")
audio_input = gr.Audio(type="filepath", label="Upload or Record Your Voice")
submit_button = gr.Button("✅ Submit Recording for Checking")
output = gr.JSON(label="Results")
submit_button.click(transcribe_audio, inputs=[audio_input, input_text], outputs=[output])
# Launch the app
app.launch()