Tomtom84 commited on
Commit
6ee4394
·
verified ·
1 Parent(s): 2c4bdb2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -8
app.py CHANGED
@@ -243,12 +243,15 @@ with gr.Blocks(title="Orpheus Text-to-Speech") as demo:
243
  outputs=[text_input, audio_output]
244
  )
245
 
246
- # Create FastAPI app and mount Gradio
247
- demo = demo.queue()
 
 
248
  app = FastAPI()
249
- app.mount("/", demo)
250
 
251
- # WebSocket TTS endpoint\@app.websocket("/ws/tts")
 
252
  async def websocket_tts(websocket: WebSocket):
253
  await websocket.accept()
254
  try:
@@ -257,10 +260,8 @@ async def websocket_tts(websocket: WebSocket):
257
  data = json.loads(msg)
258
  text = data.get("text", "")
259
  voice = data.get("voice", VOICES[0])
260
- # Generate audio for the chunk
261
  _, audio = generate_speech(text, voice, 0.7, 0.95, 1.1, 1200)
262
- # Stream audio in 0.1s chunks
263
- chunk_size = 2400 # 24000 Hz -> 2400 samples = 0.1s
264
  for i in range(0, len(audio), chunk_size):
265
  chunk = audio[i:i+chunk_size]
266
  await websocket.send_bytes(chunk.tobytes())
@@ -268,7 +269,7 @@ async def websocket_tts(websocket: WebSocket):
268
  except WebSocketDisconnect:
269
  print("Client disconnected from /ws/tts")
270
 
271
- # Launch if run directly
272
  def main():
273
  import uvicorn
274
  uvicorn.run("app:app", host="0.0.0.0", port=7860)
 
243
  outputs=[text_input, audio_output]
244
  )
245
 
246
+ # Enable queuing for Gradio
247
+ queue_demo = demo.queue()
248
+
249
+ # Create FastAPI app and mount Gradio ASGI app
250
  app = FastAPI()
251
+ app.mount("/", queue_demo.app) # mount the ASGI app, not the Blocks object itself
252
 
253
+ # WebSocket TTS endpoint
254
+ @app.websocket("/ws/tts")
255
  async def websocket_tts(websocket: WebSocket):
256
  await websocket.accept()
257
  try:
 
260
  data = json.loads(msg)
261
  text = data.get("text", "")
262
  voice = data.get("voice", VOICES[0])
 
263
  _, audio = generate_speech(text, voice, 0.7, 0.95, 1.1, 1200)
264
+ chunk_size = 2400 # 0.1s at 24kHz
 
265
  for i in range(0, len(audio), chunk_size):
266
  chunk = audio[i:i+chunk_size]
267
  await websocket.send_bytes(chunk.tobytes())
 
269
  except WebSocketDisconnect:
270
  print("Client disconnected from /ws/tts")
271
 
272
+ # Launch when run directly
273
  def main():
274
  import uvicorn
275
  uvicorn.run("app:app", host="0.0.0.0", port=7860)