Files
567-labs--instructor/examples/extract-table/run_vision_org_table.py
T
wehub-resource-sync 97e91a83f3
Ruff / Ruff (push) Has been cancelled
Test / Core Tests (push) Has been cancelled
Test / Offline Coverage Tests (Python 3.10) (push) Has been cancelled
Test / Offline Coverage Tests (Python 3.11) (push) Has been cancelled
Test / Offline Coverage Tests (Python 3.12) (push) Has been cancelled
Test / Offline Coverage Tests (Python 3.13) (push) Has been cancelled
Test / Offline Coverage Tests (Python 3.9) (push) Has been cancelled
Test / Full Coverage (Python 3.11) (push) Has been cancelled
Test / Core Provider Tests (OpenAI) (push) Has been cancelled
Test / Core Provider Tests (Anthropic) (push) Has been cancelled
Test / Core Provider Tests (Google) (push) Has been cancelled
Test / Core Provider Tests (Other) (push) Has been cancelled
Test / Anthropic Tests (push) Has been cancelled
Test / Gemini Tests (push) Has been cancelled
Test / Google GenAI Tests (push) Has been cancelled
Test / Vertex AI Tests (push) Has been cancelled
Test / OpenAI Tests (push) Has been cancelled
Test / Writer Tests (push) Has been cancelled
Test / Auto Client Tests (push) Has been cancelled
ty / type-check (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:36:38 +08:00

116 lines
4.7 KiB
Python

from openai import OpenAI
from io import StringIO
from typing import Annotated, Any
from pydantic import (
BaseModel,
BeforeValidator,
PlainSerializer,
InstanceOf,
WithJsonSchema,
)
import instructor
import pandas as pd
from rich.console import Console
console = Console()
client = instructor.from_openai(
client=OpenAI(),
mode=instructor.Mode.TOOLS,
)
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())
) # type: ignore
return data
MarkdownDataFrame = Annotated[
InstanceOf[pd.DataFrame],
BeforeValidator(md_to_df),
PlainSerializer(lambda x: x.to_markdown()),
WithJsonSchema(
{
"type": "string",
"description": """
The markdown representation of the table,
each one should be tidy, do not try to join tables
that should be separate""",
}
),
]
class Table(BaseModel):
caption: str
dataframe: MarkdownDataFrame
def extract(url: str):
return client.chat.completions.create(
model="gpt-4-turbo",
max_tokens=4000,
response_model=Table,
messages=[
{
"role": "user",
"content": [
{
"type": "image_url",
"image_url": {"url": url},
},
{
"type": "text",
"text": """
Analyze the organizational chart image and extract the relevant information to reconstruct the hierarchy.
Create a list of People objects, where each person has the following attributes:
- id: A unique identifier for the person
- name: The person's name
- role: The person's role or position in the organization
- manager_name: The name of the person who manages this person
- manager_role: The role of the person who manages this person
Ensure that the relationships between people are accurately captured in the reports and manages attributes.
Return the list of People objects as the people attribute of an Organization object.
""",
},
],
}
],
)
print(
extract(
"https://www.mindmanager.com/static/mm/images/features/org-chart/hierarchical-chart.png"
).model_dump()["dataframe"]
)
"""
| id | name | role | manager_name | manager_role |
|-------:|:-------------------|:-----------------------------------------|:------------------|:-----------------------------|
| 1 | Adele Morana | Founder, Chairman & CEO | | |
| 2 | Winston Cole | COO | Adele Morana | Founder, Chairman & CEO |
| 3 | Marcus Kim | CFO | Adele Morana | Founder, Chairman & CEO |
| 4 | Karin Ludovicus | CPO | Adele Morana | Founder, Chairman & CEO |
| 5 | Lea Erastos | Chief Business Officer | Winston Cole | COO |
| 6 | John McKinley | Chief Accounting Officer | Winston Cole | COO |
| 7 | Zahida Mahtab | VP, Global Affairs & Communication | Winston Cole | COO |
| 8 | Adelaide Zhu | VP, Central Services | Winston Cole | COO |
| 9 | Gabriel Drummond | VP, Investor Relations | Marcus Kim | CFO |
| 10 | Felicie Vasili | VP, Finance | Marcus Kim | CFO |
| 11 | Ayda Williams | VP, Global Customer & Business Marketing | Karin Ludovicius | CPO |
| 12 | Nicholas Brambilla | VP, Company Brand | Karin Ludovicius | CPO |
| 13 | Sandra Herminius | VP, Product Marketing | Karin Ludovicius | CPO |
"""