# Deploy to PythonAnywhere (free, no card, doesn't sleep)

Unlike Render (sleeps in 15 min) or Hugging Face (sleeps after 48h and
has a new-account cooldown), PythonAnywhere's free tier:
- Never asks for a card
- Stays running continuously — good fit for a multi-week demo
- Keeps your database and uploaded files on persistent disk

The one open question: free accounts can only call a whitelisted set of
external sites. Google's `.googleapis.com` is on that whitelist, which
should cover the Gemini API — but test this early (Step 7) so you have
time to fall back to another host if it's blocked.

## Step 1 — Create the account
Go to https://www.pythonanywhere.com/registration/register/beginner/
and sign up (free "Beginner" account, no card).

## Step 2 — Open a Bash console
On your dashboard: **Consoles** tab → **Bash**.

## Step 3 — Clone your code
In the Bash console:

    git clone https://github.com/fypwork6-arch/LegalEase-Ai.git
    cd LegalEase-Ai

## Step 4 — Create a virtual environment and install dependencies

    mkvirtualenv --python=/usr/bin/python3.10 legalease-venv
    pip install -r requirements.txt

(`mkvirtualenv` activates it automatically. If your console ever shows
a plain prompt without `(legalease-venv)`, run `workon legalease-venv`.)

## Step 5 — Add your secret key
Still in the Bash console:

    nano .env

Paste this (your real Gemini key instead of the placeholder), then
press `Ctrl+O`, Enter, `Ctrl+X` to save and exit:

    GEMINI_API_KEYS=your-real-gemini-key-here
    GEMINI_MODEL=gemini-2.5-flash
    SECRET_KEY=make-up-any-long-random-string-here

## Step 6 — Create the web app
1. Go to the **Web** tab → **Add a new web app**
2. Choose **Manual configuration** (not Flask/Django wizard) → Python 3.10
3. It creates a WSGI file at a path like
   `/var/www/yourusername_pythonanywhere_com_wsgi.py` — click that link to edit it
4. **Delete everything in it** and paste this instead
   (replace `yourusername` with your actual PythonAnywhere username, twice):

    ```python
    import sys, os

    project_home = '/home/yourusername/LegalEase-Ai'
    if project_home not in sys.path:
        sys.path.insert(0, project_home)
    os.chdir(project_home)

    from dotenv import load_dotenv
    load_dotenv(os.path.join(project_home, '.env'))

    from a2wsgi import ASGIMiddleware
    from backend.main import app as fastapi_app

    application = ASGIMiddleware(fastapi_app)
    ```

5. Back on the **Web** tab, under **Virtualenv**, enter:
   `/home/yourusername/.virtualenvs/legalease-venv`
6. Under **Static files** you can leave it empty — templates are served by the app itself.

## Step 7 — Reload and test
1. Click the big green **Reload** button at the top of the Web tab
2. Visit `https://yourusername.pythonanywhere.com`
3. **Immediately test the AI features** — submit a case or open the chatbot.
   - If you get a real AI response: you're done, and this will run for weeks.
   - If you see a connection/network error specifically from the Gemini
     call (check the **Error log** link on the Web tab), the Gemini
     domain is blocked for free accounts. In that case the rest of the
     site (auth, dashboards, lawyer matching, filings) still works —
     only AI calls would need a different host.

## Updating the site later
Whenever the code changes:

    cd ~/LegalEase-Ai
    git pull
    workon legalease-venv
    pip install -r requirements.txt

Then click **Reload** on the Web tab again.

## If the Gemini API turns out to be blocked
Open a support ticket from the PythonAnywhere dashboard (Help → they
sometimes whitelist specific domains for free accounts on request), or
fall back to Hugging Face Spaces once its cooldown clears — everything
there is already set up and waiting.
