Spaces:
Sleeping
Sleeping
Patrick Walukagga
commited on
Commit
·
348f9ae
1
Parent(s):
14a4318
Add README instructions
Browse files- .env.example +6 -0
- .gitignore +2 -0
- Dockerfile.api.prod +6 -2
- README.md +174 -1
- commands.md +4 -20
- infra/ecs_config.template +7 -0
- infra/ecs_config.toml +0 -9
.env.example
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
OPENAI_API_KEY=sk-xxxxxxxxxxxxxxxx
|
2 |
+
HF_TOKEN_PATRICK=hf_xxxxxxxxxxxxxxxxx
|
3 |
+
ZOTERO_LIBRARY_ID=1120xxxx
|
4 |
+
ZOTERO_API_ACCESS_KEY=Ky5RGxxxxxxxxxxxxxxxxxx
|
5 |
+
GRADIO_URL_=http://gradio:7860/
|
6 |
+
|
.gitignore
CHANGED
@@ -177,3 +177,5 @@ data/
|
|
177 |
study_export_*
|
178 |
study_files.db
|
179 |
study_files.json
|
|
|
|
|
|
177 |
study_export_*
|
178 |
study_files.db
|
179 |
study_files.json
|
180 |
+
|
181 |
+
infra/ecs_config_new.toml
|
Dockerfile.api.prod
CHANGED
@@ -2,8 +2,10 @@
|
|
2 |
# BUILDER #
|
3 |
###########
|
4 |
|
|
|
|
|
5 |
# pull official base image
|
6 |
-
FROM
|
7 |
|
8 |
# set work directory
|
9 |
WORKDIR /app
|
@@ -27,8 +29,10 @@ RUN pip wheel --no-cache-dir --no-deps --wheel-dir /app/wheels -r requirements.t
|
|
27 |
# FINAL #
|
28 |
#########
|
29 |
|
|
|
|
|
30 |
# pull official base image
|
31 |
-
FROM
|
32 |
|
33 |
# create directory for the app user
|
34 |
RUN mkdir -p /home/backend-app
|
|
|
2 |
# BUILDER #
|
3 |
###########
|
4 |
|
5 |
+
ARG AWS_ACCOUNT_ID
|
6 |
+
|
7 |
# pull official base image
|
8 |
+
FROM ${AWS_ACCOUNT_ID}.dkr.ecr.us-east-1.amazonaws.com/gradio-python:3.11.10-slim as builder
|
9 |
|
10 |
# set work directory
|
11 |
WORKDIR /app
|
|
|
29 |
# FINAL #
|
30 |
#########
|
31 |
|
32 |
+
ARG AWS_ACCOUNT_ID
|
33 |
+
|
34 |
# pull official base image
|
35 |
+
FROM ${AWS_ACCOUNT_ID}.dkr.ecr.us-east-1.amazonaws.com/gradio-python:3.11.10-slim
|
36 |
|
37 |
# create directory for the app user
|
38 |
RUN mkdir -p /home/backend-app
|
README.md
CHANGED
@@ -10,4 +10,177 @@ pinned: false
|
|
10 |
license: apache-2.0
|
11 |
---
|
12 |
|
13 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
license: apache-2.0
|
11 |
---
|
12 |
|
13 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
14 |
+
|
15 |
+
|
16 |
+
# ACRES RAG Project
|
17 |
+
|
18 |
+
## Project Setup
|
19 |
+
|
20 |
+
To test and run the project locally. Clone the project from github and change directoory to `acres`.
|
21 |
+
|
22 |
+
```sh
|
23 |
+
git clone https://github.com/SunbirdAI/acres.git
|
24 |
+
cd acres
|
25 |
+
```
|
26 |
+
|
27 |
+
Create python virtual environment and activate it.
|
28 |
+
|
29 |
+
```sh
|
30 |
+
python -m venv env
|
31 |
+
source env/bin/activate
|
32 |
+
```
|
33 |
+
|
34 |
+
Install project dependencies
|
35 |
+
|
36 |
+
```sh
|
37 |
+
pip install -r requirements.txt
|
38 |
+
```
|
39 |
+
|
40 |
+
## Run project locally
|
41 |
+
To test the project locally follow the steps below.
|
42 |
+
|
43 |
+
Copy `.env.example` to `.env` and provide the correct enviroment variable values.
|
44 |
+
|
45 |
+
```sh
|
46 |
+
cp .env.example .env
|
47 |
+
```
|
48 |
+
|
49 |
+
Run the application
|
50 |
+
|
51 |
+
```sh
|
52 |
+
python app.py
|
53 |
+
```
|
54 |
+
|
55 |
+
OR
|
56 |
+
|
57 |
+
```sh
|
58 |
+
gradio app.py
|
59 |
+
```
|
60 |
+
|
61 |
+
Browse the application with the link `http://localhost:7860/`
|
62 |
+
|
63 |
+
|
64 |
+
## Run with docker
|
65 |
+
To run the application with docker locally, first make sure you have docker installed. See [link](https://docs.docker.com/)
|
66 |
+
|
67 |
+
Build the project docker image
|
68 |
+
|
69 |
+
```sh
|
70 |
+
docker build -f Dockerfile.gradio -t gradio-app .
|
71 |
+
```
|
72 |
+
|
73 |
+
Create docker network
|
74 |
+
|
75 |
+
```sh
|
76 |
+
docker network create gradio-fastapi-network
|
77 |
+
```
|
78 |
+
|
79 |
+
Run the docker container
|
80 |
+
|
81 |
+
```sh
|
82 |
+
docker run -it -p 7860:7860 --rm --name gradio --network=gradio-fastapi-network gradio-app
|
83 |
+
```
|
84 |
+
|
85 |
+
Browse the application with the link `http://localhost:7860/`
|
86 |
+
|
87 |
+
|
88 |
+
## Deploy to AWS ECS (Elastic Container Service) with Fargate
|
89 |
+
|
90 |
+
Install and configure the AWS CLI and aws credentials. See [link](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-welcome.html)
|
91 |
+
|
92 |
+
OR: See the pdf document [here](./aws-cli.pdf)
|
93 |
+
|
94 |
+
Now follow the steps below to deploy to AWS ECS
|
95 |
+
|
96 |
+
Setup the default region and your aws account id
|
97 |
+
|
98 |
+
```sh
|
99 |
+
export AWS_DEFAULT_REGION=region # i.e us-east-1, eu-west-1
|
100 |
+
export AWS_ACCOUNT_ID=aws_account_id # ie. 2243838xxxxxx
|
101 |
+
```
|
102 |
+
|
103 |
+
Login into the AWS ECR (Elastic Container Registry) via the commandline
|
104 |
+
|
105 |
+
```sh
|
106 |
+
aws ecr get-login-password --region $AWS_DEFAULT_REGION | docker login --username AWS --password-stdin "$AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com
|
107 |
+
```
|
108 |
+
|
109 |
+
Create a python image and push to ECR. This image will be used as the base image for the application image deployed on AWS ECS.
|
110 |
+
|
111 |
+
- Create python repository
|
112 |
+
|
113 |
+
```sh
|
114 |
+
aws ecr create-repository \
|
115 |
+
--repository-name gradio-python \
|
116 |
+
--image-tag-mutability MUTABLE
|
117 |
+
```
|
118 |
+
|
119 |
+
```sh
|
120 |
+
export ECR_PYTHON_URL="$AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com/gradio-python"
|
121 |
+
echo $ECR_PYTHON_URL
|
122 |
+
```
|
123 |
+
|
124 |
+
- Pull python image and tag it to the ECR url
|
125 |
+
|
126 |
+
```sh
|
127 |
+
docker pull python:3.11.10-slim
|
128 |
+
docker tag python:3.11.10-slim $ECR_PYTHON_URL:3.11.10-slim
|
129 |
+
|
130 |
+
docker push $ECR_PYTHON_URL:3.11.10-slim
|
131 |
+
```
|
132 |
+
|
133 |
+
- Now create application repostory
|
134 |
+
|
135 |
+
```sh
|
136 |
+
aws ecr create-repository \
|
137 |
+
--repository-name gradio-app-prod \
|
138 |
+
--image-tag-mutability MUTABLE
|
139 |
+
|
140 |
+
export ECR_BACKEND_GRADIO_URL="$AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com/gradio-app-prod"
|
141 |
+
echo $ECR_BACKEND_GRADIO_URL
|
142 |
+
```
|
143 |
+
|
144 |
+
- Build the docker image for the production and push to ECR
|
145 |
+
|
146 |
+
```sh
|
147 |
+
docker build --build-arg AWS_ACCOUNT_ID=$AWS_ACCOUNT_ID -f Dockerfile.gradio.prod -t gradio-app-prod .
|
148 |
+
docker tag gradio-app-prod:latest "${ECR_BACKEND_GRADIO_URL}:latest"
|
149 |
+
docker push "${ECR_BACKEND_GRADIO_URL}:latest"
|
150 |
+
```
|
151 |
+
|
152 |
+
### Setup and Provision AWS ECS infra using AWS Cloudformation (IaC)
|
153 |
+
|
154 |
+
#### Install
|
155 |
+
To install the CFN-CLI run the command below
|
156 |
+
|
157 |
+
```sh
|
158 |
+
pip install cloudformation-cli cloudformation-cli-java-plugin cloudformation-cli-go-plugin cloudformation-cli-python-plugin cloudformation-cli-typescript-plugin
|
159 |
+
```
|
160 |
+
|
161 |
+
#### CFN-Toml
|
162 |
+
|
163 |
+
```sh
|
164 |
+
gem install cfn-toml
|
165 |
+
```
|
166 |
+
|
167 |
+
|
168 |
+
Copy `infra/ecs_config.template` to `infra/ecs_config.toml` and provide the correct `AWS Account ID` for the `ContainerImageGradio`
|
169 |
+
|
170 |
+
```sh
|
171 |
+
cp infra/ecs_config.template infra/ecs_config.toml
|
172 |
+
```
|
173 |
+
|
174 |
+
|
175 |
+
#### Deploy
|
176 |
+
|
177 |
+
To deploy the ECS infra run the commands below. It provisions the cloudformation stack changeset for review.
|
178 |
+
|
179 |
+
Log into your aws console and search for `cloudformation`. See and review the changeset. If everything is good execute the changeset to finish with the infra deployment.
|
180 |
+
|
181 |
+
Then look for the outputs to the link for the deployed application.
|
182 |
+
|
183 |
+
```sh
|
184 |
+
chmod u+x bin/cfn/*
|
185 |
+
./bin/cfn/ecs-deploy
|
186 |
+
```
|
commands.md
CHANGED
@@ -6,7 +6,7 @@ docker run -it -p 7860:7860 --rm --name gradio --network=gradio-fastapi-network
|
|
6 |
|
7 |
|
8 |
export AWS_DEFAULT_REGION=us-east-1
|
9 |
-
export AWS_ACCOUNT_ID=
|
10 |
aws ecr get-login-password --region $AWS_DEFAULT_REGION | docker login --username AWS --password-stdin "$AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com"
|
11 |
|
12 |
aws ecr create-repository \
|
@@ -29,8 +29,10 @@ aws ecr create-repository \
|
|
29 |
export ECR_BACKEND_GRADIO_URL="$AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com/gradio-app-prod"
|
30 |
echo $ECR_BACKEND_GRADIO_URL
|
31 |
|
32 |
-
|
33 |
docker build -f Dockerfile.gradio.prod -t gradio-app-prod .
|
|
|
|
|
34 |
docker tag gradio-app-prod:latest "${ECR_BACKEND_GRADIO_URL}:latest"
|
35 |
docker push "${ECR_BACKEND_GRADIO_URL}:latest"
|
36 |
|
@@ -49,21 +51,3 @@ docker build -f Dockerfile.api.prod -t fastapi-api-prod .
|
|
49 |
docker tag fastapi-api-prod:latest "${ECR_BACKEND_FASTAPI_URL}:latest"
|
50 |
docker push "${ECR_BACKEND_FASTAPI_URL}:latest"
|
51 |
|
52 |
-
|
53 |
-
Now how can I configure the two load balancers such that I can just access them without providing the ports
|
54 |
-
|
55 |
-
fastapi
|
56 |
-
|
57 |
-
|
58 |
-
```
|
59 |
-
http://dev-acres-fastapi-alb-1793670355.us-east-1.elb.amazonaws.com:8000/
|
60 |
-
```
|
61 |
-
|
62 |
-
http://dev-acres-gradio-alb-1860302806.us-east-1.elb.amazonaws.com/
|
63 |
-
|
64 |
-
gradio
|
65 |
-
|
66 |
-
|
67 |
-
```
|
68 |
-
http://dev-acres-gradio-alb-1860302806.us-east-1.elb.amazonaws.com:7860/
|
69 |
-
```
|
|
|
6 |
|
7 |
|
8 |
export AWS_DEFAULT_REGION=us-east-1
|
9 |
+
export AWS_ACCOUNT_ID=2244276xxxxx
|
10 |
aws ecr get-login-password --region $AWS_DEFAULT_REGION | docker login --username AWS --password-stdin "$AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com"
|
11 |
|
12 |
aws ecr create-repository \
|
|
|
29 |
export ECR_BACKEND_GRADIO_URL="$AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com/gradio-app-prod"
|
30 |
echo $ECR_BACKEND_GRADIO_URL
|
31 |
|
32 |
+
docker build --build-arg AWS_ACCOUNT_ID=2244276xxxxx -t your-image-name .
|
33 |
docker build -f Dockerfile.gradio.prod -t gradio-app-prod .
|
34 |
+
|
35 |
+
docker build --build-arg AWS_ACCOUNT_ID=$AWS_ACCOUNT_ID -f Dockerfile.gradio.prod -t gradio-app-prod .
|
36 |
docker tag gradio-app-prod:latest "${ECR_BACKEND_GRADIO_URL}:latest"
|
37 |
docker push "${ECR_BACKEND_GRADIO_URL}:latest"
|
38 |
|
|
|
51 |
docker tag fastapi-api-prod:latest "${ECR_BACKEND_FASTAPI_URL}:latest"
|
52 |
docker push "${ECR_BACKEND_FASTAPI_URL}:latest"
|
53 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
infra/ecs_config.template
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
[deploy]
|
2 |
+
bucket = 'dev-acres-gradio-bucket'
|
3 |
+
region = 'us-east-1'
|
4 |
+
stack_name = 'AcresRag'
|
5 |
+
|
6 |
+
[parameters]
|
7 |
+
ContainerImageGradio = '224427659xxxx.dkr.ecr.us-east-1.amazonaws.com/gradio-app-prod:latest'
|
infra/ecs_config.toml
DELETED
@@ -1,9 +0,0 @@
|
|
1 |
-
[deploy]
|
2 |
-
bucket = 'dev-acres-gradio-bucket'
|
3 |
-
region = 'us-east-1'
|
4 |
-
stack_name = 'AcresRag'
|
5 |
-
|
6 |
-
[parameters]
|
7 |
-
ContainerImageGradio = '224427659724.dkr.ecr.us-east-1.amazonaws.com/gradio-app-prod:latest'
|
8 |
-
ContainerImageFastAPI = '224427659724.dkr.ecr.us-east-1.amazonaws.com/fastapi-api-prod:latest'
|
9 |
-
CertificateArn = 'arn:aws:acm:us-east-1:224427659724:certificate/37ae35a9-60d1-4ff2-8afc-07f0d6000e75'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|