Skip to content

Commit

Permalink
ci: update dockerfile, add lambda template
Browse files Browse the repository at this point in the history
  • Loading branch information
zhu-xiaowei committed Nov 14, 2024
1 parent a19e9ed commit 7407e58
Show file tree
Hide file tree
Showing 3 changed files with 456 additions and 14 deletions.
9 changes: 3 additions & 6 deletions server/src/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
FROM python:3.12-slim
FROM public.ecr.aws/docker/library/python:3.12.0-slim-bullseye
COPY --from=public.ecr.aws/awsguru/aws-lambda-adapter:0.8.4 /lambda-adapter /opt/extensions/lambda-adapter

WORKDIR /app

COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

COPY main.py .

EXPOSE 8000
RUN pip install --no-cache-dir -r requirements.txt

CMD ["python", "main.py"]
39 changes: 31 additions & 8 deletions server/src/main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import base64
from typing import List

import uvicorn
from fastapi import FastAPI, HTTPException, Depends
from fastapi.responses import StreamingResponse, PlainTextResponse
Expand All @@ -12,11 +11,18 @@
from pydantic import BaseModel
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
from typing import Annotated
from urllib.request import urlopen, Request
import time

app = FastAPI()
security = HTTPBearer()

auth_token = ''
CACHE_DURATION = 120000
cache = {
"latest_version": "",
"last_check": 0
}


class ImageRequest(BaseModel):
Expand Down Expand Up @@ -62,11 +68,6 @@ def verify_api_key(credentials: Annotated[HTTPAuthorizationCredentials, Depends(
return credentials.credentials


@app.get("/hello/{name}")
async def hello(name: str):
return f"Hello {name}!"


@app.post("/api/converse")
async def converse(request: ConverseRequest,
_: Annotated[str, Depends(verify_api_key)]):
Expand Down Expand Up @@ -177,7 +178,7 @@ async def upgrade(request: UpgradeRequest):
global auth_token
auth_token = ''
get_api_key_from_ssm()
new_version = os.getenv("APP_VERSION", "0.0.0")
new_version = get_latest_version()
total_number = calculate_version_total(request.version)
need_upgrade = False
url = ''
Expand All @@ -196,10 +197,32 @@ def calculate_version_total(version: str) -> int:
versions = version.split(".")
total_number = 0
if len(versions) == 3:
total_number = int(versions[0]) * 100 + int(versions[1]) * 10 + int(versions[2])
total_number = int(versions[0]) * 10000 + int(versions[1]) * 100 + int(versions[2])
return total_number


def get_latest_version() -> str:
timestamp = int(time.time() * 1000)
if cache["last_check"] > 0 and timestamp - cache["last_check"] < CACHE_DURATION:
return cache["latest_version"]
req = Request(
f"https://api.github.com/repos/awslabs/clickstream-swift/tags",
headers={
'User-Agent': 'Mozilla/5.0'
}
)
try:
with urlopen(req) as response:
content = response.read().decode('utf-8')
latest_version = json.loads(content)[0]['name']
cache["latest_version"] = latest_version
cache["last_check"] = timestamp
return json.loads(content)[0]['name']
except Exception as error:
print(f"Error occurred when get github tag: {error}")
return '0.0.0'


def get_image(client, model_id, prompt, width, height):
try:
seed = random.randint(0, 2147483647)
Expand Down
Loading

0 comments on commit 7407e58

Please sign in to comment.