gptpdf

repository·main·Indexed 25 days ago

https://github.com/cosmosshadow/gptpdf

A tool that uses large visual models, such as GPT-4o, to parse PDF documents into high-fidelity Markdown. It preserves typography, math formulas, tables, and images, and supports customizable prompts and parallel processing via the parse_pdf function.

Tokens
2.5K
Snippets
6
Records
15
Agent score
84%

What's inside gptpdf

  1. Extract and purify S25DH reaction products

    main

    To isolate products from the reaction mixture:

    1. Extraction: Extract the reaction mixture with ethyl acetate (3 × 0.25 of the reaction medium volume).
    2. Washing/Drying: Wash combined extracts with saturated $KCl_{aq}$, then dry over anhydrous magnesium sulfate.
    3. Evaporation: Evaporate under reduced pressure to obtain a residue.
    4. Purification: Purify the residue using column chromatography on silica with an ethyl acetate:hexane (1:1) solvent system.
  2. Set up a local testing environment

    main
    To test the package locally, create a new Python virtual environment, activate it, and install the current directory as a package. If you have a python alias configured in your shell, you may need to temporarily unalias it to ensure the virtual environment's Python is used.
  3. Optimize S25DH reaction medium for hydrophobic substrates

    main

    To improve the conversion rate of S25DH (Steroid C25 dehydrogenase) with hydrophobic substrates like cholest-4-en-3-one, cholecalciferol, or cholesterol, optimize the ratio of the solubilizer HBC (2-hydroxypropyl-β-cyclodextrin) and the organic co-solvent EGME (2-methoxyethanol).

    Using EGME as a substitute for 1,4-dioxane can significantly increase conversion rates and substrate loading. Based on experimental results:

    • For cholest-4-en-3-one: Use approximately 8% (w/v) HBC and 1.25% (v/v) EGME.
    • For cholecalciferol: Use 6–8% (w/v) HBC and 5% (v/v) EGME.
    • For cholesterol: Use 6–9% (w/v) HBC and 2.5% (v/v) EGME.
  4. Run local tests with environment variables

    main

    After setting up the environment, navigate to the test directory. Before running the tests, you must export the environment variables defined in the .env file. You can then execute the test suite using python test.py.

    cd test
    # Export environment variables from .env
    export $(grep -v '^#' .env | sed 's/^export //g' | xargs)
    python test.py
  5. Maintain anaerobic conditions for S25DH stability

    main

    S25DH enzymes are oxygen-sensitive, particularly in their reduced state. To ensure long-term catalyst performance and prevent enzyme inactivation:

    • Conduct reactions under anaerobic conditions (e.g., using a glove box with 97% $N_2$ / 3% $H_2$).
    • Avoid aerobic atmospheres, as they lead to rapid enzyme inactivation (e.g., loss of activity within 96 hours for immobilized enzymes).
    • Replenish the electron acceptor ($K_3[Fe(CN)_6]$) whenever concentrations reach low levels to maintain activity.
  6. Configure custom prompts for parse_pdf

    main

    If the default prompts do not yield optimal results for your specific model, you can pass a custom prompt dictionary to parse_pdf. The dictionary supports three keys: prompt, rect_prompt, and role_prompt.

    prompt = {
        "prompt": "Custom prompt text",
        "rect_prompt": "Custom rect prompt",
        "role_prompt": "Custom role prompt"
    }
    
    content, image_paths = parse_pdf(
        pdf_path=pdf_path,
        output_dir='./output',
        model="gpt-4o",
        prompt=prompt,
        verbose=False,
    )
  7. Customize extraction prompts

    main

    You can provide custom prompts to parse_pdf() to change the language or the formatting style of the output. The function uses three distinct prompt types:

    1. prompt (User Prompt): Controls how the model converts image content to Markdown (e.g., language requirements, formula styles like $$ $$).
    2. rect_prompt (Rectangle Prompt): Instructs the model on how to handle specific identified regions (e.g., "If the region is a table, use ![]() format").
    3. role_prompt (System Prompt): Defines the model's persona (e.g., "You are a PDF document parser").
  8. Customize prompts in parse_pdf

    main

    You can customize the prompt, rect_prompt, and role_prompt to adapt the model's behavior to specific requirements or different models.

    content, image_paths = parse_pdf(
        pdf_path=pdf_path,
        output_dir='./output',
        model="gpt-4o",
        prompt="自定义主提示词",
        rect_prompt="自定义矩形区域提示词",
        role_prompt="自定义角色提示词",
        verbose=False,
    )
  9. Use parse_pdf for local PDF to Markdown conversion

    main

    Use the parse_pdf function to convert a PDF file into Markdown content and extract associated images. You must provide an OpenAI API key either as an argument or via the OPENAI_API_KEY environment variable.

    from gptpdf import parse_pdf
    
    api_key = 'Your OpenAI API Key'
    content, image_paths = parse_pdf(pdf_path, api_key=api_key)
    print(content)
  10. API Reference: parse_pdf

    main

    The parse_pdf function parses a PDF file into a Markdown file and returns the Markdown content along with a list of all extracted image paths.

    Signature:

    def parse_pdf(
            pdf_path: str,
            output_dir: str = './',
            prompt: Optional[Dict] = None,
            api_key: Optional[str] = None,
            base_url: Optional[str] = None,
            model: str = 'gpt-4o',
            verbose: bool = False,
            gpt_worker: int = 1
    ) -> Tuple[str, List[str]]:

    Parameters:

    • pdf_path (str): Path to the PDF file.
    • output_dir (str, default: './'): Output directory to store all images and the Markdown file.
    • api_key (Optional[str], optional): OpenAI API key. If not provided, the OPENAI_API_KEY environment variable will be used.
    • base_url (Optional[str], optional): OpenAI base URL. If not provided, the OPENAI_BASE_URL environment variable will be used. This allows using other services with OpenAI-compatible interfaces (e.g., GLM-4V).
    • model (str, default: 'gpt-4o'): OpenAI API formatted multimodal large model. Supports models like qwen-vl-max, GLM-4V, Yi-Vision, or Azure OpenAI (by setting base_url to the Azure endpoint and using the deployed model name).
    • verbose (bool, default: False): When enabled, the content parsed by the large model is displayed in the command line.
    • gpt_worker (int, default: 1): Number of GPT parsing worker threads. Increase this to speed up parsing on high-performance machines.
    • prompt (dict, optional): A dictionary to provide custom prompts. The dictionary can contain:
      • prompt: Guides the model on processing/converting text content in images.
      • rect_prompt: Handles specific marked areas like tables or images.
      • role_prompt: Defines the model's role for the parsing task.

    Returns:

    • Tuple[str, List[str]]: A tuple containing the Markdown string and a list of image file paths.
    content, image_paths = parse_pdf(
        pdf_path=pdf_path,
        output_dir='./output',
        model="gpt-4o",
        prompt=prompt,
        verbose=False,
    )
  11. Perform S2SDH activity detection via UV-Vis or HPLC

    main

    Two primary methods are used to monitor S2SDH activity:

    UV-Vis Detection

    • Wavelength: 420 nm
    • Buffer: 70 mM $KH_2PO_4/K_2HPO_4$ (pH 7.0)
    • Electron Acceptor: 0.2 mM $K_3[Fe(CN)_6]$
    • Conditions: 30 °C

    HPLC Detection

    • Column: Ascentis® Express RP-Amide (2.7 µm, 7.5 cm × 4.6 mm)
    • Flow Rate: 1 mL/min
    • Gradient (C3-ketones/cholecalciferol): 55–98% acetonitrile/$H_2O$/10 mM $NH_4CH_3COO$ (DAD(+)–ESI-MS)
    • Gradient (C3-alcohols/esters): 95–98% acetonitrile/$H_2O$/0.01% HCOOH (DAD(+)–APCI-MS)
    • Detection Wavelengths:
      • 240 nm: cholest-4-en-3-one, cholest-1,4-dien-3-one
      • 265 nm: cholecalciferol, ergocalciferol
      • 280 nm: 7-dehydrocholesterol
      • 205 nm: cholesterol, cholesteryl succinate