Functions | fal.ai Private Apps
In addition to fal Apps, fal also supports defining serverless functions that can be called from your code just like any other Python function, but they are executed on a remote machine.
Functions are useful for one-off jobs that have a natural end, such as copying data to the /data
volume or training a model.
import fal
@fal.function(machine_type="GPU", requirements=["diffusers", "torch", "transformers"])def generate_image(prompt: str): import torch from diffusers import FluxPipeline from io import BytesIO pipe = FluxPipeline.from_pretrained("black-forest-labs/FLUX.1-schnell", torch_dtype=torch.bfloat16) pipe.enable_model_cpu_offload()
out = pipe( prompt=prompt, guidance_scale=0., height=768, width=1360, num_inference_steps=4, max_sequence_length=256, ).images[0]
buffer = BytesIO() out.save(buffer, format="PNG") image_bytes = buffer.getvalue() return image_bytes
image_bytes = generate_image("A cat holding a sign that says hello world")with open("image.png", "wb") as f: f.write(image_bytes)