NLPV commited on
Commit
bf2d620
Β·
verified Β·
1 Parent(s): 17a319c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -38
app.py CHANGED
@@ -1,4 +1,4 @@
1
- import gradio as gr
2
  from gtts import gTTS
3
  import time
4
  import difflib
@@ -14,41 +14,17 @@ def play_text(text):
14
  os.system(f"start {temp_file.name}") # Windows
15
  return "βœ… Text is being read out. Please listen and read it yourself."
16
 
 
17
  def transcribe_audio(audio, original_text):
18
  recognizer = sr.Recognizer()
19
  with sr.AudioFile(audio) as source:
20
  audio_data = recognizer.record(source)
21
-
22
  try:
23
  start_time = time.time()
24
-
25
- # Debug: Check audio data length and properties
26
- print(f"Audio Data Length: {len(audio_data.frame_data)}")
27
- print(f"Sample Rate: {audio_data.sample_rate}")
28
-
29
- # Split the audio into chunks (1-minute chunks in this example)
30
- audio_length = len(audio_data.frame_data)
31
- chunk_size = 60000 # 1 minute (60,000 ms)
32
-
33
- # Splitting audio data into chunks
34
- chunks = [audio_data.frame_data[i:i+chunk_size] for i in range(0, audio_length, chunk_size)]
35
- print(f"Number of chunks: {len(chunks)}")
36
-
37
- transcription = ""
38
- for chunk in chunks:
39
- audio_chunk = sr.AudioData(chunk, audio_data.sample_rate, audio_data.sample_width)
40
-
41
- # Debug: Print chunk size
42
- print(f"Transcribing Chunk: {len(chunk)}")
43
-
44
- # Using Google Speech Recognition (supports Hindi)
45
- transcription += recognizer.recognize_google(audio_chunk, language="hi-IN") + " "
46
-
47
  end_time = time.time()
48
 
49
- # Debug: Output full transcription
50
- print(f"Transcribed Text: {transcription}")
51
-
52
  # Calculate Accuracy
53
  original_words = original_text.strip().split()
54
  transcribed_words = transcription.strip().split()
@@ -57,26 +33,19 @@ def transcribe_audio(audio, original_text):
57
 
58
  # Calculate speed
59
  duration = end_time - start_time # time to process (not speaking time)
60
- speed = round(len(transcribed_words) / duration, 2) # words per second
61
 
62
- # Compare words and highlight mistakes
63
- wrong_words = []
64
- for i, word in enumerate(original_words):
65
- if i >= len(transcribed_words) or word != transcribed_words[i]:
66
- wrong_words.append(f"πŸ”΄ {word}")
67
 
68
  result = {
69
  "πŸ“ Transcribed Text": transcription,
70
  "🎯 Accuracy (%)": accuracy,
71
- "⏱️ Speaking Speed (words/sec)": speed,
72
- "❌ Incorrect Words": ' '.join(wrong_words) if wrong_words else "None"
73
  }
74
  return result
75
  except Exception as e:
76
  return {"error": str(e)}
77
 
78
- return {"error": str(e)}
79
-
80
  # Gradio App
81
  with gr.Blocks() as app:
82
  gr.Markdown("## πŸ—£οΈ Hindi Reading & Pronunciation Practice App")
@@ -98,3 +67,5 @@ with gr.Blocks() as app:
98
 
99
  # Launch the app
100
  app.launch()
 
 
 
1
+ mport gradio as gr
2
  from gtts import gTTS
3
  import time
4
  import difflib
 
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()
 
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")
 
67
 
68
  # Launch the app
69
  app.launch()
70
+
71
+