cropYeild / test_api.py
Jathin Chetty
Update API to support bulk crop predictions
5d4f29d
Raw
History Blame Contribute Delete
1 kB
import urllib.request
import json
url = "http://127.0.0.1:7860/predict" # For local testing we use localhost
payload = {
"state": "andhra pradesh",
"district": "ananthapur",
# Notice we omitted the "crop" field to test the bulk prediction!
"season": "kharif",
"crop_area": 100.0,
"rainfall": 600.0,
"avg_temp": 28.0,
"humidity": 70.0,
"dominant_soil": "sandy alfisol",
"dominant_soil_percent": 100.0,
"N": 100.0,
"P": 50.0,
"K": 50.0,
"ph": 6.5
}
data = json.dumps(payload).encode('utf-8')
req = urllib.request.Request(url, data=data, headers={'Content-Type': 'application/json'})
try:
with urllib.request.urlopen(req) as response:
result = json.loads(response.read().decode('utf-8'))
print("Success! Bulk Response:")
print(json.dumps(result, indent=2))
except urllib.error.HTTPError as e:
print(f"Error {e.code}: {e.reason}")
print(e.read().decode('utf-8'))
except Exception as e:
print(f"Error: {e}")