Spaces:
Sleeping
Sleeping
Upload 9 files
Browse files- Dockerfile +17 -0
- api/feature_engineering.py +29 -0
- api/main.py +193 -0
- data/features_data.csv +0 -0
- models/feature_names.json +1 -0
- models/model.pkl +3 -0
- models/preprocessor.pkl +3 -0
- models/remodel.ipynb +0 -0
- requirements.txt +9 -0
Dockerfile
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.11-slim
|
| 2 |
+
|
| 3 |
+
RUN apt-get update && apt-get install -y --no-install-recommends \
|
| 4 |
+
libgomp1 \
|
| 5 |
+
&& rm -rf /var/lib/apt/lists/*
|
| 6 |
+
|
| 7 |
+
WORKDIR /app
|
| 8 |
+
|
| 9 |
+
COPY requirements.txt /app/requirements.txt
|
| 10 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 11 |
+
|
| 12 |
+
COPY . /app
|
| 13 |
+
|
| 14 |
+
ENV PORT=7860
|
| 15 |
+
EXPOSE 7860
|
| 16 |
+
|
| 17 |
+
CMD ["uvicorn", "api.main:app", "--host", "0.0.0.0", "--port", "7860"]
|
api/feature_engineering.py
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
+
import numpy as np
|
| 3 |
+
|
| 4 |
+
def feature_engineering(input_df: pd.DataFrame) -> pd.DataFrame:
|
| 5 |
+
# Get season
|
| 6 |
+
def get_season(month):
|
| 7 |
+
if month in [12, 1, 2]:
|
| 8 |
+
return 'Winter'
|
| 9 |
+
elif month in [3, 4, 5]:
|
| 10 |
+
return 'Spring'
|
| 11 |
+
elif month in [6, 7, 8]:
|
| 12 |
+
return 'Summer'
|
| 13 |
+
else:
|
| 14 |
+
return 'Autumn'
|
| 15 |
+
|
| 16 |
+
input_df['season'] = input_df['month'].apply(get_season)
|
| 17 |
+
|
| 18 |
+
# Soil ratios
|
| 19 |
+
input_df['soil_total'] = input_df['sand'] + input_df['silt'] + input_df['clay']
|
| 20 |
+
input_df['soil_total'] = input_df['soil_total'].replace(0, np.nan)
|
| 21 |
+
input_df['sand_ratio'] = input_df['sand'] / input_df['soil_total']
|
| 22 |
+
input_df['silt_ratio'] = input_df['silt'] / input_df['soil_total']
|
| 23 |
+
input_df['clay_ratio'] = input_df['clay'] / input_df['soil_total']
|
| 24 |
+
input_df = input_df.drop(columns=['soil_total'])
|
| 25 |
+
|
| 26 |
+
# Combined feature
|
| 27 |
+
input_df['soc_cec'] = input_df['soc'] * input_df['cec']
|
| 28 |
+
|
| 29 |
+
return input_df
|
api/main.py
ADDED
|
@@ -0,0 +1,193 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# main.py
|
| 2 |
+
from fastapi import FastAPI, HTTPException
|
| 3 |
+
from pydantic import BaseModel
|
| 4 |
+
import pandas as pd
|
| 5 |
+
import numpy as np
|
| 6 |
+
import joblib, json, os
|
| 7 |
+
from math import radians, cos, sin, asin, sqrt
|
| 8 |
+
|
| 9 |
+
# =========================
|
| 10 |
+
# تحميل الموديل والـ artifacts
|
| 11 |
+
# =========================
|
| 12 |
+
MODEL_DIR = os.path.join(os.path.dirname(__file__), "..", "models")
|
| 13 |
+
MODEL_PATH = os.path.join(MODEL_DIR, "model.pkl")
|
| 14 |
+
PREPROCESSOR_PATH = os.path.join(MODEL_DIR, "preprocessor.pkl")
|
| 15 |
+
FEATURES_PATH = os.path.join(MODEL_DIR, "feature_names.json")
|
| 16 |
+
|
| 17 |
+
model = joblib.load(MODEL_PATH)
|
| 18 |
+
preprocessor = joblib.load(PREPROCESSOR_PATH)
|
| 19 |
+
with open(FEATURES_PATH) as f:
|
| 20 |
+
feature_names = json.load(f)
|
| 21 |
+
|
| 22 |
+
# =========================
|
| 23 |
+
# تحميل البيانات من CSV
|
| 24 |
+
# =========================
|
| 25 |
+
DATA_PATH = os.path.join(os.path.dirname(__file__), "..", "data", "features_data.csv")
|
| 26 |
+
features_df = pd.read_csv(DATA_PATH)
|
| 27 |
+
|
| 28 |
+
# =========================
|
| 29 |
+
# Feature Engineering
|
| 30 |
+
# =========================
|
| 31 |
+
def get_season(month: int) -> str:
|
| 32 |
+
if month in [12, 1, 2]:
|
| 33 |
+
return "Winter"
|
| 34 |
+
elif month in [3, 4, 5]:
|
| 35 |
+
return "Spring"
|
| 36 |
+
elif month in [6, 7, 8]:
|
| 37 |
+
return "Summer"
|
| 38 |
+
return "Autumn"
|
| 39 |
+
|
| 40 |
+
def feature_engineering(df: pd.DataFrame) -> pd.DataFrame:
|
| 41 |
+
df = df.copy()
|
| 42 |
+
df["season"] = df["month"].apply(get_season)
|
| 43 |
+
|
| 44 |
+
soil_total = df[["sand", "silt", "clay"]].sum(axis=1).replace(0, np.nan)
|
| 45 |
+
df["sand_ratio"] = (df["sand"] / soil_total).fillna(0.0)
|
| 46 |
+
df["silt_ratio"] = (df["silt"] / soil_total).fillna(0.0)
|
| 47 |
+
df["clay_ratio"] = (df["clay"] / soil_total).fillna(0.0)
|
| 48 |
+
|
| 49 |
+
df["soc_cec"] = df["soc"] * df["cec"]
|
| 50 |
+
|
| 51 |
+
return df
|
| 52 |
+
|
| 53 |
+
# =========================
|
| 54 |
+
# Input Schema
|
| 55 |
+
# =========================
|
| 56 |
+
class MapClick(BaseModel):
|
| 57 |
+
lat: float
|
| 58 |
+
lon: float
|
| 59 |
+
|
| 60 |
+
# =========================
|
| 61 |
+
# Helpers
|
| 62 |
+
# =========================
|
| 63 |
+
def haversine(lat1, lon1, lat2, lon2):
|
| 64 |
+
"""حساب المسافة بين نقطتين (كم)"""
|
| 65 |
+
R = 6371.0
|
| 66 |
+
dlat = radians(lat2 - lat1)
|
| 67 |
+
dlon = radians(lon2 - lon1)
|
| 68 |
+
a = sin(dlat/2)**2 + cos(radians(lat1)) * cos(radians(lat2)) * sin(dlon/2)**2
|
| 69 |
+
c = 2 * asin(sqrt(a))
|
| 70 |
+
return R * c
|
| 71 |
+
|
| 72 |
+
def fetch_nearest_features(lat: float, lon: float, max_km=15):
|
| 73 |
+
"""إرجاع أقرب نقطة من CSV"""
|
| 74 |
+
best = None
|
| 75 |
+
best_dist = 1e9
|
| 76 |
+
|
| 77 |
+
for _, row in features_df.iterrows():
|
| 78 |
+
d = haversine(lat, lon, row["latitude"], row["longitude"])
|
| 79 |
+
if d < best_dist:
|
| 80 |
+
best_dist = d
|
| 81 |
+
best = row.to_dict()
|
| 82 |
+
best["distance_km"] = d
|
| 83 |
+
|
| 84 |
+
if best and best_dist <= max_km:
|
| 85 |
+
return best
|
| 86 |
+
return None
|
| 87 |
+
|
| 88 |
+
def classify_desertification(ndvi: float) -> str:
|
| 89 |
+
if ndvi < 0.2:
|
| 90 |
+
return "شديد"
|
| 91 |
+
elif ndvi < 0.5:
|
| 92 |
+
return "متوسط"
|
| 93 |
+
return "منخفض"
|
| 94 |
+
|
| 95 |
+
# =========================
|
| 96 |
+
# توصيات ديناميكية
|
| 97 |
+
# =========================
|
| 98 |
+
def recommend_actions(severity: str, row: dict) -> list:
|
| 99 |
+
recs = []
|
| 100 |
+
|
| 101 |
+
if severity == "شديد":
|
| 102 |
+
recs.extend([
|
| 103 |
+
"التشجير وزراعة أحزمة خضراء",
|
| 104 |
+
"استخدام تقنيات حصاد المياه",
|
| 105 |
+
"منع الرعي الجائر وتنظيمه",
|
| 106 |
+
"تطبيق أنظمة ري حديثة (تنقيط/رش)"
|
| 107 |
+
])
|
| 108 |
+
elif severity == "متوسط":
|
| 109 |
+
recs.extend([
|
| 110 |
+
"إدارة التربة وتحسين خصوبتها (إضافة مواد عضوية)",
|
| 111 |
+
"تنويع المحاصيل واستخدام محاصيل تغطية",
|
| 112 |
+
"تحسين ممارسات الري",
|
| 113 |
+
"الحد من إزالة الغطاء النباتي"
|
| 114 |
+
])
|
| 115 |
+
else: # منخفض
|
| 116 |
+
recs.extend([
|
| 117 |
+
"المحافظة على الغطاء النباتي الحالي",
|
| 118 |
+
"المتابعة والرصد الدوري للتغيرات",
|
| 119 |
+
"التوسع في الزراعة المستدامة",
|
| 120 |
+
"التوعية المجتمعية لمنع الممارسات الضارة"
|
| 121 |
+
])
|
| 122 |
+
|
| 123 |
+
# توصيات إضافية مبنية على الـ features
|
| 124 |
+
rainfall = row.get("tp_m", 0)
|
| 125 |
+
temp = row.get("t2m_c", 0)
|
| 126 |
+
sand_ratio = row.get("sand_ratio", 0)
|
| 127 |
+
clay_ratio = row.get("clay_ratio", 0)
|
| 128 |
+
soc_cec = row.get("soc_cec", 0)
|
| 129 |
+
|
| 130 |
+
if rainfall > 200 and sand_ratio > 0.6 and severity == "منخفض":
|
| 131 |
+
recs.append("تشجيع الزراعات الاقتصادية سريعة النمو لاستغلال الموارد المائية")
|
| 132 |
+
|
| 133 |
+
if rainfall < 50 and clay_ratio > 0.4 and severity != "منخفض":
|
| 134 |
+
recs.append("إنشاء سدود صغيرة أو خزانات لتخزين مياه الأمطار")
|
| 135 |
+
|
| 136 |
+
if temp > 35 and severity == "شديد":
|
| 137 |
+
recs.append("زراعة محاصيل مقاومة للجفاف والحرارة (مثل الشعير والدخن)")
|
| 138 |
+
|
| 139 |
+
if sand_ratio > 0.7:
|
| 140 |
+
recs.append("إضافة مواد عضوية أو طينية للتربة الرملية لزيادة قدرتها على الاحتفاظ بالماء")
|
| 141 |
+
|
| 142 |
+
if clay_ratio > 0.5:
|
| 143 |
+
recs.append("تجهيز أنظمة صرف زراعي لمنع تملح التربة الثقيلة")
|
| 144 |
+
|
| 145 |
+
if soc_cec < 0.2 and severity != "منخفض":
|
| 146 |
+
recs.append("إضافة محسنات للتربة (كمبوست/سماد عضوي) لرفع خصوبة التربة")
|
| 147 |
+
|
| 148 |
+
return recs
|
| 149 |
+
|
| 150 |
+
# =========================
|
| 151 |
+
# FastAPI app
|
| 152 |
+
# =========================
|
| 153 |
+
app = FastAPI()
|
| 154 |
+
|
| 155 |
+
@app.post("/predict-by-location")
|
| 156 |
+
def predict_by_location(click: MapClick):
|
| 157 |
+
try:
|
| 158 |
+
# 1. دور على أقرب نقطة من CSV
|
| 159 |
+
row = fetch_nearest_features(click.lat, click.lon, max_km=15)
|
| 160 |
+
if not row:
|
| 161 |
+
raise HTTPException(status_code=404, detail="لا توجد بيانات قريبة لهذه الإحداثيات")
|
| 162 |
+
|
| 163 |
+
# 2. حوّل النقطة DataFrame
|
| 164 |
+
df = pd.DataFrame([row])
|
| 165 |
+
|
| 166 |
+
# 3. Feature engineering
|
| 167 |
+
df = feature_engineering(df)
|
| 168 |
+
|
| 169 |
+
# 4. تأكد إن الأعمدة مرتبة حسب feature_names
|
| 170 |
+
df = df[feature_names]
|
| 171 |
+
|
| 172 |
+
# 5. Prediction
|
| 173 |
+
X_trans = preprocessor.transform(df)
|
| 174 |
+
ndvi_pred = float(model.predict(X_trans)[0])
|
| 175 |
+
|
| 176 |
+
# 6. تقييم التصحر + توصيات
|
| 177 |
+
severity = classify_desertification(ndvi_pred)
|
| 178 |
+
recs = recommend_actions(severity, df.iloc[0].to_dict())
|
| 179 |
+
|
| 180 |
+
return {
|
| 181 |
+
"clicked_point": {"lat": click.lat, "lon": click.lon},
|
| 182 |
+
"nearest_data_point": {
|
| 183 |
+
"lat": row["latitude"],
|
| 184 |
+
"lon": row["longitude"],
|
| 185 |
+
"distance_km": round(row["distance_km"], 2)
|
| 186 |
+
},
|
| 187 |
+
"predicted_ndvi": round(ndvi_pred, 3),
|
| 188 |
+
"desertification_level": severity,
|
| 189 |
+
"recommendations": recs
|
| 190 |
+
}
|
| 191 |
+
|
| 192 |
+
except Exception as e:
|
| 193 |
+
raise HTTPException(status_code=500, detail=str(e))
|
data/features_data.csv
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
models/feature_names.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
["longitude", "latitude", "year", "month", "area", "t2m_c", "td2m_c", "rh_pct", "tp_m", "ssrd_jm2", "LC_Type1", "sand", "silt", "clay", "soc", "ph", "bdod", "cec", "POP", "season", "sand_ratio", "silt_ratio", "clay_ratio", "soc_cec"]
|
models/model.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:a593035e1a75aefdd28cbdbecebe1a6a583d32345ff98b48f27856bd275c165d
|
| 3 |
+
size 158170789
|
models/preprocessor.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:db331fab9ee29069314601a54db2460d5a573bb6b7cf5f29920c9f69db45ec90
|
| 3 |
+
size 4494
|
models/remodel.ipynb
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
requirements.txt
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi==0.111.0
|
| 2 |
+
uvicorn[standard]==0.30.1
|
| 3 |
+
pandas==2.2.2
|
| 4 |
+
numpy==1.26.4
|
| 5 |
+
lightgbm==4.3.0
|
| 6 |
+
joblib==1.4.2
|
| 7 |
+
pydantic==2.8.2
|
| 8 |
+
python-dotenv==1.0.1
|
| 9 |
+
scikit-learn==1.5.1
|