talha77 commited on
Commit
7e1fa40
·
verified ·
1 Parent(s): cff28b6

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -7
app.py CHANGED
@@ -20,13 +20,31 @@ app = FastAPI(
20
  version="1.1.0",
21
  )
22
 
23
- # Load the TTS model once at import time (synchronous).
24
- # This avoids the "event loop already running" issue that occurs
25
- # when calling Auralis.from_pretrained() inside an async startup hook.
26
- tts = TTS().from_pretrained(
27
- "AstraMindAI/xttsv2",
28
- gpt_model="AstraMindAI/xtts2-gpt",
29
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
 
31
 
32
  @app.get("/health")
 
20
  version="1.1.0",
21
  )
22
 
23
+ tts: TTS | None = None
24
+
25
+
26
+ @app.on_event("startup")
27
+ async def load_model() -> None:
28
+ """
29
+ Load the XTTSv2 + GPT model once when the application starts.
30
+
31
+ We create the model inside a worker thread so Auralis can freely
32
+ manage its own event loop without conflicting with FastAPI/uvicorn.
33
+ """
34
+ global tts
35
+
36
+ if tts is not None:
37
+ return
38
+
39
+ loop = asyncio.get_event_loop()
40
+
41
+ def _init_model() -> TTS:
42
+ return TTS().from_pretrained(
43
+ "AstraMindAI/xttsv2",
44
+ gpt_model="AstraMindAI/xtts2-gpt",
45
+ )
46
+
47
+ tts = await loop.run_in_executor(None, _init_model)
48
 
49
 
50
  @app.get("/health")