ravindraog commited on
Commit
6d21387
·
verified ·
1 Parent(s): 5cf2d8d

Upload folder using huggingface_hub

Browse files
.gitignore ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ .env
2
+ _pycache_/
3
+ Model1-crop-rec\__pycache__
4
+ Model-yeild-rec\yield_model_from_csv.joblib
Model-crop-rec/__pycache__/model1crop-rec.cpython-313.pyc ADDED
Binary file (8.95 kB). View file
 
Model-crop-rec/crop_imputer.joblib ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:cb75c61e69929761446a89bf2ca95e1c5e9e9c5d524f4795f716a8b0cd2aa007
3
+ size 847
Model-crop-rec/crop_model.joblib ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:8f2cca809359586cef47f38ef48e9fc7c61b516494e06632829eeec3194906e8
3
+ size 11030105
Model-plant/plant_disease_model.h5 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:2928928345656a1e78c9fd63b7743135824346899068e39657366032e604e13e
3
+ size 97589648
Model-yeild-rec/yield_model_from_csv.joblib ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:196329e9bad8817270ea1f5806b37bcbfdf2d797b795b28363cb6a23f076d555
3
+ size 126713923
__pycache__/runmodel.cpython-313.pyc ADDED
Binary file (18.9 kB). View file
 
requirements.txt ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ fastapi
2
+ uvicorn[standard]
3
+ pandas
4
+ numpy
5
+ joblib
6
+ scikit-learn
7
+ pymongo
8
+ openai
9
+ requests
10
+ kaggle
11
+ dotenv
12
+ tensorflow
13
+ Pillow
runmodel.py ADDED
@@ -0,0 +1,298 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, HTTPException, File, UploadFile
2
+ from pydantic import BaseModel
3
+ import pandas as pd
4
+ import numpy as np
5
+ import joblib
6
+ from pymongo import MongoClient
7
+ from datetime import datetime
8
+ from openai import OpenAI
9
+ import os
10
+ import dotenv
11
+ import json
12
+ import io
13
+ import tensorflow as tf
14
+ from PIL import Image
15
+
16
+ # -------------------------
17
+ # Config
18
+ # -------------------------
19
+ dotenv.load_dotenv()
20
+
21
+ MONGO_URI = os.getenv("MONGO_URI")
22
+ MONGO_DB = os.getenv("MONGO_DB")
23
+ MONGO_COLL = os.getenv("MONGO_COLL")
24
+ HF_TOKEN = os.getenv("HF_TOKEN")
25
+
26
+ # Model paths
27
+ CROP_MODEL_PATH = "Model-crop-rec/crop_model.joblib"
28
+ CROP_IMPUTER_PATH = "Model-crop-rec/crop_imputer.joblib"
29
+ YIELD_MODEL_PATH = "Model-yeild-rec/yield_model_from_csv.joblib"
30
+ DISEASE_MODEL_PATH = "Model-plant/plant_disease_model.h5" # <-- NEW
31
+
32
+ # --- NEW: Disease Class Names ---
33
+ ## IMPORTANT: Update with your actual class names in the correct order ##
34
+ DISEASE_CLASS_NAMES = ["Early Blight", "Late Blight", "Healthy"]
35
+
36
+ # Feature sets
37
+ CROP_FEATURES = ["N", "P", "K", "temperature", "humidity", "ph", "rainfall"]
38
+ YIELD_FEATURES = ['Year', 'rainfall_mm', 'pesticides_tonnes', 'avg_temp', 'Area', 'Item']
39
+ AI_MODEL_NAME = "openai/gpt-oss-120b:cerebras"
40
+
41
+ # -------------------------
42
+ # Load models & imputer
43
+ # -------------------------
44
+ print("Loading machine learning models...")
45
+ clf = joblib.load(CROP_MODEL_PATH)
46
+ imp = joblib.load(CROP_IMPUTER_PATH)
47
+ yield_model = joblib.load(YIELD_MODEL_PATH)
48
+ print("Crop, Imputer, and Yield models loaded successfully.")
49
+
50
+ # --- NEW: Load Disease Detection Model ---
51
+ disease_model = None
52
+ try:
53
+ if os.path.exists(DISEASE_MODEL_PATH):
54
+ disease_model = tf.keras.models.load_model(DISEASE_MODEL_PATH)
55
+ print("Plant disease detection model loaded successfully.")
56
+ else:
57
+ print(f"Warning: Disease model not found at {DISEASE_MODEL_PATH}")
58
+ except Exception as e:
59
+ print(f"Error loading disease model: {e}")
60
+
61
+
62
+ # Database connection
63
+ client = MongoClient(MONGO_URI)
64
+ coll = client[MONGO_DB][MONGO_COLL]
65
+
66
+ # -------------------------
67
+ # LLM Client (for gpt-oss-120b text generation)
68
+ # -------------------------
69
+ llm_client = None
70
+ if HF_TOKEN:
71
+ try:
72
+ llm_client = OpenAI(
73
+ base_url="https://router.huggingface.co/v1",
74
+ api_key=HF_TOKEN,
75
+ default_headers={"Accept-Encoding": "identity"}
76
+ )
77
+ print("LLM client for text generation initialized.")
78
+ except Exception as e:
79
+ print(f"Warning: LLM client init failed: {e}")
80
+ llm_client = None
81
+
82
+ # -------------------------
83
+ # NEW: Image Preprocessing Helper
84
+ # -------------------------
85
+ def preprocess_image(image_bytes: bytes) -> np.ndarray:
86
+ """Reads image bytes, resizes to 128x128, normalizes, and prepares for the model."""
87
+ img = Image.open(io.BytesIO(image_bytes))
88
+ img = img.convert("RGB") # Ensure 3 channels
89
+ img = img.resize((128, 128))
90
+ img_array = tf.keras.preprocessing.image.img_to_array(img)
91
+ img_array = img_array / 255.0 # Normalize to [0, 1]
92
+ img_batch = np.expand_dims(img_array, axis=0) # Add batch dimension
93
+ return img_batch
94
+
95
+ # -------------------------
96
+ # Core Logic Functions
97
+ # -------------------------
98
+ def get_crop_recommendation_logic(farmer_doc: dict):
99
+ # This function remains the same
100
+ if any(pd.isna(farmer_doc.get(k)) for k in CROP_FEATURES):
101
+ raise ValueError("Missing required fields for crop recommendation.")
102
+ feat = {k: float(farmer_doc[k]) for k in CROP_FEATURES}
103
+ actual_crop = str(farmer_doc.get("crop", "")).strip().lower()
104
+ df_in = pd.DataFrame([feat], columns=CROP_FEATURES)
105
+ df_in_imp = pd.DataFrame(imp.transform(df_in), columns=CROP_FEATURES)
106
+ probs = clf.predict_proba(df_in_imp)[0]
107
+ prob_map = dict(zip(clf.classes_, probs))
108
+ crop_df = pd.DataFrame(list(coll.find()))
109
+ centroids = crop_df.groupby("crop")[CROP_FEATURES].mean()
110
+ centroid_matrix = centroids.values
111
+ dists = np.linalg.norm(centroid_matrix - df_in_imp.values.reshape(1, -1), axis=1)
112
+ sims = 1.0 / (1.0 + dists)
113
+ sim_map = dict(zip(centroids.index, sims))
114
+ all_scores = {}
115
+ for crop in clf.classes_:
116
+ p = prob_map.get(crop, 0.0)
117
+ s = sim_map.get(crop, 0.0)
118
+ all_scores[crop.lower()] = { "crop": crop, "probability": float(p), "centroid_similarity": float(s), "final_score": float(0.5 * p + 0.5 * s) }
119
+ advice_for_existing_crop = None
120
+ if actual_crop and actual_crop in all_scores and llm_client:
121
+ advice_for_existing_crop = get_cultivation_advice(llm_client, all_scores[actual_crop]['crop'], feat)
122
+ sorted_scores = sorted(all_scores.values(), key=lambda x: x['final_score'], reverse=True)
123
+ new_recommendations = [s for s in sorted_scores if s['crop'].lower() != actual_crop][:3]
124
+ advice_for_new_crop = None
125
+ if new_recommendations and llm_client:
126
+ advice_for_new_crop = get_cultivation_advice(llm_client, new_recommendations[0]['crop'], feat)
127
+ return {
128
+ "features_used": feat,
129
+ "new_crop_recommendations": new_recommendations,
130
+ "advice_for_top_new_crop": advice_for_new_crop,
131
+ "advice_for_existing_crop": advice_for_existing_crop
132
+ }
133
+
134
+ def get_yield_prediction_logic(farmer_doc: dict):
135
+ # This function remains the same
136
+ yield_input = {
137
+ 'Year': farmer_doc.get('year', datetime.now().year),
138
+ 'rainfall_mm': farmer_doc.get('rainfall'),
139
+ 'pesticides_tonnes': farmer_doc.get('pesticides_tonnes', 0.0),
140
+ 'avg_temp': farmer_doc.get('temperature'),
141
+ 'Area': farmer_doc.get('state', 'Unknown'),
142
+ 'Item': farmer_doc.get('crop')
143
+ }
144
+ missing_fields = [k for k in YIELD_FEATURES if yield_input.get(k) is None]
145
+ if missing_fields:
146
+ raise ValueError(f"Missing required fields for yield prediction: {missing_fields}")
147
+ yield_input_data = pd.DataFrame([yield_input], columns=YIELD_FEATURES)
148
+ predicted_yield_hg_ha = yield_model.predict(yield_input_data)[0]
149
+ predicted_yield_quintal_ha = float(round(predicted_yield_hg_ha / 10, 2))
150
+ return {
151
+ "predicted_yield_quintal_per_hectare": predicted_yield_quintal_ha,
152
+ "features_used": yield_input
153
+ }
154
+
155
+ # -------------------------
156
+ # LLM Helper Functions
157
+ # -------------------------
158
+
159
+ def get_cultivation_advice(client, crop_name, features):
160
+ if not client: return "LLM client not available for advice generation."
161
+ prompt = f"Provide concise cultivation advice for '{crop_name}' given these conditions: N={features['N']:.2f}, P={features['P']:.2f}, K={features['K']:.2f}, pH={features['ph']:.2f}, Temp={features['temperature']:.2f}C, Humidity={features['humidity']:.2f}%, Rainfall={features['rainfall']:.2f}mm. Suggest land prep, sowing, fertilization, irrigation in bullets."
162
+ try:
163
+ completion = client.chat.completions.create(model=AI_MODEL_NAME, messages=[{"role": "user", "content": prompt}], temperature=0.7, max_tokens=512)
164
+ return completion.choices[0].message.content.strip()
165
+ except Exception as e: return f"Could not generate advice: {e}"
166
+
167
+ def generate_yield_advice(client, farmer_data, prediction):
168
+ if not client: return "LLM client not available for advice generation."
169
+ prompt = f"An expert agronomist providing 2-3 key bullet points to improve crop yield. Farmer's crop: '{farmer_data.get('crop')}' in an area of {farmer_data.get('areaHectare')} ha. Our model predicts a yield of {prediction:.2f} quintals per hectare. Focus on practical steps."
170
+ try:
171
+ completion = client.chat.completions.create(model=AI_MODEL_NAME, messages=[{"role": "user", "content": prompt}], temperature=0.7, max_tokens=512)
172
+ return completion.choices[0].message.content.strip()
173
+ except Exception as e: return f"Could not generate yield advice: {e}"
174
+
175
+ def get_intent_from_llm(client, query: str):
176
+ if not client: return {"intent": "UNKNOWN", "error": "LLM client not available."}
177
+ prompt = f"""Analyze the user's query and classify it into one of the intents: 'CROP_RECOMMENDATION', 'YIELD_PREDICTION', or 'GREETING'. Respond ONLY with a JSON object like {{"intent": "YOUR_CLASSIFICATION"}}. User query: "{query}" """
178
+ try:
179
+ completion = client.chat.completions.create(model=AI_MODEL_NAME, messages=[{"role": "user", "content": prompt}], temperature=0.1, max_tokens=50)
180
+ result_text = completion.choices[0].message.content.strip()
181
+ return json.loads(result_text)
182
+ except Exception as e:
183
+ print(f"LLM intent classification failed: {e}")
184
+ return {"intent": "UNKNOWN"}
185
+
186
+ # -------------------------
187
+ # FastAPI App & Pydantic Models
188
+ # -------------------------
189
+ app = FastAPI(title="Farmer AI Services API")
190
+
191
+ class FarmerRequest(BaseModel):
192
+ farmerId: str
193
+
194
+ class VoiceQueryRequest(BaseModel):
195
+ farmerId: str
196
+ query: str
197
+
198
+ # -------------------------
199
+ # API Endpoints
200
+ # -------------------------
201
+
202
+ @app.get("/")
203
+ def read_root():
204
+ return {"status": "API is running", "timestamp": datetime.now().isoformat()}
205
+
206
+ @app.post("/m1/crop-recommendation")
207
+ def recommend_crop(req: FarmerRequest):
208
+ farmer_doc = coll.find_one({"farmerId": req.farmerId})
209
+ if not farmer_doc:
210
+ raise HTTPException(status_code=404, detail="Farmer not found")
211
+ try:
212
+ result = get_crop_recommendation_logic(farmer_doc)
213
+ return { "farmerId": req.farmerId, **result, "recommended_at": datetime.utcnow().isoformat() }
214
+ except ValueError as e:
215
+ raise HTTPException(status_code=400, detail=str(e))
216
+ except Exception as e:
217
+ raise HTTPException(status_code=500, detail=f"An unexpected error occurred: {e}")
218
+
219
+ @app.post("/m1/yield")
220
+ def predict_yield(req: FarmerRequest):
221
+ farmer_doc = coll.find_one({"farmerId": req.farmerId})
222
+ if not farmer_doc:
223
+ raise HTTPException(status_code=404, detail="Farmer not found")
224
+ try:
225
+ result = get_yield_prediction_logic(farmer_doc)
226
+ advice = generate_yield_advice(llm_client, farmer_doc, result["predicted_yield_quintal_per_hectare"])
227
+ return { "farmerId": req.farmerId, **result, "yield_advice": advice, "predicted_at": datetime.utcnow().isoformat() }
228
+ except ValueError as e:
229
+ raise HTTPException(status_code=400, detail=str(e))
230
+ except Exception as e:
231
+ raise HTTPException(status_code=500, detail=f"Yield prediction failed: {e}")
232
+
233
+ @app.post("/m1/voice-query")
234
+ def handle_voice_query(req: VoiceQueryRequest):
235
+ farmer_doc = coll.find_one({"farmerId": req.farmerId})
236
+ if not farmer_doc:
237
+ raise HTTPException(status_code=404, detail="Farmer not found")
238
+ intent_data = get_intent_from_llm(llm_client, req.query)
239
+ intent = intent_data.get("intent")
240
+ response_text = "I'm sorry, I couldn't process that request."
241
+ if intent == "CROP_RECOMMENDATION":
242
+ try:
243
+ result = get_crop_recommendation_logic(farmer_doc)
244
+ top_crop = result['new_crop_recommendations'][0]['crop'] if result['new_crop_recommendations'] else 'a suitable crop'
245
+ response_text = f"Based on your farm's data, I recommend planting {top_crop}. {result['advice_for_top_new_crop']}"
246
+ except Exception as e:
247
+ response_text = f"I tried to get a crop recommendation, but an error occurred: {e}"
248
+ elif intent == "YIELD_PREDICTION":
249
+ try:
250
+ result = get_yield_prediction_logic(farmer_doc)
251
+ yield_val = result['predicted_yield_quintal_per_hectare']
252
+ advice = generate_yield_advice(llm_client, farmer_doc, yield_val)
253
+ response_text = f"The predicted yield for your {farmer_doc.get('crop', 'crop')} is {yield_val} quintals per hectare. Here is some advice to improve it: {advice}"
254
+ except Exception as e:
255
+ response_text = f"I tried to predict the yield, but an error occurred: {e}"
256
+ elif intent == "GREETING":
257
+ response_text = "Hello! How can I assist you with your farm today? You can ask for a crop recommendation or a yield prediction."
258
+ else: # UNKNOWN
259
+ response_text = "I'm sorry, I didn't understand that. Please ask for a crop recommendation or a yield prediction."
260
+ return {"response_text": response_text}
261
+
262
+ # -------------------------
263
+ # NEW: Plant Disease Endpoint
264
+ # -------------------------
265
+ @app.post("/m2/plant-disease")
266
+ async def detect_plant_disease(file: UploadFile = File(...)):
267
+ """Endpoint for plant disease detection from an image."""
268
+ if not disease_model:
269
+ raise HTTPException(status_code=503, detail="Disease detection service is unavailable.")
270
+
271
+ # Validate file type
272
+ if not file.content_type.startswith("image/"):
273
+ raise HTTPException(status_code=400, detail="Invalid file type. Please upload an image.")
274
+
275
+ try:
276
+ # Read and process the image
277
+ image_bytes = await file.read()
278
+ processed_image = preprocess_image(image_bytes)
279
+
280
+ # Make prediction
281
+ predictions = disease_model.predict(processed_image)
282
+
283
+ # Process result
284
+ predicted_index = np.argmax(predictions[0])
285
+ confidence = float(predictions[0][predicted_index])
286
+ predicted_class = DISEASE_CLASS_NAMES[predicted_index]
287
+
288
+ return {
289
+ "predicted_class": predicted_class,
290
+ "confidence": f"{confidence:.2%}", # Format as percentage string
291
+ "details": {
292
+ "raw_predictions": predictions[0].tolist(),
293
+ "class_names": DISEASE_CLASS_NAMES
294
+ }
295
+ }
296
+ except Exception as e:
297
+ print(f"Error during disease prediction: {e}")
298
+ raise HTTPException(status_code=500, detail="Failed to process the image.")