Files
wehub-resource-sync 97e91a83f3
Ruff / Ruff (push) Waiting to run
Test / Core Tests (push) Waiting to run
Test / Offline Coverage Tests (Python 3.10) (push) Waiting to run
Test / Offline Coverage Tests (Python 3.11) (push) Waiting to run
Test / Offline Coverage Tests (Python 3.12) (push) Waiting to run
Test / Offline Coverage Tests (Python 3.13) (push) Waiting to run
Test / Offline Coverage Tests (Python 3.9) (push) Waiting to run
Test / Full Coverage (Python 3.11) (push) Waiting to run
Test / Core Provider Tests (OpenAI) (push) Blocked by required conditions
Test / Core Provider Tests (Anthropic) (push) Blocked by required conditions
Test / Core Provider Tests (Google) (push) Blocked by required conditions
Test / Core Provider Tests (Other) (push) Blocked by required conditions
Test / Anthropic Tests (push) Blocked by required conditions
Test / Gemini Tests (push) Blocked by required conditions
Test / Google GenAI Tests (push) Blocked by required conditions
Test / Vertex AI Tests (push) Blocked by required conditions
Test / OpenAI Tests (push) Blocked by required conditions
Test / Writer Tests (push) Blocked by required conditions
Test / Auto Client Tests (push) Blocked by required conditions
ty / type-check (push) Waiting to run
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 |
"""