### CLI Example: Convert PDF with Custom Model and Output Source: https://github.com/leoneversberg/pdf2md_llm/blob/main/README.md An example of using the pdf2md_llm CLI to convert 'example.pdf' to Markdown, specifying a different model and output folder. This demonstrates how to override default settings for model selection and where the output file will be saved. ```bash pdf2md_llm example.pdf --model "Qwen/Qwen2.5-VL-3B-Instruct-AWQ" --output_folder "./output" ``` -------------------------------- ### Install pdf2md_llm Package Source: https://github.com/leoneversberg/pdf2md_llm/blob/main/README.md Installs the pdf2md_llm package using pip. Ensure you have Python and pip installed. A CUDA-compatible GPU is recommended for running local LLMs with vLLM. ```bash pip install pdf2md-llm ``` -------------------------------- ### Python: Initialize LLM Model with llm_model Factory Source: https://context7.com/leoneversberg/pdf2md_llm/llms.txt Create LLM model instances using the `llm_model` factory function. This function supports automatic model type detection and configuration, including specifying model names, data types (e.g., 'half' for AWQ), maximum context length, and sequence processing limits. It also includes error handling for unsupported models. ```python from pdf2md_llm.llm import llm_model # Create model instance with AWQ quantization llm = llm_model( model="Qwen/Qwen2.5-VL-3B-Instruct-AWQ", dtype="half", # Required for AWQ models max_model_len=7000, # Maximum context length max_num_seqs=1 # Process one sequence at a time ) # Alternative: Use larger model for better accuracy llm_large = llm_model( model="Qwen/Qwen2.5-VL-7B-Instruct", dtype="auto", # Automatic dtype selection max_model_len=8192 ) # Unsupported model handling try: unsupported = llm_model(model="gpt-4") except ValueError as e: print(f"Error: {e}") # Output: Error: Unsupported model: gpt-4 ``` -------------------------------- ### Convert PDF to Images and Process with LLM (Python) Source: https://context7.com/leoneversberg/pdf2md_llm/llms.txt This Python script demonstrates the core functionality of pdf2md_llm. It first converts a PDF file into a series of JPEG images using the PdfToImg class. Then, it initializes an LLM model (Qwen/Qwen2.5-VL-3B-Instruct-AWQ) to process each image, converting it into Markdown text. The generated Markdown from all pages is appended to a single output Markdown file. Error handling is included for both PDF conversion and LLM processing steps. ```python from pdf2md_llm.pdf_to_img import PdfToImg from pdf2md_llm.llm_model import llm_model from vllm import SamplingParams import os # Step 1: Convert PDF to images pdf2img = PdfToImg( size=(700, None), dpi=200, fmt="jpeg", output_folder="./out" ) try: img_files = pdf2img.convert("example.pdf") print(f"Converted {len(img_files)} pages to images") except Exception as e: print(f"PDF conversion error: {e}") exit(1) # Step 2: Initialize LLM model try: llm = llm_model( model="Qwen/Qwen2.5-VL-3B-Instruct-AWQ", dtype="half", max_num_seqs=1, max_model_len=7000 ) except Exception as e: print(f"Model loading error: {e}") exit(1) # Step 3: Configure sampling sampling_params = SamplingParams( temperature=0.1, min_p=0.1, max_tokens=8192, stop_token_ids=[] ) # Step 4: Process all pages and combine into single Markdown file base_filename = os.path.splitext(os.path.basename("example.pdf"))[0] output_path = os.path.join(pdf2img.output_folder, f"{base_filename}.md") # Remove existing output file if os.path.exists(output_path): os.remove(output_path) # Generate Markdown for each page for img_file in img_files: try: markdown_text = llm.generate( img_file, prompt="Convert the provided image of a PDF document strictly into valid Markdown.", sampling_params=sampling_params ) # Append to output file with open(output_path, "a", encoding="utf-8") as f: f.write(markdown_text) print(f"Processed: {img_file}") except Exception as e: print(f"Error processing {img_file}: {e}") continue print(f"Conversion complete: {output_path}") ``` -------------------------------- ### Python: Generate Markdown from Image with Qwen25VLModel Source: https://context7.com/leoneversberg/pdf2md_llm/llms.txt Generate Markdown text from image files using the `generate` method of a vision-language model instance. This method takes an image file path, a prompt, and sampling parameters to control the text generation process. It's suitable for converting visual information into structured Markdown. ```python from vllm import SamplingParams from pdf2md_llm.llm import llm_model # Initialize model llm = llm_model( model="Qwen/Qwen2.5-VL-3B-Instruct-AWQ", dtype="half", max_num_seqs=1 ) # Configure sampling parameters for generation sampling_params = SamplingParams( temperature=0.1, # Low temperature for deterministic output min_p=0.1, # Minimum probability threshold max_tokens=8192, # Maximum tokens to generate stop_token_ids=[] # No special stop tokens ) # Generate Markdown from single image markdown_text = llm.generate( file_path="./images/page_0001.jpeg", prompt="Convert this image to Markdown, preserving formatting", sampling_params=sampling_params ) print(markdown_text) # Expected output: # # Document Title # # This is the converted content... # ``` -------------------------------- ### Python: Convert PDF to Images using PdfToImg Source: https://context7.com/leoneversberg/pdf2md_llm/llms.txt Convert PDF files into image files using the PdfToImg class. This class allows for configurable settings such as DPI, image format (e.g., PNG), output size, and output folder. It returns a list of paths to the generated images or raises a ValueError on failure. ```python from pdf2md_llm.pdf2img import PdfToImg # Initialize converter with custom settings pdf2img = PdfToImg( dpi=300, # High resolution for detailed documents fmt="png", # Use PNG format for better quality size=(1000, None), # Width 1000px, height auto-scaled output_folder="./images" ) # Convert PDF to images try: img_paths = pdf2img.convert("report.pdf") print(f"Generated {len(img_paths)} images:") for path in img_paths: print(f" - {path}") except ValueError as e: print(f"Conversion failed: {e}") # Expected output: # Generated 5 images: # - ./images/page_0001.png # - ./images/page_0002.png # - ./images/page_0003.png # - ./images/page_0004.png # - ./images/page_0005.png ``` -------------------------------- ### CLI: Convert PDF to Markdown Source: https://github.com/leoneversberg/pdf2md_llm/blob/main/README.md Converts a specified PDF file to Markdown format using the command-line interface. Options are available to customize the LLM model, image processing, and output location. The default model is Qwen/Qwen2.5-VL-3B-Instruct-AWQ. ```bash pdf2md_llm [options] ``` -------------------------------- ### CLI: Convert PDF to Markdown with pdf2md_llm Source: https://context7.com/leoneversberg/pdf2md_llm/llms.txt Convert PDF files to Markdown via Command Line Interface with customizable model and image processing options. Supports specifying output folders, DPI, max model length, and custom prompts for specialized document types. Handles multiple PDF files and provides status updates during conversion. ```bash # Basic conversion with default settings pdf2md_llm example.pdf # Advanced usage with custom model and output folder pdf2md_llm document.pdf \ --model "Qwen/Qwen2.5-VL-7B-Instruct-AWQ" \ --output_folder "./output" \ --dpi 300 \ --max_model_len 8192 # Custom prompt for specialized document types pdf2md_llm technical_report.pdf \ --model "Qwen/Qwen2.5-VL-3B-Instruct-AWQ" \ --prompt "Convert this technical document to Markdown, preserving all code blocks and formulas" \ --dtype "half" \ --output_folder "./converted" # Expected output: # Converting document.pdf to images ... # Parsing /output/page_0001.jpeg ... # Parsing /output/page_0002.jpeg ... # Markdown file saved to: ./output/document.md ``` -------------------------------- ### Implement Custom Vision Model for vLLM (Python) Source: https://context7.com/leoneversberg/pdf2md_llm/llms.txt This Python code defines a `CustomVisionModel` class that extends the `BaseModel` and integrates with vLLM for custom vision-language model processing. It allows users to specify a custom model name and leverages vLLM's `LLM` class for generation. The `generate` method handles prompt formatting, model inference, and post-processing of the generated text to Markdown. This enables extensibility for different vLLM-compatible vision models. ```python from pdf2md_llm.models.base_model import BaseModel, _post_process_text from vllm import LLM, SamplingParams class CustomVisionModel(BaseModel): """Custom vision-language model implementation""" def __init__(self, model="custom/model-name", **kwargs): super().__init__(model, **kwargs) self.llm = LLM( model=self.model, limit_mm_per_prompt={"image": 1}, **kwargs ) def generate(self, file_path: str, prompt: str = None, sampling_params: SamplingParams = None) -> str: # Implement custom generation logic if prompt is None: prompt = "Convert this PDF page to Markdown" # Model-specific message formatting message = self._format_message(file_path, prompt) # Generate and post-process outputs = self.llm.generate([message], sampling_params=sampling_params) generated_text = outputs[0].outputs[0].text return _post_process_text(generated_text) # Register custom model in llm.py factory function # Modify llm_model() to include: # elif "custom-model" in model.lower(): # return CustomVisionModel(model=model, **kwargs) ``` -------------------------------- ### Python API: Convert PDF to Markdown Source: https://github.com/leoneversberg/pdf2md_llm/blob/main/README.md Converts a PDF file to Markdown using the Python API. This snippet demonstrates initializing the PDF to image converter and the LLM model, then iterating through converted images to generate and append Markdown text to a file. It requires the vllm library for LLM inference. ```python from vllm import SamplingParams from pdf2md_llm.llm import llm_model from pdf2md_llm.pdf2img import PdfToImg pdf2img = PdfToImg(size=(700, None), output_folder="./out") img_files = pdf2img.convert("example.pdf") llm = llm_model( model="Qwen/Qwen2.5-VL-3B-Instruct-AWQ", # Name of the huggingface model dtype="half", # Model data type ) sampling_params = SamplingParams( temperature=0.1, min_p=0.1, max_tokens=8192, stop_token_ids=[], ) # Append all pages to one output Markdown file for img_file in img_files: markdown_text = llm.generate( img_file, sampling_params=sampling_params ) # convert image to Markdown with LLM with open("example.md", "a", encoding="utf-8") as myfile: myfile.write(markdown_text) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.