# abhayrautela213/crime-rate-prediction architecture

> Architecture diagram of the GitHub repository abhayrautela213/crime-rate-prediction, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-03). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/abhayrautela213/crime-rate-prediction
- Repository: https://github.com/abhayrautela213/crime-rate-prediction

## Overview

##### Project Type & Purpose
- This is a full-stack prediction application. Its primary goal is to take input parameters (city, crime type, year) from a web user interface and output a prediction made by the machine learning model.
- The README details the scope (crime prediction on historical data between 2014 and 2021 using a Random Forest model) and explains the usage and user interactions.

##### File Structure
- Frontend:
- The “templates” directory holds HTML files (index.html and result.html) which are likely used by a web framework (e.g., Flask) to render the user interface.
- The “static” directory has assets such as CSS (styles.css), JavaScript (main.js), and images, which provide UI styling and interactivity.
- Backend:
- The file “app.py” appears to be the primary script that sets up the web server, routes user requests, and orchestrates the overall application logic.
- “crp.ipynb” is a Jupyter Notebook, likely used for model development and testing. It provides insights into the experimental model building process.
- “requirements.txt” lists all dependencies, indicating the technical stack (e.g., scikit-learn, Flask) required by the project.
- Machine Learning Components:
- The “Model” folder contains “model.pkl”, which is the pre-trained Random Forest Regression model used for predictions.
- The “Mappings” directory includes text files mapping cities and types; these are likely used to convert user-friendly names into formats that the model can accept.
- The “Dataset” directory contains Excel files with historical crime data, which may have been used for training or validating the model.
- Reports & Documentation:
- The “Report” directory holds documentation and presentation files detailing the project’s literature survey, design, and performance results.

##### Key Components
a. Web Frontend:
- Templates (HTML) for input (index.html) and output (result.html)
- Static assets (CSS, JS, images) used for UI styling and interactivity
b. Web Server & Application Logic:
- The main controller “app.py” handles HTTP requests, processes form data, and coordinates with the backend prediction logic.
c. Machine Learning Model:
- The pre-trained “model.pkl” encapsulated in the “Model” directory represents the core predictive engine.
- Mappings (city and crime type) from the “Mappings” folder enable the correct pre-processing of inputs.
d. Data Storage/Source:
- Datasets (both “crp.xlsx” and “new_dataset.xlsx” in the “Dataset” folder) provide underlying historical data for reference or future model improvements.
e. Ancillary Documentation:
- The “Report” folder contains design and presentation docs. Although not directly contributing to runtime architecture, they offer context about design decisions, and insights into evaluation/testing.

##### Relationships and Interactions
- The frontend (templates and static assets) sends user input (city, crime category, and year) to the back end via the web server implemented in “app.py”.
- “app.py” then utilizes the mappings to pre-process this input. It loads the machine learning model (model.pkl) to perform prediction.
- The predicted crime rate is then passed back to the frontend where it is rendered on “result.html” for the user.
- Underneath, there’s an assumed separation of concerns where the data (master datasets and mappings) and machine-learning model are isolated from the web server code.

##### Architectural Patterns/Design Principles
- Separation of Concerns: Clearly delineates the UI (templates/static), application logic (app.py), and machine learning components (Model, Mappings, Dataset).
- MVC-Like Approach (loosely applied): The “model” is handled via machine learning (in a separate module), the “view” through the HTML templates, and the “controller” in app.py.
- Dependency Management: Use of requirements.txt signifies proper dependency management.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% External Entity
    User["User Browser"]:::user

    %% Frontend Subgraph
    subgraph "Web User Interface (Frontend)"
        InputForm["Input Form (index.html)"]:::frontend
        OutputDisplay["Output Display (result.html)"]:::frontend
        UI_Static["Static Assets (CSS, JS, Images)"]:::frontend
    end

    %% Backend Subgraph
    subgraph "Application Server (Backend)"
        App["Application Server (app.py)"]:::backend
    end

    %% Machine Learning Subgraph
    subgraph "ML Prediction Engine"
        ML_Model["Pre-trained Model (model.pkl)"]:::ml
        subgraph "Input Preprocessing (Mappings)"
            CityMapping["City Mapping"]:::ml
            TypeMapping["Crime Type Mapping"]:::ml
        end
    end

    %% Data Storage Subgraph
    subgraph "Historical Datasets"
        DS1["Dataset (crp.xlsx)"]:::data
        DS2["Dataset (new_dataset.xlsx)"]:::data
    end

    %% Documentation Subgraph
    subgraph "Project Documentation"
        Docs["Reports & Docs (Report)"]:::docs
    end

    %% Data Flow Connections
    User -->|"submit_input"| InputForm
    InputForm -->|"send_request"| App
    App -->|"apply_mapping"| CityMapping
    App -->|"apply_mapping"| TypeMapping
    App -->|"load_model"| ML_Model
    ML_Model -->|"predict"| App
    App -->|"render_result"| OutputDisplay

    %% Indicate Model Training Dependency (non-runtime)
    ML_Model ---|"trained_on"| DS1
    ML_Model ---|"trained_on"| DS2

    %% Styling Classes
    classDef frontend fill:#AED6F1,stroke:#1F618D,stroke-width:2px;
    classDef backend fill:#F9E79F,stroke:#B7950B,stroke-width:2px;
    classDef ml fill:#F5B7B1,stroke:#C0392B,stroke-width:2px;
    classDef data fill:#ABEBC6,stroke:#1E8449,stroke-width:2px;
    classDef docs fill:#D2B4DE,stroke:#7D3C98,stroke-width:2px;
    classDef user fill:#D5DBDB,stroke:#566573,stroke-width:2px;

    %% Click Events
    click InputForm "https://github.com/abhayrautela213/crime-rate-prediction/blob/main/templates/index.html"
    click OutputDisplay "https://github.com/abhayrautela213/crime-rate-prediction/blob/main/templates/result.html"
    click UI_Static "https://github.com/abhayrautela213/crime-rate-prediction/tree/main/static/"
    click App "https://github.com/abhayrautela213/crime-rate-prediction/blob/main/app.py"
    click ML_Model "https://github.com/abhayrautela213/crime-rate-prediction/blob/main/Model/model.pkl"
    click CityMapping "https://github.com/abhayrautela213/crime-rate-prediction/blob/main/Mappings/City_Mapping.txt"
    click TypeMapping "https://github.com/abhayrautela213/crime-rate-prediction/blob/main/Mappings/Type_Mapping.txt"
    click DS1 "https://github.com/abhayrautela213/crime-rate-prediction/blob/main/Dataset/crp.xlsx"
    click DS2 "https://github.com/abhayrautela213/crime-rate-prediction/blob/main/Dataset/new_dataset.xlsx"
    click Docs "https://github.com/abhayrautela213/crime-rate-prediction/tree/main/Report/"
```
