lep1 commited on
Commit
85afbda
·
verified ·
1 Parent(s): aedb0d3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -12
app.py CHANGED
@@ -105,27 +105,29 @@ try:
105
  except Exception as ex:
106
  st.write("Please try again with images with types of JPG, JPEG, PNG ...")
107
 
 
 
 
 
108
  def text_to_speech_gtts(text, lang='en', filename="output.mp3"):
109
  """将文本转换为语音并保存音频文件"""
 
110
  tts = gTTS(text=text, lang=lang)
111
- audio_path = "./output_audio/output.mp3"
112
  tts.save(audio_path)
113
  return audio_path
114
 
115
  try:
116
  # 生成语音文件
117
  audio_file_path = text_to_speech_gtts(result)
118
-
119
  # 在 Streamlit 中播放音频
120
  st.audio(audio_file_path, format="audio/mp3")
121
- except Exception as ex:
122
- st.write("An error occurred while processing the audio.")
123
-
124
  # 提供下载按钮
125
- with open(audio_file_path, "rb") as audio_file:
126
- st.download_button(
127
- label="Download Braille Audio",
128
- data=audio_file,
129
- file_name="detected_braille.mp3",
130
- mime="audio/mp3",
131
- )
 
 
 
105
  except Exception as ex:
106
  st.write("Please try again with images with types of JPG, JPEG, PNG ...")
107
 
108
+ # 创建保存音频的文件夹
109
+ AUDIO_OUTPUT_FOLDER = "./output_audio"
110
+ os.makedirs(AUDIO_OUTPUT_FOLDER, exist_ok=True)
111
+
112
  def text_to_speech_gtts(text, lang='en', filename="output.mp3"):
113
  """将文本转换为语音并保存音频文件"""
114
+ audio_path = os.path.join(AUDIO_OUTPUT_FOLDER, filename)
115
  tts = gTTS(text=text, lang=lang)
 
116
  tts.save(audio_path)
117
  return audio_path
118
 
119
  try:
120
  # 生成语音文件
121
  audio_file_path = text_to_speech_gtts(result)
 
122
  # 在 Streamlit 中播放音频
123
  st.audio(audio_file_path, format="audio/mp3")
 
 
 
124
  # 提供下载按钮
125
+ with open(audio_file_path, "rb") as audio_file:
126
+ st.download_button(
127
+ label="Download Braille Audio",
128
+ data=audio_file,
129
+ file_name="detected_braille.mp3",
130
+ mime="audio/mp3",
131
+ )
132
+ except Exception as ex:
133
+ st.write("An error occurred while processing the audio.")