File size: 3,653 Bytes
16c8745
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# 🌾 AgriEdge: Smart Farm Assistant

An AI-powered assistant that uses real-time sensor data and textbook-based agricultural knowledge to provide insights, analysis, and actionable suggestions for small to medium-scale farms. Comes with both command-line and web interfaces.

---

## πŸš€ Features

- πŸ“‘ Analyzes **real-time farm sensor data** (soil, water, environment)
- πŸ“š Retrieves context from **agricultural PDF documents**
- πŸ€– Uses **retrieval-augmented generation (RAG)** for grounded reasoning
- 🧠 Powered by **Ollama + LLaMA 3**
- πŸ“ Generates **natural language summaries and actionable insights**
- πŸ”’ Runs **fully local** β€” no cloud, no data sharing
- 🌐 Supports **Streamlit-based dashboard** for non-technical users

---

## πŸ“ Project Structure

```bash

smartfarm/

β”œβ”€β”€ main.py                       # Command-line interface

β”œβ”€β”€ app.py                        # Streamlit web app

β”œβ”€β”€ llm/

β”‚   β”œβ”€β”€ ollama_llm.py             # Query handler using LLM + sensor data + RAG

β”‚   └── rag_pipeline.py           # PDF retrieval pipeline using FAISS

β”œβ”€β”€ logger.py                     # Logging setup

β”œβ”€β”€ prompt.txt                    # Prompt template for LLM

β”œβ”€β”€ data/

β”‚   β”œβ”€β”€ farm_data_log.json        # JSON file logging sensor readings

β”‚   β”œβ”€β”€ docs/                     # Agricultural PDFs for knowledge retrieval

β”‚   └── faiss_index/              # Auto-generated FAISS vector index

```

---

## πŸ› οΈ Installation & Setup

### 1. Clone the Repository

```bash

git clone https://github.com/your-username/smartfarm.git

cd smartfarm

```

### 2. Install Python Dependencies

```bash

pip install -r requirements.txt

```

### 3. Set Up Ollama

Make sure you have Ollama installed and running.

Download the LLaMA 3 model:

```bash

ollama run llama3

```

Make sure Ollama is running in the background before using the assistant.

### 4. Add Sensor Data

Append new entries to `data/farm_data_log.json`. Example format:

```json

{

  "timestamp": "2025-07-22T21:00:00+01:00",

  "soil": {"moisture": "High", "pH": 6.8, "temperature": 24.9},

  "water": {"pH": 7.2, "turbidity": "8 NTU", "temperature": 23.3},

  "environment": {"humidity": "85%", "temperature": 26.0, "rainfall": "Moderate"}

}

```

### 5. Add Agricultural Documents (Optional)

Place your farming-related PDFs inside:

```bash

data/docs/

```

The system will automatically build a searchable vector index.

---

## ▢️ Usage

### πŸ“Ÿ Command-Line Mode

```bash

python main.py

```

You’ll be prompted to enter queries like:

```markdown

> Is the soil suitable for planting now?

> Has the turbidity improved compared to earlier?

```

Type `exit` to quit.

### 🌐 Streamlit Web Interface

Launch the UI with:

```bash

streamlit run app.py

```

What you can do:

- View the most recent sensor snapshot
- Ask farm-related questions like:
  - "What is the current soil condition?"
  - "Is it safe to irrigate now?"
  - "Has rainfall increased compared to earlier?"

---

## πŸ’‘ Notes

- The system analyzes only the most recent sensor reading but uses the previous 2 for historical comparison (internally).
- No internet connection is required once the vector store and model are set up.
- Logs are written automatically to `logs/`.

---

## πŸ§ͺ Example Questions

- "Is the soil moisture improving?"
- "What is the overall environmental condition right now?"
- "Is the water quality good for irrigation?"

---

## πŸ“„ License

MIT License