File size: 5,510 Bytes
823a364
919f403
823a364
 
 
 
64ab4c9
823a364
 
 
 
 
348f9ae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17249df
 
 
 
 
 
 
 
 
348f9ae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17249df
 
 
 
 
 
 
 
 
348f9ae
 
 
 
 
40b7cbf
348f9ae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17249df
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
348f9ae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
---
title: 'ACRES: Center For Rapid Evidence Synthesis'
emoji: πŸ‘
colorFrom: gray
colorTo: pink
sdk: gradio
sdk_version: 5.6.0
app_file: app.py
pinned: false
license: apache-2.0
---

Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference


# ACRES RAG Project

## Project Setup

To test and run the project locally. Clone the project from github and change directoory to `acres`.

```sh
git clone https://github.com/SunbirdAI/acres.git
cd acres
```

Create python virtual environment and activate it.

```sh
python -m venv env
source env/bin/activate
```

Install project dependencies

```sh
pip install -r requirements.txt
```

## Run project locally
To test the project locally follow the steps below.

Copy `.env.example` to `.env` and provide the correct enviroment variable values.

```sh
cp .env.example .env
```

Run the application

```sh
python app.py
```

OR

```sh
gradio app.py
```

Browse the application with the link `http://localhost:7860/`

### Run the api
Make sure the gradio app is running on port `7860` and then run the command below in another terminal tab in the same directory.

```sh
uvicorn api:app --reload
```

Browse the api at `http://localhost:8000/docs`


## Run with docker
To run the application with docker locally, first make sure you have docker installed. See [link](https://docs.docker.com/)

Build the project docker image

```sh
docker build -f Dockerfile.gradio -t gradio-app .
```

Create docker network

```sh
docker network create gradio-fastapi-network
```

Run the docker container 

```sh
docker run -it -p 7860:7860 --rm --name gradio --network=gradio-fastapi-network gradio-app
```

Browse the application with the link `http://localhost:7860/`

To run the api with docker run the commands below. The gradio container should be run first before running the api.

```sh
docker build -f Dockerfile.api -t fastapi-app .
docker run -it -p 8000:8000 --rm --name fastapi --network=gradio-fastapi-network fastapi-app
```

Browse the api at `http://localhost:8000/docs`


## Deploy to AWS ECS (Elastic Container Service) with Fargate

Install and configure the AWS CLI and aws credentials. See [link](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-welcome.html)

OR: See the pdf document [here](https://docs.aws.amazon.com/pdfs/cli/latest/userguide/aws-cli.pdf#getting-started-quickstart)

Now follow the steps below to deploy to AWS ECS

Setup the default region and your aws account id

```sh
export AWS_DEFAULT_REGION=region # i.e us-east-1, eu-west-1
export AWS_ACCOUNT_ID=aws_account_id # ie. 2243838xxxxxx
```

Login into the AWS ECR (Elastic Container Registry) via the commandline

```sh
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
```

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.

- Create python repository

```sh
aws ecr create-repository \
  --repository-name gradio-python \
  --image-tag-mutability MUTABLE
```

```sh
export ECR_PYTHON_URL="$AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com/gradio-python"
echo $ECR_PYTHON_URL
```

- Pull python image and tag it to the ECR url

```sh
docker pull python:3.11.10-slim
docker tag python:3.11.10-slim $ECR_PYTHON_URL:3.11.10-slim

docker push $ECR_PYTHON_URL:3.11.10-slim
```

- Now create application repostory

```sh
aws ecr create-repository \
  --repository-name gradio-app-prod \
  --image-tag-mutability MUTABLE

export ECR_BACKEND_GRADIO_URL="$AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com/gradio-app-prod"
echo $ECR_BACKEND_GRADIO_URL
```

- Build the docker image for the production and push to ECR

```sh
docker build --build-arg AWS_ACCOUNT_ID=$AWS_ACCOUNT_ID -f Dockerfile.gradio.prod -t gradio-app-prod .
docker tag gradio-app-prod:latest "${ECR_BACKEND_GRADIO_URL}:latest"
docker push "${ECR_BACKEND_GRADIO_URL}:latest"
```

- Now create fastapi repostory

```sh
aws ecr create-repository \
  --repository-name fastapi-api-prod \
  --image-tag-mutability MUTABLE

export ECR_BACKEND_FASTAPI_URL="$AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com/fastapi-api-prod"
echo $ECR_BACKEND_FASTAPI_URL
```

- Build the docker image for the production and push to ECR

```sh
docker build -f Dockerfile.api.prod -t fastapi-api-prod .
docker tag fastapi-api-prod:latest "${ECR_BACKEND_FASTAPI_URL}:latest"
docker push "${ECR_BACKEND_FASTAPI_URL}:latest"
```

### Setup and Provision AWS ECS infra using AWS Cloudformation (IaC)

#### Install
To install the CFN-CLI run the command below

```sh
pip install cloudformation-cli cloudformation-cli-java-plugin cloudformation-cli-go-plugin cloudformation-cli-python-plugin cloudformation-cli-typescript-plugin
```

#### CFN-Toml

```sh
gem install cfn-toml
```


Copy `infra/ecs_config.template` to `infra/ecs_config.toml` and provide the correct `AWS Account ID` for the `ContainerImageGradio`

```sh
cp infra/ecs_config.template infra/ecs_config.toml
```


#### Deploy

To deploy the ECS infra run the commands below. It provisions the cloudformation stack changeset for review. 

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.

Then look for the outputs to the link for the deployed application.

```sh
chmod u+x bin/cfn/*
./bin/cfn/ecs-deploy
```