mr2along commited on
Commit
9586c71
1 Parent(s): 91a2ea1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -14
app.py CHANGED
@@ -2,7 +2,7 @@ import speech_recognition as sr
2
  import difflib
3
  import gradio as gr
4
  from gtts import gTTS
5
- import os
6
 
7
  # Step 1: Transcribe the audio file
8
  def transcribe_audio(audio):
@@ -26,16 +26,17 @@ def transcribe_audio(audio):
26
  # Step 2: Create pronunciation audio for incorrect words
27
  def create_pronunciation_audio(word):
28
  tts = gTTS(word)
29
- audio_filename = f"pronunciation_{word}.mp3"
30
- tts.save(audio_filename)
31
- return audio_filename
 
32
 
33
  # Step 3: Compare the transcribed text with the input paragraph
34
  def compare_texts(reference_text, transcribed_text):
35
  word_scores = []
36
  reference_words = reference_text.split()
37
  transcribed_words = transcribed_text.split()
38
- incorrect_words_audio_links = [] # Store audio links for incorrect words
39
 
40
  sm = difflib.SequenceMatcher(None, reference_text, transcribed_text)
41
  similarity_score = round(sm.ratio() * 100, 2)
@@ -57,24 +58,28 @@ def compare_texts(reference_text, transcribed_text):
57
  # Incorrect words in red
58
  html_output += f'<span style="color: red;">{word}</span> '
59
  # Create pronunciation audio for the incorrect word
60
- audio_link = create_pronunciation_audio(word)
61
- incorrect_words_audio_links.append(audio_link)
62
  except IndexError:
63
  html_output += f'<span style="color: red;">{word}</span> ' # Words in reference that were not transcribed
64
 
65
- # Provide audio links for incorrect words
66
- if incorrect_words_audio_links:
67
  html_output += "<br><strong>Pronunciation for Incorrect Words:</strong><br>"
68
- for audio in incorrect_words_audio_links:
69
- html_output += f'<a href="{audio}" target="_blank">Listen</a><br>'
 
 
70
 
71
  return html_output
72
 
73
  # Step 4: Text-to-Speech Function
74
  def text_to_speech(paragraph):
75
  tts = gTTS(paragraph)
76
- tts.save("paragraph.mp3")
77
- return "paragraph.mp3"
 
 
78
 
79
  # Gradio Interface Function
80
  def gradio_function(paragraph, audio):
@@ -103,7 +108,7 @@ interface = gr.Interface(
103
  tts_interface = gr.Interface(
104
  fn=text_to_speech,
105
  inputs=gr.Textbox(lines=5, label="Input Paragraph to Read Aloud"),
106
- outputs=gr.Audio(label="Text-to-Speech Output", type="filepath"),
107
  title="Text-to-Speech",
108
  description="This tool will read your input paragraph aloud."
109
  )
 
2
  import difflib
3
  import gradio as gr
4
  from gtts import gTTS
5
+ import io
6
 
7
  # Step 1: Transcribe the audio file
8
  def transcribe_audio(audio):
 
26
  # Step 2: Create pronunciation audio for incorrect words
27
  def create_pronunciation_audio(word):
28
  tts = gTTS(word)
29
+ audio_buffer = io.BytesIO()
30
+ tts.save(audio_buffer)
31
+ audio_buffer.seek(0)
32
+ return audio_buffer
33
 
34
  # Step 3: Compare the transcribed text with the input paragraph
35
  def compare_texts(reference_text, transcribed_text):
36
  word_scores = []
37
  reference_words = reference_text.split()
38
  transcribed_words = transcribed_text.split()
39
+ incorrect_words_audios = [] # Store audio buffers for incorrect words
40
 
41
  sm = difflib.SequenceMatcher(None, reference_text, transcribed_text)
42
  similarity_score = round(sm.ratio() * 100, 2)
 
58
  # Incorrect words in red
59
  html_output += f'<span style="color: red;">{word}</span> '
60
  # Create pronunciation audio for the incorrect word
61
+ audio_buffer = create_pronunciation_audio(word)
62
+ incorrect_words_audios.append((word, audio_buffer))
63
  except IndexError:
64
  html_output += f'<span style="color: red;">{word}</span> ' # Words in reference that were not transcribed
65
 
66
+ # Provide audio for incorrect words
67
+ if incorrect_words_audios:
68
  html_output += "<br><strong>Pronunciation for Incorrect Words:</strong><br>"
69
+ for word, audio in incorrect_words_audios:
70
+ html_output += f'{word}: '
71
+ # Return the audio buffer as part of the HTML output
72
+ html_output += f'<audio controls><source src="data:audio/mp3;base64,{audio.getvalue().decode()}" type="audio/mpeg">Your browser does not support the audio tag.</audio><br>'
73
 
74
  return html_output
75
 
76
  # Step 4: Text-to-Speech Function
77
  def text_to_speech(paragraph):
78
  tts = gTTS(paragraph)
79
+ audio_buffer = io.BytesIO()
80
+ tts.save(audio_buffer)
81
+ audio_buffer.seek(0)
82
+ return audio_buffer
83
 
84
  # Gradio Interface Function
85
  def gradio_function(paragraph, audio):
 
108
  tts_interface = gr.Interface(
109
  fn=text_to_speech,
110
  inputs=gr.Textbox(lines=5, label="Input Paragraph to Read Aloud"),
111
+ outputs=gr.Audio(label="Text-to-Speech Output"),
112
  title="Text-to-Speech",
113
  description="This tool will read your input paragraph aloud."
114
  )