SIH-Crop-Yield-API / tests /test_predictor.py
AshrafGalibSk's picture
Upload folder using huggingface_hub
bbd5f9c verified
Raw
History Blame Contribute Delete
1.47 kB
#!/usr/bin/env python3
import subprocess
import time
import requests
import json
# Start the server in the background
print("Starting server...")
server_process = subprocess.Popen(
["python3", "start.py"],
env={"PORT": "8005"},
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
# Wait for server to start
time.sleep(8)
try:
# Test health endpoint first
response = requests.get("http://localhost:8005/health", timeout=10)
print(f"Health check: {response.status_code}")
health_data = response.json()
print(f"Model loaded: {health_data.get('model_loaded')}")
# Test prediction endpoint with sample data
prediction_data = {
"year": 2024,
"state": "Punjab",
"crop": "Rice",
"season": "Kharif",
"area": 10.0,
"production": 25.0,
"rainfall": 1200,
"fertilizer": 75,
"pesticide": 8
}
response = requests.post(
"http://localhost:8005/predict",
json=prediction_data,
timeout=10
)
print(f"Prediction status: {response.status_code}")
if response.status_code == 200:
print("✅ Prediction successful!")
print(response.json())
else:
print("❌ Prediction failed:")
print(response.text)
except Exception as e:
print(f"Error testing: {e}")
finally:
# Stop the server
server_process.terminate()
server_process.wait()
print("Server stopped")