Tomtom84 commited on
Commit
ac6dc4e
·
1 Parent(s): d505397
Files changed (1) hide show
  1. app.py +35 -26
app.py CHANGED
@@ -1,7 +1,8 @@
1
- from fastapi import FastAPI, WebSocket, WebSocketDisconnect
 
2
  import outetts
3
  import io
4
-
5
  # Initialize the interface
6
  interface = outetts.Interface(
7
  config=outetts.ModelConfig.auto_config(
@@ -23,29 +24,37 @@ app = FastAPI()
23
  def greet_json():
24
  return {"Hello": "World!"}
25
 
26
- @app.websocket("/ws/tts")
27
- async def websocket_tts(websocket: WebSocket):
28
- await websocket.accept()
 
 
29
  try:
30
- while True:
31
- # Empfange Text-Chunk vom Client
32
- data = await websocket.receive_text()
33
- # Generiere Audio aus Text
34
- output = interface.generate(
35
- config=outetts.GenerationConfig(
36
- text=data,
37
- generation_type=outetts.GenerationType.CHUNKED,
38
- speaker=speaker,
39
- sampler_config=outetts.SamplerConfig(
40
- temperature=0.4
41
- ),
42
- )
 
43
  )
44
- # Schreibe Audio in BytesIO
45
- audio_buffer = io.BytesIO()
46
- output.save(audio_buffer)
47
- audio_bytes = audio_buffer.getvalue()
48
- # Sende Audiodaten als Bytes zurück
49
- await websocket.send_bytes(audio_bytes)
50
- except WebSocketDisconnect:
51
- pass
 
 
 
 
 
 
1
+ from fastapi import FastAPI, Request
2
+ from fastapi.responses import StreamingResponse, JSONResponse
3
  import outetts
4
  import io
5
+ import json
6
  # Initialize the interface
7
  interface = outetts.Interface(
8
  config=outetts.ModelConfig.auto_config(
 
24
  def greet_json():
25
  return {"Hello": "World!"}
26
 
27
+ @app.post("/tts")
28
+ async def tts_endpoint(request: Request):
29
+ """
30
+ Accepts JSON {"text": "..."} and streams the generated audio as WAV.
31
+ """
32
  try:
33
+ data = await request.json()
34
+ text = data.get("text")
35
+ if not text:
36
+ return JSONResponse({"error": "Missing 'text' in request"}, status_code=400)
37
+
38
+ # Generate audio from text
39
+ output = interface.generate(
40
+ config=outetts.GenerationConfig(
41
+ text=text,
42
+ generation_type=outetts.GenerationType.CHUNKED,
43
+ speaker=speaker,
44
+ sampler_config=outetts.SamplerConfig(
45
+ temperature=0.4
46
+ ),
47
  )
48
+ )
49
+ audio_buffer = io.BytesIO()
50
+ output.save(audio_buffer)
51
+ audio_buffer.seek(0)
52
+
53
+ def audio_stream():
54
+ yield audio_buffer.read()
55
+
56
+ return StreamingResponse(audio_stream(), media_type="audio/wav")
57
+ except Exception as e:
58
+ return JSONResponse({"error": str(e)}, status_code=500)
59
+
60
+ # WebSocket endpoint removed; use POST /tts for TTS requests.