Spaces:
Configuration error
Configuration error
File size: 2,006 Bytes
22bc712 360d0c5 22bc712 360d0c5 22bc712 360d0c5 22bc712 |
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 |
import inspect
import re
from typing import Union
from fastapi import Depends, Form
from pydantic import BaseModel
# Adapted from
# https://github.com/fastapi/fastapi/discussions/8971#discussioncomment-7892972
def FormDepends(cls: type[BaseModel]):
new_parameters = []
for field_name, model_field in cls.model_fields.items():
new_parameters.append(
inspect.Parameter(
name=field_name,
kind=inspect.Parameter.POSITIONAL_ONLY,
default=(
Form(...)
if model_field.is_required()
else Form(model_field.default)
),
annotation=model_field.annotation,
)
)
async def as_form_func(**data):
return cls(**data)
sig = inspect.signature(as_form_func)
sig = sig.replace(parameters=new_parameters)
as_form_func.__signature__ = sig # type: ignore
return Depends(as_form_func)
def _to_list_of_strings(input_value: Union[str, list[str]]) -> list[str]:
def split_and_strip(value: str) -> list[str]:
if re.search(r"[;,]", value):
return [item.strip() for item in re.split(r"[;,]", value)]
else:
return [value.strip()]
if isinstance(input_value, str):
return split_and_strip(input_value)
elif isinstance(input_value, list):
result = []
for item in input_value:
result.extend(split_and_strip(str(item)))
return result
else:
raise ValueError("Invalid input: must be a string or a list of strings.")
# Helper functions to parse inputs coming as Form objects
def _str_to_bool(value: Union[str, bool]) -> bool:
if isinstance(value, bool):
return value # Already a boolean, return as-is
if isinstance(value, str):
value = value.strip().lower() # Normalize input
return value in ("true", "1", "yes")
return False # Default to False if none of the above matches
|