NLPV commited on
Commit
2cf982a
Β·
verified Β·
1 Parent(s): b63476f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -15
app.py CHANGED
@@ -14,38 +14,39 @@ 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
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")
 
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
  try:
20
+ with sr.AudioFile(audio) as source:
21
+ audio_data = recognizer.record(source)
22
+
23
+ # Try chunking if needed
24
  transcription = recognizer.recognize_google(audio_data, language="hi-IN")
 
25
 
26
+ # Clean and split the text better (remove punctuations etc.)
27
+ import re
28
+ original_words = re.findall(r'\w+', original_text.strip())
29
+ transcribed_words = re.findall(r'\w+', transcription.strip())
30
+
31
  matcher = difflib.SequenceMatcher(None, original_words, transcribed_words)
32
  accuracy = round(matcher.ratio() * 100, 2)
33
 
34
+ speed = round(len(transcribed_words) / (len(audio_data.frame_data) / audio_data.sample_rate), 2)
35
+
 
 
 
 
36
  result = {
37
  "πŸ“ Transcribed Text": transcription,
38
  "🎯 Accuracy (%)": accuracy,
39
  "⏱️ Speaking Speed (words/sec)": speed
40
  }
41
  return result
42
+ except sr.UnknownValueError:
43
+ return {"error": "Could not understand audio"}
44
+ except sr.RequestError as e:
45
+ return {"error": f"Request error: {e}"}
46
  except Exception as e:
47
  return {"error": str(e)}
48
 
49
+
50
  # Gradio App
51
  with gr.Blocks() as app:
52
  gr.Markdown("## πŸ—£οΈ Hindi Reading & Pronunciation Practice App")