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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# ===============================
|
| 2 |
+
# Imports
|
| 3 |
+
# ===============================
|
| 4 |
+
import torch
|
| 5 |
+
import spaces
|
| 6 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
# ===============================
|
| 10 |
+
# Load Model & Tokenizer (ONCE)
|
| 11 |
+
# ===============================
|
| 12 |
+
print("Loading Aksara v1 model...")
|
| 13 |
+
|
| 14 |
+
tokenizer = AutoTokenizer.from_pretrained(
|
| 15 |
+
"cropinailab/aksara_v1",
|
| 16 |
+
use_fast=True,
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 20 |
+
"cropinailab/aksara_v1",
|
| 21 |
+
torch_dtype=torch.bfloat16,
|
| 22 |
+
device_map="cuda", # ensures GPU usage on Spaces
|
| 23 |
+
)
|
| 24 |
+
|
| 25 |
+
model.eval()
|
| 26 |
+
|
| 27 |
+
print("Model loaded successfully!")
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
# ===============================
|
| 31 |
+
# Generation Function (GPU)
|
| 32 |
+
# ===============================
|
| 33 |
+
@spaces.GPU
|
| 34 |
+
def generate_agri_response(plant, disease):
|
| 35 |
+
prompt = f"""
|
| 36 |
+
You are an agricultural expert specializing in plant pathology, crop nutrition, and safe farm management.
|
| 37 |
+
Your job is to provide accurate, scientifically correct, and legally safe advice.
|
| 38 |
+
|
| 39 |
+
Plant: {plant}
|
| 40 |
+
Issue: {disease}
|
| 41 |
+
|
| 42 |
+
Your response MUST follow this structure clearly and must be 100% accurate:
|
| 43 |
+
|
| 44 |
+
### 1. About the Disease
|
| 45 |
+
- Explain what the disease is and identify the correct pathogen type (fungus, bacteria, virus, pest, oomycete, etc.)
|
| 46 |
+
- Describe how it spreads (only scientifically correct modes of spread)
|
| 47 |
+
- Avoid any incorrect or exaggerated claims
|
| 48 |
+
|
| 49 |
+
### 2. Symptoms
|
| 50 |
+
- Describe accurate symptoms on each relevant plant part:
|
| 51 |
+
- Leaves
|
| 52 |
+
- Stems
|
| 53 |
+
- Roots
|
| 54 |
+
- Fruit (only if the plant actually produces edible fruit; e.g., do NOT mention fruits for potatoes)
|
| 55 |
+
- Tubers/roots if the crop is root-based
|
| 56 |
+
- Do NOT invent symptoms or confuse this disease with others.
|
| 57 |
+
|
| 58 |
+
### 3. Safe & Legal Treatment Options
|
| 59 |
+
Provide ONLY safe, standard treatments used by agricultural extension services.
|
| 60 |
+
Include:
|
| 61 |
+
- Copper-based fungicides
|
| 62 |
+
- Mancozeb
|
| 63 |
+
- Chlorothalonil
|
| 64 |
+
- Sulfur (only when relevant)
|
| 65 |
+
- Biological controls (Trichoderma, Bacillus subtilis, Pseudomonas fluorescens)
|
| 66 |
+
- Cultural practices: removing infected leaves, improving airflow, reducing moisture
|
| 67 |
+
|
| 68 |
+
Rules for treatments:
|
| 69 |
+
- NEVER provide exact dosages, mixing ratios, or spray quantities
|
| 70 |
+
- Do NOT recommend any unsafe or banned chemicals
|
| 71 |
+
- Do NOT claim that neem oil treats fungal/oomycete diseases (only mention it for insects if relevant)
|
| 72 |
+
|
| 73 |
+
Clarify:
|
| 74 |
+
- Fertilizers DO NOT cure disease; they only improve plant strength and recovery.
|
| 75 |
+
|
| 76 |
+
### 4. Prevention
|
| 77 |
+
Include scientifically correct preventive methods:
|
| 78 |
+
- Resistant/tolerant varieties
|
| 79 |
+
- Crop rotation (specify years if relevant)
|
| 80 |
+
- Proper spacing & airflow
|
| 81 |
+
- Mulching and soil moisture control
|
| 82 |
+
- Use of drip irrigation (avoid overhead)
|
| 83 |
+
- Early detection & monitoring
|
| 84 |
+
- Removing volunteer plants and debris
|
| 85 |
+
|
| 86 |
+
### 5. Nutrient Requirements for This Plant
|
| 87 |
+
Explain the essential nutrients needed for this specific crop:
|
| 88 |
+
- Nitrogen (N)
|
| 89 |
+
- Phosphorus (P)
|
| 90 |
+
- Potassium (K)
|
| 91 |
+
- Calcium (Ca)
|
| 92 |
+
- Magnesium (Mg)
|
| 93 |
+
- Sulfur (S)
|
| 94 |
+
- Micronutrients: Iron (Fe), Zinc (Zn), Boron (B), Manganese (Mn), Copper (Cu), Molybdenum (Mo)
|
| 95 |
+
|
| 96 |
+
Explain:
|
| 97 |
+
- The role of each nutrient
|
| 98 |
+
- Why it matters for growth, immunity, yield, and stress resistance
|
| 99 |
+
|
| 100 |
+
### 6. Fertilizer Recommendations (No Dosages)
|
| 101 |
+
List fertilizers that improve plant nutrition and increase disease resistance (but do NOT treat disease directly):
|
| 102 |
+
|
| 103 |
+
**Chemical / Mineral Fertilizers**
|
| 104 |
+
- NPK (balanced or crop-specific)
|
| 105 |
+
- Urea (N source)
|
| 106 |
+
- DAP (N + P)
|
| 107 |
+
- MOP or SOP (potassium sources for disease resistance)
|
| 108 |
+
- Rock phosphate (long-term P)
|
| 109 |
+
- Gypsum (Ca + S)
|
| 110 |
+
|
| 111 |
+
**Organic Fertilizers**
|
| 112 |
+
- Compost
|
| 113 |
+
- Vermicompost
|
| 114 |
+
- Bone meal
|
| 115 |
+
- Seaweed extract (improves immunity & stress tolerance)
|
| 116 |
+
- Panchagavya / Jeevamrut (if culturally relevant)
|
| 117 |
+
|
| 118 |
+
**Biofertilizers (beneficial microbes)**
|
| 119 |
+
- Azotobacter / Azospirillum (N-fixing)
|
| 120 |
+
- PSB (phosphate-solubilizing bacteria)
|
| 121 |
+
- KMB (potassium-mobilizing bacteria)
|
| 122 |
+
- Trichoderma-enriched compost (suppresses soil-borne pathogens)
|
| 123 |
+
|
| 124 |
+
For each fertilizer:
|
| 125 |
+
- Explain what nutrient it provides
|
| 126 |
+
- Explain when it is useful (early growth, fruiting, disease recovery, root development)
|
| 127 |
+
- DO NOT provide dosages or exact application rates
|
| 128 |
+
|
| 129 |
+
### 7. Additional Good Practices
|
| 130 |
+
Include:
|
| 131 |
+
- Irrigation management
|
| 132 |
+
- Soil drainage improvement
|
| 133 |
+
- Tool sanitation
|
| 134 |
+
- Field hygiene
|
| 135 |
+
- Proper storage of harvested produce (if disease affects storage)
|
| 136 |
+
"""
|
| 137 |
+
|
| 138 |
+
# Tokenize
|
| 139 |
+
inputs = tokenizer(
|
| 140 |
+
prompt,
|
| 141 |
+
return_tensors="pt",
|
| 142 |
+
).to(model.device)
|
| 143 |
+
|
| 144 |
+
# Generate
|
| 145 |
+
outputs = model.generate(
|
| 146 |
+
**inputs,
|
| 147 |
+
max_new_tokens=600,
|
| 148 |
+
temperature=0.7,
|
| 149 |
+
top_p=0.9,
|
| 150 |
+
repetition_penalty=1.15,
|
| 151 |
+
do_sample=True,
|
| 152 |
+
pad_token_id=tokenizer.eos_token_id,
|
| 153 |
+
)
|
| 154 |
+
|
| 155 |
+
full_output = tokenizer.decode(
|
| 156 |
+
outputs[0],
|
| 157 |
+
skip_special_tokens=True,
|
| 158 |
+
)
|
| 159 |
+
|
| 160 |
+
# Remove echoed prompt safely
|
| 161 |
+
if full_output.startswith(prompt):
|
| 162 |
+
cleaned = full_output[len(prompt):].strip()
|
| 163 |
+
else:
|
| 164 |
+
cleaned = full_output.replace(prompt, "").strip()
|
| 165 |
+
|
| 166 |
+
return cleaned
|
| 167 |
+
|
| 168 |
+
|
| 169 |
+
# ===============================
|
| 170 |
+
# Local Test (optional)
|
| 171 |
+
# ===============================
|
| 172 |
+
if __name__ == "__main__":
|
| 173 |
+
print(generate_agri_response("Potato", "Late Blight"))
|