Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,13 +2,20 @@
|
|
| 2 |
# Imports
|
| 3 |
# ===============================
|
| 4 |
import torch
|
|
|
|
| 5 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 6 |
|
| 7 |
|
| 8 |
# ===============================
|
| 9 |
-
#
|
| 10 |
# ===============================
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
tokenizer = AutoTokenizer.from_pretrained(
|
| 14 |
"cropinailab/aksara_v1",
|
|
@@ -17,16 +24,16 @@ tokenizer = AutoTokenizer.from_pretrained(
|
|
| 17 |
|
| 18 |
model = AutoModelForCausalLM.from_pretrained(
|
| 19 |
"cropinailab/aksara_v1",
|
| 20 |
-
torch_dtype=torch.float32,
|
| 21 |
)
|
| 22 |
|
| 23 |
model.eval()
|
| 24 |
|
| 25 |
-
print("Model loaded successfully
|
| 26 |
|
| 27 |
|
| 28 |
# ===============================
|
| 29 |
-
# Generation Function
|
| 30 |
# ===============================
|
| 31 |
def generate_agri_response(plant, disease):
|
| 32 |
prompt = f"""
|
|
@@ -48,93 +55,42 @@ Your response MUST follow this structure clearly and must be 100% accurate:
|
|
| 48 |
- Leaves
|
| 49 |
- Stems
|
| 50 |
- Roots
|
| 51 |
-
- Fruit (only if the plant actually produces edible fruit
|
| 52 |
- Tubers/roots if the crop is root-based
|
| 53 |
-
- Do NOT invent symptoms or confuse this disease with others.
|
| 54 |
|
| 55 |
### 3. Safe & Legal Treatment Options
|
| 56 |
-
Provide ONLY safe, standard treatments used by agricultural extension services.
|
| 57 |
-
Include:
|
| 58 |
- Copper-based fungicides
|
| 59 |
- Mancozeb
|
| 60 |
- Chlorothalonil
|
| 61 |
-
-
|
| 62 |
-
-
|
| 63 |
-
-
|
| 64 |
-
|
| 65 |
-
Rules for treatments:
|
| 66 |
-
- NEVER provide exact dosages, mixing ratios, or spray quantities
|
| 67 |
-
- Do NOT recommend any unsafe or banned chemicals
|
| 68 |
-
- Do NOT claim that neem oil treats fungal/oomycete diseases (only mention it for insects if relevant)
|
| 69 |
-
|
| 70 |
-
Clarify:
|
| 71 |
-
- Fertilizers DO NOT cure disease; they only improve plant strength and recovery.
|
| 72 |
|
| 73 |
### 4. Prevention
|
| 74 |
-
|
| 75 |
-
-
|
| 76 |
-
-
|
| 77 |
-
-
|
| 78 |
-
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
### 5. Nutrient Requirements for This Plant
|
| 84 |
-
Explain the essential nutrients needed for this specific crop:
|
| 85 |
-
- Nitrogen (N)
|
| 86 |
-
- Phosphorus (P)
|
| 87 |
-
- Potassium (K)
|
| 88 |
-
- Calcium (Ca)
|
| 89 |
-
- Magnesium (Mg)
|
| 90 |
-
- Sulfur (S)
|
| 91 |
-
- Micronutrients: Iron (Fe), Zinc (Zn), Boron (B), Manganese (Mn), Copper (Cu), Molybdenum (Mo)
|
| 92 |
-
|
| 93 |
-
Explain:
|
| 94 |
-
- The role of each nutrient
|
| 95 |
-
- Why it matters for growth, immunity, yield, and stress resistance
|
| 96 |
|
| 97 |
### 6. Fertilizer Recommendations (No Dosages)
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
- NPK (balanced or crop-specific)
|
| 102 |
-
- Urea (N source)
|
| 103 |
-
- DAP (N + P)
|
| 104 |
-
- MOP or SOP (potassium sources for disease resistance)
|
| 105 |
-
- Rock phosphate (long-term P)
|
| 106 |
-
- Gypsum (Ca + S)
|
| 107 |
-
|
| 108 |
-
**Organic Fertilizers**
|
| 109 |
-
- Compost
|
| 110 |
-
- Vermicompost
|
| 111 |
-
- Bone meal
|
| 112 |
-
- Seaweed extract (improves immunity & stress tolerance)
|
| 113 |
-
- Panchagavya / Jeevamrut (if culturally relevant)
|
| 114 |
-
|
| 115 |
-
**Biofertilizers (beneficial microbes)**
|
| 116 |
-
- Azotobacter / Azospirillum (N-fixing)
|
| 117 |
-
- PSB (phosphate-solubilizing bacteria)
|
| 118 |
-
- KMB (potassium-mobilizing bacteria)
|
| 119 |
-
- Trichoderma-enriched compost (suppresses soil-borne pathogens)
|
| 120 |
-
|
| 121 |
-
For each fertilizer:
|
| 122 |
-
- Explain what nutrient it provides
|
| 123 |
-
- Explain when it is useful (early growth, fruiting, disease recovery, root development)
|
| 124 |
-
- DO NOT provide dosages or exact application rates
|
| 125 |
|
| 126 |
### 7. Additional Good Practices
|
| 127 |
-
|
| 128 |
-
-
|
| 129 |
-
- Soil drainage improvement
|
| 130 |
- Tool sanitation
|
| 131 |
-
- Field hygiene
|
| 132 |
-
- Proper storage of harvested produce (if disease affects storage)
|
| 133 |
"""
|
| 134 |
|
| 135 |
inputs = tokenizer(prompt, return_tensors="pt")
|
| 136 |
|
| 137 |
-
with torch.no_grad():
|
| 138 |
outputs = model.generate(
|
| 139 |
**inputs,
|
| 140 |
max_new_tokens=600,
|
|
@@ -147,17 +103,65 @@ Include:
|
|
| 147 |
|
| 148 |
full_output = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 149 |
|
| 150 |
-
# Remove
|
| 151 |
if full_output.startswith(prompt):
|
| 152 |
-
|
| 153 |
else:
|
| 154 |
-
|
|
|
|
|
|
|
| 155 |
|
| 156 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 157 |
|
| 158 |
|
| 159 |
# ===============================
|
| 160 |
-
#
|
| 161 |
# ===============================
|
| 162 |
if __name__ == "__main__":
|
| 163 |
-
|
|
|
|
| 2 |
# Imports
|
| 3 |
# ===============================
|
| 4 |
import torch
|
| 5 |
+
import gradio as gr
|
| 6 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 7 |
|
| 8 |
|
| 9 |
# ===============================
|
| 10 |
+
# Optional CPU optimizations
|
| 11 |
# ===============================
|
| 12 |
+
torch.set_num_threads(4) # adjust based on CPU cores
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
# ===============================
|
| 16 |
+
# Load Model & Tokenizer (ONCE)
|
| 17 |
+
# ===============================
|
| 18 |
+
print("Loading Aksara v1 model on CPU...")
|
| 19 |
|
| 20 |
tokenizer = AutoTokenizer.from_pretrained(
|
| 21 |
"cropinailab/aksara_v1",
|
|
|
|
| 24 |
|
| 25 |
model = AutoModelForCausalLM.from_pretrained(
|
| 26 |
"cropinailab/aksara_v1",
|
| 27 |
+
torch_dtype=torch.float32, # CPU-safe
|
| 28 |
)
|
| 29 |
|
| 30 |
model.eval()
|
| 31 |
|
| 32 |
+
print("Model loaded successfully!")
|
| 33 |
|
| 34 |
|
| 35 |
# ===============================
|
| 36 |
+
# Core Generation Function
|
| 37 |
# ===============================
|
| 38 |
def generate_agri_response(plant, disease):
|
| 39 |
prompt = f"""
|
|
|
|
| 55 |
- Leaves
|
| 56 |
- Stems
|
| 57 |
- Roots
|
| 58 |
+
- Fruit (only if the plant actually produces edible fruit)
|
| 59 |
- Tubers/roots if the crop is root-based
|
|
|
|
| 60 |
|
| 61 |
### 3. Safe & Legal Treatment Options
|
|
|
|
|
|
|
| 62 |
- Copper-based fungicides
|
| 63 |
- Mancozeb
|
| 64 |
- Chlorothalonil
|
| 65 |
+
- Biological controls (Trichoderma, Bacillus, Pseudomonas)
|
| 66 |
+
- Cultural practices (airflow, sanitation, moisture control)
|
| 67 |
+
- NO dosages or banned chemicals
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
### 4. Prevention
|
| 70 |
+
- Resistant varieties
|
| 71 |
+
- Crop rotation
|
| 72 |
+
- Spacing & airflow
|
| 73 |
+
- Drip irrigation
|
| 74 |
+
- Field hygiene
|
| 75 |
+
|
| 76 |
+
### 5. Nutrient Requirements
|
| 77 |
+
Explain roles of:
|
| 78 |
+
N, P, K, Ca, Mg, S, Fe, Zn, B, Mn, Cu, Mo
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
|
| 80 |
### 6. Fertilizer Recommendations (No Dosages)
|
| 81 |
+
- Chemical
|
| 82 |
+
- Organic
|
| 83 |
+
- Biofertilizers
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
|
| 85 |
### 7. Additional Good Practices
|
| 86 |
+
- Irrigation
|
| 87 |
+
- Drainage
|
|
|
|
| 88 |
- Tool sanitation
|
|
|
|
|
|
|
| 89 |
"""
|
| 90 |
|
| 91 |
inputs = tokenizer(prompt, return_tensors="pt")
|
| 92 |
|
| 93 |
+
with torch.no_grad():
|
| 94 |
outputs = model.generate(
|
| 95 |
**inputs,
|
| 96 |
max_new_tokens=600,
|
|
|
|
| 103 |
|
| 104 |
full_output = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 105 |
|
| 106 |
+
# Remove prompt echo safely
|
| 107 |
if full_output.startswith(prompt):
|
| 108 |
+
response = full_output[len(prompt):].strip()
|
| 109 |
else:
|
| 110 |
+
response = full_output.replace(prompt, "").strip()
|
| 111 |
+
|
| 112 |
+
return response
|
| 113 |
|
| 114 |
+
|
| 115 |
+
# ===============================
|
| 116 |
+
# Gradio Interface
|
| 117 |
+
# ===============================
|
| 118 |
+
with gr.Blocks(title="🌱 Agricultural Disease Advisor") as demo:
|
| 119 |
+
gr.Markdown(
|
| 120 |
+
"""
|
| 121 |
+
# 🌱 Agricultural Disease Advisor
|
| 122 |
+
**CPU-based AI assistant for plant disease management**
|
| 123 |
+
|
| 124 |
+
Enter the crop and disease name to receive **safe, scientific, and legal agricultural guidance**.
|
| 125 |
+
"""
|
| 126 |
+
)
|
| 127 |
+
|
| 128 |
+
with gr.Row():
|
| 129 |
+
with gr.Column(scale=1):
|
| 130 |
+
plant_input = gr.Textbox(
|
| 131 |
+
label="🌾 Plant / Crop Name",
|
| 132 |
+
placeholder="e.g., Potato, Tomato, Rice",
|
| 133 |
+
)
|
| 134 |
+
|
| 135 |
+
disease_input = gr.Textbox(
|
| 136 |
+
label="🦠 Disease / Issue",
|
| 137 |
+
placeholder="e.g., Late Blight, Leaf Curl Virus",
|
| 138 |
+
)
|
| 139 |
+
|
| 140 |
+
generate_btn = gr.Button("🔍 Generate Advice", variant="primary")
|
| 141 |
+
|
| 142 |
+
with gr.Column(scale=2):
|
| 143 |
+
output_text = gr.Markdown(
|
| 144 |
+
label="📋 Agricultural Guidance",
|
| 145 |
+
)
|
| 146 |
+
|
| 147 |
+
generate_btn.click(
|
| 148 |
+
fn=generate_agri_response,
|
| 149 |
+
inputs=[plant_input, disease_input],
|
| 150 |
+
outputs=output_text,
|
| 151 |
+
)
|
| 152 |
+
|
| 153 |
+
gr.Markdown(
|
| 154 |
+
"""
|
| 155 |
+
---
|
| 156 |
+
⚠️ **Disclaimer:**
|
| 157 |
+
This tool provides general agricultural guidance only.
|
| 158 |
+
Always consult local agricultural extension services for field-level decisions.
|
| 159 |
+
"""
|
| 160 |
+
)
|
| 161 |
|
| 162 |
|
| 163 |
# ===============================
|
| 164 |
+
# Launch
|
| 165 |
# ===============================
|
| 166 |
if __name__ == "__main__":
|
| 167 |
+
demo.launch()
|