-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #454 from VikParuchuri/vik_dev
LLM based image captioning
- Loading branch information
Showing
8 changed files
with
134 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
from marker.processors.llm import BaseLLMProcessor | ||
|
||
from google.ai.generativelanguage_v1beta.types import content | ||
|
||
from marker.schema import BlockTypes | ||
from marker.schema.blocks import Block | ||
from marker.schema.document import Document | ||
from marker.schema.groups.page import PageGroup | ||
|
||
|
||
class LLMImageDescriptionProcessor(BaseLLMProcessor): | ||
block_types = (BlockTypes.Picture, BlockTypes.Figure,) | ||
extract_images: bool = True | ||
image_description_prompt = """You are a document analysis expert who specializes in creating text descriptions for images. | ||
You will receive an image of a picture or figure. Your job will be to create a short description of the image. | ||
**Instructions:** | ||
1. Carefully examine the provided image. | ||
2. Analyze any text that was extracted from within the image. | ||
3. Output a 3-4 sentence description of the image. Make sure there is enough specific detail to accurately describe the image. If there are numbers included, try to be specific. | ||
**Example:** | ||
Input: | ||
```text | ||
"Fruit Preference Survey" | ||
20, 15, 10 | ||
Apples, Bananas, Oranges | ||
``` | ||
Output: | ||
In this figure, a bar chart titled "Fruit Preference Survey" is showing the number of people who prefer different types of fruits. The x-axis shows the types of fruits, and the y-axis shows the number of people. The bar chart shows that most people prefer apples, followed by bananas and oranges. 20 people prefer apples, 15 people prefer bananas, and 10 people prefer oranges. | ||
**Input:** | ||
""" | ||
|
||
def process_rewriting(self, document: Document, page: PageGroup, block: Block): | ||
if self.extract_images: | ||
# We will only run this processor if we're not extracting images | ||
# Since this processor replaces images with descriptions | ||
return | ||
|
||
prompt = self.image_description_prompt + '```text\n`' + block.raw_text(document) + '`\n```\n' | ||
image = self.extract_image(page, block) | ||
response_schema = content.Schema( | ||
type=content.Type.OBJECT, | ||
enum=[], | ||
required=["image_description"], | ||
properties={ | ||
"image_description": content.Schema( | ||
type=content.Type.STRING | ||
) | ||
}, | ||
) | ||
|
||
response = self.model.generate_response(prompt, image, block, response_schema) | ||
|
||
if not response or "image_description" not in response: | ||
block.update_metadata(llm_error_count=1) | ||
return | ||
|
||
image_description = response["image_description"] | ||
if len(image_description) < 10: | ||
block.update_metadata(llm_error_count=1) | ||
return | ||
|
||
block.description = image_description |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import pytest | ||
|
||
from marker.renderers.markdown import MarkdownRenderer | ||
|
||
|
||
@pytest.mark.config({"page_range": [0]}) | ||
@pytest.mark.filename("A17_FlightPlan.pdf") | ||
def test_disable_extract_images(pdf_document): | ||
renderer = MarkdownRenderer({"extract_images": False}) | ||
md = renderer(pdf_document).markdown | ||
|
||
# Verify markdown | ||
assert len(md) == 0 | ||
|
||
|
||
@pytest.mark.config({"page_range": [0]}) | ||
@pytest.mark.filename("A17_FlightPlan.pdf") | ||
def test_extract_images(pdf_document): | ||
renderer = MarkdownRenderer() | ||
md = renderer(pdf_document).markdown | ||
|
||
# Verify markdown | ||
assert "jpeg" in md |