This a fully functioning example project for serverless Generative AI-as-a-Service (aka generative-ai-serverless).
The project was originally built as a fun project to generate custom images with AI based on the interests of a particular person or organization then to give the recipient(s) a URL that they could access and hit refresh to get new fun and random images. Basically, gen ai-as-a-Gift where the recipient(s) have their own custom URL (using a URL shortener) to access and share without needing to know the details of the service and AI models.
When deployed, this project is a self-contained scalable serverless environment for Generative AI in AWS using API Gateway and AWS Lambda. While you are likely familiar with serverless web services, for the purposes of this project this means that you do not have to maintain servers and infrastructure. Amazon Web Services (AWS) handles horizontal scaling, and you only pay for the time and resources used (which is perfect for a project like this or many types of large scale services). In many cases, running this project and services within AWS will be completely free if you are within the free tier limits.
- π Python 3.11 or greater
- AWS CLI Profile (
access keyandsecret access key) - Stability AI API Key
-
Clone this repository locally
-
Setup a
virtual environmentfrom the local directory that the repository was cloned into from the command-line:$ python3 -m pip install virtualenv $ python3 -m virtualenv venv
Note:
virtual environmentsmay be created in a different way, depending on yourpythoninstallation and configuration. For example, for some setups thevirtual environmentsetup will look similar to this without apythonversion and using thevenvpackage:$ python -m venv venv
-
Activate the
virtual environmentfrom within the directory of the local repository:$ venv/bin/activate # Note: The relative path to the activate script # may be at a location like the following: # $ venv\scripts\activate # Optionally, you may run one of the `activate` # script helper files in the top-level directory # of the source for this repository. # For example, on `windows` run `activate.bat` # from the command-line (which this will do): # > activate
-
Get/set your API keys. Create a new accounts at
Stability AIif you do not already have API credentials or a key(s).
$ export OPEN_AI_API_KEY=<open_ai_token>
$ export STABILITY_AI_API_KEY=<stability_ai_token>- Install the requirements needed to run the application locally with the following command from the same activated-
virtual environmentcommand-line window:
$ pip install -r requirements.txt- Run the local server from within the activated
virtual environmentwith auto-reload on code changes:
$ flask --app main.py --debug run- Access the
debugserver locally by opening a URL in your web browser of choice at the route returned from the server run process, i.e. typicallyhttp://127.0.0.1:5000. You should see aHello!response.
* Serving Flask app 'main.py'
* Debug mode: off
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
* Running on http://127.0.0.1:5000- Generate images at the
/funroute, e.g.http://127.0.0.1:5000/fun. You may also customize the images by appending the?prompt=<prompt modification>querystring likehttp://127.0.0.1:5000/fun?prompt=flying cat in a futuristic city.
The following will create a serverless stack of an API Gateway, Lambda, etc. in AWS and give you a URL that is exeternally accessible.
- Run the following command (it will error if you do not have the AWS CLI configured locally) from an activated-
virtual environmentcommand-line window with a deployment configuration fromzappa_settings.py(this project defaults to the configuration name ofgen-ai). The configuration file uses thezappa settings format:
$ zappa deploy gen-aiThe command line output will look similar to the following as the service deploys:
Deploying...
Your application is now live at: https://abc123.execute-api.us-east-1.amazonaws.com/gen-aiThe URL that is provided as part of the deploy or update process is a publicly accessible URL that you may access, share, run, etc. π
- If you make any changes to the service and wish to redeploy it (and replace all running instances) then run the following command from an activated-
virtual environmentcommand-line window
$ zappa update gen-aiNote: To teardown the resources and stack and make the URL no longer accessible run:
$ zappa undeploy gen-ai

