Quick Start - Deploy Your First Model
Deploy a text-to-image AI model in under 5 minutes. This tutorial walks you through creating your own image generation API using Stable Diffusion XL.
Before You Start
You’ll need:
- Python - we recommend 3.11
- A fal account (sign up is free)
Step 1: Install the CLI
pip install fal
Step 2: Authenticate
Get your API key from the fal dashboard and authenticate:
fal auth login
This opens your browser to authenticate with fal. Once complete, your credentials are saved locally.
Step 3: Create Your First Image Generator
Create a file called image_generator.py
with this Stable Diffusion XL text-to-image model:
import falfrom pydantic import BaseModel, Fieldfrom fal.toolkit import Image
class Input(BaseModel): prompt: str = Field( description="The prompt to generate an image from", examples=["A beautiful image of a cat"], )
class Output(BaseModel): image: Image
class MyApp(fal.App, keep_alive=300, name="my-demo-app"): machine_type = "GPU-H100" requirements = [ "hf-transfer==0.1.9", "diffusers[torch]==0.32.2", "transformers[sentencepiece]==4.51.0", "accelerate==1.6.0", ]
def setup(self): # Enable HF Transfer for faster downloads import os
os.environ["HF_HUB_ENABLE_HF_TRANSFER"] = "1"
import torch from diffusers import StableDiffusionXLPipeline
# Load any model you want, we'll use stabilityai/stable-diffusion-xl-base-1.0 # Huggingface models will be automatically downloaded to # the persistent storage of your account (/data) self.pipe = StableDiffusionXLPipeline.from_pretrained( "stabilityai/stable-diffusion-xl-base-1.0", torch_dtype=torch.float16, variant="fp16", use_safetensors=True, ).to("cuda")
# Warmup the model before the first request self.warmup()
def warmup(self): self.pipe("A beautiful image of a cat")
@fal.endpoint("/") def run(self, request: Input) -> Output: result = self.pipe(request.prompt) image = Image.from_pil(result.images[0]) return Output(image=image)
Step 4: Test Your Image Generator
First, run your model locally to test it:
fal run image_generator.py::MyApp
This command:
- Starts your AI image generation app as a service
- Downloads and loads the Stable Diffusion XL model (cached after first run)
- Provides two URLs for interaction: a direct HTTP proxy and a web playground
- Allows you to test your app without authentication while
fal run
is active
The first execution will take a couple of minutes as the model is downloaded. Subsequent runs will be faster as the model is cached.
As soon as the app is ready, you’ll see a message like this:
INFO: Application startup complete.
Meaning you can now test your app by making HTTP requests to the provided URL:
curl $FAL_RUN_URL -H 'content-type: application/json' -H 'accept: application/json, */*;q=0.5' -d '{"prompt":"A cyberpunk cityscape at night with neon lights"}'
Alternatively, you can use the auto-generated playground web UI to interact with your app through a browser interface.
Step 5: Deploy Your Model (Optional)
Once you’re satisfied, deploy it to create a persistent URL for your model:
fal deploy image_generator.py::MyApp
This command:
- Deploys your model as a serverless app on GPU instances
- Makes your app available for requests, with runners managed automatically based on demand and scaling configuration (see scaling to learn more)
- Returns a unique persistent URL for your deployed model
What Just Happened?
🎉 Congratulations! You’ve successfully:
- Created an AI model: Built a Stable Diffusion XL image generator
- Tested locally: Ran your model and generated an image from a text prompt
- Deployed to the cloud: Made your model accessible via REST API (if you chose to deploy)
The beauty of this workflow is you can test and iterate locally before deploying to production.
Next Steps
Now that you have a working model, you can:
- Learn core concepts - Understand models, endpoints, and scaling
- Follow a complete tutorial - Build a more sophisticated model