HusseinBashir commited on
Commit
3f5fedd
·
verified ·
1 Parent(s): 83f39e3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -25
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
- if hasattr(output, "waveform"):
106
- waveform = output.waveform
107
- elif isinstance(output, dict) and "waveform" in output:
108
- waveform = output["waveform"]
109
- elif isinstance(output, (tuple, list)):
110
- waveform = output[0]
111
- else:
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
- if test:
120
- duration_s = 2.0
121
- sample_rate = 22050
122
- t = np.linspace(0, duration_s, int(sample_rate * duration_s), endpoint=False)
123
- freq = 440
124
- waveform = 0.5 * np.sin(2 * math.pi * freq * t).astype(np.float32)
125
- pcm_waveform = (waveform * 32767).astype(np.int16)
126
- buf = io.BytesIO()
127
- scipy.io.wavfile.write(buf, rate=sample_rate, data=pcm_waveform)
128
- buf.seek(0)
129
- return StreamingResponse(buf, media_type="audio/wav")
 
130
  normalized = normalize_text(text)
131
  inputs = tokenizer(normalized, return_tensors="pt").to(device)
132
  with torch.no_grad():
133
  output = model(**inputs)
134
- if hasattr(output, "waveform"):
135
- waveform = output.waveform
136
- elif isinstance(output, dict) and "waveform" in output:
137
- waveform = output["waveform"]
138
- elif isinstance(output, (tuple, list)):
139
- waveform = output[0]
140
- else:
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)