Spaces:
Sleeping
Sleeping
Upload 7 files
Browse files- .env +1 -0
- Dockerfile +11 -0
- disease_config.json +361 -0
- main.py +237 -0
- models/README.txt +1 -0
- models/plant_weights.weights.h5 +3 -0
- requirements.txt +8 -0
.env
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
GEMINI_API_KEY=AQ.Ab8RN6IV4H0ieIzGArl227Vlu-nx9kzvUkAn2uNekV8pt-g6qw
|
Dockerfile
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.10-slim
|
| 2 |
+
|
| 3 |
+
WORKDIR /code
|
| 4 |
+
|
| 5 |
+
COPY ./requirements.txt /code/requirements.txt
|
| 6 |
+
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
|
| 7 |
+
|
| 8 |
+
COPY . /code
|
| 9 |
+
|
| 10 |
+
# Hugging Face strictly requires apps to listen on port 7860
|
| 11 |
+
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
|
disease_config.json
ADDED
|
@@ -0,0 +1,361 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"model": {
|
| 3 |
+
"name": "FarmGuard Nigeria - Plant Disease Detection",
|
| 4 |
+
"version": "1.0.0",
|
| 5 |
+
"description": "MobileNetV2-based plant disease detection for Nigerian crops",
|
| 6 |
+
"framework": "tensorflow",
|
| 7 |
+
"input_shape": [224, 224, 3],
|
| 8 |
+
"supported_formats": ["jpeg", "jpg", "png"]
|
| 9 |
+
},
|
| 10 |
+
"classes": [
|
| 11 |
+
{
|
| 12 |
+
"id": "Corn_(maize)___Cercospora_leaf_spot Gray_leaf_spot",
|
| 13 |
+
"name": "Cercospora Leaf Spot / Gray Leaf Spot",
|
| 14 |
+
"plant": "Maize",
|
| 15 |
+
"disease": "Cercospora Leaf Spot",
|
| 16 |
+
"severity": "moderate",
|
| 17 |
+
"symptoms": ["Rectangular grey or tan lesions between leaf veins", "Lesions run parallel to leaf veins", "Premature leaf death in severe cases"],
|
| 18 |
+
"causes": "Fungal infection — Cercospora zeae-maydis, favoured by warm humid weather",
|
| 19 |
+
"treatment": {
|
| 20 |
+
"chemical": "Apply strobilurin fungicides (azoxystrobin, pyraclostrobin). Available as Amistar or Headline at agro-dealers.",
|
| 21 |
+
"cultural": "Remove and destroy infected crop residue after harvest. Improve field drainage.",
|
| 22 |
+
"preventive": "Plant resistant maize varieties (IITA TELA maize). Practice 2-year crop rotation with legumes."
|
| 23 |
+
},
|
| 24 |
+
"urgency": "Moderate — treat within 2 weeks of first symptoms",
|
| 25 |
+
"economic_impact": "Can reduce yield by 20-50% if untreated in severe outbreaks"
|
| 26 |
+
},
|
| 27 |
+
{
|
| 28 |
+
"id": "Corn_(maize)___Common_rust_",
|
| 29 |
+
"name": "Common Rust",
|
| 30 |
+
"plant": "Maize",
|
| 31 |
+
"disease": "Common Rust",
|
| 32 |
+
"severity": "moderate",
|
| 33 |
+
"symptoms": ["Small, circular to elongated brick-red pustules on both leaf surfaces", "Pustules turn dark brown/black as they mature", "Severe infection causes yellowing and premature death of leaves"],
|
| 34 |
+
"causes": "Fungal infection — Puccinia sorghi, spreads by wind-blown spores",
|
| 35 |
+
"treatment": {
|
| 36 |
+
"chemical": "Apply mancozeb (Dithane M-45) or propiconazole (Tilt 25EC) fungicide. Mix 2g/litre and spray every 10-14 days.",
|
| 37 |
+
"cultural": "Remove heavily infected plants. Avoid planting in areas with poor air circulation.",
|
| 38 |
+
"preventive": "Use rust-resistant varieties. Plant early to avoid peak rust season. Apply potassium fertiliser to strengthen plant resistance."
|
| 39 |
+
},
|
| 40 |
+
"urgency": "Moderate — treat within 1-2 weeks",
|
| 41 |
+
"economic_impact": "Can reduce yield by 10-35% in susceptible varieties"
|
| 42 |
+
},
|
| 43 |
+
{
|
| 44 |
+
"id": "Corn_(maize)___Northern_Leaf_Blight",
|
| 45 |
+
"name": "Northern Leaf Blight",
|
| 46 |
+
"plant": "Maize",
|
| 47 |
+
"disease": "Northern Leaf Blight",
|
| 48 |
+
"severity": "high",
|
| 49 |
+
"symptoms": ["Long, cigar-shaped grey-green to tan lesions (5-15cm) on leaves", "Lesions may have wavy margins", "Severe infection causes large areas of leaf tissue to die"],
|
| 50 |
+
"causes": "Fungal infection — Exserohilum turcicum, favoured by cool, moist conditions",
|
| 51 |
+
"treatment": {
|
| 52 |
+
"chemical": "Apply propiconazole (Tilt 25EC) at 0.5ml/litre or mancozeb at first sign of disease. Repeat every 14 days.",
|
| 53 |
+
"cultural": "Remove and burn infected crop debris. Improve field drainage and air circulation.",
|
| 54 |
+
"preventive": "Plant resistant varieties (IITA TZSR series). Rotate with legumes like cowpea or soybean."
|
| 55 |
+
},
|
| 56 |
+
"urgency": "High — treat within 1 week of detection",
|
| 57 |
+
"economic_impact": "Can reduce yield by 30-50% in severe cases"
|
| 58 |
+
},
|
| 59 |
+
{
|
| 60 |
+
"id": "Corn_(maize)___healthy",
|
| 61 |
+
"name": "Healthy",
|
| 62 |
+
"plant": "Maize",
|
| 63 |
+
"disease": "Healthy",
|
| 64 |
+
"severity": "none",
|
| 65 |
+
"symptoms": ["Deep green, upright leaves with no lesions", "Strong healthy stalk", "Normal growth pattern"],
|
| 66 |
+
"causes": "No disease present",
|
| 67 |
+
"treatment": {
|
| 68 |
+
"chemical": "No treatment needed",
|
| 69 |
+
"cultural": "Continue regular care — ensure adequate NPK fertiliser and weeding",
|
| 70 |
+
"preventive": "Monitor weekly for early signs of fall armyworm and rust. Apply nitrogen at 4-6 leaf stage."
|
| 71 |
+
},
|
| 72 |
+
"urgency": "None — plant is healthy",
|
| 73 |
+
"economic_impact": "No negative impact — maintain good agronomic practices for optimal yield"
|
| 74 |
+
},
|
| 75 |
+
{
|
| 76 |
+
"id": "Orange___Haunglongbing_(Citrus_greening)",
|
| 77 |
+
"name": "Citrus Greening (Huanglongbing)",
|
| 78 |
+
"plant": "Orange / Citrus",
|
| 79 |
+
"disease": "Huanglongbing (HLB)",
|
| 80 |
+
"severity": "high",
|
| 81 |
+
"symptoms": ["Asymmetric yellowing (blotchy mottle) on leaves — unlike nutrient deficiency which is symmetric", "Small, lopsided, bitter fruits", "Twig die-back from top of tree downward", "Fruit remains green on one side when ripe"],
|
| 82 |
+
"causes": "Bacterial infection — Candidatus Liberibacter, spread by Asian citrus psyllid insect",
|
| 83 |
+
"treatment": {
|
| 84 |
+
"chemical": "No cure exists. Control psyllid vector with imidacloprid (Confidor) soil drench or foliar spray to protect healthy trees.",
|
| 85 |
+
"cultural": "Remove and destroy infected trees immediately to prevent spread to healthy trees. Do not leave stumps.",
|
| 86 |
+
"preventive": "Use certified disease-free grafted seedlings from reputable nurseries. Apply systemic insecticide to control psyllid vector."
|
| 87 |
+
},
|
| 88 |
+
"urgency": "Critical — remove infected trees immediately, no cure",
|
| 89 |
+
"economic_impact": "Tree becomes unproductive within 3-5 years. Orchard can be completely destroyed if spread is not controlled."
|
| 90 |
+
},
|
| 91 |
+
{
|
| 92 |
+
"id": "Pepper,_bell___Bacterial_spot",
|
| 93 |
+
"name": "Bacterial Spot",
|
| 94 |
+
"plant": "Pepper",
|
| 95 |
+
"disease": "Bacterial Spot",
|
| 96 |
+
"severity": "moderate",
|
| 97 |
+
"symptoms": ["Small water-soaked spots on leaves that turn brown with yellow halo", "Spots may merge causing large brown areas", "Infected fruits have raised, scabby, water-soaked lesions", "Premature leaf and fruit drop"],
|
| 98 |
+
"causes": "Bacterial infection — Xanthomonas campestris pv. vesicatoria, spread by rain splash and handling",
|
| 99 |
+
"treatment": {
|
| 100 |
+
"chemical": "Apply copper oxychloride (Cobox/Funguran) at 3g/litre. Spray every 7-10 days during wet season. Available at agro-dealers nationwide.",
|
| 101 |
+
"cultural": "Remove infected leaves and fruits. Avoid overhead irrigation. Do not work in field when plants are wet.",
|
| 102 |
+
"preventive": "Use certified disease-free seeds. Treat seeds with hot water (50°C for 25 minutes) before planting. Practice 2-year rotation."
|
| 103 |
+
},
|
| 104 |
+
"urgency": "Moderate — treat within 1-2 weeks",
|
| 105 |
+
"economic_impact": "Can reduce marketable fruit yield by 20-40%"
|
| 106 |
+
},
|
| 107 |
+
{
|
| 108 |
+
"id": "Pepper,_bell___healthy",
|
| 109 |
+
"name": "Healthy",
|
| 110 |
+
"plant": "Pepper",
|
| 111 |
+
"disease": "Healthy",
|
| 112 |
+
"severity": "none",
|
| 113 |
+
"symptoms": ["Deep green glossy leaves with no spots", "Upright healthy stems", "Normal flower and fruit development"],
|
| 114 |
+
"causes": "No disease present",
|
| 115 |
+
"treatment": {
|
| 116 |
+
"chemical": "No treatment needed",
|
| 117 |
+
"cultural": "Maintain adequate staking, watering and NPK fertilisation",
|
| 118 |
+
"preventive": "Monitor weekly for whitefly and aphids which spread viruses. Use yellow sticky traps."
|
| 119 |
+
},
|
| 120 |
+
"urgency": "None — plant is healthy",
|
| 121 |
+
"economic_impact": "No negative impact"
|
| 122 |
+
},
|
| 123 |
+
{
|
| 124 |
+
"id": "Potato___Early_blight",
|
| 125 |
+
"name": "Early Blight",
|
| 126 |
+
"plant": "Potato",
|
| 127 |
+
"disease": "Early Blight",
|
| 128 |
+
"severity": "moderate",
|
| 129 |
+
"symptoms": ["Circular to angular dark brown spots with concentric rings (target-board pattern)", "Yellow halo around spots", "Spots first appear on older lower leaves", "Premature defoliation in severe cases"],
|
| 130 |
+
"causes": "Fungal infection — Alternaria solani, favoured by warm days and cool nights",
|
| 131 |
+
"treatment": {
|
| 132 |
+
"chemical": "Apply mancozeb (Dithane M-45) at 2g/litre every 7-10 days. Chlorothalonil (Bravo) is also effective. Both available at agro-dealers.",
|
| 133 |
+
"cultural": "Remove and destroy infected leaves. Avoid overhead watering. Mulch around plant base.",
|
| 134 |
+
"preventive": "Use certified disease-free seed tubers. Apply balanced NPK fertiliser — stressed plants are more susceptible."
|
| 135 |
+
},
|
| 136 |
+
"urgency": "Moderate — treat within 1-2 weeks",
|
| 137 |
+
"economic_impact": "Can reduce yield by 10-30% if left untreated"
|
| 138 |
+
},
|
| 139 |
+
{
|
| 140 |
+
"id": "Potato___Late_blight",
|
| 141 |
+
"name": "Late Blight",
|
| 142 |
+
"plant": "Potato",
|
| 143 |
+
"disease": "Late Blight",
|
| 144 |
+
"severity": "high",
|
| 145 |
+
"symptoms": ["Large, irregular, water-soaked, pale green to brown lesions on leaves", "White cottony fungal growth on underside of leaf in humid conditions", "Dark brown lesions on stems", "Infected tubers show reddish-brown dry rot under skin"],
|
| 146 |
+
"causes": "Oomycete — Phytophthora infestans. Spreads very rapidly in cool, wet, humid weather.",
|
| 147 |
+
"treatment": {
|
| 148 |
+
"chemical": "Apply metalaxyl+mancozeb (Ridomil Gold) immediately at 2.5g/litre. This spreads VERY fast — spray within 24 hours of detection and repeat every 7 days.",
|
| 149 |
+
"cultural": "Remove and destroy all infected plant material immediately. Do not compost. Harvest tubers early if infection is severe.",
|
| 150 |
+
"preventive": "Apply preventive fungicide sprays before wet season. Use resistant varieties. Ensure good field drainage."
|
| 151 |
+
},
|
| 152 |
+
"urgency": "CRITICAL — treat within 24 hours. Can destroy entire crop in days.",
|
| 153 |
+
"economic_impact": "Can destroy 100% of crop within 1-2 weeks under favourable conditions. Caused the Irish Potato Famine."
|
| 154 |
+
},
|
| 155 |
+
{
|
| 156 |
+
"id": "Potato___healthy",
|
| 157 |
+
"name": "Healthy",
|
| 158 |
+
"plant": "Potato",
|
| 159 |
+
"disease": "Healthy",
|
| 160 |
+
"severity": "none",
|
| 161 |
+
"symptoms": ["Dark green healthy leaves", "Strong upright stems", "No lesions or discolouration"],
|
| 162 |
+
"causes": "No disease present",
|
| 163 |
+
"treatment": {
|
| 164 |
+
"chemical": "No treatment needed",
|
| 165 |
+
"cultural": "Maintain adequate hilling, irrigation and NPK fertilisation",
|
| 166 |
+
"preventive": "Apply preventive fungicide before rainy season. Monitor weekly for late blight symptoms."
|
| 167 |
+
},
|
| 168 |
+
"urgency": "None — plant is healthy",
|
| 169 |
+
"economic_impact": "No negative impact"
|
| 170 |
+
},
|
| 171 |
+
{
|
| 172 |
+
"id": "Soybean___healthy",
|
| 173 |
+
"name": "Healthy",
|
| 174 |
+
"plant": "Soybean",
|
| 175 |
+
"disease": "Healthy",
|
| 176 |
+
"severity": "none",
|
| 177 |
+
"symptoms": ["Trifoliate dark green leaves with no spots", "Normal nodule formation on roots", "Healthy pod development"],
|
| 178 |
+
"causes": "No disease present",
|
| 179 |
+
"treatment": {
|
| 180 |
+
"chemical": "No treatment needed",
|
| 181 |
+
"cultural": "Ensure adequate phosphorus and potassium. Inoculate seeds with Bradyrhizobium for nitrogen fixation.",
|
| 182 |
+
"preventive": "Monitor for soybean rust (Phakopsora pachyrhizi) which is the main threat. Apply preventive fungicide at flowering."
|
| 183 |
+
},
|
| 184 |
+
"urgency": "None — plant is healthy",
|
| 185 |
+
"economic_impact": "No negative impact"
|
| 186 |
+
},
|
| 187 |
+
{
|
| 188 |
+
"id": "Tomato___Bacterial_spot",
|
| 189 |
+
"name": "Bacterial Spot",
|
| 190 |
+
"plant": "Tomato",
|
| 191 |
+
"disease": "Bacterial Spot",
|
| 192 |
+
"severity": "moderate",
|
| 193 |
+
"symptoms": ["Small, dark, water-soaked spots on leaves, stems and fruits", "Spots turn brown with yellow halo on leaves", "Raised, scabby brown lesions on fruits", "Severe defoliation in wet conditions"],
|
| 194 |
+
"causes": "Bacterial infection — Xanthomonas vesicatoria, spread by rain, wind and contaminated tools",
|
| 195 |
+
"treatment": {
|
| 196 |
+
"chemical": "Apply copper oxychloride (Cobox) at 3g/litre every 7-10 days. Copper hydroxide (Kocide) is also effective.",
|
| 197 |
+
"cultural": "Remove infected leaves and fruits. Avoid overhead irrigation. Disinfect tools with bleach solution.",
|
| 198 |
+
"preventive": "Use certified disease-free seeds. Treat seeds with hot water before planting. Rotate with non-solanaceous crops."
|
| 199 |
+
},
|
| 200 |
+
"urgency": "Moderate — treat within 1-2 weeks",
|
| 201 |
+
"economic_impact": "Can reduce marketable yield by 20-40%"
|
| 202 |
+
},
|
| 203 |
+
{
|
| 204 |
+
"id": "Tomato___Early_blight",
|
| 205 |
+
"name": "Early Blight",
|
| 206 |
+
"plant": "Tomato",
|
| 207 |
+
"disease": "Early Blight",
|
| 208 |
+
"severity": "moderate",
|
| 209 |
+
"symptoms": ["Dark brown circular spots with concentric rings on older lower leaves", "Yellow halo around spots", "Stem lesions near soil line (collar rot)", "Premature defoliation from bottom upward"],
|
| 210 |
+
"causes": "Fungal infection — Alternaria solani, favoured by alternating wet and dry periods",
|
| 211 |
+
"treatment": {
|
| 212 |
+
"chemical": "Apply copper-based fungicide or mancozeb (Dithane M-45) at 2g/litre. Spray every 7-10 days. Available at all agro-dealers.",
|
| 213 |
+
"cultural": "Remove infected leaves immediately. Avoid wetting foliage when watering. Mulch around base of plants.",
|
| 214 |
+
"preventive": "Stake plants for good air circulation. Apply balanced fertiliser — nitrogen deficiency increases susceptibility."
|
| 215 |
+
},
|
| 216 |
+
"urgency": "Moderate — treat within 1-2 weeks",
|
| 217 |
+
"economic_impact": "Can reduce yield by 15-30% and reduce fruit quality"
|
| 218 |
+
},
|
| 219 |
+
{
|
| 220 |
+
"id": "Tomato___Late_blight",
|
| 221 |
+
"name": "Late Blight",
|
| 222 |
+
"plant": "Tomato",
|
| 223 |
+
"disease": "Late Blight",
|
| 224 |
+
"severity": "high",
|
| 225 |
+
"symptoms": ["Large irregular pale green to brown water-soaked lesions on leaves", "White cottony growth on underside of leaf in humid conditions", "Dark brown lesions on stems", "Brown firm rot on green or ripe fruits"],
|
| 226 |
+
"causes": "Oomycete — Phytophthora infestans. Same pathogen as potato late blight. Spreads extremely fast.",
|
| 227 |
+
"treatment": {
|
| 228 |
+
"chemical": "Apply Ridomil Gold (metalaxyl + mancozeb) IMMEDIATELY at 2.5g/litre. Repeat every 7 days. Do not delay.",
|
| 229 |
+
"cultural": "Remove and destroy ALL infected plant parts. Do not leave on field. Harvest fruits before they become infected.",
|
| 230 |
+
"preventive": "Apply preventive fungicide before wet season. Ensure good drainage. Avoid dense planting."
|
| 231 |
+
},
|
| 232 |
+
"urgency": "CRITICAL — treat within 24 hours. Can destroy crop within 1 week.",
|
| 233 |
+
"economic_impact": "Can destroy entire tomato crop within 1-2 weeks. Major cause of tomato losses in Nigeria."
|
| 234 |
+
},
|
| 235 |
+
{
|
| 236 |
+
"id": "Tomato___Leaf_Mold",
|
| 237 |
+
"name": "Leaf Mold",
|
| 238 |
+
"plant": "Tomato",
|
| 239 |
+
"disease": "Leaf Mold",
|
| 240 |
+
"severity": "moderate",
|
| 241 |
+
"symptoms": ["Pale green to yellow spots on upper leaf surface", "Olive-green to grey-brown velvety growth on underside of leaves", "Infected leaves curl, wither and drop", "Most common in greenhouses or dense plantings"],
|
| 242 |
+
"causes": "Fungal infection — Passalora fulva, thrives in high humidity (>85%) and moderate temperatures",
|
| 243 |
+
"treatment": {
|
| 244 |
+
"chemical": "Apply chlorothalonil (Bravo) or mancozeb. Improve ventilation before spraying. Spray both upper and lower leaf surfaces.",
|
| 245 |
+
"cultural": "Remove infected leaves. Reduce humidity by improving spacing and ventilation. Avoid overhead irrigation.",
|
| 246 |
+
"preventive": "Maintain plant spacing of at least 60cm. Stake plants. Use resistant varieties where available."
|
| 247 |
+
},
|
| 248 |
+
"urgency": "Moderate — treat within 1-2 weeks",
|
| 249 |
+
"economic_impact": "Can reduce yield by 10-20% mainly through reduced photosynthesis"
|
| 250 |
+
},
|
| 251 |
+
{
|
| 252 |
+
"id": "Tomato___Septoria_leaf_spot",
|
| 253 |
+
"name": "Septoria Leaf Spot",
|
| 254 |
+
"plant": "Tomato",
|
| 255 |
+
"disease": "Septoria Leaf Spot",
|
| 256 |
+
"severity": "moderate",
|
| 257 |
+
"symptoms": ["Many small circular spots with dark brown border and grey-white centre", "Tiny black dots visible in centre of spots (fungal pycnidia)", "Starts on lower leaves and moves upward", "Severe defoliation leaving only top leaves"],
|
| 258 |
+
"causes": "Fungal infection — Septoria lycopersici, spreads by rain splash and tools",
|
| 259 |
+
"treatment": {
|
| 260 |
+
"chemical": "Apply mancozeb (Dithane M-45) or chlorothalonil at first sign. Spray every 7-10 days. Remove lower infected leaves first.",
|
| 261 |
+
"cultural": "Remove infected leaves. Avoid working in field when wet. Mulch to prevent soil splash.",
|
| 262 |
+
"preventive": "Rotate with non-solanaceous crops for 2-3 seasons. Destroy crop residue after harvest."
|
| 263 |
+
},
|
| 264 |
+
"urgency": "Moderate — treat within 2 weeks",
|
| 265 |
+
"economic_impact": "Reduces photosynthesis and can cut yield by 15-25%"
|
| 266 |
+
},
|
| 267 |
+
{
|
| 268 |
+
"id": "Tomato___Spider_mites Two-spotted_spider_mite",
|
| 269 |
+
"name": "Spider Mites (Two-spotted)",
|
| 270 |
+
"plant": "Tomato",
|
| 271 |
+
"disease": "Spider Mite Infestation",
|
| 272 |
+
"severity": "moderate",
|
| 273 |
+
"symptoms": ["Fine stippling (tiny yellow dots) on upper leaf surface", "Bronzing or yellowing of leaves", "Fine silky webbing on underside of leaves and between stems", "Tiny moving dots (mites) visible on underside of leaves", "Leaves dry up and fall in severe infestation"],
|
| 274 |
+
"causes": "Pest — Tetranychus urticae. Population explodes in hot dry weather. Spreads by wind and contact.",
|
| 275 |
+
"treatment": {
|
| 276 |
+
"chemical": "Apply abamectin (Dynamec/Vertimec) at 1ml/5litres or spiromesifen (Oberon) — these are specific miticides. Regular insecticides do NOT kill mites effectively.",
|
| 277 |
+
"cultural": "Spray plants with strong water jets to dislodge mites. Remove heavily infested leaves.",
|
| 278 |
+
"preventive": "Avoid dusty conditions. Maintain adequate irrigation — water-stressed plants are more susceptible. Avoid overuse of broad-spectrum insecticides which kill natural predators."
|
| 279 |
+
},
|
| 280 |
+
"urgency": "Moderate — treat within 1 week, populations double every 3-5 days in hot weather",
|
| 281 |
+
"economic_impact": "Can reduce yield by 20-40% and cause complete crop failure in severe dry season outbreaks"
|
| 282 |
+
},
|
| 283 |
+
{
|
| 284 |
+
"id": "Tomato___Target_Spot",
|
| 285 |
+
"name": "Target Spot",
|
| 286 |
+
"plant": "Tomato",
|
| 287 |
+
"disease": "Target Spot",
|
| 288 |
+
"severity": "moderate",
|
| 289 |
+
"symptoms": ["Circular brown spots with concentric rings (similar to early blight but larger)", "Yellow halo around spots", "Affects leaves, stems and fruits", "Dark brown to black sunken lesions on fruits"],
|
| 290 |
+
"causes": "Fungal infection — Corynespora cassiicola, favoured by warm humid conditions",
|
| 291 |
+
"treatment": {
|
| 292 |
+
"chemical": "Apply azoxystrobin (Amistar) or chlorothalonil (Bravo) fungicide every 10-14 days.",
|
| 293 |
+
"cultural": "Remove infected plant material. Stake plants for better air circulation. Avoid overhead irrigation.",
|
| 294 |
+
"preventive": "Rotate crops. Destroy crop debris after harvest. Plant with adequate spacing."
|
| 295 |
+
},
|
| 296 |
+
"urgency": "Moderate — treat within 2 weeks",
|
| 297 |
+
"economic_impact": "Can reduce yield by 10-25%"
|
| 298 |
+
},
|
| 299 |
+
{
|
| 300 |
+
"id": "Tomato___Tomato_Yellow_Leaf_Curl_Virus",
|
| 301 |
+
"name": "Tomato Yellow Leaf Curl Virus (TYLCV)",
|
| 302 |
+
"plant": "Tomato",
|
| 303 |
+
"disease": "Yellow Leaf Curl Virus",
|
| 304 |
+
"severity": "high",
|
| 305 |
+
"symptoms": ["Upward curling and cupping of young leaves", "Yellow margins on curled leaves", "Stunted plants with reduced internode length", "Very few or no flowers and fruits", "Silverleaf whitefly visible on underside of leaves"],
|
| 306 |
+
"causes": "Viral disease spread by silverleaf whitefly (Bemisia tabaci). No chemical cure for virus.",
|
| 307 |
+
"treatment": {
|
| 308 |
+
"chemical": "No cure for the virus. Control whitefly vector with imidacloprid (Confidor) or spiromesifen (Oberon) every 7 days. Use yellow sticky traps.",
|
| 309 |
+
"cultural": "Remove and destroy ALL infected plants immediately to prevent spread. Do not leave infected plants in field.",
|
| 310 |
+
"preventive": "Use TYLCV-resistant varieties (Cobra F1, Lindo F1, Tylka F1 — available from seed companies). Use insect-proof netting in nurseries."
|
| 311 |
+
},
|
| 312 |
+
"urgency": "High — remove infected plants immediately, no cure",
|
| 313 |
+
"economic_impact": "Can cause 100% yield loss. Major cause of tomato crop failure in Nigeria, especially dry season."
|
| 314 |
+
},
|
| 315 |
+
{
|
| 316 |
+
"id": "Tomato___Tomato_mosaic_virus",
|
| 317 |
+
"name": "Tomato Mosaic Virus (ToMV)",
|
| 318 |
+
"plant": "Tomato",
|
| 319 |
+
"disease": "Mosaic Virus",
|
| 320 |
+
"severity": "high",
|
| 321 |
+
"symptoms": ["Light and dark green mosaic pattern on leaves", "Leaf distortion, blistering and fern-like appearance", "Stunted plant growth", "Reduced and malformed fruits", "Internal browning of fruits"],
|
| 322 |
+
"causes": "Viral disease — Tomato mosaic virus. Spread by mechanical contact, contaminated tools, and hands. Very stable virus.",
|
| 323 |
+
"treatment": {
|
| 324 |
+
"chemical": "No chemical cure for virus. Disinfect all tools with 10% bleach or 70% alcohol. Wash hands thoroughly before handling plants.",
|
| 325 |
+
"cultural": "Remove and destroy infected plants. Do not smoke near plants (tobacco mosaic virus can infect tomatoes). Disinfect tools between plants.",
|
| 326 |
+
"preventive": "Use certified virus-free seeds or seedlings. Wash hands and disinfect tools frequently. Control aphids which may spread other viruses."
|
| 327 |
+
},
|
| 328 |
+
"urgency": "High — remove infected plants immediately",
|
| 329 |
+
"economic_impact": "Can reduce yield by 25-50%. Infected fruits are unmarketable."
|
| 330 |
+
},
|
| 331 |
+
{
|
| 332 |
+
"id": "Tomato___healthy",
|
| 333 |
+
"name": "Healthy",
|
| 334 |
+
"plant": "Tomato",
|
| 335 |
+
"disease": "Healthy",
|
| 336 |
+
"severity": "none",
|
| 337 |
+
"symptoms": ["Dark green healthy leaves", "Strong upright stems", "Normal flower and fruit development", "No spots, lesions or discolouration"],
|
| 338 |
+
"causes": "No disease present",
|
| 339 |
+
"treatment": {
|
| 340 |
+
"chemical": "No treatment needed",
|
| 341 |
+
"cultural": "Maintain staking, adequate NPK fertilisation and consistent irrigation",
|
| 342 |
+
"preventive": "Monitor weekly for whitefly, aphids and early signs of blight. Apply preventive copper spray before rainy season."
|
| 343 |
+
},
|
| 344 |
+
"urgency": "None — plant is healthy",
|
| 345 |
+
"economic_impact": "No negative impact — maintain good practices for optimal yield"
|
| 346 |
+
}
|
| 347 |
+
],
|
| 348 |
+
"severity_levels": {
|
| 349 |
+
"none": {"level": 0, "color": "#16a34a", "description": "No disease — plant is healthy"},
|
| 350 |
+
"low": {"level": 1, "color": "#ca8a04", "description": "Minor infection — monitor closely"},
|
| 351 |
+
"moderate": {"level": 2, "color": "#ea580c", "description": "Moderate infection — treatment recommended within 1-2 weeks"},
|
| 352 |
+
"high": {"level": 3, "color": "#dc2626", "description": "Severe infection — immediate treatment required"},
|
| 353 |
+
"very_high": {"level": 4, "color": "#7f1d1d", "description": "Critical — aggressive treatment needed urgently"}
|
| 354 |
+
},
|
| 355 |
+
"confidence_thresholds": {
|
| 356 |
+
"high_confidence": 0.70,
|
| 357 |
+
"medium_confidence": 0.40,
|
| 358 |
+
"low_confidence": 0.20,
|
| 359 |
+
"uncertain": 0.10
|
| 360 |
+
}
|
| 361 |
+
}
|
main.py
ADDED
|
@@ -0,0 +1,237 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
FarmGuard Nigeria - Plant Disease Detection API
|
| 3 |
+
Using Google Gemini Vision for accurate real photo analysis
|
| 4 |
+
"""
|
| 5 |
+
import json
|
| 6 |
+
import logging
|
| 7 |
+
import os
|
| 8 |
+
import base64
|
| 9 |
+
from google import genai
|
| 10 |
+
from google.genai import types
|
| 11 |
+
from dotenv import load_dotenv
|
| 12 |
+
from fastapi import FastAPI, File, UploadFile, HTTPException
|
| 13 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 14 |
+
|
| 15 |
+
# Load .env file
|
| 16 |
+
load_dotenv()
|
| 17 |
+
|
| 18 |
+
# Setup logging
|
| 19 |
+
logging.basicConfig(level=logging.INFO)
|
| 20 |
+
logger = logging.getLogger(__name__)
|
| 21 |
+
|
| 22 |
+
app = FastAPI(
|
| 23 |
+
title="FarmGuard Nigeria API",
|
| 24 |
+
description="AI-powered plant disease detection for Nigerian farmers",
|
| 25 |
+
version="2.0.0"
|
| 26 |
+
)
|
| 27 |
+
|
| 28 |
+
app.add_middleware(
|
| 29 |
+
CORSMiddleware,
|
| 30 |
+
allow_origins=["*"],
|
| 31 |
+
allow_methods=["*"],
|
| 32 |
+
allow_headers=["*"],
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
+
# Load disease config
|
| 36 |
+
config_path = os.path.join(os.path.dirname(__file__), "disease_config.json")
|
| 37 |
+
with open(config_path) as f:
|
| 38 |
+
DISEASE_CONFIG = json.load(f)
|
| 39 |
+
|
| 40 |
+
# Setup Gemini
|
| 41 |
+
GEMINI_KEY = os.getenv("GEMINI_API_KEY")
|
| 42 |
+
gemini_client = None
|
| 43 |
+
|
| 44 |
+
if GEMINI_KEY:
|
| 45 |
+
gemini_client = genai.Client(api_key=GEMINI_KEY)
|
| 46 |
+
logger.info("Gemini Vision API ready")
|
| 47 |
+
else:
|
| 48 |
+
logger.warning("No GEMINI_API_KEY found in .env file")
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
@app.get("/")
|
| 52 |
+
def root():
|
| 53 |
+
return {
|
| 54 |
+
"message": "FarmGuard Nigeria API is running!",
|
| 55 |
+
"version": "2.0.0",
|
| 56 |
+
"gemini_ready": gemini_client is not None,
|
| 57 |
+
"docs": "/docs"
|
| 58 |
+
}
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
@app.get("/api/health")
|
| 62 |
+
def health():
|
| 63 |
+
return {
|
| 64 |
+
"status": "healthy",
|
| 65 |
+
"gemini_ready": gemini_client is not None,
|
| 66 |
+
}
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
@app.post("/api/predict")
|
| 70 |
+
async def predict(file: UploadFile = File(...)):
|
| 71 |
+
"""Detect plant disease from uploaded leaf image using Gemini Vision."""
|
| 72 |
+
|
| 73 |
+
if file.content_type not in ["image/jpeg", "image/jpg", "image/png"]:
|
| 74 |
+
raise HTTPException(
|
| 75 |
+
status_code=400,
|
| 76 |
+
detail="Only JPEG and PNG images are supported"
|
| 77 |
+
)
|
| 78 |
+
|
| 79 |
+
image_bytes = await file.read()
|
| 80 |
+
|
| 81 |
+
if len(image_bytes) > 8 * 1024 * 1024:
|
| 82 |
+
raise HTTPException(
|
| 83 |
+
status_code=400,
|
| 84 |
+
detail="Image too large. Maximum size is 8MB"
|
| 85 |
+
)
|
| 86 |
+
|
| 87 |
+
if gemini_client is None:
|
| 88 |
+
raise HTTPException(
|
| 89 |
+
status_code=503,
|
| 90 |
+
detail="Gemini API not configured. Add GEMINI_API_KEY to your .env file"
|
| 91 |
+
)
|
| 92 |
+
|
| 93 |
+
try:
|
| 94 |
+
mime_type = file.content_type
|
| 95 |
+
|
| 96 |
+
prompt = """You are an expert Nigerian agricultural plant pathologist.
|
| 97 |
+
|
| 98 |
+
Analyse this plant leaf image and respond ONLY with a JSON object in this exact format with no extra text:
|
| 99 |
+
{
|
| 100 |
+
"crop_identified": true,
|
| 101 |
+
"plant": "crop name e.g. Tomato, Maize, Cassava, Potato, Pepper, Yam, Plantain",
|
| 102 |
+
"disease": "disease name or Healthy",
|
| 103 |
+
"is_healthy": false,
|
| 104 |
+
"confidence": 0.85,
|
| 105 |
+
"severity": "none or low or moderate or high or very_high",
|
| 106 |
+
"symptoms": ["symptom 1", "symptom 2", "symptom 3"],
|
| 107 |
+
"causes": "what causes this disease",
|
| 108 |
+
"treatment": {
|
| 109 |
+
"chemical": "specific chemical treatment with Nigerian product names e.g. Dithane M-45, Cobox, Ridomil Gold, Confidor",
|
| 110 |
+
"cultural": "cultural and physical management practices",
|
| 111 |
+
"preventive": "prevention measures for Nigerian farmers"
|
| 112 |
+
},
|
| 113 |
+
"urgency": "treatment urgency description",
|
| 114 |
+
"economic_impact": "potential yield and economic impact on Nigerian farmer",
|
| 115 |
+
"message": ""
|
| 116 |
+
}
|
| 117 |
+
|
| 118 |
+
Rules:
|
| 119 |
+
- If you cannot see a plant leaf clearly, set crop_identified to false and explain in message
|
| 120 |
+
- Focus on crops grown in Nigeria: Tomato, Maize, Cassava, Potato, Pepper, Yam, Plantain, Rice, Cowpea, Soybean, Orange, Groundnut, Banana
|
| 121 |
+
- Always include locally available Nigerian product names in chemical treatment
|
| 122 |
+
- Be specific and practical for Nigerian smallholder farmers
|
| 123 |
+
- Respond with ONLY the JSON object, nothing else"""
|
| 124 |
+
|
| 125 |
+
# Call Gemini with new SDK
|
| 126 |
+
response = gemini_client.models.generate_content(
|
| 127 |
+
model="gemini-2.5-flash",
|
| 128 |
+
contents=[
|
| 129 |
+
types.Part.from_bytes(data=image_bytes, mime_type=mime_type),
|
| 130 |
+
prompt
|
| 131 |
+
]
|
| 132 |
+
)
|
| 133 |
+
|
| 134 |
+
text = response.text.strip()
|
| 135 |
+
|
| 136 |
+
# Remove markdown code blocks if present
|
| 137 |
+
if "```json" in text:
|
| 138 |
+
text = text.split("```json")[1].split("```")[0].strip()
|
| 139 |
+
elif "```" in text:
|
| 140 |
+
text = text.split("```")[1].split("```")[0].strip()
|
| 141 |
+
|
| 142 |
+
result = json.loads(text)
|
| 143 |
+
|
| 144 |
+
# Handle unidentified image
|
| 145 |
+
if not result.get("crop_identified", False):
|
| 146 |
+
return {
|
| 147 |
+
"success": False,
|
| 148 |
+
"crop_identified": False,
|
| 149 |
+
"message": result.get("message", "Could not identify a plant leaf. Please upload a clear close-up photo of a single leaf."),
|
| 150 |
+
"top_prediction": None,
|
| 151 |
+
"all_predictions": [],
|
| 152 |
+
"is_healthy": False
|
| 153 |
+
}
|
| 154 |
+
|
| 155 |
+
severity = result.get("severity", "moderate")
|
| 156 |
+
severity_colors = {
|
| 157 |
+
"none": "#16a34a", "low": "#ca8a04",
|
| 158 |
+
"moderate": "#ea580c", "high": "#dc2626", "very_high": "#7f1d1d"
|
| 159 |
+
}
|
| 160 |
+
|
| 161 |
+
top_prediction = {
|
| 162 |
+
"plant": result.get("plant", "Unknown"),
|
| 163 |
+
"disease": result.get("disease", "Unknown"),
|
| 164 |
+
"confidence": float(result.get("confidence", 0.8)),
|
| 165 |
+
"severity": severity,
|
| 166 |
+
"severity_color": severity_colors.get(severity, "#ea580c"),
|
| 167 |
+
"symptoms": result.get("symptoms", []),
|
| 168 |
+
"causes": result.get("causes", ""),
|
| 169 |
+
"treatment": result.get("treatment", {}),
|
| 170 |
+
"urgency": result.get("urgency", ""),
|
| 171 |
+
"economic_impact": result.get("economic_impact", "")
|
| 172 |
+
}
|
| 173 |
+
|
| 174 |
+
conf = float(result.get("confidence", 0.8))
|
| 175 |
+
confidence_level = "high" if conf >= 0.7 else "medium" if conf >= 0.4 else "low"
|
| 176 |
+
|
| 177 |
+
return {
|
| 178 |
+
"success": True,
|
| 179 |
+
"crop_identified": True,
|
| 180 |
+
"is_healthy": result.get("is_healthy", False),
|
| 181 |
+
"top_prediction": top_prediction,
|
| 182 |
+
"all_predictions": [top_prediction],
|
| 183 |
+
"confidence_level": confidence_level,
|
| 184 |
+
"message": None
|
| 185 |
+
}
|
| 186 |
+
|
| 187 |
+
except json.JSONDecodeError as e:
|
| 188 |
+
logger.error(f"JSON parse error: {e}")
|
| 189 |
+
raise HTTPException(
|
| 190 |
+
status_code=500,
|
| 191 |
+
detail="Failed to parse AI response. Please try again."
|
| 192 |
+
)
|
| 193 |
+
except Exception as e:
|
| 194 |
+
logger.error(f"Gemini error: {e}")
|
| 195 |
+
raise HTTPException(
|
| 196 |
+
status_code=500,
|
| 197 |
+
detail=f"AI analysis failed: {str(e)}"
|
| 198 |
+
)
|
| 199 |
+
|
| 200 |
+
|
| 201 |
+
@app.get("/api/recommend")
|
| 202 |
+
def recommend(state: str = "Rivers", soil: str = "loamy", season: str = "rainy"):
|
| 203 |
+
"""Get crop recommendations based on location, soil and season."""
|
| 204 |
+
|
| 205 |
+
recommendations = {
|
| 206 |
+
"rainy": {
|
| 207 |
+
"crops": ["Cassava", "Yam", "Maize", "Plantain", "Cocoyam", "Waterleaf", "Fluted Pumpkin (Ugu)"],
|
| 208 |
+
"tip": "Rainy season is ideal for root crops and leafy vegetables. Ensure good drainage to prevent waterlogging."
|
| 209 |
+
},
|
| 210 |
+
"dry": {
|
| 211 |
+
"crops": ["Tomato", "Pepper", "Onion", "Carrot", "Cabbage", "Cowpea", "Soybean", "Groundnut"],
|
| 212 |
+
"tip": "Dry season farming requires irrigation. Vegetables thrive well with consistent water supply."
|
| 213 |
+
}
|
| 214 |
+
}
|
| 215 |
+
|
| 216 |
+
soil_tips = {
|
| 217 |
+
"loamy": "Loamy soil is excellent and suitable for almost all Nigerian crops. Maintain organic matter with compost.",
|
| 218 |
+
"sandy": "Sandy soil drains fast — best for cassava, groundnut and sweet potato. Add organic matter to improve water retention.",
|
| 219 |
+
"clay": "Clay soil retains water well — suitable for rice and yam. Ensure adequate drainage ridges to avoid waterlogging."
|
| 220 |
+
}
|
| 221 |
+
|
| 222 |
+
season_data = recommendations.get(season.lower(), recommendations["rainy"])
|
| 223 |
+
soil_tip = soil_tips.get(soil.lower(), "Prepare soil well with compost before planting.")
|
| 224 |
+
|
| 225 |
+
return {
|
| 226 |
+
"state": state,
|
| 227 |
+
"season": season,
|
| 228 |
+
"soil_type": soil,
|
| 229 |
+
"recommended_crops": season_data["crops"],
|
| 230 |
+
"season_tip": season_data["tip"],
|
| 231 |
+
"soil_tip": soil_tip
|
| 232 |
+
}
|
| 233 |
+
|
| 234 |
+
|
| 235 |
+
if __name__ == "__main__":
|
| 236 |
+
import uvicorn
|
| 237 |
+
uvicorn.run("main:app", host="0.0.0.0", port=8000, reload=True)
|
models/README.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
placeholder - replace with plant_weights.weights.h5
|
models/plant_weights.weights.h5
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:805f5c71dda0a81f148ace65abe4912bd777dfc0b9323253f1886a311fcea475
|
| 3 |
+
size 13463744
|
requirements.txt
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi==0.104.1
|
| 2 |
+
uvicorn==0.24.0
|
| 3 |
+
python-multipart==0.0.6
|
| 4 |
+
pydantic==2.5.0
|
| 5 |
+
pillow==10.1.0
|
| 6 |
+
numpy==1.26.4
|
| 7 |
+
tensorflow==2.19.0
|
| 8 |
+
python-dotenv==1.0.0
|