Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -102,13 +102,13 @@ async def synthesize_post(data: TextIn):
|
|
102 |
inputs = tokenizer(text, return_tensors="pt").to(device)
|
103 |
with torch.no_grad():
|
104 |
output = model(**inputs)
|
105 |
-
|
106 |
-
waveform
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
return {"error": "Waveform not found in model output"}
|
113 |
sample_rate = getattr(model.config, "sampling_rate", 22050)
|
114 |
wav_bytes = waveform_to_wav_bytes(waveform, sample_rate=sample_rate)
|
@@ -116,28 +116,29 @@ async def synthesize_post(data: TextIn):
|
|
116 |
|
117 |
@app.get("/synthesize")
|
118 |
async def synthesize_get(text: str = Query(..., description="Text to synthesize"), test: bool = Query(False)):
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
|
|
130 |
normalized = normalize_text(text)
|
131 |
inputs = tokenizer(normalized, return_tensors="pt").to(device)
|
132 |
with torch.no_grad():
|
133 |
output = model(**inputs)
|
134 |
-
|
135 |
-
waveform
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
return {"error": "Waveform not found in model output"}
|
142 |
sample_rate = getattr(model.config, "sampling_rate", 22050)
|
143 |
wav_bytes = waveform_to_wav_bytes(waveform, sample_rate=sample_rate)
|
|
|
102 |
inputs = tokenizer(text, return_tensors="pt").to(device)
|
103 |
with torch.no_grad():
|
104 |
output = model(**inputs)
|
105 |
+
waveform = (
|
106 |
+
output.waveform if hasattr(output, "waveform") else
|
107 |
+
output["waveform"] if isinstance(output, dict) and "waveform" in output else
|
108 |
+
output[0] if isinstance(output, (tuple, list)) else
|
109 |
+
None
|
110 |
+
)
|
111 |
+
if waveform is None:
|
112 |
return {"error": "Waveform not found in model output"}
|
113 |
sample_rate = getattr(model.config, "sampling_rate", 22050)
|
114 |
wav_bytes = waveform_to_wav_bytes(waveform, sample_rate=sample_rate)
|
|
|
116 |
|
117 |
@app.get("/synthesize")
|
118 |
async def synthesize_get(text: str = Query(..., description="Text to synthesize"), test: bool = Query(False)):
|
119 |
+
if test:
|
120 |
+
paragraphs = text.count("\n") + 1 # Tirinta paragraphs-ka qoraalka
|
121 |
+
duration_s = paragraphs * 6 # 6 ilbiriqsi per paragraph
|
122 |
+
sample_rate = 22050
|
123 |
+
t = np.linspace(0, duration_s, int(sample_rate * duration_s), endpoint=False)
|
124 |
+
freq = 440
|
125 |
+
waveform = 0.5 * np.sin(2 * math.pi * freq * t).astype(np.float32)
|
126 |
+
pcm_waveform = (waveform * 32767).astype(np.int16)
|
127 |
+
buf = io.BytesIO()
|
128 |
+
scipy.io.wavfile.write(buf, rate=sample_rate, data=pcm_waveform)
|
129 |
+
buf.seek(0)
|
130 |
+
return StreamingResponse(buf, media_type="audio/wav")
|
131 |
normalized = normalize_text(text)
|
132 |
inputs = tokenizer(normalized, return_tensors="pt").to(device)
|
133 |
with torch.no_grad():
|
134 |
output = model(**inputs)
|
135 |
+
waveform = (
|
136 |
+
output.waveform if hasattr(output, "waveform") else
|
137 |
+
output["waveform"] if isinstance(output, dict) and "waveform" in output else
|
138 |
+
output[0] if isinstance(output, (tuple, list)) else
|
139 |
+
None
|
140 |
+
)
|
141 |
+
if waveform is None:
|
142 |
return {"error": "Waveform not found in model output"}
|
143 |
sample_rate = getattr(model.config, "sampling_rate", 22050)
|
144 |
wav_bytes = waveform_to_wav_bytes(waveform, sample_rate=sample_rate)
|