Skip to content

Applications

fal Applications are FastAPI applications that can be deployed to fal’s private serverless compute infrastructure.

FastAPI and fal Applications are fully compatible with Pydantic. Any features of Pydantic used in fal endpoint arguments will also work.

Pydantic features can be used for data validation in your endpoint. In the example below, you can set some of the parameters as optional, set default values, and apply other types of validation such as constraints and types.

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.29.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
# Returning files and images from functions
Saving images to your persistent directory is not always a convenient way to access them (you can use the File Explorer provided by the fal Web UI.) Alternatively, when dealing with image inputs and outputs, you can use fal's file and image classes to simplify the process.
```py {2,11,21,26}
import fal
from fal.toolkit import Image
MODEL_NAME = "google/ddpm-cat-256"
```py {2,11,21,26}
class FalModel(
fal.App,
requirements=[
"diffusers[torch]",
"transformers",
"pydantic",
],
):
machine_type = "GPU"
@fal.endpoint("/")
def generate_image(self, input: Input) -> Output:
from diffusers import DDPMPipeline
pipe = DDPMPipeline.from_pretrained(MODEL_NAME, use_safetensors=True)
pipe = pipe.to("cuda")
result = pipe(num_inference_steps=25)
return Image.from_pil(result.images[0])

Constructing an Image object on a serverless function automatically uploads it to fal’s block storage system and gives you a signed link for 2 days in which you can view or download it securely to have a copy of it as long as you need.