lep1 commited on
Commit
29c70aa
1 Parent(s): 8194cc6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -9
app.py CHANGED
@@ -105,22 +105,24 @@ try:
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(
@@ -129,5 +131,6 @@ try:
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.")
 
105
  except Exception as ex:
106
  st.write("Please try again with images with types of JPG, JPEG, PNG ...")
107
 
108
+ import tempfile
109
+
110
+ def text_to_speech_gtts(text, lang='en'):
111
+ """将文本转换为语音并保存为临时音频文件"""
112
+ # 创建一个临时文件
113
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as temp_audio_file:
114
+ audio_path = temp_audio_file.name
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
+
123
  # 在 Streamlit 中播放音频
124
  st.audio(audio_file_path, format="audio/mp3")
125
+
126
  # 提供下载按钮
127
  with open(audio_file_path, "rb") as audio_file:
128
  st.download_button(
 
131
  file_name="detected_braille.mp3",
132
  mime="audio/mp3",
133
  )
134
+
135
  except Exception as ex:
136
  st.write("An error occurred while processing the audio.")