Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,35 +1,70 @@
|
|
1 |
import gradio as gr
|
2 |
from gtts import gTTS
|
3 |
-
import os
|
4 |
import time
|
|
|
|
|
|
|
|
|
5 |
|
6 |
-
|
7 |
-
|
8 |
-
return "❌ Please enter some text!"
|
9 |
-
|
10 |
-
# Generate TTS
|
11 |
tts = gTTS(text=text, lang='hi', slow=False)
|
12 |
-
|
13 |
-
tts.save(
|
|
|
|
|
14 |
|
15 |
-
|
|
|
|
|
|
|
|
|
16 |
try:
|
17 |
-
|
18 |
-
#
|
19 |
-
|
20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
except Exception as e:
|
22 |
-
|
|
|
|
|
|
|
|
|
23 |
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
)
|
34 |
-
|
35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
from gtts import gTTS
|
|
|
3 |
import time
|
4 |
+
import difflib
|
5 |
+
import tempfile
|
6 |
+
import os
|
7 |
+
import speech_recognition as sr
|
8 |
|
9 |
+
# Function to play the text (optional)
|
10 |
+
def play_text(text):
|
|
|
|
|
|
|
11 |
tts = gTTS(text=text, lang='hi', slow=False)
|
12 |
+
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.mp3')
|
13 |
+
tts.save(temp_file.name)
|
14 |
+
os.system(f"start {temp_file.name}") # Windows
|
15 |
+
return "✅ Text is being read out. Please listen and read it yourself."
|
16 |
|
17 |
+
# Function to transcribe user's audio
|
18 |
+
def transcribe_audio(audio, original_text):
|
19 |
+
recognizer = sr.Recognizer()
|
20 |
+
with sr.AudioFile(audio) as source:
|
21 |
+
audio_data = recognizer.record(source)
|
22 |
try:
|
23 |
+
start_time = time.time()
|
24 |
+
# Using Google Speech Recognition (supports Hindi)
|
25 |
+
transcription = recognizer.recognize_google(audio_data, language="hi-IN")
|
26 |
+
end_time = time.time()
|
27 |
+
|
28 |
+
# Calculate Accuracy
|
29 |
+
original_words = original_text.strip().split()
|
30 |
+
transcribed_words = transcription.strip().split()
|
31 |
+
matcher = difflib.SequenceMatcher(None, original_words, transcribed_words)
|
32 |
+
accuracy = round(matcher.ratio() * 100, 2)
|
33 |
+
|
34 |
+
# Calculate speed
|
35 |
+
duration = end_time - start_time # time to process (not speaking time)
|
36 |
+
# Better: estimate speaking time from audio length if needed (advanced)
|
37 |
+
|
38 |
+
speed = round(len(transcribed_words) / duration, 2) # words per second
|
39 |
+
|
40 |
+
result = {
|
41 |
+
"📝 Transcribed Text": transcription,
|
42 |
+
"🎯 Accuracy (%)": accuracy,
|
43 |
+
"⏱️ Speaking Speed (words/sec)": speed
|
44 |
+
}
|
45 |
+
return result
|
46 |
except Exception as e:
|
47 |
+
return {"error": str(e)}
|
48 |
+
|
49 |
+
# Gradio App
|
50 |
+
with gr.Blocks() as app:
|
51 |
+
gr.Markdown("## 🗣️ Hindi Reading & Pronunciation Practice App")
|
52 |
|
53 |
+
with gr.Row():
|
54 |
+
input_text = gr.Textbox(label="Paste Hindi Text Here", placeholder="यहाँ हिंदी टेक्स्ट लिखें...")
|
55 |
+
play_button = gr.Button("🔊 Listen to Text")
|
56 |
+
|
57 |
+
play_button.click(play_text, inputs=[input_text], outputs=[])
|
58 |
+
|
59 |
+
gr.Markdown("### 🎤 Now record yourself reading the text aloud below:")
|
60 |
+
audio_input = gr.Audio(source="microphone", type="filepath", label="Record Your Voice")
|
61 |
+
|
62 |
+
submit_button = gr.Button("✅ Submit Recording for Checking")
|
63 |
+
|
64 |
+
output = gr.JSON(label="Results")
|
65 |
+
|
66 |
+
submit_button.click(transcribe_audio, inputs=[audio_input, input_text], outputs=[output])
|
67 |
+
|
68 |
+
# Launch the app
|
69 |
+
app.launch()
|
70 |
+
|