NLPV commited on
Commit
d6fa022
·
verified ·
1 Parent(s): 05f7c31

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -26
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
- def read_hindi_text(text):
7
- if not text.strip():
8
- return "❌ Please enter some text!"
9
-
10
- # Generate TTS
11
  tts = gTTS(text=text, lang='hi', slow=False)
12
- filename = "hindi_output.mp3"
13
- tts.save(filename)
 
 
14
 
15
- # Play audio
 
 
 
 
16
  try:
17
- os.system("start " + filename) # Windows
18
- # os.system("afplay " + filename) # Mac
19
- # os.system("xdg-open " + filename) # Linux
20
- output_message = f"✅ Hindi Text saved as '{filename}' and playing now!"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
  except Exception as e:
22
- output_message = f"⚠️ Error playing the file: {str(e)}"
 
 
 
 
23
 
24
- return output_message
25
-
26
- # Gradio interface
27
- iface = gr.Interface(
28
- fn=read_hindi_text,
29
- inputs=gr.Textbox(lines=4, placeholder="यहाँ हिंदी टेक्स्ट लिखें..."),
30
- outputs="text",
31
- title="Hindi Text-to-Speech",
32
- description="हिंदी टेक्स्ट डालें और 'Read' बटन दबाएँ। टेक्स्ट को आवाज़ में बदला जाएगा।"
33
- )
34
-
35
- iface.launch()
 
 
 
 
 
 
 
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
+