How vsatyakiran/smart-harvest works
Project Type and Purpose
• Smart-Harvest is a full-stack web application that performs two main services:
a) Crop recommendation based on soil attributes (N, P, K, pH) and local weather (via a zipcode-based API).
b) Fertilizer recommendation for a chosen crop and soil profile.
• It uses a Python/Flask backend (app.py), pre-trained ML models (Model.pkl, labels.pkl), static front-end assets (HTML/CSS/JS under static and templates), and small data files (fertilizer.csv, StatesSeason.json, allcrop.json).
File Structure Analysis
• Top-level code:
• app.py: central Flask application handling HTTP routes and orchestrating ML calls.
• model.py, fertilizer.py: encapsulate prediction logic for crop and fertilizer modules.
• app.ipynb: Jupyter notebook used for model training (not used at runtime).
• Data/Models:
• Model.pkl, labels.pkl: serialized ML artifacts.
• fertilizer.csv, StatesSeason.json, static/allcrop.json: contain fertilizer rules, seasonal state data, crop details.
• Frontend:
• templates/*.html: Jinja2 templates for pages (homepage, crop info, fertilizer results, etc.).
• static/: SCSS/CSS styles, JavaScript modules (homepage.js, crop_data.js, etc.), images.
• No external persistent database; data is loaded from JSON/CSV files at runtime.
Key Components
a. User Interface (Browser)
b. Static Content Server (Flask static/ endpoint serving CSS/JS/images)
c. Template Renderer (Flask templates/ with Jinja2)
d. Application Server (Flask in app.py):
• HTTP endpoints: /, /recommend-crop, /recommend-fertilizer, /crop-info, etc.
• Controllers mapping requests to service modules.
e. Crop Recommendation Module (model.py + Model.pkl, labels.pkl)
f. Fertilizer Recommendation Module (fertilizer.py + fertilizer.csv)
g. Data Stores (flat files: JSON and CSV under project root/static)
h. External Weather API (zipcode → weather data)
i. (Optional) Model Training Pipeline (app.ipynb), separate from runtime.
Interactions and Data Flows
• Browser → HTTP GET / → Flask serves rendered homepage.html + static assets.
• User submits form (soil attributes + zipcode) → HTTP POST /recommend-crop →
• Flask calls weather API to fetch rainfall/temperature for zipcode.
• Combines soil + weather features → model.py.predict() loads Model.pkl → returns top 5 crops.
• Response rendered via templates/Fertilizer_result.html or JSON.
• User clicks a crop → HTTP GET /crop-info?crop=X → Flask loads static/allcrop.json → renders Crop_info.html.
• Fertilizer recommendation: form submission → HTTP POST /recommend-fertilizer → Flask calls fertilizer.py.recommend() with soil & crop → uses fertilizer.csv logic → renders Fertilizer_result.html.
Architectural Patterns and Design Principles
• MVC-like separation:
• Model modules (model.py, fertilizer.py) encapsulate business logic.
• Views (templates/*.html) handle markup.
• Controllers (Flask routes in app.py) orchestrate flow.
• Stateless HTTP services; no persistent DB, uses file-based data.
• ML artifacts are decoupled from code and loaded at runtime.
Technologies and Frameworks
• Backend: Python 3, Flask web framework.
• ML: scikit-learn (SVM, RandomForest, LogisticRegression), pickled models.
• Frontend: HTML5, SCSS/CSS, vanilla JavaScript.
• Data: JSON, CSV files.
• External Service: Weather API (e.g., OpenWeatherMap).