There's more...
Check out the reference for File
when doing more generic file I/O, and other
methods of Image
to see all supported formats.
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<2",
],
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.
Check out the reference for File
when doing more generic file I/O, and other
methods of Image
to see all supported formats.
Don't forget to add a dependency against pydantic version 1.x (version 2
not yet compatible) to your function if you want to use the Image
class.