hivecorp commited on
Commit
7d70e82
·
verified ·
1 Parent(s): adc73b2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -7
app.py CHANGED
@@ -7,13 +7,18 @@ import nltk
7
  import srt
8
  from pydub import AudioSegment, silence
9
  import datetime
 
 
 
 
 
10
 
11
  # 📦 Safe punkt download
12
  nltk_data_path = os.path.join(os.path.expanduser("~"), "nltk_data")
13
  nltk.download("punkt", download_dir=nltk_data_path)
14
  nltk.data.path.append(nltk_data_path)
15
 
16
- # 🔊 Generate audio
17
  async def text_to_speech(text, voice, rate, pitch):
18
  if not text.strip():
19
  return None, None, "Please enter some text."
@@ -97,11 +102,14 @@ async def create_demo():
97
 
98
  return demo
99
 
100
- # Entry point
101
- import nest_asyncio
102
- nest_asyncio.apply()
103
 
104
- demo = asyncio.run(create_demo())
105
- demo.queue()
106
- demo.launch()
 
 
107
 
 
 
 
7
  import srt
8
  from pydub import AudioSegment, silence
9
  import datetime
10
+ import nest_asyncio
11
+ import threading
12
+
13
+ # Enable nested event loop (required for Spaces)
14
+ nest_asyncio.apply()
15
 
16
  # 📦 Safe punkt download
17
  nltk_data_path = os.path.join(os.path.expanduser("~"), "nltk_data")
18
  nltk.download("punkt", download_dir=nltk_data_path)
19
  nltk.data.path.append(nltk_data_path)
20
 
21
+ # 🗣️ Text-to-Speech and SRT generator
22
  async def text_to_speech(text, voice, rate, pitch):
23
  if not text.strip():
24
  return None, None, "Please enter some text."
 
102
 
103
  return demo
104
 
105
+ # Global demo instance (for Hugging Face Spaces compatibility)
106
+ demo = None
 
107
 
108
+ def launch_app():
109
+ global demo
110
+ demo = asyncio.run(create_demo())
111
+ demo.queue()
112
+ demo.launch()
113
 
114
+ # Start the app in a thread (non-blocking, Space can detect `demo`)
115
+ threading.Thread(target=launch_app).start()