Tomtom84 commited on
Commit
a9f2d93
·
1 Parent(s): 9b7bbc2
Files changed (2) hide show
  1. __pycache__/app.cpython-312.pyc +0 -0
  2. app.py +39 -35
__pycache__/app.cpython-312.pyc CHANGED
Binary files a/__pycache__/app.cpython-312.pyc and b/__pycache__/app.cpython-312.pyc differ
 
app.py CHANGED
@@ -3,16 +3,14 @@ from fastapi.responses import StreamingResponse, JSONResponse
3
  import outetts
4
  import io
5
  import json
6
- import asyncio
7
- import os
8
-
9
  # Initialize the interface
10
  interface = outetts.Interface(
11
  config=outetts.ModelConfig.auto_config(
12
  model=outetts.Models.VERSION_1_0_SIZE_1B,
13
  # For llama.cpp backend
14
- # backend=outetts.Backend.LLAMACPP,
15
- # quantization=outetts.LlamaCppQuantization.FP16
16
  # For transformers backend
17
  backend=outetts.Backend.HF,
18
  )
@@ -27,40 +25,46 @@ app = FastAPI()
27
  def greet_json():
28
  return {"Hello": "World!"}
29
 
30
- async def process_chunk(text_chunk: str, websocket: WebSocket):
31
- try:
32
- output = interface.generate(
33
- config=outetts.GenerationConfig(
34
- text=text_chunk,
35
- generation_type=outetts.GenerationType.CHUNKED,
36
- speaker=speaker,
37
- sampler_config=outetts.SamplerConfig(
38
- temperature=0.4
39
- ),
40
- )
41
- )
42
- # Save audio to buffer
43
- audio_buffer = io.BytesIO()
44
- output.save(audio_buffer)
45
- audio_buffer.seek(0)
46
- audio_bytes = audio_buffer.read()
47
- # Send audio bytes back
48
- await websocket.send_bytes(audio_bytes)
49
- except Exception as e:
50
- await websocket.send_text(json.dumps({"error": str(e)}))
51
-
52
  @app.websocket("/ws/tts")
53
  async def websocket_tts(websocket: WebSocket):
54
  await websocket.accept()
55
- tasks: set[asyncio.Task] = set()
56
  try:
57
  while True:
 
58
  data = await websocket.receive_text()
59
- # Schedule processing without awaiting
60
- task = asyncio.create_task(process_chunk(data, websocket))
61
- tasks.add(task)
62
- task.add_done_callback(lambda t: tasks.discard(t))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63
  except WebSocketDisconnect:
64
- # Cancel all pending tasks
65
- for task in tasks:
66
- task.cancel()
 
3
  import outetts
4
  import io
5
  import json
6
+ import base64
 
 
7
  # Initialize the interface
8
  interface = outetts.Interface(
9
  config=outetts.ModelConfig.auto_config(
10
  model=outetts.Models.VERSION_1_0_SIZE_1B,
11
  # For llama.cpp backend
12
+ #backend=outetts.Backend.LLAMACPP,
13
+ #quantization=outetts.LlamaCppQuantization.FP16
14
  # For transformers backend
15
  backend=outetts.Backend.HF,
16
  )
 
25
  def greet_json():
26
  return {"Hello": "World!"}
27
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
  @app.websocket("/ws/tts")
29
  async def websocket_tts(websocket: WebSocket):
30
  await websocket.accept()
 
31
  try:
32
  while True:
33
+ # Empfange Text-Chunk vom Client
34
  data = await websocket.receive_text()
35
+ # Status: Warming up
36
+ await websocket.send_text(json.dumps({"generation_status": "Warming up TTS model"}))
37
+ output = interface.generate(
38
+ config=outetts.GenerationConfig(
39
+ text=data,
40
+ generation_type=outetts.GenerationType.CHUNKED,
41
+ speaker=speaker,
42
+ sampler_config=outetts.SamplerConfig(
43
+ temperature=0.4
44
+ ),
45
+ )
46
+ )
47
+ # Status: Generating linguistic features
48
+ await websocket.send_text(json.dumps({"generation_status": "Generating linguistic features"}))
49
+ # Stream audio chunks
50
+ for chunk in output.stream(chunk_size=4096):
51
+ audio_b64 = base64.b64encode(chunk).decode("ascii")
52
+ await websocket.send_text(json.dumps({
53
+ "data": {
54
+ "audio_bytes": audio_b64,
55
+ "duration": None,
56
+ "request_finished": False
57
+ }
58
+ }))
59
+ # Final event
60
+ await websocket.send_text(json.dumps({
61
+ "data": {
62
+ "audio_bytes": "",
63
+ "duration": None,
64
+ "request_finished": True
65
+ }
66
+ }))
67
  except WebSocketDisconnect:
68
+ pass
69
+ except Exception as e:
70
+ await websocket.send_text(json.dumps({"error": str(e)}))