AxonIQ-Testing / setup.py
ZeroNinerOne's picture
Upload 14 files
a4f8c46 verified
Raw History Blame Contribute Delete
13.5 kB
#!/usr/bin/env python3
"""
NeuroCheck v9.1 β€” Full Local Setup Script
==========================================
Run once before first launch:
python3 setup.py
What this does:
1. Checks Python version (needs 3.10+)
2. Creates / activates a virtual environment (.venv)
3. Installs ALL pip requirements
4. Builds the spaCy English model (en_model/)
5. Verifies NegEx pipeline
6. Initialises the SQLite database (neurocheck.db)
7. Checks Ollama + pulls llama3.1:8b if missing
8. Prints a ready summary + how to start the server
"""
from __future__ import annotations
import subprocess, sys, os, pathlib, json, textwrap, shutil, platform, urllib.request
ROOT = pathlib.Path(__file__).parent.resolve()
VENV = ROOT / ".venv"
DB = ROOT / "neurocheck.db"
MODEL = ROOT / "en_model"
ENV = ROOT / ".env"
# ── colour helpers ────────────────────────────────────────────────────────────
def green(s): return f"\033[92m{s}\033[0m"
def red(s): return f"\033[91m{s}\033[0m"
def yellow(s): return f"\033[93m{s}\033[0m"
def bold(s): return f"\033[1m{s}\033[0m"
def cyan(s): return f"\033[96m{s}\033[0m"
def header(msg):
print(f"\n{bold(cyan('══'))} {bold(msg)}")
def ok(msg): print(f" {green('βœ”')} {msg}")
def warn(msg): print(f" {yellow('⚠')} {msg}")
def fail(msg): print(f" {red('✘')} {msg}")
def step(msg): print(f" {cyan('β†’')} {msg}")
def run(cmd, *, cwd=None, capture=False, check=True):
r = subprocess.run(cmd, cwd=cwd or ROOT,
capture_output=capture,
text=True)
if check and r.returncode != 0:
fail(f"Command failed: {' '.join(str(c) for c in cmd)}")
if capture:
print(r.stderr or r.stdout)
sys.exit(1)
return r
# ── Resolve python inside venv ────────────────────────────────────────────────
def venv_python():
if platform.system() == "Windows":
return VENV / "Scripts" / "python.exe"
return VENV / "bin" / "python"
def venv_pip():
return [str(venv_python()), "-m", "pip"]
# ─────────────────────────────────────────────────────────────────────────────
# STEP 0 β€” Python version check
# ─────────────────────────────────────────────────────────────────────────────
header("Step 0 Β· Python version check")
major, minor = sys.version_info.major, sys.version_info.minor
if major < 3 or (major == 3 and minor < 10):
fail(f"Python 3.10+ required. You have {major}.{minor}.")
fail("Download from https://python.org/downloads")
sys.exit(1)
ok(f"Python {major}.{minor} βœ“")
# ─────────────────────────────────────────────────────────────────────────────
# STEP 1 β€” Virtual environment
# ─────────────────────────────────────────────────────────────────────────────
header("Step 1 Β· Virtual environment")
if VENV.exists():
ok(f".venv already exists at {VENV}")
else:
step("Creating .venv …")
run([sys.executable, "-m", "venv", str(VENV)])
ok(".venv created")
PY = str(venv_python())
step(f"Using Python: {PY}")
# ─────────────────────────────────────────────────────────────────────────────
# STEP 2 β€” Install requirements
# ─────────────────────────────────────────────────────────────────────────────
header("Step 2 Β· Install Python dependencies")
req_file = ROOT / "requirements.txt"
if not req_file.exists():
fail("requirements.txt not found"); sys.exit(1)
step("Upgrading pip …")
run(venv_pip() + ["install", "--quiet", "--upgrade", "pip"])
ok("pip upgraded")
step("Installing requirements (this may take 3-8 minutes first time) …")
run(venv_pip() + ["install", "--quiet", "-r", str(req_file)])
ok("All requirements installed βœ“")
# ─────────────────────────────────────────────────────────────────────────────
# STEP 3 β€” Build spaCy model
# ─────────────────────────────────────────────────────────────────────────────
header("Step 3 Β· spaCy English model")
if MODEL.exists() and (MODEL / "config.cfg").exists():
ok(f"Model already built at {MODEL}")
else:
step("Building blank spaCy English model …")
script = textwrap.dedent(f"""
import spacy
from spacy.lang.en import English
from pathlib import Path
nlp = English()
nlp.add_pipe('sentencizer')
out = Path(r'{MODEL}')
out.mkdir(parents=True, exist_ok=True)
nlp.to_disk(str(out))
print('Model saved to', out)
""")
run([PY, "-c", script])
ok("spaCy model built βœ“")
# ─────────────────────────────────────────────────────────────────────────────
# STEP 4 β€” Verify NegEx
# ─────────────────────────────────────────────────────────────────────────────
header("Step 4 Β· Verify NegEx pipeline")
script = textwrap.dedent(f"""
import spacy
from negspacy.termsets import termset
from negspacy.negation import Negex
nlp = spacy.load(r'{MODEL}')
ts = termset('en_clinical')
nlp.add_pipe('negex', config={{'ent_types': ['SYMPTOM'], 'neg_termset': ts.get_patterns()}})
print('NegEx OK')
""")
r = run([PY, "-c", script], capture=True, check=False)
if "NegEx OK" in (r.stdout or ""):
ok("NegEx pipeline verified βœ“")
else:
warn(f"NegEx warning: {r.stderr.strip()[:200]}")
warn("App will still run β€” NegEx may degrade negation detection")
# ─────────────────────────────────────────────────────────────────────────────
# STEP 5 β€” .env file
# ─────────────────────────────────────────────────────────────────────────────
header("Step 5 Β· Environment file (.env)")
if not ENV.exists():
warn(".env not found β€” creating from defaults")
import shutil as _sh
example = ROOT / ".env.example"
if example.exists():
_sh.copy(example, ENV)
else:
ENV.write_text(
"SPACY_MODEL=en_model\nNEUROCHECK_DB=neurocheck.db\n"
"JWT_SECRET=CHANGE_THIS\nJWT_EXPIRE_MINUTES=10080\n"
"HOST=0.0.0.0\nPORT=8000\nWORKERS=1\n",
encoding="utf-8",
)
warn(f"Edit {ENV} and set a real JWT_SECRET!")
else:
env_text = ENV.read_text(encoding="utf-8")
if "CHANGE_THIS" in env_text:
warn(f".env exists but JWT_SECRET is still the placeholder!")
warn(" Open .env and set: JWT_SECRET=<your random string>")
warn(" Generate one: python3 -c \"import secrets; print(secrets.token_hex(32))\"")
else:
ok(".env configured βœ“")
# ─────────────────────────────────────────────────────────────────────────────
# STEP 6 β€” Initialise SQLite database
# ─────────────────────────────────────────────────────────────────────────────
header("Step 6 Β· SQLite database")
# Load .env vars so database.py sees NEUROCHECK_DB
for line in ENV.read_text(encoding="utf-8").splitlines():
line = line.strip()
if line and not line.startswith("#") and "=" in line:
k, _, v = line.partition("=")
os.environ.setdefault(k.strip(), v.strip())
db_path = ROOT / os.environ.get("NEUROCHECK_DB", "neurocheck.db")
step(f"Database path: {db_path}")
script = textwrap.dedent(f"""
import sys, os
sys.path.insert(0, r'{ROOT}')
os.environ['NEUROCHECK_DB'] = r'{db_path}'
from api.database import init_db
init_db()
print('DB_OK')
""")
r = run([PY, "-c", script], capture=True, check=False)
if "DB_OK" in (r.stdout or ""):
ok(f"Database initialised: {db_path} βœ“")
# Show table info
import sqlite3
if db_path.exists():
with sqlite3.connect(str(db_path)) as conn:
tables = conn.execute("SELECT name FROM sqlite_master WHERE type='table'").fetchall()
for (t,) in tables:
count = conn.execute(f"SELECT COUNT(*) FROM {t}").fetchone()[0]
ok(f" Table '{t}': {count} rows")
else:
fail(f"Database init failed:\n{r.stderr.strip()[:500]}")
sys.exit(1)
# ─────────────────────────────────────────────────────────────────────────────
# STEP 7 β€” Ollama check
# ─────────────────────────────────────────────────────────────────────────────
header("Step 7 Β· Ollama & LLM model")
ollama_bin = shutil.which("ollama")
if not ollama_bin:
warn("Ollama not found in PATH.")
warn("Download + install from: https://ollama.com/download")
warn("Then run: ollama pull llama3.1:8b")
warn("The app will start but /chat will fail until Ollama is running.")
else:
ok(f"Ollama found: {ollama_bin}")
# Check if server running
try:
with urllib.request.urlopen("http://localhost:11434/api/tags", timeout=3) as resp:
tags = json.loads(resp.read())
models = [m["name"] for m in tags.get("models", [])]
if any("llama3.1" in m for m in models):
ok(f"llama3.1 model present βœ“")
else:
warn("llama3.1:8b not pulled yet.")
step("Pulling llama3.1:8b (β‰ˆ4.7 GB) β€” this runs in background …")
subprocess.Popen(["ollama", "pull", "llama3.1:8b"])
warn("Pull started in background. Wait for it before using /chat.")
except Exception:
warn("Ollama is installed but not running.")
warn("Start it with: ollama serve")
warn("Then pull model: ollama pull llama3.1:8b")
# ─────────────────────────────────────────────────────────────────────────────
# FINAL SUMMARY
# ─────────────────────────────────────────────────────────────────────────────
print(f"""
{bold(green('══════════════════════════════════════════════════'))}
{bold(green(' NeuroCheck setup complete!'))}
{bold(green('══════════════════════════════════════════════════'))}
{bold('To start the server:')}
{cyan('# Option A β€” using the venv directly (recommended)')}
{yellow('.venv/bin/python -m uvicorn api.main:app --host 0.0.0.0 --port 8000 --reload')}
{cyan('# Option B β€” using the setup script')}
{yellow('.venv/bin/python scripts/setup_and_run.py')}
{bold('Once running, open:')}
🌐 http://localhost:8000 β†’ MS Symptom Classifier
πŸ’¬ http://localhost:8000/chat-ui β†’ AI Chat (login required)
πŸ“– http://localhost:8000/docs β†’ API docs (Swagger UI)
{bold('Database:')}
πŸ“ {db_path}
Tables: users Β· chat_sessions Β· messages
{bold('Before first user login:')}
Open .env and change JWT_SECRET to a real secret.
Generate one:
{yellow("python3 -c \"import secrets; print(secrets.token_hex(32))\"")}
""")