TODAM-tw / todam-ticket-system

The frontend with gradio and combined with the API endpoints for the ticket system.
Apache License 2.0
0 stars 0 forks source link

Deploy Gradio with Serverless AWS Lambda #10

Closed 1chooo closed 4 months ago

1chooo commented 4 months ago

Serverless Machine Learning Applications with Hugging Face Gradio and AWS Lambda

或許我們可以直接用 Docker 把 gradio 給打包成 lambda_function,需要把目前架構改寫,用 cdk 去執行包好的 image 把前端介面 run 在 AWS Lambda 達成 Serverless 架構。

# app.py
import gradio as gr

def build_aws_lambda_gradio():
    with gr.Blocks(
        title='AWS Lambda Gradio Test',
    ) as demo:

        gr.HTML(
            "<h1 align=center>AWS Lambda Gradio Test</h1>"
        )

    return demo

if __name__ == "__main__":
    build_aws_lambda_gradio().launch()
# requirements.txt
gradio
# cdk.py
import os
from pathlib import Path
from constructs import Construct
from aws_cdk import App, Stack, Environment, Duration, CfnOutput
from aws_cdk import Environment, Stack
from aws_cdk.aws_lambda import DockerImageFunction, DockerImageCode, Architecture, FunctionUrlAuthType

my_environment = Environment(account=os.environ["CDK_DEFAULT_ACCOUNT"], region=os.environ["CDK_DEFAULT_REGION"])

class GradioLambda(Stack):
    def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
        super().__init__(scope, construct_id, **kwargs)

        # create function
        lambda_fn = DockerImageFunction(
            self,
            "GradioApp",
            code=DockerImageCode.from_image_asset(str(Path.cwd()), file="Dockerfile"),
            architecture=Architecture.X86_64,
            memory_size=8192,
            timeout=Duration.minutes(2),
        )
        # add HTTPS url
        fn_url = lambda_fn.add_function_url(auth_type=FunctionUrlAuthType.NONE)
        CfnOutput(self, "functionUrl", value=fn_url.url)

app = App()
rust_lambda = GradioLambda(app, "GradioLambda", env=my_environment)

app.synth()
// cdk.json
{
  "app": "python3 cdk.py"
}

[!WARNING] Dockerfile 內的版本可能需要調整

FROM public.ecr.aws/docker/library/python:3.8.12-slim-buster

COPY --from=public.ecr.aws/awsguru/aws-lambda-adapter:0.4.0 /lambda-adapter /opt/extensions/lambda-adapter
WORKDIR /var/task

COPY requirements.txt  ./requirements.txt
RUN python -m pip install -r requirements.txt

COPY app.py  ...
CMD ["python3", "app.py"]
1chooo commented 4 months ago

@sh1un 在 #11 有新增 app.py 囉!搭配 app/, app.py, .env 直接透過 python app.py 即可執行

sh1un commented 4 months ago

@sh1un 在 #11 有新增 app.py 囉!搭配 app/, app.py, .env 直接透過 python app.py 即可執行

OK 相當感謝!

sh1un commented 4 months ago

Dockerfile 撰寫後來參考了這個 github repo: https://github.com/awslabs/aws-lambda-web-adapter/tree/main/examples/fastapi

1chooo commented 4 months ago

Dockerfile 撰寫後來參考了這個 github repo: https://github.com/awslabs/aws-lambda-web-adapter/tree/main/examples/fastapi

我之後加進文件的 reference,感謝