Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding gradio example #553

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
247 changes: 247 additions & 0 deletions examples/cdk/gradio/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,247 @@
flagged/

.aws-sam

# Created by https://www.gitignore.io/api/osx,linux,python,windows,pycharm,visualstudiocode

### Linux ###
*~

# temporary files which can be created if a process still has a handle open of a deleted file
.fuse_hidden*

# KDE directory preferences
.directory

# Linux trash folder which might appear on any partition or disk
.Trash-*

# .nfs files are created when an open file is removed but is still being accessed
.nfs*

### OSX ###
*.DS_Store
.AppleDouble
.LSOverride

# Icon must end with two \r
Icon

# Thumbnails
._*

# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent

# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk

### PyCharm ###
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839

# User-specific stuff:
.idea/**/workspace.xml
.idea/**/tasks.xml
.idea/dictionaries

# Sensitive or high-churn files:
.idea/**/dataSources/
.idea/**/dataSources.ids
.idea/**/dataSources.xml
.idea/**/dataSources.local.xml
.idea/**/sqlDataSources.xml
.idea/**/dynamic.xml
.idea/**/uiDesigner.xml

# Gradle:
.idea/**/gradle.xml
.idea/**/libraries

# CMake
cmake-build-debug/

# Mongo Explorer plugin:
.idea/**/mongoSettings.xml

## File-based project format:
*.iws

## Plugin-specific files:

# IntelliJ
/out/

# mpeltonen/sbt-idea plugin
.idea_modules/

# JIRA plugin
atlassian-ide-plugin.xml

# Cursive Clojure plugin
.idea/replstate.xml

# Ruby plugin and RubyMine
/.rakeTasks

# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
fabric.properties

### PyCharm Patch ###
# Comment Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-215987721

# *.iml
# modules.xml
# .idea/misc.xml
# *.ipr

# Sonarlint plugin
.idea/sonarlint

### Python ###
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
*.egg-info/
.installed.cfg
*.egg

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.coverage.*
.cache
.pytest_cache/
nosetests.xml
coverage.xml
*.cover
.hypothesis/

# Translations
*.mo
*.pot

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
target/

# Jupyter Notebook
.ipynb_checkpoints

# pyenv
.python-version

# celery beat schedule file
celerybeat-schedule.*

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/

### VisualStudioCode ###
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
.history

### Windows ###
# Windows thumbnail cache files
Thumbs.db
ehthumbs.db
ehthumbs_vista.db

# Folder config file
Desktop.ini

# Recycle Bin used on file shares
$RECYCLE.BIN/

# Windows Installer files
*.cab
*.msi
*.msm
*.msp

# Windows shortcuts
*.lnk

# Build folder

*/build/*

# End of https://www.gitignore.io/api/osx,linux,python,windows,pycharm,visualstudiocode
126 changes: 126 additions & 0 deletions examples/cdk/gradio/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
# Gradio example

A basic Gradio application example. You can build and test it locally as a typical Gradio application.

Using AWS Lambda Web Adapter, You can package this web application into Docker image, push to ECR, and deploy to Lambda, ECS/EKS, or EC2.

The application can be deployed in an AWS account using the [Serverless Application Model](https://github.com/awslabs/serverless-application-model).


Here will see how to do it by [AWS CDK](https://aws.amazon.com/cdk/)

# Pre-requisites
* [AWS CLI](https://aws.amazon.com/cli/)
* [Python](https://www.python.org/)
* [Docker](https://www.docker.com/products/docker-desktop)
* [AWS CDK](https://docs.aws.amazon.com/cdk/v2/guide/getting_started.html)



### IAM Rules:
- permission to Pull images from Public Repository
```json
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "ecr-public:PutRegistryAlias",
"Resource": "arn:aws:ecr-public::123456789012:registry/*"
}
]
}
```

### CDK Env variables
```bash
CDK_DEFAULT_ACCOUNT=$(aws sts get-caller-identity --query Account)
CDK_DEFAULT_REGION=$(aws configure get region)
```



## Dockerfile

```Dockerfile
# Base Image
FROM public.ecr.aws/docker/library/python:3.8.12-slim-buster
# Lambda Adapter
# lambda adapter binary into /opt/extensions. This is the only change to run the application on Lambda.
COPY --from=public.ecr.aws/awsguru/aws-lambda-adapter:0.8.4 /lambda-adapter /opt/extensions/lambda-adapter
ENV PORT=8080

WORKDIR /var/task

# Install system dependencies if needed
RUN apt-get update && apt-get install -y --no-install-recommends build-essential && rm -rf /var/lib/apt/lists/*

# Copy and install Python dependencies
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

# Copy application code
COPY . .

# Expose Gradio default port
EXPOSE 8080

# Use explicit Python interpreter path
CMD ["python", "app.py"]
```

Line 2 *copies lambda adapter binary into /opt/extensions. This is the only change to run the Flask application on Lambda.*

# Build and Deploy

In cdk.json we focusing on cdk.py
```json
{
"app": "python3 cdk.py"
}
```


In addition to Lambda function add function url so it'll easy for redirect. but it'll refresh whenever lambda function refreshes.

```py
# STACK
class GradioLambdaFn(Stack):
def __init__(self,scope:Construct,construct_id:str,**kwargs)->None:
super().__init__(scope,construct_id,**kwargs)

# Lambda Fn
lambda_fn = DockerImageFunction(
self,
id="AWSLambdaAdapterGradioExample",
code=DockerImageCode.from_image_asset( directory= os.path.dirname(__file__) , file="Dockerfile"),
architecture=Architecture.X86_64,
timeout=Duration.minutes(10),
)

# HTTP URL add
fn_url = lambda_fn.add_function_url(auth_type=FunctionUrlAuthType.NONE)

# print
CfnOutput(self,id="functionURL",value=fn_url.url,description="cfn_output")

# APP
app = App()
gradio_lambda = GradioLambdaFn(app,"GradioLambda",env=env)
app.synth()
```



# Deploy CDK
Navigate to folder and use the CDK CLI to build a CloudFormation Stack

To deploy cdk app into AWS
```bash
cdk synth
cdk deploy --verbose
cdk destroy
```

## Verify it works
Open URL's URL in a browser, here we print in the console by `CfnOutput` you should see "Gradio" on the page.
Loading