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

example: Table extraction w/ Pydantic #288

Merged
merged 3 commits into from
Dec 18, 2023
Merged

example: Table extraction w/ Pydantic #288

merged 3 commits into from
Dec 18, 2023

Conversation

jxnl
Copy link
Owner

@jxnl jxnl commented Dec 18, 2023

This is a peculiar example, and I'm unsure how to work with types in this context.

Here's the code I have:

class Table(BaseModel):
    caption: str = Field(
        description="The caption of the table, which should provide a detailed description"
    )
    table_md: str = Field(description="The markdown representation of the table")

    def df(self):
        return (
            pd.read_csv(
                StringIO(self.table_md),  # Remove whitespaces
                sep="|",
                index_col=1,
            )
            .dropna(axis=1, how="all")
            .iloc[1:]
        )

When I use the OpenAI vision model to extract tables from a given URL:

url = "https://a.storyblok.com/f/47007/2400x2000/bf383abc3c/231031_uk-ireland-in-three-charts_table_v01_b.png"
tables = extract_table(url)
for tbl in tables:
    print(tbl.caption)
    print(tbl.table_md)
    print(tbl.df())

The table displays the top 10 grossing Android apps in Ireland for October 2023, provided by SensorTower. The rankings are based on app revenue, and the categories indicate the primary function of each app.

Rank App Category
1 Google One Productivity
2 Disney+ Entertainment
3 TikTok - Videos, Music & LIVE Entertainment
4 Candy Crush Saga Games
5 Tinder: Dating, Chat & Friends Social networking
6 Coin Master Games
7 Roblox Games
8 Bumble - Dating & Make Friends Dating
9 Royal Match Games
10 Spotify: Music and Podcasts Music & Audio

The table lists the top 10 grossing iOS apps in Ireland for October 2023, provided by SensorTower. The rankings are based on app revenue, and the categories indicate the primary function of each app.

Rank App Category
1 Tinder: Dating, Chat & Friends Social networking
2 Disney+ Entertainment
3 YouTube: Watch, Listen, Stream Entertainment
4 Audible: Audio Entertainment Entertainment
5 Candy Crush Saga Games
6 TikTok - Videos, Music & LIVE Entertainment
7 Bumble - Dating & Make Friends Dating
8 Roblox Games
9 LinkedIn: Job Search & News Business
10 Duolingo - Language Lessons Education

It would be useful to provide users or instructors with some helper annotations. For example:

class Table:
   caption: str
   table: Annotated[???]

By handling the prompt and validation properly:

  • The table attribute should have a JSON schema type of str and a descriptive prompt.
  • A validator should parse the markdown into a DataFrame, so that Table(...).table becomes a DataFrame.

Summary by CodeRabbit

  • New Features

    • Introduced a guide on extracting tables from images using the new vision model.
    • Added new functions for table conversion to markdown and dataframe formats.
    • Implemented a Table class for better table data management.
  • Documentation

    • Updated the examples index with a new section on table extraction from images.
  • New Examples

    • Provided a practical example script for table extraction using the vision model.

Copy link
Contributor

coderabbitai bot commented Dec 18, 2023

Walkthrough

The recent updates introduce a guide and tools for extracting markdown tables from images using OpenAI's new vision model. A Table class and functions for conversion to markdown and DataFrame formats were added, enhancing the ability to process and utilize visual table data.

Changes

File Path Change Summary
.../extracting_tables.md Added a comprehensive guide on extracting markdown tables from images using type annotations and OpenAI's vision model.
.../index.md Updated the topics list with a new section on extracting tables from images.
examples/vision/run_table.py Introduced functionality to extract tables from images and convert them to markdown using OpenAI's GPT-4 Vision model.

🐇✨
In the realm of code, where tables hide,
A vision model now takes a stride.
From images, data leaps and bounds,
In markdown's grace, it now resounds.

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on X ?


Tips

Chat with CodeRabbit Bot (@coderabbitai)

  • You can reply to a review comment made by CodeRabbit.
  • You can tag CodeRabbit on specific lines of code or files in the PR by tagging @coderabbitai in a comment.
  • You can tag @coderabbitai in a PR comment and ask one-off questions about the PR and the codebase. Use quoted replies to pass the context for follow-up questions.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • The JSON schema for the configuration file is available here.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/coderabbit-overrides.v2.json

@jxnl jxnl changed the title add example example: Table extraction w/ Pydantic Dec 18, 2023
@jxnl
Copy link
Owner Author

jxnl commented Dec 18, 2023

Top 10 Grossing Apps in October 2023 for Ireland

Top 10 Grossing Apps in October 2023 (Ireland) for Android Platforms

Rank App Name Category
1 Google One Productivity
2 Disney+ Entertainment
3 TikTok - Videos, Music & LIVE Entertainment
4 Candy Crush Saga Games
5 Tinder: Dating, Chat & Friends Social networking
6 Coin Master Games
7 Roblox Games
8 Bumble - Dating & Make Friends Dating
9 Royal Match Games
10 Spotify: Music and Podcasts Music & Audio

Top 10 Grossing Apps in October 2023 (Ireland) for iOS Platforms

Rank App Name Category
1 Tinder: Dating, Chat & Friends Social networking
2 Disney+ Entertainment
3 YouTube: Watch, Listen, Stream Entertainment
4 Audible: Audio Entertainment Entertainment
5 Candy Crush Saga Games
6 TikTok - Videos, Music & LIVE Entertainment
7 Bumble - Dating & Make Friends Dating
8 Roblox Games
9 LinkedIn: Job Search & News Business
10 Duolingo - Language Lessons Education

@jxnl jxnl marked this pull request as ready for review December 18, 2023 23:27
@jxnl jxnl merged commit 8510af2 into main Dec 18, 2023
13 checks passed
@jxnl jxnl deleted the vision-preview branch December 18, 2023 23:27
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 6

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 098f43a and 128b2f0.
Files ignored due to filter (1)
  • mkdocs.yml
Files selected for processing (3)
  • docs/examples/extracting_tables.md (1 hunks)
  • docs/examples/index.md (1 hunks)
  • examples/vision/run_table.py (1 hunks)
Files skipped from review due to trivial changes (1)
  • docs/examples/index.md
Additional comments: 1
examples/vision/run_table.py (1)
  • 37-50: Verify the usage of InstanceOf, BeforeValidator, and PlainSerializer as they are not standard Pydantic validators. Ensure that these are custom extensions that are correctly implemented and used.

Comment on lines +17 to +30
def md_to_df(data: Any) -> Any:
# Convert markdown to DataFrame
if isinstance(data, str):
return (
pd.read_csv(
StringIO(data), # Process data
sep="|",
index_col=1,
)
.dropna(axis=1, how="all")
.iloc[1:]
.applymap(lambda x: x.strip())
)
return data
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding error handling for the md_to_df function to manage cases where the markdown data is not in the expected format or pd.read_csv throws an exception.

