Spaces:
Paused
Paused
Update app.py
Browse files780 unmodified lines781782783784784785786787788789790791792793794786787788796789790799800801802803804805806807808791792793794795810811812813796797798799815800801802803804818819805806807821822823808809810811812780 unmodified lines # PDF Processing Handlers def handle_pdf_process(pdf, fmt, ctx_size): """Process PDF and update UI state""" """Process PDF, format text, and return formatted text and snippets.""" if not pdf: return ( "Please upload a PDF file.", # progress_status "", # processed_text "", # pdf_content [], # snippets gr.update(choices=[], value=None), # snippet_selector None # download_files ) return "Please upload a PDF file.", "", "", [], gr.update(choices=[], value=None), None
try: # Extract and format text text = extract_text_from_pdf(pdf.name) if text.startswith("Error"): return ( text, "", "", [], gr.update(choices=[], value=None), None ) formatted_text = format_content(text, fmt) return text, "", "", [], gr.update(choices=[], value=None), None
# Format the text *before* splitting into snippets: formatted_text = format_content(text, fmt) # Call format_content here! snippets_list = split_into_snippets(formatted_text, ctx_size) # Create downloadable full text with tempfile.NamedTemporaryFile(delete=False, mode='w', suffix='.txt') as f: f.write(formatted_text)
with tempfile.NamedTemporaryFile(delete=False, mode='w', suffix=f'.{fmt}') as f: # Correct suffix f.write(formatted_text) # Write the *formatted* text download_file = f.name
snippet_choices = update_snippet_choices(snippets_list) # Pre-calculate choices
return ( f"PDF processed successfully! Generated {len(snippets_list)} snippets.", formatted_text, formatted_text, formatted_text, # Return the *formatted* text formatted_text, # Update the state with formatted text snippets_list, gr.update(choices=update_snippet_choices(snippets_list), value="Snippet 1 of " + str(len(snippets_list))), download_file # Return the file for download_full_text #[download_file] gr.update(choices=snippet_choices, value=snippet_choices[0] if snippet_choices else None), download_file ) except Exception as e:app.py+17-31