The software world has made it fun and exciting for builders to create solutions and bring their ideas to life quicker than ever before. Sometimes, you just want to build something cool to show off or to learn a new concept or AI model by building it. Whether it's for a quick MVP or a POC to gauge traction in the real world, learning the combo of Streamlit for the frontend and FastAPI for your backend will make you a beast at this game. š¦¾
š What is FastAPI?
FastAPI is a fast and modern web framework for building APIs with Python. It boasts numerous advantages, such as:
- Speed: High performance, on par with Node.js and Go.
- Ease of Use: Simple and intuitive syntax.
- Less Code: Requires fewer lines of code to achieve the same functionality compared to other frameworks.
But there are other frameworks like Django and Flask, to name a few, that can also work well. Personally, I prefer FastAPI for many reasons, and the goal is to solve problems efficiently. So, remember to stick to one framework you like and understand. š
š ļø FastAPI Basics
1. Setting Up the Virtual Environment
First, let's set up our virtual environment to keep our project dependencies isolated.
python -m venv .venv
2. Activating the Virtual Environment
Activate the environment you just created:
source .venv/bin/activate
For Windows users:
.venv\Scripts\activate
3. Installing Required Packages
Next, install FastAPI and Uvicorn (an ASGI server):
pip install fastapi uvicorn
4. Creating the Main Application File
Create a new file named main.py
:
touch main.py
5. Writing the Basic FastAPI Application
Copy and paste the following code into your main.py
file:
from fastapi import FastAPI
import uvicorn
app = FastAPI()
@app.get("/")
async def root():
return {"message": "Hello World"}
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=8000)
6. Running the FastAPI Application
In your terminal, run the application:
python main.py
FastAPI automatically generates documentation using OpenAPI, which is one of the many features AI engineers love! š
Navigate to your local URL:
Yay!! Our first API is up and running with a simple "Hello World" response. But the fun is just beginning! š
š Project Summary
We will be building a simple FastAPI endpoint that takes a prompt and, with the help of Replicate's hosted Flux Image GenAI model, generates a cool image. Then, using Streamlit for our client-side, we'll call the endpoint using requests
to generate and present the image to the user. šØāØ
š¤ What is Replicate.com?
Replicate lets you run AI models with a cloud API, without having to understand machine learning or manage your own infrastructure. It's a game-changer for developers looking to integrate powerful AI capabilities quickly and effortlessly.
Steps to Get Started with Replicate:
- Create or Log In: Go to Replicate and create an account or log in.
- Access the Flux Model: Navigate to Flux 1.1 Pro.
Replicate provides excellent documentation for their APIs:
Select the Python documentation for seamless integration:
Obtaining Your Replicate API Token
To get the API key, follow these two steps:
Add Billing Information:
Go to Replicate Billing and follow the instructions to add your billing information.Create API Key:
Go to Replicate API Tokens to create your API key.
Export your API token in your terminal:
export REPLICATE_API_TOKEN=<paste-your-token-here>
š„ļø Enhancing FastAPI with Replicate
Install the necessary packages:
pip install replicate loguru
- Replicate: To interact with Replicate's API.
- Loguru: For logging errors, information, etc. Loguru Documentation
Example Code Provided by Replicate
import replicate
input = {
"prompt": "black forest gateau cake spelling out the words \"FLUX 1.1 Pro\", tasty, food photography",
"prompt_upsampling": True
}
output = replicate.run(
"black-forest-labs/flux-1.1-pro",
input=input
)
print(output)
#=> "https://replicate.delivery/czjl/HQZ89fz0ogUSJi2RLpuTyYER..."
Customizing the Code for Our Image Generation Endpoint
Replace the above with the following enhanced code in main.py
:
from fastapi import FastAPI
from pydantic import BaseModel
import uvicorn
import replicate
from loguru import logger
from typing import Dict
app = FastAPI()
# Configure Loguru to log into app.log with rotation
logger.add("app.log", rotation="500 MB")
class ImageInput(BaseModel):
prompt: str
prompt_upsampling: bool = True
@app.post("/generate")
async def generate_image(input_data: ImageInput) -> Dict[str, str]:
input_prompt = {
"prompt": input_data.prompt,
"prompt_upsampling": input_data.prompt_upsampling
}
# Generate image using Replicate's Flux model
output = replicate.run(
"black-forest-labs/flux-1.1-pro",
input=input_prompt
)
# Handle the output to get the image URL
if isinstance(output, replicate.helpers.FileOutput):
url = str(output)
else:
url = output
return {"message": "Image generated", "url": url}
@app.get("/")
async def root() -> Dict[str, str]:
logger.info("Root endpoint accessed")
return {"message": "Welcome to the Flux Image Generation API"}
if __name__ == "__main__":
logger.info("Starting the application")
uvicorn.run(app, host="127.0.0.1", port=8000)
Code Breakdown š§
-
Importing Necessary Libraries:
from fastapi import FastAPI from pydantic import BaseModel import uvicorn import replicate from loguru import logger from typing import Dict
-
Initializing FastAPI and Loguru:
app = FastAPI() logger.add("app.log", rotation="500 MB")
- FastAPI Instance: Creates the FastAPI app.
- Loguru Configuration: Logs will be stored in
app.log
with a rotation size of 500 MB.
-
Defining the Data Model:
class ImageInput(BaseModel): prompt: str prompt_upsampling: bool = True
- ImageInput: A Pydantic model that ensures the incoming data has the required structure.
-
Creating the
/generate
Endpoint:
@app.post("/generate") async def generate_image(input_data: ImageInput) -> Dict[str, str]: input_prompt = { "prompt": input_data.prompt, "prompt_upsampling": input_data.prompt_upsampling } # Generate image using Replicate's Flux model output = replicate.run( "black-forest-labs/flux-1.1-pro", input=input_prompt ) # Handle the output to get the image URL if isinstance(output, replicate.helpers.FileOutput): url = str(output) else: url = output return {"message": "Image generated", "url": url}
- Purpose: Takes a prompt from the user, generates an image using Replicate's Flux model, and returns the image URL.
-
Creating the Root Endpoint:
@app.get("/") async def root() -> Dict[str, str]: logger.info("Root endpoint accessed") return {"message": "Welcome to the Flux Image Generation API"}
- Purpose: A simple root endpoint that logs access and returns a welcome message.
-
Running the Application:
if __name__ == "__main__": logger.info("Starting the application") uvicorn.run(app, host="127.0.0.1", port=8000)
- Purpose: Starts the FastAPI application using Uvicorn.
šØ Building the Client with Streamlit
Now, let's create the client-side application using Streamlit to interact with our FastAPI backend.
1. Installing Streamlit and Requests
Install the necessary packages:
pip install streamlit requests
2. Creating the Streamlit Client
Create a new file named client.py
and add the following code:
import requests
import streamlit as st
def generate_image(prompt: str) -> str:
response = requests.post("http://localhost:8000/generate", json={"prompt": prompt})
return response.json()["url"]
def main():
st.title("šØ Flux 1.1 Pro Image Generator")
prompt = st.text_input("Enter a prompt for the image")
if st.button("Generate Image"):
if prompt:
with st.spinner("Generating image..."):
url = generate_image(prompt)
st.image(url, caption="Your Generated Image")
else:
st.error("ā Please enter a prompt")
if __name__ == "__main__":
main()
3. Code Explanation š
-
Importing Libraries:
import requests import streamlit as st
- Requests: To make HTTP requests to our FastAPI backend.
- Streamlit: To build the frontend interface.
-
Defining the
generate_image
Function:
def generate_image(prompt: str) -> str: response = requests.post("http://localhost:8000/generate", json={"prompt": prompt}) return response.json()["url"]
- Purpose: Sends the user's prompt to the FastAPI
/generate
endpoint and retrieves the generated image URL.
- Purpose: Sends the user's prompt to the FastAPI
-
Creating the Main Function:
def main(): st.title("šØ Flux 1.1 Pro Image Generator") prompt = st.text_input("Enter a prompt for the image") if st.button("Generate Image"): if prompt: with st.spinner("Generating image..."): url = generate_image(prompt) st.image(url, caption="Your Generated Image") else: st.error("ā Please enter a prompt")
- Title: Displays the app title.
- Text Input: Allows users to enter a prompt for image generation.
- Button: When clicked, it triggers the image generation process.
- Spinner: Shows a loading animation while the image is being generated.
- Image Display: Displays the generated image with a caption.
- Error Handling: Alerts the user if no prompt is entered.
-
Running the Main Function:
if __name__ == "__main__": main()
- Purpose: Ensures that the
main
function runs when the script is executed.
- Purpose: Ensures that the
4. Running the Streamlit Client
In your terminal, run the Streamlit app:
streamlit run client.py
Navigate to your local URL, typically http://localhost:8501.
5. Generating an Image
Enter a prompt like "a car"
and click on Generate Image.
Boom! Your AI-generated image appears in seconds. šāØ
š ļø Quick Setup
For a seamless setup, clone the repository and follow along with the README.md
:
š Replicate-Streamlit-FastAPI GitHub Repository
š Additional Resources
- Python FastAPI Crash Course
- Official FastAPI Documentation
- Replicate-Streamlit-FastAPI GitHub Repository
- Great Bolierplate Github Repository
š We so done!!
Congratulations on getting this far! š You've just built a powerful AI-powered image generator using FastAPI for the backend and Streamlit for the frontend. Now, you can launch amazing AI solutions that will make the world a better place. šāØ
š Ready to Level Up Your Tech Insights?
Hi there! š¤
Iām Cyprian Aarons, a Senior Software Engineer specializing in AI.
Join me on š¦ Twitter for daily deep-dives into cutting-edge tech, behind-the-scenes looks at innovative projects, and strategies to help you thrive in the digital era.
āØ Stay Connected & Informed! āØ
š š¢ Follow me on Twitter
š š° Subscribe to my Newsletter
š Why Follow or Subscribe?
- š In-Depth Analysis: Get detailed insights into the latest AI and tech trends.
- š„ Exclusive Content: Access behind-the-scenes content and project updates.
- š” Actionable Strategies: Learn strategies to excel in the digital landscape.
- š¬ Regular Updates: Receive newsletters packed with valuable information straight to your inbox.
Top comments (2)
Great introduction! Streamlit and FastAPI are a powerful combo for building AI-powered apps quickly and efficiently. Whether you're prototyping or showcasing a new model, these tools make development faster and more accessible. Perfect for those looking to dive into AI and create impressive MVPs.
App developers in India
Hello Abhilaasha Thank You !!