Skip to content

jgill333/generative-ai-serverless

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

1 Commit
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸŽβ™ΎοΈ Generative AI-as-a-Service - Serverless Template

This a fully functioning example project for serverless Generative AI-as-a-Service (aka generative-ai-serverless).

Purpose

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.

Connect

LinkedIn

Pre-requisites

Python

  • 🐍 Python 3.11 or greater
  • AWS CLI Profile (access key and secret access key)
  • Stability AI API Key

Setup Steps

  1. Clone this repository locally

  2. Setup a virtual environment from the local directory that the repository was cloned into from the command-line:

    $ python3 -m pip install virtualenv
    $ python3 -m virtualenv venv

    Note: virtual environments may be created in a different way, depending on your python installation and configuration. For example, for some setups the virtual environment setup will look similar to this without a python version and using the venv package:

    $ python -m venv venv
  3. Activate the virtual environment from 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
  4. Get/set your API keys. Create a new accounts at Stability AI if 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>
  1. Install the requirements needed to run the application locally with the following command from the same activated-virtual environment command-line window:
$ pip install -r requirements.txt
  1. Run the local server from within the activated virtual environment with auto-reload on code changes:
$ flask --app main.py --debug run
  1. Access the debug server locally by opening a URL in your web browser of choice at the route returned from the server run process, i.e. typically http://127.0.0.1:5000. You should see a Hello! 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
  1. Generate images at the /fun route, e.g. http://127.0.0.1:5000/fun. You may also customize the images by appending the ?prompt=<prompt modification> querystring like http://127.0.0.1:5000/fun?prompt=flying cat in a futuristic city.

Deploy Steps

The following will create a serverless stack of an API Gateway, Lambda, etc. in AWS and give you a URL that is exeternally accessible.

  1. Run the following command (it will error if you do not have the AWS CLI configured locally) from an activated-virtual environment command-line window with a deployment configuration from zappa_settings.py (this project defaults to the configuration name of gen-ai). The configuration file uses the zappa settings format:
$ zappa deploy gen-ai

The 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-ai

The URL that is provided as part of the deploy or update process is a publicly accessible URL that you may access, share, run, etc. πŸŽ‰

  1. 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 environment command-line window
$ zappa update gen-ai

Note: To teardown the resources and stack and make the URL no longer accessible run:

$ zappa undeploy gen-ai

About

🎁 🐍 Generative AI-as-a-Service - Serverless template

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors