Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
from fastapi import FastAPI, HTTPException
|
| 3 |
+
from pydantic import BaseModel, validator
|
| 4 |
+
import pandas as pd
|
| 5 |
+
import joblib
|
| 6 |
+
import numpy as np
|
| 7 |
+
|
| 8 |
+
# Creăm aplicația FastAPI
|
| 9 |
+
app = FastAPI(
|
| 10 |
+
title="API Predicție UCS",
|
| 11 |
+
description="API pentru predicția rezistenței la compresiune neconfinată a solurilor stabilizate cu ciment"
|
| 12 |
+
)
|
| 13 |
+
|
| 14 |
+
# Definim structura datelor de intrare folosind Pydantic
|
| 15 |
+
class SoilInput(BaseModel):
|
| 16 |
+
cement_perecent: float
|
| 17 |
+
curing_period: float
|
| 18 |
+
compaction_rate: float
|
| 19 |
+
|
| 20 |
+
# Adăugăm validatori pentru a ne asigura că datele sunt în intervalele corecte
|
| 21 |
+
@validator('cement_perecent')
|
| 22 |
+
def validate_cement(cls, v):
|
| 23 |
+
if not 5 <= v <= 15:
|
| 24 |
+
raise ValueError("Procentul de ciment trebuie să fie între 5% și 15%")
|
| 25 |
+
return v
|
| 26 |
+
|
| 27 |
+
@validator('curing_period')
|
| 28 |
+
def validate_curing(cls, v):
|
| 29 |
+
if not 7 <= v <= 90:
|
| 30 |
+
raise ValueError("Perioada de maturare trebuie să fie între 7 și 90 zile")
|
| 31 |
+
return v
|
| 32 |
+
|
| 33 |
+
@validator('compaction_rate')
|
| 34 |
+
def validate_compaction(cls, v):
|
| 35 |
+
if not 85 <= v <= 100:
|
| 36 |
+
raise ValueError("Rata de compactare trebuie să fie între 85% și 100%")
|
| 37 |
+
return v
|
| 38 |
+
|
| 39 |
+
# Încărcăm modelul la pornirea aplicației
|
| 40 |
+
model = joblib.load('model.joblib')
|
| 41 |
+
|
| 42 |
+
@app.post("/predict")
|
| 43 |
+
async def predict(soil_data: SoilInput):
|
| 44 |
+
"""
|
| 45 |
+
Endpoint pentru predicția UCS bazată pe parametrii solului
|
| 46 |
+
"""
|
| 47 |
+
try:
|
| 48 |
+
# Creăm DataFrame-ul pentru predicție
|
| 49 |
+
input_df = pd.DataFrame([soil_data.dict()])
|
| 50 |
+
|
| 51 |
+
# Facem predicția
|
| 52 |
+
prediction = model.predict(input_df)
|
| 53 |
+
|
| 54 |
+
# Returnăm rezultatul
|
| 55 |
+
return {
|
| 56 |
+
"success": True,
|
| 57 |
+
"prediction": float(prediction[0]),
|
| 58 |
+
"units": "kPa",
|
| 59 |
+
"input_parameters": soil_data.dict()
|
| 60 |
+
}
|
| 61 |
+
except Exception as e:
|
| 62 |
+
raise HTTPException(status_code=400, detail=str(e))
|
| 63 |
+
|
| 64 |
+
@app.get("/model-info")
|
| 65 |
+
async def model_info():
|
| 66 |
+
"""
|
| 67 |
+
Endpoint pentru informații despre model
|
| 68 |
+
"""
|
| 69 |
+
return {
|
| 70 |
+
"model_type": "Random Forest Regressor",
|
| 71 |
+
"features": ["cement_perecent", "curing_period", "compaction_rate"],
|
| 72 |
+
"target": "UCS (kPa)",
|
| 73 |
+
"valid_ranges": {
|
| 74 |
+
"cement_perecent": {"min": 5, "max": 15, "units": "%"},
|
| 75 |
+
"curing_period": {"min": 7, "max": 90, "units": "days"},
|
| 76 |
+
"compaction_rate": {"min": 85, "max": 100, "units": "%"}
|
| 77 |
+
},
|
| 78 |
+
"performance_metrics": {
|
| 79 |
+
"r2_score": 0.982,
|
| 80 |
+
"rmse": 169.87,
|
| 81 |
+
"mape": 5.66
|
| 82 |
+
}
|
| 83 |
+
}
|