Spaces:
Sleeping
Sleeping
File size: 4,070 Bytes
4a84072 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 | from datetime import datetime
from enum import Enum
import numpy as np
import pandas as pd
from pydantic import BaseModel, Field, ValidationError, field_validator, model_validator
class SmallCardNum:
pass
class Continuous:
pass
class DateTime:
pass
class Nominal:
pass
class Route(BaseModel):
label: int = Field(
description="Classify user queries as: 0 for Irrelevant/Vague/Incomplete, 1 for Visualizable, and 2 for SQL-only."
)
class SQLQueryModel(BaseModel):
sql_query: str = Field(..., description="SQL query to execute.")
explanation: str = Field(..., description="Short explanation of the SQL query.")
class DataPoint(BaseModel):
x: int | float | str | None = None
y: int | float | str | None = None
bin_start: int | float | None = None
bin_end: int | float | None = None
frequency: int | float | None = None
@field_validator("bin_start", "bin_end", "frequency", "x", "y", mode="before")
@classmethod
def to_native(cls, field_value):
if field_value is not None and isinstance(
field_value, np.float64 | np.float32 | np.int64
):
return float(field_value)
if isinstance(field_value, (datetime, np.datetime64, pd.Timestamp)): # noqa: UP038
return field_value.strftime("%Y-%m-%d")
return field_value
@model_validator(mode="before")
@classmethod
def validate_keys(cls, values):
x, y = values.get("x"), values.get("y")
bin_start, bin_end, frequency = (
values.get("bin_start"),
values.get("bin_end"),
values.get("frequency"),
)
xy = x is not None and y is not None
bxy = bin_start is not None and bin_end is not None and frequency is not None
if not (xy or bxy):
raise ValueError(
"Invalid input: Must provide either (x, y) OR (bin_start, bin_end, frequency), but not a mix."
)
return values
class Data(BaseModel):
data: list[DataPoint] = Field(default_factory=list)
@classmethod
def validate_data(cls, data):
try:
return cls(data=data)
except ValidationError as e:
raise ValueError(f"Invalid data format: {e.errors()[0]}") # noqa: B904
class TableData(BaseModel):
data: pd.DataFrame = Field(default_factory=None)
class Config:
arbitrary_types_allowed = True
@model_validator(mode="after")
def timestamp_to_str(self):
# Convert all datetime columns to string format
for col in self.data.select_dtypes(include=["datetime"]).columns:
if col:
self.data[col] = self.data[col].astype(str)
return self
def model_dump(self, *args, **kwargs): # noqa: ARG002
return self.data.to_dict(orient="list")
class Charts(BaseModel):
bar: Data | None = None
line: Data | None = None
pie: Data | None = None
hist: Data | None = None
@model_validator(mode="after")
def process_charts_data(self):
def stringify(data):
if data and data.data:
for point in data.data:
if not isinstance(point.x, str):
point.x = str(point.x)
return data
if self.bar:
self.bar = stringify(self.bar)
if self.pie:
self.pie = stringify(self.pie)
return self
class PlotType(str, Enum):
bar = ("bar",)
line = ("line",)
pie = ("pie",)
hist = ("hist",)
class PlotConfig(BaseModel):
type: PlotType = Field(
description="Type of plot, e.g., 'bar', 'line', 'pie'. Supported types depend on ShadCN implementation.",
)
title: str = Field(description="Title of the plot to display above the plot.")
x_axis_label: str = Field(description="Label for the X-axis of the plot.")
y_axis_label: str = Field(description="Label for the Y-axis of the plot.")
legend: bool = Field(
default=True, description="Flag to display a legend for the plot."
)
|