NLPV commited on
Commit
18faa93
Β·
verified Β·
1 Parent(s): 5995a5d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -8
app.py CHANGED
@@ -14,15 +14,27 @@ 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
- # 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
@@ -33,14 +45,19 @@ def transcribe_audio(audio, original_text):
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:
@@ -67,5 +84,3 @@ with gr.Blocks() as app:
67
 
68
  # Launch the app
69
  app.launch()
70
-
71
-
 
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 and compare with the original text
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
+
23
  try:
24
  start_time = time.time()
25
+ # Split the audio into chunks (1-minute chunks in this example)
26
+ audio_length = len(audio_data.frame_data)
27
+ chunk_size = 60000 # 1 minute (60,000 ms)
28
+
29
+ # Splitting audio data into chunks
30
+ chunks = [audio_data.frame_data[i:i+chunk_size] for i in range(0, audio_length, chunk_size)]
31
+
32
+ transcription = ""
33
+ for chunk in chunks:
34
+ audio_chunk = sr.AudioData(chunk, audio_data.sample_rate, audio_data.sample_width)
35
+ # Using Google Speech Recognition (supports Hindi)
36
+ transcription += recognizer.recognize_google(audio_chunk, language="hi-IN") + " "
37
+
38
  end_time = time.time()
39
 
40
  # Calculate Accuracy
 
45
 
46
  # Calculate speed
47
  duration = end_time - start_time # time to process (not speaking time)
 
 
48
  speed = round(len(transcribed_words) / duration, 2) # words per second
49
 
50
+ # Compare words and highlight mistakes
51
+ wrong_words = []
52
+ for i, word in enumerate(original_words):
53
+ if i >= len(transcribed_words) or word != transcribed_words[i]:
54
+ wrong_words.append(f"πŸ”΄ {word}")
55
+
56
  result = {
57
  "πŸ“ Transcribed Text": transcription,
58
  "🎯 Accuracy (%)": accuracy,
59
+ "⏱️ Speaking Speed (words/sec)": speed,
60
+ "❌ Incorrect Words": ' '.join(wrong_words) if wrong_words else "None"
61
  }
62
  return result
63
  except Exception as e:
 
84
 
85
  # Launch the app
86
  app.launch()