This commit introduces support for Ollama as an alternative Large Language Model (LLM) provider and enhances PDF image extraction capabilities.
- **Ollama Integration:**
- Implemented `set_ollama_config` to configure Ollama's base URL from `config.ini`.
- Modified `llm.py` to dynamically select and configure the LLM (Gemini or Ollama) based on the `PROVIDER` setting.
- Updated `get_model_name` to return provider-specific default model names.
- `pdf_convertor.py` now conditionally initializes `ChatGoogleGenerativeAI` or `ChatOllama` based on the configured provider.
- **PyMuPDF Image Extraction:**
- Added a new `extract_images_from_pdf` function using PyMuPDF (`fitz`) for direct image extraction from PDF files.
- Introduced `get_extract_images_from_pdf_flag` to control this feature via `config.ini`.
- `convert_pdf_to_markdown` and `refine_content` functions were updated to utilize this new image extraction method when enabled.
- **Refinement Flow:**
- Adjusted the order of `save_md_images` in `main.py` and added an option to save the refined markdown with a specific filename (`index_refined.md`).
- **Dependencies:**
- Updated `pyproject.lock` to include new dependencies for Ollama integration (`langchain-ollama`) and PyMuPDF (`PyMuPDF`), along with platform-specific markers for NVIDIA dependencies.
44 lines
1.1 KiB
Python
Executable File
44 lines
1.1 KiB
Python
Executable File
import os
|
|
from pdf_convertor import (
|
|
convert_pdf_to_markdown,
|
|
save_md_images,
|
|
refine_content,
|
|
)
|
|
from pathlib import Path
|
|
|
|
|
|
def main():
|
|
input_dir = Path("input")
|
|
output_dir = Path("output")
|
|
|
|
output_dir.mkdir(parents=True, exist_ok=True)
|
|
|
|
for filename in os.listdir(input_dir):
|
|
if not filename.endswith(".pdf"):
|
|
continue
|
|
|
|
pdf_path = input_dir.joinpath(filename)
|
|
current_output_dir = output_dir.joinpath(
|
|
pdf_path.name.removesuffix(pdf_path.suffix)
|
|
)
|
|
|
|
current_output_dir.mkdir(parents=True, exist_ok=True)
|
|
|
|
print(f"Processing {pdf_path} -> {current_output_dir}")
|
|
|
|
with open(pdf_path, "rb") as pdf_file:
|
|
pdf_content = pdf_file.read()
|
|
md, images = convert_pdf_to_markdown(pdf_content)
|
|
save_md_images(current_output_dir, md, images)
|
|
|
|
try:
|
|
md = refine_content(md, images, pdf_content)
|
|
except BaseException:
|
|
continue
|
|
|
|
save_md_images(current_output_dir, md, images, md_name="index_refined.md")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|