Save Replicate Images in GCloud
TLDR: A python FastAPI for replicate webhooks and uploading the images to GCloud buckets or sending it as emails for free with resend.com. Github repo at the end of the article.
Replicate.com is an API provider for AI images and more. Users can make requests to their API or even create images in the web interface. Most of the time you use models from other users that fine-tuned their models, but you could also upload your own and Replicate gives you the GPU infrastructure. Billing is processed for each successful API call to a model.
The models can take up to 20 seconds to generate an image. The first issue here as a developer is usually implementing webhooks for the requests. That means Replicate notifies you with a POST Request when the image is done. Fortunately webhooks are supported natively, you just have to look up the docs and implement the feature. The next questions is how to store images. I chose Google Cloud Buckets for long-term storage and emails for fast delivery to friends. Uploading images to Google Cloud is basically free if you just do personal projects. The email provider Resend is free for up to 100 Emails per day.
To use the Github repo you would need a google keyfile from type "service_account" in form of a json object. This keyfile json object can be passed to the google cloud storage SDK:
from google.cloud import storage
credentials = Credentials.from_service_account_info(keyfile)
storage_client = storage.Client(
project="your-project", credentials=credentials
)
The second step is to get a free API key from Resend:
import resend
resend.api_key = "your_api_key"
resend.Emails.send(params)
The third step is to configure the DNS records of a domain you own. I used my personal website for this. For the exact implementation details go to the resend documentation here: https://resend.com/docs/dashboard/domains/introduction
Most replicate models have a prompt and a negative prompt as input parameters. The negative prompt is as important as the prompt itself. I suggest writing a long negative prompt and wrapping the important parts in brackets. Example: (deformed iris, deformed pupils, big pupils, asymmetrical pupils, deformed teeth, blurry teeth)
Finally I want to give you the names of my favorite image models from replicate: automioai/carlosescobar93, diaphinus/bodegakitty, lucataco/realistic-vision-v5.1
Summary:
Thanks for reading!
Github Link: https://github.com/patricklenert/ai-images