Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -72,4 +72,36 @@ def prepare_input(data: dict):
|
|
| 72 |
df["age"] = ((dt - dob).dt.days / 365.25).astype("float32")
|
| 73 |
|
| 74 |
lat1, lon1 = np.radians(df["lat"]), np.radians(df["long"])
|
| 75 |
-
lat2, lon2 = np.radians
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
df["age"] = ((dt - dob).dt.days / 365.25).astype("float32")
|
| 73 |
|
| 74 |
lat1, lon1 = np.radians(df["lat"]), np.radians(df["long"])
|
| 75 |
+
lat2, lon2 = np.radians(df["merch_lat"]), np.radians(df["merch_long"])
|
| 76 |
+
d = np.sin((lat2-lat1)/2)**2 + np.cos(lat1)*np.cos(lat2)*np.sin((lon2-lon1)/2)**2
|
| 77 |
+
df["distance"] = (6371 * 2 * np.arcsin(np.sqrt(d))).astype("float32")
|
| 78 |
+
|
| 79 |
+
df["avg_amt"] = df["amt"]
|
| 80 |
+
df["std_amt"] = 0.0
|
| 81 |
+
df["nb_trans"] = 1.0
|
| 82 |
+
|
| 83 |
+
expected_cols = [
|
| 84 |
+
'amt', 'hour', 'day_of_week', 'day', 'month', 'age', 'lat', 'long',
|
| 85 |
+
'city_pop', 'distance', 'avg_amt', 'std_amt', 'nb_trans',
|
| 86 |
+
'category', 'gender', 'state', 'merchant', 'job'
|
| 87 |
+
]
|
| 88 |
+
return df[expected_cols]
|
| 89 |
+
|
| 90 |
+
# --- 5. ENDPOINT NETTOYÉ ---
|
| 91 |
+
@app.post("/predict")
|
| 92 |
+
def predict(data: Transaction):
|
| 93 |
+
if model is None:
|
| 94 |
+
return {"error": "Modèle non chargé"}
|
| 95 |
+
try:
|
| 96 |
+
X_processed = prepare_input(data.dict())
|
| 97 |
+
prediction = model.predict(X_processed)
|
| 98 |
+
|
| 99 |
+
# On ne renvoie que la valeur brute
|
| 100 |
+
return int(prediction[0])
|
| 101 |
+
|
| 102 |
+
except Exception as e:
|
| 103 |
+
return {"error": str(e)}
|
| 104 |
+
|
| 105 |
+
if __name__ == "__main__":
|
| 106 |
+
import uvicorn
|
| 107 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|