Update app.py via AI Editor
Browse files
app.py
CHANGED
|
@@ -286,85 +286,83 @@ def update_filename(filename):
|
|
| 286 |
Output('download-ppt-button', 'disabled'),
|
| 287 |
Output('log-output', 'children'),
|
| 288 |
Input('generate-button', 'n_clicks'),
|
|
|
|
| 289 |
State('upload-file', 'contents'),
|
| 290 |
State('document-text', 'value'),
|
| 291 |
-
State('session-id', 'data')
|
| 292 |
-
)
|
| 293 |
-
def generate_slides(n_clicks, file_contents, document_text, session_id):
|
| 294 |
-
triggered = callback_context.triggered[0]['prop_id'].split('.')[0]
|
| 295 |
-
if n_clicks is None:
|
| 296 |
-
raise PreventUpdate
|
| 297 |
-
if not session_id:
|
| 298 |
-
raise PreventUpdate
|
| 299 |
-
decoded_file_text = None
|
| 300 |
-
if file_contents:
|
| 301 |
-
content_type, content_string = file_contents.split(',')
|
| 302 |
-
decoded = base64.b64decode(content_string)
|
| 303 |
-
try:
|
| 304 |
-
decoded_file_text = decoded.decode('utf-8')
|
| 305 |
-
except UnicodeDecodeError:
|
| 306 |
-
try:
|
| 307 |
-
decoded_file_text = decoded.decode('latin-1')
|
| 308 |
-
except Exception:
|
| 309 |
-
session = get_or_create_session_data(session_id)
|
| 310 |
-
with session['lock']:
|
| 311 |
-
session['log_messages'].insert(0, f"{time.strftime('%H:%M:%S')} - Could not decode uploaded file as text. Please upload a UTF-8 or Latin-1 encoded text file.")
|
| 312 |
-
log_str = '\n'.join(session['log_messages'][:100])
|
| 313 |
-
return "Could not decode uploaded file as text. Please upload a UTF-8 or Latin-1 encoded text file.", True, log_str
|
| 314 |
-
combined_text = ""
|
| 315 |
-
if decoded_file_text and document_text:
|
| 316 |
-
combined_text = decoded_file_text.strip() + "\n\n" + document_text.strip()
|
| 317 |
-
elif decoded_file_text:
|
| 318 |
-
combined_text = decoded_file_text.strip()
|
| 319 |
-
elif document_text:
|
| 320 |
-
combined_text = document_text.strip()
|
| 321 |
-
else:
|
| 322 |
-
session = get_or_create_session_data(session_id)
|
| 323 |
-
with session['lock']:
|
| 324 |
-
session['log_messages'].insert(0, f"{time.strftime('%H:%M:%S')} - Please upload a file or enter text.")
|
| 325 |
-
log_str = '\n'.join(session['log_messages'][:100])
|
| 326 |
-
return "Please upload a file or enter text.", True, log_str
|
| 327 |
-
|
| 328 |
-
try:
|
| 329 |
-
add_log("Starting slide generation...", session_id)
|
| 330 |
-
slides_markdown = process_document(combined_text, session_id)
|
| 331 |
-
add_log("Slide generation completed.", session_id)
|
| 332 |
-
session = get_or_create_session_data(session_id)
|
| 333 |
-
with session['lock']:
|
| 334 |
-
log_str = '\n'.join(session['log_messages'][:100])
|
| 335 |
-
return slides_markdown, False, log_str
|
| 336 |
-
except Exception as e:
|
| 337 |
-
add_log(f"An error occurred during slide generation: {str(e)}", session_id)
|
| 338 |
-
session = get_or_create_session_data(session_id)
|
| 339 |
-
with session['lock']:
|
| 340 |
-
log_str = '\n'.join(session['log_messages'][:100])
|
| 341 |
-
return f"An error occurred: {str(e)}", True, log_str
|
| 342 |
-
|
| 343 |
-
@app.callback(
|
| 344 |
-
Output('log-output', 'children'),
|
| 345 |
-
Output('download-ppt-button', 'disabled', allow_duplicate=True),
|
| 346 |
-
Input('generate-ppt-button', 'n_clicks'),
|
| 347 |
State('slides-content', 'value'),
|
| 348 |
State('session-id', 'data'),
|
| 349 |
prevent_initial_call=True
|
| 350 |
)
|
| 351 |
-
def
|
| 352 |
-
|
|
|
|
| 353 |
raise PreventUpdate
|
| 354 |
-
|
| 355 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 356 |
try:
|
| 357 |
-
add_log("Starting
|
| 358 |
-
|
| 359 |
-
|
| 360 |
-
|
| 361 |
-
|
| 362 |
-
|
|
|
|
| 363 |
except Exception as e:
|
| 364 |
-
|
| 365 |
-
|
| 366 |
-
|
| 367 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 368 |
|
| 369 |
@app.callback(
|
| 370 |
Output("download-pptx", "data"),
|
|
|
|
| 286 |
Output('download-ppt-button', 'disabled'),
|
| 287 |
Output('log-output', 'children'),
|
| 288 |
Input('generate-button', 'n_clicks'),
|
| 289 |
+
Input('generate-ppt-button', 'n_clicks'),
|
| 290 |
State('upload-file', 'contents'),
|
| 291 |
State('document-text', 'value'),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 292 |
State('slides-content', 'value'),
|
| 293 |
State('session-id', 'data'),
|
| 294 |
prevent_initial_call=True
|
| 295 |
)
|
| 296 |
+
def unified_callback(gen_btn, gen_ppt_btn, file_contents, document_text, slides_content, session_id):
|
| 297 |
+
ctx = callback_context
|
| 298 |
+
if not ctx.triggered or not session_id:
|
| 299 |
raise PreventUpdate
|
| 300 |
+
button_id = ctx.triggered[0]['prop_id'].split('.')[0]
|
| 301 |
+
# Slide generation logic
|
| 302 |
+
if button_id == 'generate-button':
|
| 303 |
+
decoded_file_text = None
|
| 304 |
+
if file_contents:
|
| 305 |
+
content_type, content_string = file_contents.split(',')
|
| 306 |
+
decoded = base64.b64decode(content_string)
|
| 307 |
+
try:
|
| 308 |
+
decoded_file_text = decoded.decode('utf-8')
|
| 309 |
+
except UnicodeDecodeError:
|
| 310 |
+
try:
|
| 311 |
+
decoded_file_text = decoded.decode('latin-1')
|
| 312 |
+
except Exception:
|
| 313 |
+
session = get_or_create_session_data(session_id)
|
| 314 |
+
with session['lock']:
|
| 315 |
+
session['log_messages'].insert(0, f"{time.strftime('%H:%M:%S')} - Could not decode uploaded file as text. Please upload a UTF-8 or Latin-1 encoded text file.")
|
| 316 |
+
log_str = '\n'.join(session['log_messages'][:100])
|
| 317 |
+
return "Could not decode uploaded file as text. Please upload a UTF-8 or Latin-1 encoded text file.", True, log_str
|
| 318 |
+
combined_text = ""
|
| 319 |
+
if decoded_file_text and document_text:
|
| 320 |
+
combined_text = decoded_file_text.strip() + "\n\n" + document_text.strip()
|
| 321 |
+
elif decoded_file_text:
|
| 322 |
+
combined_text = decoded_file_text.strip()
|
| 323 |
+
elif document_text:
|
| 324 |
+
combined_text = document_text.strip()
|
| 325 |
+
else:
|
| 326 |
+
session = get_or_create_session_data(session_id)
|
| 327 |
+
with session['lock']:
|
| 328 |
+
session['log_messages'].insert(0, f"{time.strftime('%H:%M:%S')} - Please upload a file or enter text.")
|
| 329 |
+
log_str = '\n'.join(session['log_messages'][:100])
|
| 330 |
+
return "Please upload a file or enter text.", True, log_str
|
| 331 |
+
|
| 332 |
try:
|
| 333 |
+
add_log("Starting slide generation...", session_id)
|
| 334 |
+
slides_markdown = process_document(combined_text, session_id)
|
| 335 |
+
add_log("Slide generation completed.", session_id)
|
| 336 |
+
session = get_or_create_session_data(session_id)
|
| 337 |
+
with session['lock']:
|
| 338 |
+
log_str = '\n'.join(session['log_messages'][:100])
|
| 339 |
+
return slides_markdown, False, log_str
|
| 340 |
except Exception as e:
|
| 341 |
+
add_log(f"An error occurred during slide generation: {str(e)}", session_id)
|
| 342 |
+
session = get_or_create_session_data(session_id)
|
| 343 |
+
with session['lock']:
|
| 344 |
+
log_str = '\n'.join(session['log_messages'][:100])
|
| 345 |
+
return f"An error occurred: {str(e)}", True, log_str
|
| 346 |
+
# PowerPoint generation logic
|
| 347 |
+
elif button_id == 'generate-ppt-button':
|
| 348 |
+
if gen_ppt_btn is None or not slides_content or not session_id:
|
| 349 |
+
raise PreventUpdate
|
| 350 |
+
session = get_or_create_session_data(session_id)
|
| 351 |
+
with session['lock']:
|
| 352 |
+
try:
|
| 353 |
+
add_log("Starting PowerPoint generation...", session_id)
|
| 354 |
+
pptx_bytes = markdown_to_pptx(slides_content, session_id)
|
| 355 |
+
session['generated_pptx'] = pptx_bytes
|
| 356 |
+
add_log("PowerPoint generation completed.", session_id)
|
| 357 |
+
log_str = '\n'.join(session['log_messages'][:100])
|
| 358 |
+
return slides_content, False, log_str
|
| 359 |
+
except Exception as e:
|
| 360 |
+
error_message = f"An error occurred during PowerPoint generation: {str(e)}"
|
| 361 |
+
add_log(error_message, session_id)
|
| 362 |
+
log_str = '\n'.join(session['log_messages'][:100])
|
| 363 |
+
return slides_content, True, log_str
|
| 364 |
+
else:
|
| 365 |
+
raise PreventUpdate
|
| 366 |
|
| 367 |
@app.callback(
|
| 368 |
Output("download-pptx", "data"),
|