Comment on lines +68 to +82
def extract_table(url: str) -> Iterable[Table]:
return client.chat.completions.create(
model="gpt-4-vision-preview",
response_model=Iterable[Table],
max_tokens=1800,
messages=[
{
"role": "user",
"content": [
{"type": "text", "text": "Extract table from image."},
{"type": "image_url", "image_url": {"url": url}}
],
}
],
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add error handling to the extract_table function to manage potential failures or unexpected responses from the OpenAI API call.

Comment on lines +22 to +33
def md_to_df(data: Any) -> Any:
if isinstance(data, str):
return (
pd.read_csv(
StringIO(data), # Get rid of whitespaces
sep="|",
index_col=1,
)
.dropna(axis=1, how="all")
.iloc[1:]
.map(lambda x: x.strip())
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The .map(lambda x: x.strip()) call will raise an AttributeError because it is applied to a DataFrame rather than a Series. This needs to be corrected to apply the strip operation to each element in the DataFrame.

-            .map(lambda x: x.strip())
+            .applymap(lambda x: x.strip() if isinstance(x, str) else x)

Committable suggestion

IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
def md_to_df(data: Any) -> Any:
if isinstance(data, str):
return (
pd.read_csv(
StringIO(data), # Get rid of whitespaces
sep="|",
index_col=1,
)
.dropna(axis=1, how="all")
.iloc[1:]
.map(lambda x: x.strip())
)
def md_to_df(data: Any) -> Any:
if isinstance(data, str):
return (
pd.read_csv(
StringIO(data), # Get rid of whitespaces
sep="|",
index_col=1,
)
.dropna(axis=1, how="all")
.iloc[1:]
.applymap(lambda x: x.strip() if isinstance(x, str) else x)
)

Comment on lines +58 to +80
def extract_table(url: str) -> Iterable[Table]:
return client.chat.completions.create(
model="gpt-4-vision-preview",
response_model=Iterable[Table],
max_tokens=1800,
messages=[
{
"role": "user",
"content": [
{
"type": "text",
"text": """Extract the table from the image, and describe it.
Each table should be tidy, do not try to join tables that
should be seperately described.""",
},
{
"type": "image_url",
"image_url": {"url": url},
},
],
}
],
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The response_model parameter in the client.chat.completions.create call is set to Iterable[Table], which is not a valid argument. This should be removed or replaced with a valid parameter.

-        response_model=Iterable[Table],

Committable suggestion

IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
def extract_table(url: str) -> Iterable[Table]:
return client.chat.completions.create(
model="gpt-4-vision-preview",
response_model=Iterable[Table],
max_tokens=1800,
messages=[
{
"role": "user",
"content": [
{
"type": "text",
"text": """Extract the table from the image, and describe it.
Each table should be tidy, do not try to join tables that
should be seperately described.""",
},
{
"type": "image_url",
"image_url": {"url": url},
},
],
}
],
)
def extract_table(url: str) -> Iterable[Table]:
return client.chat.completions.create(
model="gpt-4-vision-preview",
max_tokens=1800,
messages=[
{
"role": "user",
"content": [
{
"type": "text",
"text": """Extract the table from the image, and describe it.
Each table should be tidy, do not try to join tables that
should be seperately described.""",
},
{
"type": "image_url",
"image_url": {"url": url},
},
],
}
],
)

Comment on lines +83 to +118
if __name__ == "__main__":
url = "https://a.storyblok.com/f/47007/2400x2000/bf383abc3c/231031_uk-ireland-in-three-charts_table_v01_b.png"
tables = extract_table(url)
for tbl in tables:
print(tbl.caption, end="\n")
print(tbl.dataframe)
"""
Top 10 grossing apps in October 2023 (Ireland) for Android platforms, listing the rank, app name, and category.

App Name Category
Rank
1 Google One Productivity
2 Disney+ Entertainment
3 TikTok - Videos, Music & LIVE Entertainment
4 Candy Crush Saga Games
5 Tinder: Dating, Chat & Friends Social networking
6 Coin Master Games
7 Roblox Games
8 Bumble - Dating & Make Friends Dating
9 Royal Match Games
10 Spotify: Music and Podcasts Music & Audio

Top 10 grossing apps in October 2023 (Ireland) for iOS platforms, listing the rank, app name, and category.

App Name Category
Rank
1 Tinder: Dating, Chat & Friends Social networking
2 Disney+ Entertainment
3 YouTube: Watch, Listen, Stream Entertainment
4 Audible: Audio Entertainment Entertainment
5 Candy Crush Saga Games
6 TikTok - Videos, Music & LIVE Entertainment
7 Bumble - Dating & Make Friends Dating
8 Roblox Games
9 LinkedIn: Job Search & News Business
10 Duolingo - Language Lessons Education
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider parameterizing the URL and encapsulating the demonstration code in a function to improve reusability and maintainability.

Comment on lines +89 to +118
"""
Top 10 grossing apps in October 2023 (Ireland) for Android platforms, listing the rank, app name, and category.

App Name Category
Rank
1 Google One Productivity
2 Disney+ Entertainment
3 TikTok - Videos, Music & LIVE Entertainment
4 Candy Crush Saga Games
5 Tinder: Dating, Chat & Friends Social networking
6 Coin Master Games
7 Roblox Games
8 Bumble - Dating & Make Friends Dating
9 Royal Match Games
10 Spotify: Music and Podcasts Music & Audio

Top 10 grossing apps in October 2023 (Ireland) for iOS platforms, listing the rank, app name, and category.

App Name Category
Rank
1 Tinder: Dating, Chat & Friends Social networking
2 Disney+ Entertainment
3 YouTube: Watch, Listen, Stream Entertainment
4 Audible: Audio Entertainment Entertainment
5 Candy Crush Saga Games
6 TikTok - Videos, Music & LIVE Entertainment
7 Bumble - Dating & Make Friends Dating
8 Roblox Games
9 LinkedIn: Job Search & News Business
10 Duolingo - Language Lessons Education
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The hardcoded data at the end of the file should be removed or commented out if it's meant for documentation purposes. It's not clear why it's included in the script.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant