# rajan-git908/smart-crop-recommendation architecture

> Architecture diagram of the GitHub repository rajan-git908/smart-crop-recommendation, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-05-13). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/rajan-git908/smart-crop-recommendation
- Repository: https://github.com/rajan-git908/smart-crop-recommendation

## Overview

1. **Overall shape**
   - AgriCrop is a Flask-based monolith with a server-rendered UI and JSON API endpoints.
   - The repo is split into three main boundaries: `backend/` for business logic and routes, `frontend/` for templates and static assets, and `data/` for persisted feedback data.

2. **Entry point and request handling**
   - `backend/app.py` is the application entry point and the central composition layer.
   - It likely wires together the Flask routes for both HTML pages (`/`, dashboard, login, profile, etc.) and API endpoints such as `/api/recommend`, `/api/feedback`, `/api/crops`, and `/api/crop/<name>`.

3. **Recommendation pipeline**
   - The core data flow starts with user input of soil and weather parameters: N, P, K, pH, temperature, humidity, rainfall.
   - Requests are then passed through preprocessing, inference, and classification before a recommendation is returned to the UI or API caller.

4. **Knowledge and rules layer**
   - `backend/modules/knowledge_base.py` holds the crop domain model: ideal growing conditions for the supported crops.
   - This is the static expert-system boundary that the inference engine uses to compare input conditions against crop requirements.

5. **Preprocessing boundary**
   - `backend/modules/data_preprocessor.py` is the input sanitation layer.
   - Its role is to validate, normalize, and convert units before any reasoning or classification happens, preventing the inference and ML layers from receiving inconsistent data.

6. **Reasoning and ML subsystems**
   - `backend/modules/inference_engine.py` implements the forward-chaining expert-system logic and confidence scoring.
   - `backend/modules/classifier.py` adds the machine-learning path, described in the README as an ensemble of Naive Bayes and Decision Tree models.
   - These two subsystems appear to complement each other: rule-based reasoning for explainability and ML for statistical recommendation support.

7. **Feedback and learning loop**
   - `backend/modules/feedback_manager.py` handles user feedback submission and analysis.
   - The repository includes `data/feedback.json`, which strongly suggests feedback is persisted outside the main app code and used as a lightweight improvement loop.

8. **User identity and session management**
   - `backend/modules/auth_manager.py` is the authentication boundary for login, registration, and session handling.
   - `backend/modules/history_manager.py` tracks past recommendations per user, which feeds the profile/history screens and likely supports account-based review of results.

9. **Frontend structure**
   - `frontend/templates/` contains the server-rendered pages: landing page, dashboard, output, login/register, profile, and about.
   - `frontend/static/css/style.css` and the JavaScript files in `frontend/static/js/` provide interactivity and presentation, especially for input handling and results rendering.

10. **Persistence and setup**
   - `backend/setup_db.py` and `backend/migrate_db.py` indicate there is a database-backed subsystem for user/profile/history data, separate from the JSON feedback store.
   - The exact DB engine is not visible in the tree, but the presence of setup and migration scripts means schema management is part of the architecture.

11. **Configuration and environment**
   - `.env.example` and the README’s environment section show the app expects runtime configuration such as `SECRET_KEY`, debug mode, and Flask environment settings.
   - This keeps deployment-specific values out of code and is important for session security and local/production differences.

12. **Testing boundary**
   - `tests/test_system.py` provides a system-level test layer rather than isolated unit tests only.
   - That suggests the repo validates end-to-end behavior across the recommendation pipeline and possibly the exposed API contract.

13. **How a diagram should group it**
   - A useful architecture diagram would place the Flask app in the center, with inbound paths from browser pages and API clients.
   - From there, show branching into preprocessing, inference/ML, auth/history, and feedback persistence, with the knowledge base as a read-only domain source and the database/JSON files as the persistence layer.

