haidarsamar's picture
Upload inference.py
445bcba verified
Raw
History Blame Contribute Delete
1.05 kB
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)}