# VIPR Web App Without Docker

This guide describes the local setup of the VIPR Web App without Docker.

This setup has been tested on Linux and is the recommended platform for local web app development. On Windows, we recommend running the setup inside WSL2 rather than in a native Windows shell.

Here, `<base-dir>` is the directory that contains your VIPR repositories. The repositories `vipr-core`, `vipr-reflectometry-plugin`, `vipr-api`, and `vipr-frontend` should exist as sibling directories inside `<base-dir>`.

## Clone VIPR Projects

```bash
cd <base-dir>
git clone https://codebase.helmholtz.cloud/vipr/vipr-core.git
git clone https://codebase.helmholtz.cloud/vipr/vipr-reflectometry-plugin.git
git clone https://codebase.helmholtz.cloud/vipr/vipr-api.git
git clone https://codebase.helmholtz.cloud/vipr/vipr-frontend.git
```

## Install Redis

Recommended on Ubuntu/Debian-based systems such as Linux Mint:

```bash
sudo apt update
sudo apt install redis-server redis-tools
sudo systemctl enable redis-server
sudo systemctl start redis-server
redis-cli ping
```

If Redis is running correctly, the last command should return `PONG`.

Alternative without a system-wide installation:

```bash
cd ~
wget https://download.redis.io/redis-stable.tar.gz
tar xzf redis-stable.tar.gz
cd redis-stable
make
```

## Start Redis

If you installed Redis via `apt`, it should already be running after installation. To start it manually:

```bash
sudo systemctl start redis-server
```

You can check the status with:

```bash
systemctl status redis-server
```

If you used the local source build above, start Redis with:

```bash
~/redis-stable/src/redis-server --port 6379 --daemonize yes --logfile ~/redis.log --dir ~/
```

Note for later: shut Redis down again at the end.

For the `apt`-based installation:

```bash
sudo systemctl stop redis-server
```

For the local source build:

```bash
~/redis-stable/src/redis-cli shutdown
```

## Set Up the Environment and Prepare VIPR API

Change into `vipr-api` and create a virtual environment:

```bash
cd <base-dir>/vipr-api
python3.10 -m venv .venv
source .venv/bin/activate
pip install --upgrade pip
pip install -r requirements-dev.txt
```

Create `.env` and load the variables:

```bash
cp example.env .env
export $(grep -v '^#' .env | xargs)
```

Create the storage directories:

```bash
mkdir -p \
  storage/reflectometry \
  storage/reflectometry/flow_models/configs \
  storage/reflectometry/flow_models/saved_models \
  storage/huggingface_cache \
  storage/results
```

## Start the API

```bash
python -m vipr_api.main
```

During startup, the API downloads the HuggingFace models provided by the
installed VIPR plugins. The API should then be available at
`http://localhost:8000/docs`.

If the models are already available locally, skip the startup download:

```bash
VIPR_NO_MODEL_DOWNLOAD=true python -m vipr_api.main
```

## Start the Worker

In a new terminal:

```bash
cd <base-dir>/vipr-api
source .venv/bin/activate
export $(grep -v '^#' .env | xargs)
celery -A vipr_api.celery.celery_app worker --loglevel=debug -c 1
```

## Start the Frontend

The frontend requires Node.js and npm. We recommend [nvm](https://github.com/nvm-sh/nvm) to manage the Node.js version.

Install nvm:

```bash
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.3/install.sh | bash
```

Then install and activate the required Node.js version (pinned in `.nvmrc`):

```bash
cd <base-dir>/vipr-frontend
nvm install
nvm use
```

In a new terminal:

```bash
cd <base-dir>/vipr-frontend
npm i
npm run dev
```

The frontend should be available at `http://localhost:3000`.

## Try the FePt NSF Example

In the frontend, open the examples dialog by clicking the `Load Examples` button.

To try the FePt NSF example, download the model config and weights:

- [Model config](https://codebase.helmholtz.cloud/vipr/models/reflectometry-nsf-nr-maria/-/blob/models/models/fxc34ran/config.yaml?ref_type=heads)
- [Model weights](https://codebase.helmholtz.cloud/vipr/models/reflectometry-nsf-nr-maria/-/blob/models/models/fxc34ran/model.pt?ref_type=heads)

Save the files in the correct locations inside the `vipr-api` storage folder:

- Model config: `storage/reflectometry/flow_models/configs/fxc34ran.yaml`
- Model weights: `storage/reflectometry/flow_models/saved_models/fxc34ran.pt`

## Stop the Setup

Stop the API, worker, and frontend with `Ctrl+C` in their respective terminals.

Then stop Redis:

```bash
sudo systemctl stop redis-server
```

If you used the local source build instead, stop Redis with:

```bash
~/redis-stable/src/redis-cli shutdown
```
