# ramkitcs/precisionfarming-1 architecture

> Architecture diagram of the GitHub repository ramkitcs/precisionfarming-1, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-25). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/ramkitcs/precisionfarming-1
- Repository: https://github.com/ramkitcs/precisionfarming-1

## Overview

##### Project Type and Purpose
- This is a precision-farming “intelligent recommendation engine” written in Python, combining a lightweight Streamlit UI with a LangChain/LangGraph–based RAG (Retrieval-Augmented Generation) core. It ingests user data (location, soil, images), calls domain-specific tools (weather API, vector store, web search, image classifiers), and stitches together an actionable plan via an LLM (OpenAI).

##### File Structure Analysis
- Top-level scripts:
- StreamLitApp.py – the frontend/UI layer
- PrecisionFarming.py, RetrievalGraph.py, AgentTools.py, CropVectorStore.py – core engine and tool definitions
- Models folder: pre-trained TensorFlow CNNs for insect and leaf classification
- Guides/ folder: PDF crop guides used as vector-store sources
- helper_functions.py & notebooks: ML training and data-prep artifacts
- requirements.txt, .gitignore – environment and deployment hints

##### Key Architectural Components
a. Frontend (Streamlit UI)
b. Orchestrator (LangGraph agentic workflow in PrecisionFarming.py)
c. Tools layer:
- Weather tool (get_weather_data → external WeatherAPI)
- PH calculators (decrease_ph, increase_ph)
- Water calculator (calculate_water_needed)
- Retrieval tools (get_crop_info, tackle_insect, tackle_disease) → vector store + web search (Tivaly)
- Image classifiers (TensorFlow CNNs)
d. Retrieval Graph (RetrievalGraph.py): well-defined nodes for multi-query retrieval, relevance filtering (TruLens), web search, response grounding (Upstage), query rewriting
e. LLM Layer (OpenAI calls) – final narrative generation
f. Data stores:
- Vector database of chunked PDF guides
- Possibly local file store for model artifacts

##### Component Interactions and Data Flow
- User inputs (UI) → REST/Direct call into the orchestrator
- Orchestrator analyzes input, invokes Tools in parallel or sequence:
- Weather → returns forecast & irrigation plan
- Soil PH/Water tools → numeric calculations
- CNN classifiers → insect/disease predictions
- Retrieval tools → fetch guidance from vector store or web
- RetrievalGraph may be invoked under the hood by specific tools
- All partial results feed back into the LLM, which assembles the final actionable plan
- Output → UI renders markdown response

##### Architectural Patterns & Principles
- Agentic orchestration with LangGraph for dynamic tool-calling
- RAG pattern for grounded retrieval + fallback web search
- Separation of concerns: UI (Streamlit) vs. orchestrator vs. tools vs. data/model stores
- Modular “@tool” annotations for easy extensibility

##### Detailing and Labeling
- Edges with function names or tool calls (e.g., “get_weather_data()”)
- Conditional or fallback paths in the Retrieval Graph (e.g., “if no vector hits → web search”)
- Feedback loops (e.g., relevance filter back into retrieval)

##### Final Recommendations
- Be exhaustive but clear: break the system into its logical sub-components
- Both the high-level workflow (UI → Engine → Output) and the internal branching for RAG, classification, and calculations
- How the LangGraph agent chooses between tools to meet the prompt

## Diagram (Mermaid)

