Spaces:
Paused
Paused
Update app.py
Browse files6 unmodified lines78910101112188 unmodified lines20120220320520620720820921021121221321421520420520642 unmodified lines2492502512642522532542552562572582712592602614 unmodified lines266267268282283284285269286287270271272338 unmodified lines611612613632614615616617235 unmodified lines8538548558748568578588598608798618628638648658668678686 unmodified lines
from PyPDF2 import PdfReaderimport fitz # pymupdffrom pdf2md.converter import PDF2Markdown
import loggingimport webbrowser188 unmodified lines logging.error(f"Error in txt conversion: {e}") return f"Error: {str(e)}"
@staticmethod def 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_text except Exception as e: logging.error(f"Error in pdf2md conversion: {e}") return f"Error: {str(e)}"
@staticmethod def 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: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 file format_type: Either 'txt' or 'md' md_engine: When format_type is 'md', either 'pdf2md' or 'pymupdf' Returns: Formatted text content4 unmodified lines if 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) 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)"], choices=["txt", "md"], value="txt", label="📝 Output Format" )235 unmodified lines ] # PDF Processing Handlers def handle_pdf_process(pdf, fmt, md_eng, ctx_size): def handle_pdf_process(pdf, fmt, ctx_size): # Remove md_eng parameter if not pdf: return "Please upload a PDF file.", "", "", [], gr.update(choices=[], value=None), None
try: text = extract_text_from_pdf(pdf.name, format_type=fmt, md_engine=md_eng) text = extract_text_from_pdf(pdf.name, format_type=fmt) # Just use format_type if text.startswith("Error"): return text, "", "", [], gr.update(choices=[], value=None), None
# The important part: still do snippets processing snippets_list = split_into_snippets(text, ctx_size) snippet_choices = update_snippet_choices(snippets_list)
app.py+6-23