-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
387 lines (309 loc) · 14.1 KB
/
main.py
File metadata and controls
387 lines (309 loc) · 14.1 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
"""
AgroScan — FastAPI backend for crop disease detection.
Endpoints
---------
GET /health → model status, loaded flag, class count, device
POST /predict → image upload → class, confidence, severity,
bilingual disease info, base64 Grad-CAM overlay
POST /whatsapp → Twilio WhatsApp webhook → Hindi TwiML reply
Design notes
------------
* Model, class names and GradCAM are loaded ONCE at startup via FastAPI
lifespan and stored in app.state — zero per-request overhead.
* Grad-CAM runs a second forward+backward pass; a threading.Lock ensures
a single GradCAM instance is never used concurrently (safe for the
default thread-pool executor used by async endpoints).
* CORS is open (*) so any frontend / mobile app can call the API.
"""
import asyncio
import base64
import io
import json
import logging
import os
import threading
from contextlib import asynccontextmanager
from pathlib import Path
import httpx
import torch
from fastapi import FastAPI, File, Form, HTTPException, UploadFile
from pydantic import BaseModel
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import FileResponse, Response
from fastapi.staticfiles import StaticFiles
from PIL import Image, UnidentifiedImageError
from src.disease_info import DISEASE_INFO
from src.gradcam import GradCAM, overlay_on_image
from src.model import build_model
from src.utils import preprocess_image
# ── Logging ───────────────────────────────────────────────────────────────────
logging.basicConfig(level=logging.INFO, format="%(levelname)s %(message)s")
log = logging.getLogger("agroscan")
# ── Paths ─────────────────────────────────────────────────────────────────────
_MODELS_DIR = Path("models")
_CLASS_NAMES_PATH = _MODELS_DIR / "class_names.json"
# ── Concurrency guard for GradCAM ─────────────────────────────────────────────
_predict_lock = threading.Lock()
# ── Device selection ──────────────────────────────────────────────────────────
def _get_device() -> torch.device:
if torch.cuda.is_available():
return torch.device("cuda")
if torch.backends.mps.is_available():
return torch.device("mps")
return torch.device("cpu")
# ── Lifespan — model loaded once at startup ───────────────────────────────────
@asynccontextmanager
async def lifespan(app: FastAPI):
# ── startup ───────────────────────────────────────────────────────────────
from huggingface_hub import hf_hub_download
model_path = "/tmp/best_model.pth"
if not os.path.exists(model_path):
print("Downloading model...")
hf_hub_download(
repo_id="B2prakash/agroscan-model",
filename="best_model.pth",
repo_type="model",
local_dir="/tmp",
token=os.environ.get("HF_TOKEN")
)
print("Model downloaded!")
_CHECKPOINT_PATH = model_path
device = _get_device()
log.info(f"Device: {device}")
with open(_CLASS_NAMES_PATH) as f:
class_names = json.load(f)
log.info(f"Classes loaded: {len(class_names)}")
model = build_model(num_classes=len(class_names)).to(device)
ckpt = torch.load(_CHECKPOINT_PATH, map_location=device)
model.load_state_dict(ckpt["model_state_dict"])
model.eval()
log.info(
f"Checkpoint loaded: {_CHECKPOINT_PATH} "
f"(val_acc={ckpt.get('val_acc', 0.0):.4f})"
)
app.state.model = model
app.state.gradcam = GradCAM(model)
app.state.class_names = class_names
app.state.device = device
app.state.model_ready = True
yield # ── server running ─────────────────────────────────────────────────
# ── shutdown ──────────────────────────────────────────────────────────────
app.state.gradcam.remove_hooks()
log.info("GradCAM hooks removed. Shutdown complete.")
# ── App ───────────────────────────────────────────────────────────────────────
app = FastAPI(title="AgroScan", version="1.0.0", lifespan=lifespan)
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_methods=["*"],
allow_headers=["*"],
)
# ── Internal helpers ──────────────────────────────────────────────────────────
def _bytes_to_pil(data: bytes) -> Image.Image:
"""Parse raw bytes → RGB PIL Image, or raise HTTP 400."""
try:
return Image.open(io.BytesIO(data)).convert("RGB")
except (UnidentifiedImageError, Exception) as exc:
raise HTTPException(status_code=400, detail=f"Invalid image: {exc}")
def _run_prediction(pil_image: Image.Image, state) -> dict:
"""
Thread-safe prediction pipeline.
Returns a dict matching the /predict response schema.
"""
tensor = preprocess_image(pil_image, device=str(state.device))
# GradCAM: one no_grad forward (confidence) + one backward (heatmap)
with _predict_lock:
heatmap, class_idx, confidence = state.gradcam.generate_top_class(tensor)
class_name = state.class_names[class_idx]
entry = DISEASE_INFO[class_name]
# ── Coming-soon crop: return early without Grad-CAM ───────────────────────
if entry.get("is_coming_soon"):
return {
"class": class_name,
"confidence": round(confidence * 100, 2),
"severity": "unknown",
"is_coming_soon": True,
"disease_info": {
"name_en": entry["name_en"],
"name_hi": entry["name_hi"],
"message_en": entry["message_en"],
"message_hi": entry["message_hi"],
"contact_en": entry["contact_en"],
"contact_hi": entry["contact_hi"],
},
"gradcam_image": None,
}
# Overlay heatmap on original image → base64 JPEG
overlay = overlay_on_image(pil_image, heatmap)
buf = io.BytesIO()
overlay.save(buf, format="JPEG", quality=85)
gradcam_b64 = base64.b64encode(buf.getvalue()).decode()
return {
"class": class_name,
"confidence": round(confidence * 100, 2),
"severity": entry["severity"],
"disease_info": {
"name_en": entry["name_en"],
"name_hi": entry["name_hi"],
"cure_en": entry["cure_en"],
"cure_hi": entry["cure_hi"],
"prevention_en": entry["prevention_en"],
"prevention_hi": entry["prevention_hi"],
"pesticide_en": entry["pesticide_en"],
"pesticide_hi": entry["pesticide_hi"],
},
"gradcam_image": gradcam_b64,
}
# ── Endpoints ─────────────────────────────────────────────────────────────────
@app.get("/")
async def root():
"""Serve the frontend."""
return FileResponse("index.html")
@app.get("/health")
def health():
"""Returns model status, device, and class count."""
state = app.state
return {
"status": "ok",
"model_loaded": getattr(state, "model_ready", False),
"num_classes": len(getattr(state, "class_names", [])),
"device": str(getattr(state, "device", "unknown")),
}
@app.post("/predict")
async def predict(file: UploadFile = File(...)):
"""
Upload an image file; receive class, confidence, disease info, and
a base64-encoded Grad-CAM heatmap overlay.
"""
if not getattr(app.state, "model_ready", False):
raise HTTPException(status_code=503, detail="Model not loaded yet.")
data = await file.read()
if not data:
raise HTTPException(status_code=400, detail="Empty file.")
pil_image = _bytes_to_pil(data)
try:
loop = asyncio.get_event_loop()
result = await loop.run_in_executor(
None, _run_prediction, pil_image, app.state
)
except HTTPException:
raise
except Exception as exc:
log.exception("Prediction failed")
raise HTTPException(status_code=500, detail=f"Prediction error: {exc}")
return result
@app.post("/whatsapp")
async def whatsapp(
Body: str = Form(default=""),
MediaUrl0: str = Form(default=""),
From: str = Form(default=""),
NumMedia: str = Form(default="0"),
):
"""
Twilio WhatsApp webhook.
Downloads the farmer's photo, runs prediction, replies in Hindi via TwiML.
"""
def twiml(msg: str) -> Response:
xml = (
'<?xml version="1.0" encoding="UTF-8"?>'
"<Response>"
f"<Message>{msg}</Message>"
"</Response>"
)
return Response(content=xml, media_type="application/xml")
# No image attached
if NumMedia == "0" or not MediaUrl0:
return twiml(
"नमस्ते! कृपया अपनी फसल की फोटो भेजें। 🌱\n"
"(Hello! Please send a photo of your crop to detect the disease.)"
)
# Download image from Twilio CDN
try:
async with httpx.AsyncClient(timeout=15.0) as client:
resp = await client.get(MediaUrl0)
resp.raise_for_status()
image_data = resp.content
except Exception as exc:
log.error(f"WhatsApp image download failed: {exc}")
return twiml(
"फोटो डाउनलोड नहीं हो सकी। कृपया दोबारा भेजें। "
"(Could not download the photo. Please try again.)"
)
pil_image = _bytes_to_pil(image_data)
try:
loop = asyncio.get_event_loop()
result = await loop.run_in_executor(
None, _run_prediction, pil_image, app.state
)
except Exception as exc:
log.exception("WhatsApp prediction error")
return twiml(
"विश्लेषण में त्रुटि। कृपया पुनः प्रयास करें। "
"(Analysis failed. Please try again.)"
)
info = result["disease_info"]
sev_map = {
"none": "कोई नहीं ✅",
"low": "कम ⚠️",
"medium": "मध्यम 🟠",
"high": "अधिक 🔴",
}
sev_hi = sev_map.get(result["severity"], result["severity"])
reply = (
f"🌿 *AgroScan — फसल रोग पहचान*\n\n"
f"🔍 रोग: {info['name_hi']}\n"
f"📊 विश्वास: {result['confidence']}%\n"
f"⚠️ गंभीरता: {sev_hi}\n\n"
f"💊 *उपचार:*\n{info['cure_hi']}\n\n"
f"🛡️ *रोकथाम:*\n{info['prevention_hi']}\n\n"
f"🧪 *कीटनाशक:*\n{info['pesticide_hi']}"
)
return twiml(reply)
# ── Chat ──────────────────────────────────────────────────────────────────────
class ChatRequest(BaseModel):
message: str
language: str = "en"
disease_context: str | None = None
@app.post("/chat")
async def chat(req: ChatRequest):
"""KisanBot — AI farming assistant powered by Groq."""
api_key = os.environ.get("GROQ_API_KEY")
if not api_key:
raise HTTPException(status_code=503, detail="GROQ_API_KEY not configured.")
try:
from groq import Groq
except ImportError:
raise HTTPException(status_code=503, detail="groq package not installed.")
system_prompt = (
"You are KisanBot, an expert agricultural assistant for Indian farmers. "
"You help farmers with crop diseases, pesticides, farming advice, and treatment recommendations. "
"When disease context is provided, answer questions specifically about that disease. "
"Always be helpful, simple and practical. "
"If user writes in Hindi, respond in Hindi. "
"If user writes in English, respond in English. "
"Keep answers concise and farmer-friendly."
)
messages = [{"role": "system", "content": system_prompt}]
if req.disease_context:
messages.append({
"role": "system",
"content": f"Current scan context: {req.disease_context}"
})
messages.append({"role": "user", "content": req.message})
try:
client = Groq(api_key=api_key)
response = client.chat.completions.create(
model="llama3-8b-8192",
messages=messages,
max_tokens=512,
temperature=0.7,
)
reply = response.choices[0].message.content
except Exception as exc:
log.exception("Groq chat error")
raise HTTPException(status_code=500, detail=f"Chat error: {exc}")
return {"reply": reply}
# ── Dev entry point ───────────────────────────────────────────────────────────
if __name__ == "__main__":
import uvicorn
uvicorn.run("main:app", host="0.0.0.0", port=8000, reload=False)