mr2along commited on
Commit
0d0b31f
1 Parent(s): fab7002

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -9
app.py CHANGED
@@ -1,11 +1,12 @@
1
  import os
 
2
  import speech_recognition as sr
3
  import difflib
4
  import gradio as gr
5
  from gtts import gTTS
6
- import time
7
  from pydub import AudioSegment
8
-
9
  # Create audio directory if it doesn't exist
10
  if not os.path.exists('audio'):
11
  os.makedirs('audio')
@@ -48,6 +49,7 @@ def transcribe_audio(audio):
48
  def create_pronunciation_audio(word):
49
  time.sleep(5) # Chờ 5 giây
50
  tts = gTTS(word)
 
51
  audio_file_path = f"audio/{word}.mp3" # Save the audio to a file
52
  tts.save(audio_file_path)
53
  return audio_file_path # Return the file path of the saved audio
@@ -62,6 +64,7 @@ def compare_texts(reference_text, transcribed_text):
62
  similarity_score = round(sm.ratio() * 100, 2)
63
 
64
  # Construct HTML output
 
65
  html_output = f"<strong>Fidelity Class:</strong> "
66
  if similarity_score >= 85:
67
  html_output += f"<strong>GOOD (>=85%)</strong><br>"
@@ -71,11 +74,12 @@ def compare_texts(reference_text, transcribed_text):
71
  html_output += f"<strong>NEEDS IMPROVEMENT (50% - 70%)</strong><br>"
72
  else:
73
  html_output += f"<strong>POOR (<50%)</strong><br>"
74
-
75
  html_output += f"<strong>Quality Score:</strong> {similarity_score}%<br>"
76
  html_output += f"<strong>Transcribed Text:</strong> {transcribed_text}<br>"
77
  html_output += "<strong>Word Score List:</strong><br>"
78
 
 
79
  # Generate colored word score list
80
  for i, word in enumerate(reference_words):
81
  try:
@@ -88,13 +92,16 @@ def compare_texts(reference_text, transcribed_text):
88
  html_output += f'<span style="color: red;">{word}</span> '
89
  # Create pronunciation audio for the incorrect word
90
  audio_file_path = create_pronunciation_audio(word)
91
- incorrect_words_audios.append(audio_file_path)
 
92
  except IndexError:
93
  html_output += f'<span style="color: red;">{word}</span> ' # Words in reference that were not transcribed
94
 
95
  # Provide audio for incorrect words
96
  if incorrect_words_audios:
97
  html_output += "<br><strong>Pronunciation for Incorrect Words:</strong><br>"
 
 
98
  return [html_output, incorrect_words_audios]
99
 
100
  # Step 4: Text-to-Speech Function
@@ -111,13 +118,12 @@ def text_to_speech(paragraph):
111
  def gradio_function(paragraph, audio):
112
  # Transcribe the audio
113
  transcribed_text = transcribe_audio(audio)
114
-
115
  # Compare the original paragraph with the transcribed text
116
  comparison_result = compare_texts(paragraph, transcribed_text)
117
 
118
  # Return comparison result
119
  return comparison_result
120
-
121
  # Gradio Interface using the updated API
122
  interface = gr.Interface(
123
  fn=gradio_function,
@@ -125,7 +131,7 @@ interface = gr.Interface(
125
  gr.Textbox(lines=5, label="Input Paragraph"),
126
  gr.Audio(type="filepath", label="Record Audio")
127
  ],
128
- outputs=["html", "files"],
129
  title="Speech Recognition Comparison",
130
  description="Input a paragraph, record your audio, and compare the transcription to the original text."
131
  )
