Skip to content

fix: install lightweight CPU torch to prevent EC2 deployment freeze #13

fix: install lightweight CPU torch to prevent EC2 deployment freeze

fix: install lightweight CPU torch to prevent EC2 deployment freeze #13

Workflow file for this run

name: Deploy to EC2
on:
push:
branches:
- main
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Deploy and Restart Services
uses: appleboy/ssh-action@v1.0.3
with:
host: ${{ secrets.EC2_HOST }}
username: ${{ secrets.EC2_USERNAME }}
key: ${{ secrets.EC2_SSH_KEY }}
port: ${{ secrets.EC2_PORT }}
script: |
# Pull latest changes natively via Git
if [ -d "$HOME/agrinn/.git" ]; then
cd ~/agrinn
git fetch origin
git reset --hard origin/main
git lfs pull
else
cd ~
git clone https://github.com/adityai0/AgriNN.git agrinn
cd agrinn
git lfs pull
fi
# Build Frontend natively on EC2
cd ~/agrinn/apps/web
npm install
npm run build
# Restart Frontend PM2
pm2 delete agrinn-web || true
PORT=3000 pm2 start npm --name agrinn-web -- start
pm2 save
# Setup Backend Dependencies
cd ~/agrinn/apps/api
if [ ! -d "venv" ]; then
python3 -m venv venv
fi
source venv/bin/activate
pip install torch torchvision --index-url https://download.pytorch.org/whl/cpu
pip install -r requirements.txt
# Restart Backend Systemd Service
if [ ! -f /etc/systemd/system/agrinn-api.service ]; then
sudo bash -c "cat > /etc/systemd/system/agrinn-api.service << 'EOF'
[Unit]
Description=AgriNN FastAPI Backend
After=network.target
[Service]
User=ec2-user
WorkingDirectory=/home/ec2-user/agrinn/apps/api
Environment=\"PATH=/home/ec2-user/agrinn/apps/api/venv/bin\" \"PYTHONPATH=/home/ec2-user/agrinn\"
ExecStart=/home/ec2-user/agrinn/apps/api/venv/bin/uvicorn app.main:app --host 0.0.0.0 --port 8000
Restart=always
[Install]
WantedBy=multi-user.target
EOF"
sudo systemctl daemon-reload
sudo systemctl enable agrinn-api
fi
sudo systemctl restart agrinn-api