```mermaid
graph TB
    %% Frontend Layer
    subgraph "Frontend (UI Layer)"
        UI["StreamLitApp.py"]:::ui
    end

    %% Engine Layer
    subgraph "Engine (Orchestrator & LLM & Retrieval Graph)"
        Orch["PrecisionFarming.py - Agentic Workflow"]:::engine
        RG["RetrievalGraph.py"]:::engine
        LLM["OpenAI LLM"]:::engine
    end

    %% Tools & ML Models
    subgraph "Tools & ML Models"
        TL["AgentTools.py - Tool Definitions"]:::tool
        VS["CropVectorStore.py - Vector Store Interface"]:::tool

        subgraph "Image Classification Models"
            M1["insect.mobilenetv3large.keras"]:::ml
            M2["leaf.corn.mobilenetv3large.keras"]:::ml
            M3["leaf.cotton.mobilenetv3large.keras"]:::ml
        end
    end

    %% Helper & Config
    subgraph "Helper Functions, Notebooks & Config"
        HF["helper_functions.py"]:::tool
        NB1["TensorFlow _Corn_Leaf.ipynb"]:::tool
        NB2["TensorFlow _Cotton_Leaf.ipynb"]:::tool
        NB3["TensorFlow_Insect_Detection.ipynb"]:::tool
        NB4["TensorFlow_Soybean_Leaf-Copy1.ipynb"]:::tool
        REQ["requirements.txt"]:::config
        GIT[".gitignore"]:::config
        RD["README.md"]:::config
    end

    %% External Services & Data Stores
    subgraph "External Services & Data Stores"
        WD["Weather API"]:::external
        WS["Tivaly Web Search"]:::external
        DB["Vector DB"]:::db
        MF["Model Artifacts Store"]:::db

        subgraph "PDF Guides (Vector Source)"
            G1["corn.pdf"]:::data
            G2["cotton.pdf"]:::data
            G3["soybean.pdf"]:::data
        end

        subgraph "Insect Image Dataset"
            I1["ants"]:::data
            I2["bees"]:::data
            I3["beetle"]:::data
            I4["catterpillar"]:::data
            I5["earthworms"]:::data
            I6["earwig"]:::data
            I7["grasshopper"]:::data
            I8["moth"]:::data
            I9["slug"]:::data
            I10["snail"]:::data
            I11["wasp"]:::data
            I12["weevil"]:::data
        end
    end

    %% Workflow Connections
    UI -->|"user input"| Orch
    Orch -->|"invoke tools"| TL
    Orch -->|"use retrieval graph"| RG
    Orch -->|"generate plan"| LLM
    LLM -->|"final report"| UI

    %% Tool Invocations
    TL -->|"get_weather_data() "| WD
    TL -->|"calculate_ph() "| TL
    TL -->|"calculate_water_needed() "| TL
    TL -->|"classify insect"| M1
    TL -->|"classify leaf corn"| M2
    TL -->|"classify leaf cotton"| M3
    TL -->|"get_crop_info() "| VS
    TL -->|"tackle_insect() "| VS
    TL -->|"tackle_disease() "| VS

    %% Retrieval Graph Data Flow
    RG -->|"vector query"| DB
    RG -->|"fallback search"| WS
    VS -->|"load chunks"| DB
    DB -->|"stores guides"| G1
    DB --> G2
    DB --> G3

    %% Model & Data Stores links
    M1 --> MF
    M2 --> MF
    M3 --> MF
    HF --> MF

    I1 --> M1
    I2 --> M1
    I3 --> M1
    I4 --> M1
    I5 --> M1
    I6 --> M1
    I7 --> M1
    I8 --> M1
    I9 --> M1
    I10 --> M1
    I11 --> M1
    I12 --> M1

    %% Click Events
    click UI "https://github.com/ramkitcs/precisionfarming-1/blob/main/StreamLitApp.py"
    click Orch "https://github.com/ramkitcs/precisionfarming-1/blob/main/PrecisionFarming.py"
    click RG "https://github.com/ramkitcs/precisionfarming-1/blob/main/RetrievalGraph.py"
    click TL "https://github.com/ramkitcs/precisionfarming-1/blob/main/AgentTools.py"
    click VS "https://github.com/ramkitcs/precisionfarming-1/blob/main/CropVectorStore.py"
    click M1 "https://github.com/ramkitcs/precisionfarming-1/blob/main/models/insect.mobilenetv3large.keras"
    click M2 "https://github.com/ramkitcs/precisionfarming-1/blob/main/models/leaf.corn.mobilenetv3large.keras"
    click M3 "https://github.com/ramkitcs/precisionfarming-1/blob/main/models/leaf.cotton.mobilenetv3large.keras"
    click G1 "https://github.com/ramkitcs/precisionfarming-1/blob/main/Guides/corn.pdf"
    click G2 "https://github.com/ramkitcs/precisionfarming-1/blob/main/Guides/cotton.pdf"
    click G3 "https://github.com/ramkitcs/precisionfarming-1/blob/main/Guides/soybean.pdf"
    click I1 "https://github.com/ramkitcs/precisionfarming-1/tree/main/InsectImages/ants"
    click I2 "https://github.com/ramkitcs/precisionfarming-1/tree/main/InsectImages/bees"
    click I3 "https://github.com/ramkitcs/precisionfarming-1/tree/main/InsectImages/beetle"
    click I4 "https://github.com/ramkitcs/precisionfarming-1/tree/main/InsectImages/catterpillar"
    click I5 "https://github.com/ramkitcs/precisionfarming-1/tree/main/InsectImages/earthworms"
    click I6 "https://github.com/ramkitcs/precisionfarming-1/tree/main/InsectImages/earwig"
    click I7 "https://github.com/ramkitcs/precisionfarming-1/tree/main/InsectImages/grasshopper"
    click I8 "https://github.com/ramkitcs/precisionfarming-1/tree/main/InsectImages/moth"
    click I9 "https://github.com/ramkitcs/precisionfarming-1/tree/main/InsectImages/slug"
    click I10 "https://github.com/ramkitcs/precisionfarming-1/tree/main/InsectImages/snail"
    click I11 "https://github.com/ramkitcs/precisionfarming-1/tree/main/InsectImages/wasp"
    click I12 "https://github.com/ramkitcs/precisionfarming-1/tree/main/InsectImages/weevil"
    click HF "https://github.com/ramkitcs/precisionfarming-1/blob/main/helper_functions.py"
    click NB1 "https://github.com/ramkitcs/precisionfarming-1/blob/main/TensorFlow _Corn_Leaf.ipynb"
    click NB2 "https://github.com/ramkitcs/precisionfarming-1/blob/main/TensorFlow _Cotton_Leaf.ipynb"
    click NB3 "https://github.com/ramkitcs/precisionfarming-1/blob/main/TensorFlow_Insect_Detection.ipynb"
    click NB4 "https://github.com/ramkitcs/precisionfarming-1/blob/main/TensorFlow_Soybean_Leaf-Copy1.ipynb"
    click REQ "https://github.com/ramkitcs/precisionfarming-1/blob/main/requirements.txt"
    click GIT "https://github.com/ramkitcs/precisionfarming-1/blob/main/.gitignore"
    click RD "https://github.com/ramkitcs/precisionfarming-1/blob/main/README.md"

    %% Styles
    classDef ui fill:#D0E8FF,stroke:#0366D6,color:#0366D6;
    classDef engine fill:#DFFFE0,stroke:#28A745,color:#28A745;
    classDef tool fill:#FFF5D6,stroke:#FFA500,color:#C46300;
    classDef ml fill:#FFEFD6,stroke:#FF8C00,color:#C46300;
    classDef external fill:#F0E0FF,stroke:#800080,color:#800080;
    classDef data fill:#F8F0E0,stroke:#A0522D,color:#A0522D;
    classDef db fill:#E0F7FA,stroke:#00796B,color:#00796B;
    classDef config fill:#E8F4F8,stroke:#005B96,color:#005B96;
```
