Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -41,7 +41,6 @@ EMOTIVE_TAGS = ["<laugh>", "<chuckle>", "<sigh>", "<cough>", "<sniffle>", "<groa
|
|
41 |
# Initialize Dash app
|
42 |
app = dash.Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP])
|
43 |
|
44 |
-
# Layout
|
45 |
app.layout = dbc.Container([
|
46 |
dbc.Row([
|
47 |
dbc.Col([
|
@@ -80,15 +79,17 @@ app.layout = dbc.Container([
|
|
80 |
className="mb-2"
|
81 |
),
|
82 |
dbc.Button("Generate Podcast Script", id="generate-script-btn", color="primary", className="mb-2"),
|
|
|
83 |
], width=6),
|
84 |
dbc.Col([
|
85 |
dbc.Textarea(id="script-output", placeholder="Generated script will appear here...", rows=10, className="mb-2"),
|
86 |
-
dbc.Button("Clear", id="clear-btn", color="secondary", className="mb-2"),
|
87 |
-
html.Label("Voice 1", className="mt-
|
88 |
dcc.Dropdown(id="voice1", options=[{"label": v, "value": v} for v in VOICES], value="tara", className="mb-2"),
|
89 |
html.Label("Voice 2", className="mt-2"),
|
90 |
dcc.Dropdown(id="voice2", options=[{"label": v, "value": v} for v in VOICES], value="zac", className="mb-2"),
|
91 |
dbc.Button("Generate Audio", id="generate-audio-btn", color="success", className="mb-2"),
|
|
|
92 |
html.Div(id="audio-output"),
|
93 |
dbc.Button("Advanced Settings", id="advanced-settings-toggle", color="info", className="mb-2"),
|
94 |
dbc.Collapse([
|
@@ -166,7 +167,9 @@ def generate_audio(script_output, voice1, voice2, num_hosts, temperature, top_p,
|
|
166 |
)
|
167 |
|
168 |
code_list = parse_output(generated_ids)
|
169 |
-
|
|
|
|
|
170 |
|
171 |
silences = detect_silence(paragraph_audio)
|
172 |
if silences:
|
@@ -181,12 +184,14 @@ def generate_audio(script_output, voice1, voice2, num_hosts, temperature, top_p,
|
|
181 |
except Exception as e:
|
182 |
logger.error(f"Error generating speech: {str(e)}")
|
183 |
return None
|
184 |
-
|
185 |
@callback(
|
186 |
Output("script-output", "value"),
|
187 |
Output("audio-output", "children"),
|
188 |
Output("advanced-settings", "is_open"),
|
189 |
Output("prompt", "value"),
|
|
|
|
|
190 |
Input("generate-script-btn", "n_clicks"),
|
191 |
Input("generate-audio-btn", "n_clicks"),
|
192 |
Input("advanced-settings-toggle", "n_clicks"),
|
@@ -214,7 +219,7 @@ def combined_callback(generate_script_clicks, generate_audio_clicks, advanced_se
|
|
214 |
script_output, voice1, voice2, temperature, top_p, repetition_penalty, max_new_tokens, is_advanced_open):
|
215 |
ctx = dash.callback_context
|
216 |
if not ctx.triggered:
|
217 |
-
return dash.no_update, dash.no_update, dash.no_update, dash.no_update
|
218 |
|
219 |
trigger_id = ctx.triggered[0]['prop_id'].split('.')[0]
|
220 |
|
@@ -286,14 +291,14 @@ def combined_callback(generate_script_clicks, generate_audio_clicks, advanced_se
|
|
286 |
"""
|
287 |
|
288 |
response = model.generate_content(prompt_template)
|
289 |
-
return re.sub(r'[^a-zA-Z0-9\s.,?!<>]', '', response.text), dash.no_update, dash.no_update, dash.no_update
|
290 |
except Exception as e:
|
291 |
logger.error(f"Error generating podcast script: {str(e)}")
|
292 |
-
return f"Error: {str(e)}", dash.no_update, dash.no_update, dash.no_update
|
293 |
|
294 |
elif trigger_id == "generate-audio-btn":
|
295 |
if not script_output.strip():
|
296 |
-
return dash.no_update, html.Div("No audio generated yet."), dash.no_update, dash.no_update
|
297 |
|
298 |
final_audio = generate_audio(script_output, voice1, voice2, num_hosts, temperature, top_p, repetition_penalty, max_new_tokens)
|
299 |
|
@@ -309,17 +314,17 @@ def combined_callback(generate_script_clicks, generate_audio_clicks, advanced_se
|
|
309 |
html.Audio(src=src, controls=True),
|
310 |
html.Br(),
|
311 |
download_link
|
312 |
-
]), dash.no_update, dash.no_update
|
313 |
else:
|
314 |
-
return dash.no_update, html.Div("Error generating audio"), dash.no_update, dash.no_update
|
315 |
|
316 |
elif trigger_id == "advanced-settings-toggle":
|
317 |
-
return dash.no_update, dash.no_update, not is_advanced_open, dash.no_update
|
318 |
|
319 |
elif trigger_id == "clear-btn":
|
320 |
-
return "", html.Div("No audio generated yet."), dash.no_update, ""
|
321 |
|
322 |
-
return dash.no_update, dash.no_update, dash.no_update, dash.no_update
|
323 |
|
324 |
# Run the app
|
325 |
if __name__ == '__main__':
|
|
|
41 |
# Initialize Dash app
|
42 |
app = dash.Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP])
|
43 |
|
|
|
44 |
app.layout = dbc.Container([
|
45 |
dbc.Row([
|
46 |
dbc.Col([
|
|
|
79 |
className="mb-2"
|
80 |
),
|
81 |
dbc.Button("Generate Podcast Script", id="generate-script-btn", color="primary", className="mb-2"),
|
82 |
+
dbc.Spinner(html.Div(id="script-loading"), color="primary"),
|
83 |
], width=6),
|
84 |
dbc.Col([
|
85 |
dbc.Textarea(id="script-output", placeholder="Generated script will appear here...", rows=10, className="mb-2"),
|
86 |
+
dbc.Button("Clear", id="clear-btn", color="secondary", className="mb-2 d-block"),
|
87 |
+
html.Label("Voice 1", className="mt-3"),
|
88 |
dcc.Dropdown(id="voice1", options=[{"label": v, "value": v} for v in VOICES], value="tara", className="mb-2"),
|
89 |
html.Label("Voice 2", className="mt-2"),
|
90 |
dcc.Dropdown(id="voice2", options=[{"label": v, "value": v} for v in VOICES], value="zac", className="mb-2"),
|
91 |
dbc.Button("Generate Audio", id="generate-audio-btn", color="success", className="mb-2"),
|
92 |
+
dbc.Spinner(html.Div(id="audio-loading"), color="primary"),
|
93 |
html.Div(id="audio-output"),
|
94 |
dbc.Button("Advanced Settings", id="advanced-settings-toggle", color="info", className="mb-2"),
|
95 |
dbc.Collapse([
|
|
|
167 |
)
|
168 |
|
169 |
code_list = parse_output(generated_ids)
|
170 |
+
# Use the correct method for audio generation based on your SNAC model
|
171 |
+
paragraph_audio = snac_model.generate_audio(torch.tensor(code_list).unsqueeze(0).to(device))
|
172 |
+
paragraph_audio = paragraph_audio.cpu().numpy().flatten()
|
173 |
|
174 |
silences = detect_silence(paragraph_audio)
|
175 |
if silences:
|
|
|
184 |
except Exception as e:
|
185 |
logger.error(f"Error generating speech: {str(e)}")
|
186 |
return None
|
187 |
+
|
188 |
@callback(
|
189 |
Output("script-output", "value"),
|
190 |
Output("audio-output", "children"),
|
191 |
Output("advanced-settings", "is_open"),
|
192 |
Output("prompt", "value"),
|
193 |
+
Output("script-loading", "children"),
|
194 |
+
Output("audio-loading", "children"),
|
195 |
Input("generate-script-btn", "n_clicks"),
|
196 |
Input("generate-audio-btn", "n_clicks"),
|
197 |
Input("advanced-settings-toggle", "n_clicks"),
|
|
|
219 |
script_output, voice1, voice2, temperature, top_p, repetition_penalty, max_new_tokens, is_advanced_open):
|
220 |
ctx = dash.callback_context
|
221 |
if not ctx.triggered:
|
222 |
+
return dash.no_update, dash.no_update, dash.no_update, dash.no_update, "", ""
|
223 |
|
224 |
trigger_id = ctx.triggered[0]['prop_id'].split('.')[0]
|
225 |
|
|
|
291 |
"""
|
292 |
|
293 |
response = model.generate_content(prompt_template)
|
294 |
+
return re.sub(r'[^a-zA-Z0-9\s.,?!<>]', '', response.text), dash.no_update, dash.no_update, dash.no_update, "", ""
|
295 |
except Exception as e:
|
296 |
logger.error(f"Error generating podcast script: {str(e)}")
|
297 |
+
return f"Error: {str(e)}", dash.no_update, dash.no_update, dash.no_update, "", ""
|
298 |
|
299 |
elif trigger_id == "generate-audio-btn":
|
300 |
if not script_output.strip():
|
301 |
+
return dash.no_update, html.Div("No audio generated yet."), dash.no_update, dash.no_update, "", ""
|
302 |
|
303 |
final_audio = generate_audio(script_output, voice1, voice2, num_hosts, temperature, top_p, repetition_penalty, max_new_tokens)
|
304 |
|
|
|
314 |
html.Audio(src=src, controls=True),
|
315 |
html.Br(),
|
316 |
download_link
|
317 |
+
]), dash.no_update, dash.no_update, "", ""
|
318 |
else:
|
319 |
+
return dash.no_update, html.Div("Error generating audio"), dash.no_update, dash.no_update, "", ""
|
320 |
|
321 |
elif trigger_id == "advanced-settings-toggle":
|
322 |
+
return dash.no_update, dash.no_update, not is_advanced_open, dash.no_update, "", ""
|
323 |
|
324 |
elif trigger_id == "clear-btn":
|
325 |
+
return "", html.Div("No audio generated yet."), dash.no_update, "", "", ""
|
326 |
|
327 |
+
return dash.no_update, dash.no_update, dash.no_update, dash.no_update, "", ""
|
328 |
|
329 |
# Run the app
|
330 |
if __name__ == '__main__':
|