Spaces:
Paused
Paused
Update app.py
Browse files780 unmodified lines781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826780 unmodified lines# PDF Processing Handlersdef handle_pdf_process(pdf, fmt, ctx_size):"""Process PDF and update UI state"""if not pdf:return ("Please upload a PDF file.", # progress_status"", # processed_text"", # pdf_content[], # snippetsgr.update(choices=[], value=None), # snippet_selectorNone # download_files)try:# Extract and format texttext = extract_text_from_pdf(pdf.name)if text.startswith("Error"):return (text,"","",[],gr.update(choices=[], value=None),None)formatted_text = format_content(text, fmt)snippets_list = split_into_snippets(formatted_text, ctx_size)# Create downloadable full textwith tempfile.NamedTemporaryFile(delete=False, mode='w', suffix='.txt') as f:f.write(formatted_text)download_file = f.namereturn (f"PDF processed successfully! Generated {len(snippets_list)} snippets.",formatted_text,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])except Exception as e:780 unmodified lines781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812780 unmodified lines# PDF Processing Handlersdef handle_pdf_process(pdf, fmt, ctx_size):"""Process PDF, format text, and return formatted text and snippets."""if not pdf:return "Please upload a PDF file.", "", "", [], gr.update(choices=[], value=None), Nonetry:text = extract_text_from_pdf(pdf.name)if text.startswith("Error"):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)with tempfile.NamedTemporaryFile(delete=False, mode='w', suffix=f'.{fmt}') as f: # Correct suffixf.write(formatted_text) # Write the *formatted* textdownload_file = f.namesnippet_choices = update_snippet_choices(snippets_list) # Pre-calculate choicesreturn (f"PDF processed successfully! Generated {len(snippets_list)} snippets.",formatted_text, # Return the *formatted* textformatted_text, # Update the state with formatted textsnippets_list,gr.update(choices=snippet_choices, value=snippet_choices[0] if snippet_choices else None),download_file)except Exception as e:
app.py+17-31