Update app.py
Browse files
app.py
CHANGED
|
@@ -1,10 +1,9 @@
|
|
| 1 |
-
from fastapi import FastAPI, HTTPException
|
| 2 |
from pydantic import BaseModel, validator
|
| 3 |
import pandas as pd
|
|
|
|
| 4 |
import numpy as np
|
| 5 |
-
from sklearn.ensemble import RandomForestRegressor # sau ce model folosim
|
| 6 |
|
| 7 |
-
# Definim structura datelor de intrare
|
| 8 |
class SoilInput(BaseModel):
|
| 9 |
cement_perecent: float
|
| 10 |
curing_period: float
|
|
@@ -28,34 +27,20 @@ class SoilInput(BaseModel):
|
|
| 28 |
raise ValueError("Rata de compactare trebuie să fie între 85% și 100%")
|
| 29 |
return v
|
| 30 |
|
| 31 |
-
|
| 32 |
-
app = FastAPI(
|
| 33 |
-
title="Predicție UCS pentru Sol Stabilizat cu Ciment",
|
| 34 |
-
description="API pentru predicția rezistenței la compresiune neconfinată a solurilor stabilizate cu ciment"
|
| 35 |
-
)
|
| 36 |
|
| 37 |
-
#
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
)
|
| 45 |
-
|
| 46 |
-
# Definim ordinea caracteristicilor
|
| 47 |
-
FEATURE_ORDER = ['cement_perecent', 'curing_period', 'compaction_rate']
|
| 48 |
|
| 49 |
@app.post("/predict")
|
| 50 |
async def predict(soil_data: SoilInput):
|
| 51 |
-
"""
|
| 52 |
-
Endpoint pentru predicția UCS bazată pe parametrii solului
|
| 53 |
-
"""
|
| 54 |
try:
|
| 55 |
-
|
| 56 |
-
input_df = pd.DataFrame([soil_data.dict()])[FEATURE_ORDER]
|
| 57 |
-
|
| 58 |
-
# Facem predicția
|
| 59 |
prediction = model.predict(input_df)
|
| 60 |
|
| 61 |
return {
|
|
@@ -65,26 +50,4 @@ async def predict(soil_data: SoilInput):
|
|
| 65 |
"input_parameters": soil_data.dict()
|
| 66 |
}
|
| 67 |
except Exception as e:
|
| 68 |
-
raise HTTPException(status_code=400, detail=str(e))
|
| 69 |
-
|
| 70 |
-
@app.get("/model-info")
|
| 71 |
-
async def model_info():
|
| 72 |
-
"""
|
| 73 |
-
Endpoint pentru informații despre model
|
| 74 |
-
"""
|
| 75 |
-
return {
|
| 76 |
-
"model_type": "Random Forest Regressor",
|
| 77 |
-
"features": FEATURE_ORDER,
|
| 78 |
-
"target": "UCS (kPa)",
|
| 79 |
-
"valid_ranges": {
|
| 80 |
-
"cement_perecent": {"min": 5, "max": 15, "units": "%"},
|
| 81 |
-
"curing_period": {"min": 7, "max": 90, "units": "days"},
|
| 82 |
-
"compaction_rate": {"min": 85, "max": 100, "units": "%"}
|
| 83 |
-
},
|
| 84 |
-
"model_parameters": {
|
| 85 |
-
"n_estimators": 205,
|
| 86 |
-
"max_depth": 11,
|
| 87 |
-
"min_samples_split": 6,
|
| 88 |
-
"min_samples_leaf": 2
|
| 89 |
-
}
|
| 90 |
-
}
|
|
|
|
| 1 |
+
from fastapi import FastAPI, HTTPException, Request
|
| 2 |
from pydantic import BaseModel, validator
|
| 3 |
import pandas as pd
|
| 4 |
+
import joblib
|
| 5 |
import numpy as np
|
|
|
|
| 6 |
|
|
|
|
| 7 |
class SoilInput(BaseModel):
|
| 8 |
cement_perecent: float
|
| 9 |
curing_period: float
|
|
|
|
| 27 |
raise ValueError("Rata de compactare trebuie să fie între 85% și 100%")
|
| 28 |
return v
|
| 29 |
|
| 30 |
+
app = FastAPI()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
+
# Încărcăm direct modelul Random Forest, fără wrapper
|
| 33 |
+
try:
|
| 34 |
+
model = joblib.load('model.joblib') # Aici folosim modelul salvat fără wrapper
|
| 35 |
+
print("Model încărcat cu succes!")
|
| 36 |
+
except Exception as e:
|
| 37 |
+
print(f"Eroare la încărcarea modelului: {str(e)}")
|
| 38 |
+
raise
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
@app.post("/predict")
|
| 41 |
async def predict(soil_data: SoilInput):
|
|
|
|
|
|
|
|
|
|
| 42 |
try:
|
| 43 |
+
input_df = pd.DataFrame([soil_data.dict()])
|
|
|
|
|
|
|
|
|
|
| 44 |
prediction = model.predict(input_df)
|
| 45 |
|
| 46 |
return {
|
|
|
|
| 50 |
"input_parameters": soil_data.dict()
|
| 51 |
}
|
| 52 |
except Exception as e:
|
| 53 |
+
raise HTTPException(status_code=400, detail=str(e))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|