@@ -134,7 +140,7 @@ interface = gr.Interface(
134
  tts_interface = gr.Interface(
135
  fn=text_to_speech,
136
  inputs=gr.Textbox(lines=5, label="Input Paragraph to Read Aloud"),
137
- outputs=gr.Microphone(label="Text-to-Speech Output"),
138
  title="Text-to-Speech",
139
  description="This tool will read your input paragraph aloud."
140
  )
@@ -143,4 +149,4 @@ tts_interface = gr.Interface(
143
  demo = gr.TabbedInterface([interface, tts_interface], ["Speech Recognition", "Text-to-Speech"])
144
 
145
  # Launch Gradio app
146
- demo.launch()
 
1
  import os
2
+ import requests
3
  import speech_recognition as sr
4
  import difflib
5
  import gradio as gr
6
  from gtts import gTTS
7
+ import io
8
  from pydub import AudioSegment
9
+ import time
10
  # Create audio directory if it doesn't exist
11
  if not os.path.exists('audio'):
12
  os.makedirs('audio')
 
49
  def create_pronunciation_audio(word):
50
  time.sleep(5) # Chờ 5 giây
51
  tts = gTTS(word)
52
+ main_url="https://mr2along-speech-recognize.hf.space/gradio_api/file="
53
  audio_file_path = f"audio/{word}.mp3" # Save the audio to a file
54
  tts.save(audio_file_path)
55
  return audio_file_path # Return the file path of the saved audio
 
64
  similarity_score = round(sm.ratio() * 100, 2)
65
 
66
  # Construct HTML output
67
+ # html_output = f"<strong>Fidelity Class:</strong> # Tạo output HTML với các mức đánh giá chi tiết hơn
68
  html_output = f"<strong>Fidelity Class:</strong> "
69
  if similarity_score >= 85:
70
  html_output += f"<strong>GOOD (>=85%)</strong><br>"
 
74
  html_output += f"<strong>NEEDS IMPROVEMENT (50% - 70%)</strong><br>"
75
  else:
76
  html_output += f"<strong>POOR (<50%)</strong><br>"
77
+
78
  html_output += f"<strong>Quality Score:</strong> {similarity_score}%<br>"
79
  html_output += f"<strong>Transcribed Text:</strong> {transcribed_text}<br>"
80
  html_output += "<strong>Word Score List:</strong><br>"
81
 
82
+
83
  # Generate colored word score list
84
  for i, word in enumerate(reference_words):
85
  try:
 
92
  html_output += f'<span style="color: red;">{word}</span> '
93
  # Create pronunciation audio for the incorrect word
94
  audio_file_path = create_pronunciation_audio(word)
95
+ #incorrect_words_audios.append((word, audio_file_path))
96
+ incorrect_words_audios.append( audio_file_path)
97
  except IndexError:
98
  html_output += f'<span style="color: red;">{word}</span> ' # Words in reference that were not transcribed
99
 
100
  # Provide audio for incorrect words
101
  if incorrect_words_audios:
102
  html_output += "<br><strong>Pronunciation for Incorrect Words:</strong><br>"
103
+
104
+
105
  return [html_output, incorrect_words_audios]
106
 
107
  # Step 4: Text-to-Speech Function
 
118
  def gradio_function(paragraph, audio):
119
  # Transcribe the audio
120
  transcribed_text = transcribe_audio(audio)
 
121
  # Compare the original paragraph with the transcribed text
122
  comparison_result = compare_texts(paragraph, transcribed_text)
123
 
124
  # Return comparison result
125
  return comparison_result
126
+
127
  # Gradio Interface using the updated API
128
  interface = gr.Interface(
129
  fn=gradio_function,
 
131
  gr.Textbox(lines=5, label="Input Paragraph"),
132
  gr.Audio(type="filepath", label="Record Audio")
133
  ],
134
+ outputs=["html","files"],
135
  title="Speech Recognition Comparison",
136
  description="Input a paragraph, record your audio, and compare the transcription to the original text."
137
  )
 
140
  tts_interface = gr.Interface(
141
  fn=text_to_speech,
142
  inputs=gr.Textbox(lines=5, label="Input Paragraph to Read Aloud"),
143
+ outputs=gr.Audio(label="Text-to-Speech Output"),
144
  title="Text-to-Speech",
145
  description="This tool will read your input paragraph aloud."
146
  )
 
149
  demo = gr.TabbedInterface([interface, tts_interface], ["Speech Recognition", "Text-to-Speech"])
150
 
151
  # Launch Gradio app
152
+ demo.launch()