gsarti commited on
Commit
92cdaa1
·
1 Parent(s): 2efd326

Fix GPU exec

Browse files
Files changed (1) hide show
  1. app.py +27 -9
app.py CHANGED
@@ -2,6 +2,7 @@ import os
2
  import io
3
  import spaces
4
  import tempfile
 
5
  #import soundfile as sf
6
  import numpy as np
7
  from pydub import AudioSegment
@@ -99,15 +100,32 @@ def clean(output_markdown):
99
 
100
 
101
  @spaces.GPU
102
- async def text_to_speech(output_text, voice, speed, lang):
103
- stream = kokoro.create_stream(
104
- output_text,
105
- voice=voice,
106
- speed=float(speed),
107
- lang=lang
108
- )
109
- async for samples, sample_rate in stream:
110
- yield numpy_to_mp3(samples, sampling_rate=sample_rate)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
111
 
112
 
113
  with gr.Blocks() as demo:
 
2
  import io
3
  import spaces
4
  import tempfile
5
+ import asyncio
6
  #import soundfile as sf
7
  import numpy as np
8
  from pydub import AudioSegment
 
100
 
101
 
102
  @spaces.GPU
103
+ def text_to_speech(output_text, voice, speed, lang):
104
+ async def stream_audio(output_text, voice, speed, lang):
105
+ stream = kokoro.create_stream(
106
+ output_text,
107
+ voice=voice,
108
+ speed=float(speed),
109
+ lang=lang
110
+ )
111
+ async for samples, sample_rate in stream:
112
+ yield numpy_to_mp3(samples, sampling_rate=sample_rate)
113
+
114
+ async def run_stream():
115
+ async for chunk in stream_audio(output_text, voice, speed, lang):
116
+ yield chunk
117
+
118
+ # Create a new event loop for this thread
119
+ loop = asyncio.new_event_loop()
120
+ asyncio.set_event_loop(loop)
121
+
122
+ # Use the new loop to run the async generator
123
+ iterator = run_stream().__aiter__()
124
+ while True:
125
+ try:
126
+ yield loop.run_until_complete(iterator.__anext__())
127
+ except StopAsyncIteration:
128
+ break
129
 
130
 
131
  with gr.Blocks() as demo: