Spaces:
Paused
Paused
Update app.py
Browse files6 unmodified lines78910111213188 unmodified lines20220320420520620720820921021121221321421521621721842 unmodified lines2612622632642652662672682692702712722732744 unmodified lines279280281282283284285286287288289290338 unmodified lines629630631632633634635235 unmodified lines8718728738748758768778788798808818828838848856 unmodified linesfrom PyPDF2 import PdfReaderimport fitz # pymupdffrom pdf2md.converter import PDF2Markdownimport loggingimport webbrowser188 unmodified lineslogging.error(f"Error in txt conversion: {e}")return f"Error: {str(e)}"@staticmethoddef md_convert_with_pdf2md(pdf_path: str) -> str:"""Convert PDF to Markdown using pdf2md"""try:converter = PDF2Markdown()markdown_text = converter.convert(pdf_path)return markdown_textexcept Exception as e:logging.error(f"Error in pdf2md conversion: {e}")return f"Error: {str(e)}"@staticmethoddef md_convert_with_pymupdf(pdf_path: str) -> str:"""Convert PDF to Markdown using pymupdf"""42 unmodified lines# Initialize model registrymodel_registry = ModelRegistry()def extract_text_from_pdf(pdf_path: str, format_type: str = "txt", md_engine: str = "pdf2md") -> str:"""Extract and format text from PDF using different processors based on format.Args:pdf_path: Path to PDF fileformat_type: Either 'txt' or 'md'md_engine: When format_type is 'md', either 'pdf2md' or 'pymupdf'Returns:Formatted text content4 unmodified linesif format_type == "txt":return processor.txt_convert(pdf_path)elif format_type == "md":if md_engine == "pdf2md":return processor.md_convert_with_pdf2md(pdf_path)elif md_engine == "pymupdf":return processor.md_convert_with_pymupdf(pdf_path)else:return f"Error: Unsupported markdown engine: {md_engine}"else:return f"Error: Unsupported format type: {format_type}"except Exception as e:338 unmodified lines)format_type = gr.Radio(choices=["txt", "md (pdf2md)", "md (pymupdf)"],value="txt",label="๐ Output Format")235 unmodified lines]# PDF Processing Handlersdef handle_pdf_process(pdf, fmt, md_eng, ctx_size):if not pdf:return "Please upload a PDF file.", "", "", [], gr.update(choices=[], value=None), Nonetry:text = extract_text_from_pdf(pdf.name, format_type=fmt, md_engine=md_eng)if text.startswith("Error"):return text, "", "", [], gr.update(choices=[], value=None), Nonesnippets_list = split_into_snippets(text, ctx_size)snippet_choices = update_snippet_choices(snippets_list)6 unmodified lines789101112188 unmodified lines20120220320420520642 unmodified lines2492502512522532542552562572582592602614 unmodified lines266267268269270271272338 unmodified lines611612613614615616617235 unmodified lines8538548558568578588598608618628638648658668678686 unmodified linesfrom PyPDF2 import PdfReaderimport fitz # pymupdfimport loggingimport webbrowser188 unmodified lineslogging.error(f"Error in txt conversion: {e}")return f"Error: {str(e)}"@staticmethoddef md_convert_with_pymupdf(pdf_path: str) -> str:"""Convert PDF to Markdown using pymupdf"""42 unmodified lines# Initialize model registrymodel_registry = ModelRegistry()def extract_text_from_pdf(pdf_path: str, format_type: str = "txt") -> str:"""Extract and format text from PDF using different processors based on format.Args:pdf_path: Path to PDF fileformat_type: Either 'txt' or 'md'Returns:Formatted text content4 unmodified linesif format_type == "txt":return processor.txt_convert(pdf_path)elif format_type == "md":return processor.md_convert_with_pymupdf(pdf_path)else:return f"Error: Unsupported format type: {format_type}"except Exception as e:338 unmodified lines)format_type = gr.Radio(choices=["txt", "md"],value="txt",label="๐ Output Format")235 unmodified lines]# PDF Processing Handlersdef handle_pdf_process(pdf, fmt, ctx_size): # Remove md_eng parameterif not pdf:return "Please upload a PDF file.", "", "", [], gr.update(choices=[], value=None), Nonetry:text = extract_text_from_pdf(pdf.name, format_type=fmt) # Just use format_typeif text.startswith("Error"):return text, "", "", [], gr.update(choices=[], value=None), None# The important part: still do snippets processingsnippets_list = split_into_snippets(text, ctx_size)snippet_choices = update_snippet_choices(snippets_list)
app.py+6-23