Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
|
| 24 |
-
|
|
|
|
|
|
|
| 25 |
transcription = recognizer.recognize_google(audio_data, language="hi-IN")
|
| 26 |
-
end_time = time.time()
|
| 27 |
|
| 28 |
-
#
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
| 31 |
matcher = difflib.SequenceMatcher(None, original_words, transcribed_words)
|
| 32 |
accuracy = round(matcher.ratio() * 100, 2)
|
| 33 |
|
| 34 |
-
|
| 35 |
-
|
| 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")
|