Main entry points: [`backend/app.py`](https://github.com/rajan-git908/smart-crop-recommendation/blob/main/backend/app.py) (Flask app), [`backend/modules/data_preprocessor.py`](https://github.com/rajan-git908/smart-crop-recommendation/blob/main/backend/modules/data_preprocessor.py) (Preprocess), [`backend/modules/inference_engine.py`](https://github.com/rajan-git908/smart-crop-recommendation/blob/main/backend/modules/inference_engine.py) (Inference), [`backend/modules/classifier.py`](https://github.com/rajan-git908/smart-crop-recommendation/blob/main/backend/modules/classifier.py) (Classifier)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_backend["Backend"]
  node_app["Flask app<br/>entrypoint<br/>[app.py]"]
  node_auth["Auth<br/>session<br/>[auth_manager.py]"]
  node_history["History<br/>user records<br/>[history_manager.py]"]
  node_preprocess["Preprocess<br/>sanitizer"]
  node_inference["Inference<br/>rules engine"]
  node_classifier["Classifier<br/>ml ensemble<br/>[classifier.py]"]
  node_kb[("Knowledge base<br/>crop rules<br/>[knowledge_base.py]")]
  node_feedback["Feedback<br/>capture"]
  node_setupdb["DB setup<br/>schema init<br/>[setup_db.py]"]
  node_migratedb["DB migrate<br/>schema update<br/>[migrate_db.py]"]
end

subgraph group_frontend["Frontend"]
  node_index["Landing<br/>page<br/>[index.html]"]
  node_dashboard["Dashboard<br/>page<br/>[dashboard.html]"]
  node_output["Results<br/>page<br/>[output.html]"]
  node_login["Login<br/>page<br/>[login.html]"]
  node_profile["Profile<br/>page<br/>[profile.html]"]
end

subgraph group_data["Data"]
  node_feedbackstore[("Feedback data<br/>json store<br/>[feedback.json]")]
end

subgraph group_tests["Tests"]
  node_systemtest["System test<br/>integration<br/>[test_system.py]"]
end

node_index -->|"HTTP"| node_app
node_dashboard -->|"HTTP"| node_app
node_output -->|"HTTP"| node_app
node_login -->|"uses"| node_auth
node_profile -->|"reads"| node_history
node_app -->|"pipeline"| node_preprocess
node_preprocess -->|"clean input"| node_inference
node_preprocess -->|"clean input"| node_classifier
node_inference -->|"consults"| node_kb
node_classifier -.->|"trained with"| node_kb
node_app -->|"auth"| node_auth
node_app -->|"stores"| node_history
node_app -->|"submits"| node_feedback
node_feedback -->|"persists"| node_feedbackstore
node_setupdb -.->|"schema"| node_migratedb
node_app -.->|"init"| node_setupdb
node_app -.->|"validated by"| node_systemtest

click node_app "https://github.com/rajan-git908/smart-crop-recommendation/blob/main/backend/app.py"
click node_auth "https://github.com/rajan-git908/smart-crop-recommendation/blob/main/backend/modules/auth_manager.py"
click node_history "https://github.com/rajan-git908/smart-crop-recommendation/blob/main/backend/modules/history_manager.py"
click node_preprocess "https://github.com/rajan-git908/smart-crop-recommendation/blob/main/backend/modules/data_preprocessor.py"
click node_inference "https://github.com/rajan-git908/smart-crop-recommendation/blob/main/backend/modules/inference_engine.py"
click node_classifier "https://github.com/rajan-git908/smart-crop-recommendation/blob/main/backend/modules/classifier.py"
click node_kb "https://github.com/rajan-git908/smart-crop-recommendation/blob/main/backend/modules/knowledge_base.py"
click node_feedback "https://github.com/rajan-git908/smart-crop-recommendation/blob/main/backend/modules/feedback_manager.py"
click node_setupdb "https://github.com/rajan-git908/smart-crop-recommendation/blob/main/backend/setup_db.py"
click node_migratedb "https://github.com/rajan-git908/smart-crop-recommendation/blob/main/backend/migrate_db.py"
click node_index "https://github.com/rajan-git908/smart-crop-recommendation/blob/main/frontend/templates/index.html"
click node_dashboard "https://github.com/rajan-git908/smart-crop-recommendation/blob/main/frontend/templates/dashboard.html"
click node_output "https://github.com/rajan-git908/smart-crop-recommendation/blob/main/frontend/templates/output.html"
click node_login "https://github.com/rajan-git908/smart-crop-recommendation/blob/main/frontend/templates/login.html"
click node_profile "https://github.com/rajan-git908/smart-crop-recommendation/blob/main/frontend/templates/profile.html"
click node_feedbackstore "https://github.com/rajan-git908/smart-crop-recommendation/blob/main/data/feedback.json"
click node_systemtest "https://github.com/rajan-git908/smart-crop-recommendation/blob/main/tests/test_system.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_app,node_auth,node_history,node_preprocess,node_inference,node_classifier,node_kb,node_feedback,node_setupdb,node_migratedb toneBlue
class node_index,node_dashboard,node_output,node_login,node_profile toneAmber
class node_feedbackstore toneMint
class node_systemtest toneRose
```

## Components

### Backend

Flask app, business logic, and persistence plumbing

- [Flask app](https://github.com/rajan-git908/smart-crop-recommendation/blob/main/backend/app.py): entrypoint · Central composition layer and request router · `backend/app.py`
- [Auth](https://github.com/rajan-git908/smart-crop-recommendation/blob/main/backend/modules/auth_manager.py): session · Login, registration, and session handling · `backend/modules/auth_manager.py`
- [History](https://github.com/rajan-git908/smart-crop-recommendation/blob/main/backend/modules/history_manager.py): user records · Stores and retrieves past recommendations · `backend/modules/history_manager.py`
- [Preprocess](https://github.com/rajan-git908/smart-crop-recommendation/blob/main/backend/modules/data_preprocessor.py): sanitizer · Validates and normalizes crop inputs · `backend/modules/data_preprocessor.py`
- [Inference](https://github.com/rajan-git908/smart-crop-recommendation/blob/main/backend/modules/inference_engine.py): rules engine · Forward-chaining reasoning and confidence scoring · `backend/modules/inference_engine.py`
- [Classifier](https://github.com/rajan-git908/smart-crop-recommendation/blob/main/backend/modules/classifier.py): ml ensemble · Naive Bayes and decision-tree crop model · `backend/modules/classifier.py`
- [Knowledge base](https://github.com/rajan-git908/smart-crop-recommendation/blob/main/backend/modules/knowledge_base.py): crop rules · Static crop conditions and domain facts · `backend/modules/knowledge_base.py`
- [Feedback](https://github.com/rajan-git908/smart-crop-recommendation/blob/main/backend/modules/feedback_manager.py): capture · Feedback submission and analysis · `backend/modules/feedback_manager.py`
- [DB setup](https://github.com/rajan-git908/smart-crop-recommendation/blob/main/backend/setup_db.py): schema init · Initial database creation and bootstrap · `backend/setup_db.py`
- [DB migrate](https://github.com/rajan-git908/smart-crop-recommendation/blob/main/backend/migrate_db.py): schema update · Database migration script · `backend/migrate_db.py`

### Frontend

Server-rendered pages and static assets

- [Landing](https://github.com/rajan-git908/smart-crop-recommendation/blob/main/frontend/templates/index.html): page · Home page and user entry point · `frontend/templates/index.html`
- [Dashboard](https://github.com/rajan-git908/smart-crop-recommendation/blob/main/frontend/templates/dashboard.html): page · Main recommendation UI · `frontend/templates/dashboard.html`
- [Results](https://github.com/rajan-git908/smart-crop-recommendation/blob/main/frontend/templates/output.html): page · Recommendation output view · `frontend/templates/output.html`
- [Login](https://github.com/rajan-git908/smart-crop-recommendation/blob/main/frontend/templates/login.html): page · Authentication page · `frontend/templates/login.html`
- [Profile](https://github.com/rajan-git908/smart-crop-recommendation/blob/main/frontend/templates/profile.html): page · User profile and history view · `frontend/templates/profile.html`

### Data

Persisted feedback store

- [Feedback data](https://github.com/rajan-git908/smart-crop-recommendation/blob/main/data/feedback.json): json store · Persisted user feedback records · `data/feedback.json`

### Tests

System-level validation

- [System test](https://github.com/rajan-git908/smart-crop-recommendation/blob/main/tests/test_system.py): integration · End-to-end repository validation · `tests/test_system.py`

## Connections

- Landing → Flask app: HTTP · Landing requests enter the Flask app
- Dashboard → Flask app: HTTP · Recommendation form submits to the app
- Results → Flask app: HTTP · Result and feedback actions reach the app
- Login → Auth: uses · Authentication pages delegate to auth logic
- Profile → History: reads · Profile view loads past recommendations
- Flask app → Preprocess: pipeline · App sends user inputs through preprocessing
- Preprocess → Inference: clean input · Normalized values feed the reasoning engine
- Preprocess → Classifier: clean input · Normalized values feed the ML model
- Inference → Knowledge base: consults · Rules engine compares inputs against crop facts
- Classifier → Knowledge base: trained with · Knowledge boundary informs crop support
- Flask app → Auth: auth · App coordinates login and session flows
- Flask app → History: stores · App records recommendation history
- Flask app → Feedback: submits · Feedback endpoints hand off to feedback manager
- Feedback → Feedback data: persists · Feedback is written to JSON storage
- DB setup → DB migrate: schema · Database lifecycle scripts manage persistence
- Flask app → DB setup: init · App environment relies on DB bootstrap
- Flask app → System test: validated by · System tests exercise the end-to-end app
