Spaces:
Sleeping
Sleeping
File size: 13,517 Bytes
0e0f222 | 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 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 | #!/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))\"")}
""")
|