-
Notifications
You must be signed in to change notification settings - Fork 394
[WIP]feature: support more general image extraction #672
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
base: main
Are you sure you want to change the base?
Conversation
…age using base url 3.support normalizing base url
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Summary of Changes
Hello @alcholiclg, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!
This pull request significantly enhances the image extraction capabilities within the docling
HTML backend. The primary goal is to improve the handling of relative image URLs by accurately capturing and utilizing the base URL of the document being processed, and by centralizing and refining the image fetching and URL resolution logic.
Highlights
- Improved Base URL Handling: The system now captures the
current_url
of a document during conversion and makes it available to the HTML backend, allowing for more accurate resolution of relative image paths. This is achieved by patchingDocumentConverter._convert
to inject thecurrent_url
into the document's backend attributes and updatingDocLoader.load
to set this URL for each document. - Refactored URL Resolution Logic: The
validate_url
utility function has been renamed toresolve_url
and significantly enhanced. It now accepts an explicitbase_url
parameter and includes a newnormalized_base
helper to correctly append a trailing slash to base URLs that represent directories, ensuring more robust URL concatenation. - Centralized Image Extraction: A new
extract_image
utility function has been introduced. This function consolidates the logic for fetching images from both data URIs and standard URLs, leveraging the updatedresolve_url
for non-absolute paths. Thehtml_handle_figure
andhtml_handle_image
methods in thedocling
backend now utilize this centralized function. - Expanded Unit Tests: The unit tests for image extraction (
tests/doc/test_img.py
) have been updated to include more comprehensive scenarios. Test variables for URLs are now lists, allowing for multiple URLs to be tested per category, and extracted images are saved into task-specific directories for better organization and verification.
Using Gemini Code Assist
The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.
Invoking Gemini
You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command>
or @gemini-code-assist <command>
. Below is a summary of the supported commands.
Feature | Command | Description |
---|---|---|
Code Review | /gemini review |
Performs a code review for the current pull request in its current state. |
Pull Request Summary | /gemini summary |
Provides a summary of the current pull request in its current state. |
Comment | @gemini-code-assist | Responds in comments when explicitly tagged, both in issue comments and review comments. |
Help | /gemini help |
Displays a list of available commands. |
Customization
To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/
folder in the base of the repository. Detailed instructions can be found here.
Limitations & Feedback
Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments or fill out our survey to provide feedback.
You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.
Footnotes
-
Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
The code changes introduce the ability to load multiple signed images to a registry using a bulk load feature. The review focuses on a potential UnboundLocalError
, a hardcoded special case, and the handling of lists of URLs in the tests.
need_append_slash = ('.' not in parsed.path.split('/')[-1] | ||
or 'arxiv.org' in parsed.netloc) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This logic includes a hardcoded special case for arxiv.org
. This makes the function less generic and might require adding more special cases in the future as you encounter other sites with similar URL structures. Consider adding a code comment to explain why this special case is necessary. If a more general solution is feasible, that would be even better for long-term maintainability.
tests/doc/test_img.py
Outdated
os.path.join(save_dir, 'picture_' + str(idx) + '.png')) | ||
assert len( | ||
doc_results[0].pictures) > 0, 'No pictures found in the document.' | ||
doc_results = doc_loader.load(urls_or_files=self.absolute_path_img_url) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The load
method is called with self.absolute_path_img_url
, which is a list. However, the loop iterates through doc_results
without using the task_id
to access the corresponding URL from the list. This could lead to incorrect assertions or errors if the number of documents loaded doesn't match the number of URLs.
tests/doc/test_img.py
Outdated
os.path.join(save_dir, 'picture_' + str(idx) + '.png')) | ||
assert len( | ||
doc_results[0].pictures) > 0, 'No pictures found in the document.' | ||
doc_results = doc_loader.load(urls_or_files=self.relative_path_img_url) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The load
method is called with self.relative_path_img_url
, which is a list. However, the loop iterates through doc_results
without using the task_id
to access the corresponding URL from the list. This could lead to incorrect assertions or errors if the number of documents loaded doesn't match the number of URLs.
tests/doc/test_img.py
Outdated
os.path.join(save_dir, 'picture_' + str(idx) + '.png')) | ||
assert len( | ||
doc_results[0].pictures) > 0, 'No pictures found in the document.' | ||
doc_results = doc_loader.load(urls_or_files=self.figure_tag_img_url) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The load
method is called with self.figure_tag_img_url
, which is a list. However, the loop iterates through doc_results
without using the task_id
to access the corresponding URL from the list. This could lead to incorrect assertions or errors if the number of documents loaded doesn't match the number of URLs.
tests/doc/test_img.py
Outdated
os.path.join(save_dir, 'picture_' + str(idx) + '.png')) | ||
assert len( | ||
doc_results[0].pictures) > 0, 'No pictures found in the document.' | ||
doc_results = doc_loader.load(urls_or_files=self.data_uri_img_url) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The load
method is called with self.data_uri_img_url
, which is a list. However, the loop iterates through doc_results
without using the task_id
to access the corresponding URL from the list. This could lead to incorrect assertions or errors if the number of documents loaded doesn't match the number of URLs.
initialize need_append_slash to False Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Change Summary
Related issue number
Checklist
pre-commit install
andpre-commit run --all-files
before git commit, and passed lint check.