Skip to content

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.

import fal
from fal.toolkit import Image
MODEL_NAME = "google/ddpm-cat-256"
@fal.function(
requirements=[
"diffusers[torch]",
"transformers",
"pydantic",
],
machine_type="GPU-A100",
)
def generate_image():
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])
if __name__ == "__main__":
cat_image = generate_image()
print(f"Here is your cat: {cat_image.url}")

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.