File size: 1,045 Bytes
445bcba | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | import pickle
import numpy as np
# تحميل الموديل
with open("random_forest_pkl.pkl", "rb") as f:
model = pickle.load(f)
# خريطة التصنيفات
label_map = {
0: "غير خصبة",
1: "خصبة",
2: "عالية الخصوبة"
}
def predict(inputs):
"""
تنبؤ خصوبة التربة بناءً على مدخلات عددية
"""
try:
if isinstance(inputs, dict):
features = inputs.get("inputs")
elif isinstance(inputs, list):
features = inputs
else:
return {"error": "تنسيق البيانات غير صحيح"}
if not isinstance(features, list) or len(features) != 12:
return {"error": "مطلوب 12 خاصية في قائمة"}
data = np.array(features).reshape(1, -1)
code = model.predict(data)[0]
label = label_map.get(code, "غير معروف")
return [{"label": label}]
except Exception as e:
return {"error": str(e)} |