This repository was archived by the owner on Jan 18, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdeploy.py
More file actions
165 lines (140 loc) · 5.51 KB
/
deploy.py
File metadata and controls
165 lines (140 loc) · 5.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# openadapter/providers/omniparser/deploy.py
import os
from pathlib import Path
from typing import Optional
from pydantic_settings import BaseSettings
from loguru import logger
import boto3
from cdktf import App, TerraformStack
from constructs import Construct
from cdktf_aws_provider import AwsProvider, ecr, ecs
class Config(BaseSettings):
"""Configuration for OmniParser deployment."""
AWS_ACCESS_KEY_ID: str
AWS_SECRET_ACCESS_KEY: str
AWS_REGION: str = "us-east-1"
PROJECT_NAME: str = "omniparser"
# ECR Settings
ECR_REPOSITORY_NAME: str = "omniparser"
# ECS Settings
ECS_CLUSTER_NAME: str = "omniparser-cluster"
ECS_SERVICE_NAME: str = "omniparser-service"
ECS_TASK_FAMILY: str = "omniparser-task"
ECS_CONTAINER_NAME: str = "omniparser"
# Container Settings
CONTAINER_PORT: int = 8000
HOST_PORT: int = 8000
CPU: int = 2048 # 2 vCPU
MEMORY: int = 16384 # 16GB
GPU_COUNT: int = 1
# Model Settings
SOM_MODEL_PATH: str = "/app/weights/icon_detect/model.pt"
CAPTION_MODEL_NAME: str = "florence2"
CAPTION_MODEL_PATH: str = "/app/weights/icon_caption_florence"
DEVICE: str = "cuda"
BOX_THRESHOLD: float = 0.05
class Config:
env_file = ".env"
env_file_encoding = "utf-8"
class OmniParserStack(TerraformStack):
def __init__(self, scope: Construct, id: str, config: Config):
super().__init__(scope, id)
# AWS Provider
AwsProvider(self, "AWS",
region=config.AWS_REGION,
access_key=config.AWS_ACCESS_KEY_ID,
secret_key=config.AWS_SECRET_ACCESS_KEY
)
# ECR Repository
repository = ecr.EcrRepository(self, "Repository",
name=config.ECR_REPOSITORY_NAME,
image_tag_mutability="MUTABLE",
force_delete=True
)
# ECS Cluster
cluster = ecs.EcsCluster(self, "Cluster",
name=config.ECS_CLUSTER_NAME,
capacity_providers=["FARGATE_SPOT"],
default_capacity_provider_strategies=[{
"capacity_provider": "FARGATE_SPOT",
"weight": 1
}]
)
# ECS Task Definition
task_definition = ecs.EcsTaskDefinition(self, "TaskDefinition",
family=config.ECS_TASK_FAMILY,
requires_compatibilities=["FARGATE"],
network_mode="awsvpc",
cpu=str(config.CPU),
memory=str(config.MEMORY),
container_definitions=json.dumps([{
"name": config.ECS_CONTAINER_NAME,
"image": f"{repository.repository_url}:latest",
"cpu": config.CPU,
"memory": config.MEMORY,
"essential": True,
"portMappings": [{
"containerPort": config.CONTAINER_PORT,
"hostPort": config.HOST_PORT,
"protocol": "tcp"
}],
"environment": [
{"name": "SOM_MODEL_PATH", "value": config.SOM_MODEL_PATH},
{"name": "CAPTION_MODEL_NAME", "value": config.CAPTION_MODEL_NAME},
{"name": "CAPTION_MODEL_PATH", "value": config.CAPTION_MODEL_PATH},
{"name": "DEVICE", "value": config.DEVICE},
{"name": "BOX_THRESHOLD", "value": str(config.BOX_THRESHOLD)}
],
"resourceRequirements": [{
"type": "GPU",
"value": str(config.GPU_COUNT)
}],
"logConfiguration": {
"logDriver": "awslogs",
"options": {
"awslogs-group": f"/ecs/{config.PROJECT_NAME}",
"awslogs-region": config.AWS_REGION,
"awslogs-stream-prefix": "ecs"
}
}
}])
)
def build_and_push_image(config: Config):
"""Build and push Docker image to ECR."""
ecr_client = boto3.client('ecr',
region_name=config.AWS_REGION,
aws_access_key_id=config.AWS_ACCESS_KEY_ID,
aws_secret_access_key=config.AWS_SECRET_ACCESS_KEY
)
# Create ECR repository if it doesn't exist
try:
ecr_client.create_repository(repositoryName=config.ECR_REPOSITORY_NAME)
except ecr_client.exceptions.RepositoryAlreadyExistsException:
pass
# Get ECR login token
auth = ecr_client.get_authorization_token()
token = auth['authorizationData'][0]['authorizationToken']
endpoint = auth['authorizationData'][0]['proxyEndpoint']
# Build Docker image
logger.info("Building Docker image...")
os.system(f"docker build -t {config.ECR_REPOSITORY_NAME} .")
# Tag and push image
repository_uri = f"{endpoint.replace('https://', '')}/{config.ECR_REPOSITORY_NAME}"
os.system(f"docker tag {config.ECR_REPOSITORY_NAME}:latest {repository_uri}:latest")
os.system(f"docker push {repository_uri}:latest")
logger.info(f"Image pushed to {repository_uri}")
return repository_uri
def deploy(config: Config = Config()):
"""Deploy OmniParser to AWS ECS."""
# Initialize CDK app
app = App()
# Build and push Docker image
repository_uri = build_and_push_image(config)
# Create stack
OmniParserStack(app, "omniparser", config)
# Synthesize and deploy
app.synth()
os.system("cdktf deploy --auto-approve")
logger.info("OmniParser deployed successfully")
if __name__ == "__main__":
deploy()