XSify commited on
Commit
1c0a758
·
verified ·
1 Parent(s): 9a66ea5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -52
app.py CHANGED
@@ -1,52 +1,56 @@
1
- import os
2
- import torch
3
- import numpy as np
4
- from fastapi import FastAPI, UploadFile, Form
5
- from fastapi.responses import FileResponse
6
- from TTS.api import TTS
7
- import tempfile
8
- import soundfile as sf
9
-
10
- # Forzar consentimiento de licencia
11
- os.environ["COQUI_TOS_AGREED"] = "1"
12
-
13
- # Monkey patch temporal de torch.load
14
- original_torch_load = torch.load
15
-
16
- def patched_torch_load(f, *args, **kwargs):
17
- kwargs["weights_only"] = False
18
- return original_torch_load(f, *args, **kwargs)
19
-
20
- torch.load = patched_torch_load
21
-
22
- # Cargar modelo XTTS
23
- tts = TTS(model_name="tts_models/multilingual/multi-dataset/xtts_v2")
24
-
25
- app = FastAPI()
26
-
27
- @app.post("/generate-audio/")
28
- async def generate_audio(
29
- text: str = Form(...),
30
- language: str = Form(...),
31
- speaker_wav: UploadFile = Form(...)
32
- ):
33
- # Guardar archivo temporalmente
34
- with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp:
35
- contents = await speaker_wav.read()
36
- tmp.write(contents)
37
- tmp_path = tmp.name
38
-
39
- # Generar audio
40
- audio = tts.tts(
41
- text=text,
42
- speaker_wav=tmp_path,
43
- language=language,
44
- split_sentences=True,
45
- emotion="Angry"
46
- )
47
-
48
- # Guardar output
49
- out_path = tempfile.mktemp(suffix=".wav")
50
- sf.write(out_path, audio, 24000)
51
-
52
- return FileResponse(out_path, media_type="audio/wav", filename="output.wav")
 
 
 
 
 
1
+ import os
2
+ import torch
3
+ import numpy as np
4
+ from fastapi import FastAPI, UploadFile, Form
5
+ from fastapi.responses import FileResponse
6
+ from TTS.api import TTS
7
+ import tempfile
8
+ import soundfile as sf
9
+
10
+ # Forzar consentimiento de licencia
11
+ os.environ["COQUI_TOS_AGREED"] = "1"
12
+
13
+ # Monkey patch temporal de torch.load
14
+ original_torch_load = torch.load
15
+
16
+ def patched_torch_load(f, *args, **kwargs):
17
+ kwargs["weights_only"] = False
18
+ return original_torch_load(f, *args, **kwargs)
19
+
20
+ torch.load = patched_torch_load
21
+
22
+ # Cargar modelo XTTS
23
+ tts = TTS(model_name="tts_models/multilingual/multi-dataset/xtts_v2")
24
+
25
+ app = FastAPI()
26
+
27
+ @app.post("/generate-audio/")
28
+ async def generate_audio(
29
+ text: str = Form(...),
30
+ language: str = Form(...),
31
+ speaker_wav: UploadFile = Form(...)
32
+ ):
33
+ print("PRIOR WITH")
34
+ # Guardar archivo temporalmente
35
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp:
36
+ contents = await speaker_wav.read()
37
+ tmp.write(contents)
38
+ tmp_path = tmp.name
39
+
40
+ print("PRIOR AUDIO")
41
+ # Generar audio
42
+ audio = tts.tts(
43
+ text=text,
44
+ speaker_wav=tmp_path,
45
+ language=language,
46
+ split_sentences=True,
47
+ emotion="Angry"
48
+ )
49
+
50
+ print("PRIOR MKTEMP")
51
+ # Guardar output
52
+ out_path = tempfile.mktemp(suffix=".wav")
53
+ sf.write(out_path, audio, 24000)
54
+
55
+ print("PRIOR RETURN")
56
+ return FileResponse(out_path, media_type="audio/wav", filename="output.wav")