mr2along commited on
Commit
3b3baf1
·
verified ·
1 Parent(s): ee9137a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -14
app.py CHANGED
@@ -1,12 +1,11 @@
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,7 +48,6 @@ def transcribe_audio(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,8 +62,6 @@ def compare_texts(reference_text, transcribed_text):
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> # Tạo output HTML với các mức đánh giá chi tiết hơn
69
  html_output = f"<strong>Fidelity Class:</strong> "
70
  if similarity_score >= 85:
71
  html_output += f"<strong>GOOD (>=85%)</strong><br>"
@@ -76,9 +72,7 @@ def compare_texts(reference_text, transcribed_text):
76
  else:
77
  html_output += f"<strong>POOR (<50%)</strong><br>"
78
 
79
- html_output += f"<strong>Quality Score:</strong> {similarity_score}%<br>"
80
- html_output += f"<strong>Transcribed Text:</strong> {transcribed_text}<br>"
81
- html_output += f"<strong>Quality Score:</strong> {similarity_score}<br>"
82
  html_output += f"<strong>Transcribed Text:</strong> {transcribed_text}<br>"
83
  html_output += "<strong>Word Score List:</strong><br>"
84
 
@@ -94,15 +88,15 @@ html_output += f"<strong>Transcribed Text:</strong> {transcribed_text}<br>"
94
  html_output += f'<span style="color: red;">{word}</span> '
95
  # Create pronunciation audio for the incorrect word
96
  audio_file_path = create_pronunciation_audio(word)
97
- #incorrect_words_audios.append((word, audio_file_path))
98
- incorrect_words_audios.append( audio_file_path)
99
  except IndexError:
100
  html_output += f'<span style="color: red;">{word}</span> ' # Words in reference that were not transcribed
101
 
102
  # Provide audio for incorrect words
103
  if incorrect_words_audios:
104
  html_output += "<br><strong>Pronunciation for Incorrect Words:</strong><br>"
105
-
 
106
 
107
  return [html_output, incorrect_words_audios]
108
 
@@ -120,12 +114,13 @@ def text_to_speech(paragraph):
120
  def gradio_function(paragraph, audio):
121
  # Transcribe the audio
122
  transcribed_text = transcribe_audio(audio)
 
123
  # Compare the original paragraph with the transcribed text
124
  comparison_result = compare_texts(paragraph, transcribed_text)
125
 
126
  # Return comparison result
127
  return comparison_result
128
-
129
  # Gradio Interface using the updated API
130
  interface = gr.Interface(
131
  fn=gradio_function,
@@ -133,7 +128,7 @@ interface = gr.Interface(
133
  gr.Textbox(lines=5, label="Input Paragraph"),
134
  gr.Audio(type="filepath", label="Record Audio")
135
  ],
136
- outputs=["html","files"],
137
  title="Speech Recognition Comparison",
138
  description="Input a paragraph, record your audio, and compare the transcription to the original text."
139
  )
 
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
  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
  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>"
 
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
 
 
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
+ for audio_path in incorrect_words_audios:
99
+ html_output += f'<audio controls><source src="{audio_path}" type="audio/mpeg">Your browser does not support the audio tag.</audio><br>'
100
 
101
  return [html_output, incorrect_words_audios]
102
 
 
114
  def gradio_function(paragraph, audio):
115
  # Transcribe the audio
116
  transcribed_text = transcribe_audio(audio)
117
+
118
  # Compare the original paragraph with the transcribed text
119
  comparison_result = compare_texts(paragraph, transcribed_text)
120
 
121
  # Return comparison result
122
  return comparison_result
123
+
124
  # Gradio Interface using the updated API
125
  interface = gr.Interface(
126
  fn=gradio_function,
 
128
  gr.Textbox(lines=5, label="Input Paragraph"),
129
  gr.Audio(type="filepath", label="Record Audio")
130
  ],
131
+ outputs=["html", "files"],
132
  title="Speech Recognition Comparison",
133
  description="Input a paragraph, record your audio, and compare the transcription to the original text."
134
  )