akashub commited on
Commit
f4ffd7e
Β·
1 Parent(s): b20698b

fix(README.md setup)

Browse files
Files changed (4) hide show
  1. .gitignore +1 -0
  2. README.md +10 -210
  3. README_ref.md +213 -0
  4. requirements.txt +0 -0
.gitignore ADDED
@@ -0,0 +1 @@
 
 
1
+ .env
README.md CHANGED
@@ -1,213 +1,13 @@
1
- # 🌾 Alert Summary Prototype Backend
2
-
3
- **Multi-stage MCP Pipeline for Agricultural Intelligence**
4
-
5
- ---
6
-
7
- ## 🎯 Overview
8
-
9
- Farmer.Chat uses a **4-stage pipeline** to process agricultural queries:
10
-
11
- ```
12
- Query β†’ Router β†’ Executor (Parallel) β†’ Compiler β†’ Translator β†’ Advice
13
- ```
14
-
15
- ### Architecture
16
-
17
- 1. **Stage 1: Query Router** - Analyzes farmer's question and selects relevant MCP servers
18
- 2. **Stage 2: MCP Executor** - Calls multiple APIs in parallel (weather, soil, water, elevation, pests)
19
- 3. **Stage 3: Response Compiler** - Merges data from all sources
20
- 4. **Stage 4: Farmer Translator** - Converts technical data to actionable farmer advice
21
-
22
- ---
23
-
24
- ## πŸ”Œ API Endpoints
25
-
26
- ### `POST /api/query`
27
- Process a farmer's question
28
-
29
- **Request:**
30
- ```json
31
- {
32
- "query": "Should I plant rice today?",
33
- "location": {
34
- "name": "Bangalore",
35
- "lat": 12.8716,
36
- "lon": 77.4946
37
- }
38
- }
39
- ```
40
-
41
- **Response:**
42
- ```json
43
- {
44
- "success": true,
45
- "query": "Should I plant rice today?",
46
- "advice": "...",
47
- "routing": {...},
48
- "data": {...},
49
- "execution_time_seconds": 3.5
50
- }
51
- ```
52
-
53
- ### `POST /api/export-pdf`
54
- Export query result as PDF
55
-
56
- **Request:** Same as `/api/query`
57
-
58
- **Response:** PDF file download
59
-
60
- ### `GET /api/health`
61
- Health check
62
-
63
- ### `GET /api/servers`
64
- List available MCP servers
65
-
66
- ---
67
-
68
- ## πŸ› οΈ MCP Servers
69
-
70
- | Server | Data Source | Information |
71
- |--------|-------------|-------------|
72
- | **weather** | Open-Meteo | Current weather, 7-day forecasts |
73
- | **soil_properties** | SoilGrids | Clay, sand, pH, nutrients |
74
- | **water** | GRACE Satellite | Groundwater levels, drought status |
75
- | **elevation** | OpenElevation | Field elevation, terrain data |
76
- | **pests** | iNaturalist | Recent pest observations |
77
-
78
- ---
79
-
80
- ## πŸš€ Deployment Instructions
81
-
82
- ### 1. Create Hugging Face Space
83
-
84
- 1. Go to https://huggingface.co/new-space
85
- 2. Space name: `farmer-chat-backend`
86
- 3. Owner: `aakashdg`
87
- 4. SDK: **Gradio** (we'll use FastAPI inside)
88
- 5. Set to **Public**
89
-
90
- ### 2. Upload Files
91
-
92
- Upload all files maintaining this structure:
93
- ```
94
- farmer-chat-backend/
95
- β”œβ”€β”€ app.py
96
- β”œβ”€β”€ requirements.txt
97
- β”œβ”€β”€ README.md
98
- β”œβ”€β”€ src/
99
- β”‚ β”œβ”€β”€ __init__.py
100
- β”‚ β”œβ”€β”€ pipeline.py
101
- β”‚ β”œβ”€β”€ router.py
102
- β”‚ β”œβ”€β”€ executor.py
103
- β”‚ β”œβ”€β”€ compiler.py
104
- β”‚ β”œβ”€β”€ translator.py
105
- β”‚ β”œβ”€β”€ pdf_generator.py
106
- β”‚ └── servers/
107
- β”‚ β”œβ”€β”€ __init__.py
108
- β”‚ └── (all server classes in one file or separate)
109
- ```
110
-
111
- ### 3. Set Environment Variables
112
-
113
- In Space Settings β†’ Variables and secrets:
114
- - Add secret: `OPENAI_API_KEY` = your OpenAI API key
115
-
116
- ### 4. Deploy!
117
-
118
- Space will auto-deploy. Access at:
119
- ```
120
- https://huggingface.co/spaces/aakashdg/farmer-chat-backend
121
- ```
122
-
123
  ---
124
-
125
- ## πŸ§ͺ Testing
126
-
127
- ### Test with cURL:
128
-
129
- ```bash
130
- curl -X POST https://huggingface.co/spaces/aakashdg/farmer-chat-backend/api/query \
131
- -H "Content-Type: application/json" \
132
- -d '{
133
- "query": "What is the soil composition?",
134
- "location": {"name": "Bangalore", "lat": 12.8716, "lon": 77.4946}
135
- }'
136
- ```
137
-
138
- ### Test with Python:
139
-
140
- ```python
141
- import requests
142
-
143
- response = requests.post(
144
- "https://huggingface.co/spaces/aakashdg/farmer-chat-backend/api/query",
145
- json={
146
- "query": "Will it rain this week?",
147
- "location": {"name": "Bangalore", "lat": 12.8716, "lon": 77.4946}
148
- }
149
- )
150
-
151
- print(response.json())
152
- ```
153
-
154
- ---
155
-
156
- ## πŸ“Š Performance
157
-
158
- - **Parallel execution**: All MCP servers called simultaneously
159
- - **Typical response time**: 3-5 seconds
160
- - **Success rate**: ~95% (graceful degradation if servers fail)
161
-
162
- ---
163
-
164
- ## πŸ” Security
165
-
166
- - OpenAI API key stored as HF Space secret
167
- - CORS enabled for frontend integration
168
- - Rate limiting: 100 queries/hour per IP (configurable)
169
-
170
- ---
171
-
172
- ## πŸ“ˆ Scaling
173
-
174
- To add more MCP servers:
175
-
176
- 1. Create new server class in `src/servers/`
177
- 2. Add to `MCP_SERVER_REGISTRY` in `executor.py`
178
- 3. Router will automatically include it in routing decisions
179
-
180
- ---
181
-
182
- ## πŸ› Troubleshooting
183
-
184
- ### "OPENAI_API_KEY not set"
185
- - Check HF Space Settings β†’ Variables and secrets
186
- - Ensure secret name is exactly `OPENAI_API_KEY`
187
-
188
- ### Slow responses
189
- - Normal for first query (cold start)
190
- - Subsequent queries faster due to caching
191
-
192
- ### Server failures
193
- - System uses graceful degradation
194
- - If one server fails, others still provide data
195
- - Check `failed_servers` in response
196
-
197
- ---
198
-
199
- ## πŸ“ž Support
200
-
201
- - GitHub Issues: [Link to repo]
202
- - Creator: @aakashdg
203
- - Built for: Farmer.chat product demo
204
-
205
- ---
206
-
207
- ## πŸ“„ License
208
-
209
- MIT License
210
-
211
  ---
212
 
213
- **Built with ❀️ for farmers**
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
+ title: Alert Summary Fc Backend
3
+ emoji: πŸ“‰
4
+ colorFrom: red
5
+ colorTo: red
6
+ sdk: gradio
7
+ sdk_version: 6.0.2
8
+ app_file: app.py
9
+ pinned: false
10
+ license: mit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
  ---
12
 
13
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
README_ref.md ADDED
@@ -0,0 +1,213 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 🌾 Alert Summary Prototype Backend
2
+
3
+ **Multi-stage MCP Pipeline for Agricultural Intelligence**
4
+
5
+ ---
6
+
7
+ ## 🎯 Overview
8
+
9
+ Farmer.Chat uses a **4-stage pipeline** to process agricultural queries:
10
+
11
+ ```
12
+ Query β†’ Router β†’ Executor (Parallel) β†’ Compiler β†’ Translator β†’ Advice
13
+ ```
14
+
15
+ ### Architecture
16
+
17
+ 1. **Stage 1: Query Router** - Analyzes farmer's question and selects relevant MCP servers
18
+ 2. **Stage 2: MCP Executor** - Calls multiple APIs in parallel (weather, soil, water, elevation, pests)
19
+ 3. **Stage 3: Response Compiler** - Merges data from all sources
20
+ 4. **Stage 4: Farmer Translator** - Converts technical data to actionable farmer advice
21
+
22
+ ---
23
+
24
+ ## πŸ”Œ API Endpoints
25
+
26
+ ### `POST /api/query`
27
+ Process a farmer's question
28
+
29
+ **Request:**
30
+ ```json
31
+ {
32
+ "query": "Should I plant rice today?",
33
+ "location": {
34
+ "name": "Bangalore",
35
+ "lat": 12.8716,
36
+ "lon": 77.4946
37
+ }
38
+ }
39
+ ```
40
+
41
+ **Response:**
42
+ ```json
43
+ {
44
+ "success": true,
45
+ "query": "Should I plant rice today?",
46
+ "advice": "...",
47
+ "routing": {...},
48
+ "data": {...},
49
+ "execution_time_seconds": 3.5
50
+ }
51
+ ```
52
+
53
+ ### `POST /api/export-pdf`
54
+ Export query result as PDF
55
+
56
+ **Request:** Same as `/api/query`
57
+
58
+ **Response:** PDF file download
59
+
60
+ ### `GET /api/health`
61
+ Health check
62
+
63
+ ### `GET /api/servers`
64
+ List available MCP servers
65
+
66
+ ---
67
+
68
+ ## πŸ› οΈ MCP Servers
69
+
70
+ | Server | Data Source | Information |
71
+ |--------|-------------|-------------|
72
+ | **weather** | Open-Meteo | Current weather, 7-day forecasts |
73
+ | **soil_properties** | SoilGrids | Clay, sand, pH, nutrients |
74
+ | **water** | GRACE Satellite | Groundwater levels, drought status |
75
+ | **elevation** | OpenElevation | Field elevation, terrain data |
76
+ | **pests** | iNaturalist | Recent pest observations |
77
+
78
+ ---
79
+
80
+ ## πŸš€ Deployment Instructions
81
+
82
+ ### 1. Create Hugging Face Space
83
+
84
+ 1. Go to https://huggingface.co/new-space
85
+ 2. Space name: `farmer-chat-backend`
86
+ 3. Owner: `aakashdg`
87
+ 4. SDK: **Gradio** (we'll use FastAPI inside)
88
+ 5. Set to **Public**
89
+
90
+ ### 2. Upload Files
91
+
92
+ Upload all files maintaining this structure:
93
+ ```
94
+ farmer-chat-backend/
95
+ β”œβ”€β”€ app.py
96
+ β”œβ”€β”€ requirements.txt
97
+ β”œβ”€β”€ README.md
98
+ β”œβ”€β”€ src/
99
+ β”‚ β”œβ”€β”€ __init__.py
100
+ β”‚ β”œβ”€β”€ pipeline.py
101
+ β”‚ β”œβ”€β”€ router.py
102
+ β”‚ β”œβ”€β”€ executor.py
103
+ β”‚ β”œβ”€β”€ compiler.py
104
+ β”‚ β”œβ”€β”€ translator.py
105
+ β”‚ β”œβ”€β”€ pdf_generator.py
106
+ β”‚ └── servers/
107
+ β”‚ β”œβ”€β”€ __init__.py
108
+ β”‚ └── (all server classes in one file or separate)
109
+ ```
110
+
111
+ ### 3. Set Environment Variables
112
+
113
+ In Space Settings β†’ Variables and secrets:
114
+ - Add secret: `OPENAI_API_KEY` = your OpenAI API key
115
+
116
+ ### 4. Deploy!
117
+
118
+ Space will auto-deploy. Access at:
119
+ ```
120
+ https://huggingface.co/spaces/aakashdg/farmer-chat-backend
121
+ ```
122
+
123
+ ---
124
+
125
+ ## πŸ§ͺ Testing
126
+
127
+ ### Test with cURL:
128
+
129
+ ```bash
130
+ curl -X POST https://huggingface.co/spaces/aakashdg/farmer-chat-backend/api/query \
131
+ -H "Content-Type: application/json" \
132
+ -d '{
133
+ "query": "What is the soil composition?",
134
+ "location": {"name": "Bangalore", "lat": 12.8716, "lon": 77.4946}
135
+ }'
136
+ ```
137
+
138
+ ### Test with Python:
139
+
140
+ ```python
141
+ import requests
142
+
143
+ response = requests.post(
144
+ "https://huggingface.co/spaces/aakashdg/farmer-chat-backend/api/query",
145
+ json={
146
+ "query": "Will it rain this week?",
147
+ "location": {"name": "Bangalore", "lat": 12.8716, "lon": 77.4946}
148
+ }
149
+ )
150
+
151
+ print(response.json())
152
+ ```
153
+
154
+ ---
155
+
156
+ ## πŸ“Š Performance
157
+
158
+ - **Parallel execution**: All MCP servers called simultaneously
159
+ - **Typical response time**: 3-5 seconds
160
+ - **Success rate**: ~95% (graceful degradation if servers fail)
161
+
162
+ ---
163
+
164
+ ## πŸ” Security
165
+
166
+ - OpenAI API key stored as HF Space secret
167
+ - CORS enabled for frontend integration
168
+ - Rate limiting: 100 queries/hour per IP (configurable)
169
+
170
+ ---
171
+
172
+ ## πŸ“ˆ Scaling
173
+
174
+ To add more MCP servers:
175
+
176
+ 1. Create new server class in `src/servers/`
177
+ 2. Add to `MCP_SERVER_REGISTRY` in `executor.py`
178
+ 3. Router will automatically include it in routing decisions
179
+
180
+ ---
181
+
182
+ ## πŸ› Troubleshooting
183
+
184
+ ### "OPENAI_API_KEY not set"
185
+ - Check HF Space Settings β†’ Variables and secrets
186
+ - Ensure secret name is exactly `OPENAI_API_KEY`
187
+
188
+ ### Slow responses
189
+ - Normal for first query (cold start)
190
+ - Subsequent queries faster due to caching
191
+
192
+ ### Server failures
193
+ - System uses graceful degradation
194
+ - If one server fails, others still provide data
195
+ - Check `failed_servers` in response
196
+
197
+ ---
198
+
199
+ ## πŸ“ž Support
200
+
201
+ - GitHub Issues: [Link to repo]
202
+ - Creator: @aakashdg
203
+ - Built for: Farmer.chat product demo
204
+
205
+ ---
206
+
207
+ ## πŸ“„ License
208
+
209
+ MIT License
210
+
211
+ ---
212
+
213
+ **Built with ❀️ for farmers**
requirements.txt ADDED
File without changes