Skip to content

Setup‐Guide

DHANUSH G edited this page Mar 4, 2026 · 1 revision

⚡ Setup Guide

Back to Home | Architecture | API-Reference


📋 Prerequisites

Before you begin, ensure you have the following installed:

Requirement Version Check
Python 3.9 or higher python --version
Node.js 18.0 or higher node --version
npm 8.0 or higher npm --version
Git Any recent version git --version

📥 Step 1: Clone the Repository

git clone https://github.com/DHANUSHGCODE/AI-Powered-Security-Monitoring-Threat-Detection-Platform.git
cd AI-Powered-Security-Monitoring-Threat-Detection-Platform

🐍 Step 2: Backend Setup

2a. Create a Virtual Environment (Recommended)

cd backend

# Create virtual environment
python -m venv venv

# Activate — Windows
venv\Scripts\activate

# Activate — macOS / Linux
source venv/bin/activate

2b. Install Dependencies

pip install -r requirements.txt

Key packages installed:

  • fastapi — Web framework
  • uvicorn — ASGI server
  • sqlalchemy — ORM
  • scikit-learn — ML model
  • pandas, numpy — Data processing
  • pydantic — Request validation
  • pytest, httpx — Testing

🤖 Step 3: Train the AI Model

From the root directory of the project:

python ai-model/train_model.py

This will:

  1. Generate synthetic log data → data/generated_logs.csv
  2. Train an Isolation Forest model
  3. Save the model → ai-model/isolation_forest_model.pkl

Note: The model must be trained before starting the backend server, otherwise /predict/ calls will return a 503 error.


🚀 Step 4: Start the Backend Server

From the root directory:

uvicorn backend.main:app --reload --port 8000
URL Purpose
http://localhost:8000 Base API URL
http://localhost:8000/docs Interactive Swagger UI
http://localhost:8000/redoc ReDoc API documentation

You should see:

INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO:     Started reloader process
INFO:     Started server process
INFO:     Waiting for application startup.
INFO:     Application startup complete.

🎞️ Step 5: Frontend Setup

Open a new terminal and run:

cd frontend
npm install
npm run dev

The Next.js dashboard will be available at:

  • http://localhost:3000

Ensure the backend is running on port 8000 before starting the frontend, as the dashboard fetches data from the API.


🧪 Step 6: Run the Test Suite

From the root directory:

# Set PYTHONPATH so backend module imports work
export PYTHONPATH=.   # macOS / Linux
set PYTHONPATH=.      # Windows CMD
$env:PYTHONPATH="."   # Windows PowerShell

# Run all tests
pytest backend/tests/ -v

# Run specific test file
pytest backend/tests/test_logs.py -v

# Run with coverage
pytest backend/tests/ --cov=backend --cov-report=term-missing

Expected output:

backend/tests/test_health.py::test_health_root         PASSED
backend/tests/test_logs.py::test_create_log_success    PASSED
backend/tests/test_logs.py::test_list_logs_success     PASSED
... (all tests passing)

🐳 Docker Setup (Coming Soon)

Docker Compose support is on the roadmap. Once available:

# Build and start all services
docker-compose up --build

# Services:
# - backend: http://localhost:8000
# - frontend: http://localhost:3000

⚙️ Environment Variables

Create a .env file in the backend/ directory for configuration:

# Database
DATABASE_URL=sqlite:///./security_logs.db

# For PostgreSQL (production):
# DATABASE_URL=postgresql://user:password@localhost:5432/security_db

# Model
MODEL_PATH=ai-model/isolation_forest_model.pkl

# API
API_HOST=0.0.0.0
API_PORT=8000
DEBUG=true

🛠️ Troubleshooting

ModuleNotFoundError: No module named 'backend'

# Always run from the root directory with PYTHONPATH set:
export PYTHONPATH=.
pytest backend/tests/

503 Service Unavailable on /predict/

# Model not trained. Run:
python ai-model/train_model.py

Port 8000 already in use

# Kill existing process:
lsof -ti:8000 | xargs kill   # macOS/Linux
netstat -ano | findstr :8000  # Windows (then taskkill /PID <pid> /F)

Frontend API connection errors

  • Ensure backend is running on http://localhost:8000
  • Check browser console for CORS errors
  • Verify NEXT_PUBLIC_API_URL in frontend/.env.local if configured

Back to Home | Architecture | Next: API-Reference