slliac commited on
Commit
8da5beb
·
verified ·
1 Parent(s): fc8a45b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -13
app.py CHANGED
@@ -7,6 +7,7 @@ import edge_tts
7
  import asyncio
8
  import os
9
  import io
 
10
 
11
 
12
  # Initialize session state for storing data
@@ -20,8 +21,8 @@ if 'story_zh' not in st.session_state:
20
  st.session_state.story_zh = None
21
  if 'audio_generated' not in st.session_state:
22
  st.session_state.audio_generated = False
23
- if 'audio_data' not in st.session_state:
24
- st.session_state.audio_data = None
25
 
26
 
27
  # function part
@@ -65,21 +66,23 @@ async def text2audio_cantonese(text):
65
  voice = "zh-HK-HiuMaanNeural" # Female Cantonese voice
66
  # Alternative: "zh-HK-WanLungNeural" for male voice
67
 
 
 
 
 
 
68
  communicate = edge_tts.Communicate(text, voice)
 
69
 
70
- # Save to BytesIO object
71
- audio_bytes = io.BytesIO()
72
- await communicate.save(audio_bytes)
73
- audio_bytes.seek(0) # Reset the pointer to the start
74
-
75
  return {
76
- 'audio': audio_bytes,
77
  'success': True
78
  }
79
  except Exception as e:
80
  st.error(f"音頻製作出左問題: {str(e)}")
81
  return {
82
- 'audio': None,
83
  'success': False
84
  }
85
 
@@ -350,12 +353,14 @@ if uploaded_file is not None:
350
  if not st.session_state.audio_generated:
351
  # Need to run async function with asyncio
352
  audio_result = asyncio.run(text2audio_cantonese(st.session_state.story_zh))
353
- st.session_state.audio_data = audio_result['audio']
354
  st.session_state.audio_generated = audio_result['success']
355
 
356
  # Play the audio
357
- if st.session_state.audio_data:
358
- st.audio(st.session_state.audio_data, format="audio/mp3")
 
 
359
  else:
360
  st.error("哎呀!再試多次啦!")
361
 
@@ -364,12 +369,19 @@ if uploaded_file is not None:
364
  os.remove(temp_file_path)
365
  else:
366
  # Clear session state when no file is uploaded
 
 
 
 
 
 
 
367
  st.session_state.scenario = None
368
  st.session_state.scenario_zh = None
369
  st.session_state.story = None
370
  st.session_state.story_zh = None
371
  st.session_state.audio_generated = False
372
- st.session_state.audio_data = None
373
 
374
  # Welcome message in Cantonese
375
  st.markdown("""
 
7
  import asyncio
8
  import os
9
  import io
10
+ import tempfile
11
 
12
 
13
  # Initialize session state for storing data
 
21
  st.session_state.story_zh = None
22
  if 'audio_generated' not in st.session_state:
23
  st.session_state.audio_generated = False
24
+ if 'audio_path' not in st.session_state:
25
+ st.session_state.audio_path = None
26
 
27
 
28
  # function part
 
66
  voice = "zh-HK-HiuMaanNeural" # Female Cantonese voice
67
  # Alternative: "zh-HK-WanLungNeural" for male voice
68
 
69
+ # Create a temporary file
70
+ temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
71
+ temp_file.close()
72
+
73
+ # Configure edge-tts to save to the file path
74
  communicate = edge_tts.Communicate(text, voice)
75
+ await communicate.save(temp_file.name)
76
 
77
+ # Return the path to the audio file
 
 
 
 
78
  return {
79
+ 'path': temp_file.name,
80
  'success': True
81
  }
82
  except Exception as e:
83
  st.error(f"音頻製作出左問題: {str(e)}")
84
  return {
85
+ 'path': None,
86
  'success': False
87
  }
88
 
 
353
  if not st.session_state.audio_generated:
354
  # Need to run async function with asyncio
355
  audio_result = asyncio.run(text2audio_cantonese(st.session_state.story_zh))
356
+ st.session_state.audio_path = audio_result['path']
357
  st.session_state.audio_generated = audio_result['success']
358
 
359
  # Play the audio
360
+ if st.session_state.audio_path and os.path.exists(st.session_state.audio_path):
361
+ with open(st.session_state.audio_path, "rb") as audio_file:
362
+ audio_bytes = audio_file.read()
363
+ st.audio(audio_bytes, format="audio/mp3")
364
  else:
365
  st.error("哎呀!再試多次啦!")
366
 
 
369
  os.remove(temp_file_path)
370
  else:
371
  # Clear session state when no file is uploaded
372
+ # Also clean up any temporary audio files
373
+ if st.session_state.audio_path and os.path.exists(st.session_state.audio_path):
374
+ try:
375
+ os.remove(st.session_state.audio_path)
376
+ except:
377
+ pass
378
+
379
  st.session_state.scenario = None
380
  st.session_state.scenario_zh = None
381
  st.session_state.story = None
382
  st.session_state.story_zh = None
383
  st.session_state.audio_generated = False
384
+ st.session_state.audio_path = None
385
 
386
  # Welcome message in Cantonese
387
  st.markdown("""