Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add PDFParser #187

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion src/smolagents/default_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

from huggingface_hub import hf_hub_download, list_spaces


from transformers import AutoModel, AutoTokenizer
from transformers.utils import is_offline_mode, is_torch_available

from .local_python_executor import (
Expand Down Expand Up @@ -329,6 +329,36 @@ def decode(self, outputs):
return self.pre_processor.batch_decode(outputs, skip_special_tokens=True)[0]


class PDFParsingTool(Tool):
name = "pdf_parser"
description = """Parses a given PDF into markdown."""
inputs = {
"image": {"type": "image", "description": "The path to PDF to be parsed."},
}
output_type = "string"

def __init__(self):
super().__init__(self)
self.tokenizer = AutoTokenizer.from_pretrained(
"ucaslcl/GOT-OCR2_0", trust_remote_code=True
)
self.model = (
AutoModel.from_pretrained(
"ucaslcl/GOT-OCR2_0",
trust_remote_code=True,
low_cpu_mem_usage=True,
device_map="cuda",
use_safetensors=True,
)
.eval()
.cuda()
)

def forward(self, image) -> str:
res = self.model.chat(self.tokenizer, image, ocr_type="format", render=True)
return res


TOOL_MAPPING = {
tool_class.name: tool_class
for tool_class in [
Expand All @@ -346,4 +376,5 @@ def decode(self, outputs):
"GoogleSearchTool",
"VisitWebpageTool",
"SpeechToTextTool",
"PDFParsingTool",
]
Loading