传递参数并利用 Pydantic

fal 应用程序和 FAST API 与 Pydantic 完全兼容。fal 端点参数中使用的任何 Pydantic 功能也都可以使用。

Pydantic 功能可用于端点中的数据验证。在下面的示例中,您可以将某些参数设置为可选,设置默认值,并应用其他类型的验证,例如约束和类型。

py
import fal
from pydantic import BaseModel
from fal.toolkit import Image

class ImageModelInput(BaseModel):
    seed: int | None = Field(
        default=None,
        description="""
            The same seed and the same prompt given to the same version of Stable Diffusion
            will output the same image every time.
        """,
        examples=[176400],
    )
    num_inference_steps: int = Field(
        default=25,
        description="""
            Increasing the amount of steps tell the model that it should take more steps
            to generate your final result which can increase the amount of detail in your image.
        """,
        gt=0,
        le=100,
    )

class MyApp(fal.App(keep_alive=300)):
    machine_type = "GPU-A100"
    requirements = [
        "diffusers==0.28.0",
        "torch==2.3.0",
        "accelerate",
        "transformers",
    ]

    def setup(self):
        import torch
        from diffusers import StableDiffusionXLPipeline, DPMSolverSinglestepScheduler

        self.pipe = StableDiffusionXLPipeline.from_pretrained(
            "sd-community/sdxl-flash",
            torch_dtype=torch.float16,
        ).to("cuda")
        self.pipe.scheduler = DPMSolverSinglestepScheduler.from_config(
            self.pipe.scheduler.config,
            timestep_spacing="trailing",
        )

    @fal.endpoint("/")
    def generate_image(self, request: ImageModelInput) -> Image:
        result = self.pipe(request.prompt, num_inference_steps=7, guidance_scale=3)
        image = Image.from_pil(result.images[0])
        return image