Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
from fastapi import FastAPI, HTTPException
|
| 2 |
-
from pydantic import BaseModel, validator
|
| 3 |
import pandas as pd
|
| 4 |
import joblib
|
| 5 |
import numpy as np
|
|
@@ -19,30 +19,60 @@ except Exception as e:
|
|
| 19 |
|
| 20 |
|
| 21 |
# Definirea clasei pentru inputuri
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
class SoilInput(BaseModel):
|
| 23 |
cement_perecent: float
|
| 24 |
curing_period: float
|
| 25 |
compaction_rate: float
|
| 26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
@validator('cement_perecent')
|
| 28 |
def validate_cement(cls, v):
|
| 29 |
if not 0 <= v <= 15:
|
| 30 |
raise ValueError("Procentul de ciment trebuie să fie între 0% și 15%")
|
| 31 |
return v
|
| 32 |
|
| 33 |
-
@validator('curing_period')
|
| 34 |
-
def validate_curing(cls, v):
|
| 35 |
-
if not 1 <= v <= 90:
|
| 36 |
-
raise ValueError("Perioada de maturare trebuie să fie între 1 și 90 zile")
|
| 37 |
-
return v
|
| 38 |
-
|
| 39 |
@validator('compaction_rate')
|
| 40 |
def validate_compaction(cls, v):
|
| 41 |
if not 0.5 <= v <= 1.5:
|
| 42 |
raise ValueError("Viteza de compactare trebuie să fie între 0.5 și 1.5 mm/min")
|
| 43 |
return v
|
| 44 |
-
|
| 45 |
-
|
| 46 |
@app.post("/predict")
|
| 47 |
async def predict(soil_data: SoilInput):
|
| 48 |
"""
|
|
|
|
| 1 |
from fastapi import FastAPI, HTTPException
|
| 2 |
+
from pydantic import BaseModel, validator, root_validator
|
| 3 |
import pandas as pd
|
| 4 |
import joblib
|
| 5 |
import numpy as np
|
|
|
|
| 19 |
|
| 20 |
|
| 21 |
# Definirea clasei pentru inputuri
|
| 22 |
+
# class SoilInput(BaseModel):
|
| 23 |
+
# cement_perecent: float
|
| 24 |
+
# curing_period: float
|
| 25 |
+
# compaction_rate: float
|
| 26 |
+
|
| 27 |
+
# @validator('cement_perecent')
|
| 28 |
+
# def validate_cement(cls, v):
|
| 29 |
+
# if not 0 <= v <= 15:
|
| 30 |
+
# raise ValueError("Procentul de ciment trebuie să fie între 0% și 15%")
|
| 31 |
+
# return v
|
| 32 |
+
|
| 33 |
+
# @validator('curing_period')
|
| 34 |
+
# def validate_curing(cls, v):
|
| 35 |
+
# if not 1 <= v <= 90:
|
| 36 |
+
# raise ValueError("Perioada de maturare trebuie să fie între 1 și 90 zile")
|
| 37 |
+
# return v
|
| 38 |
+
|
| 39 |
+
# @validator('compaction_rate')
|
| 40 |
+
# def validate_compaction(cls, v):
|
| 41 |
+
# if not 0.5 <= v <= 1.5:
|
| 42 |
+
# raise ValueError("Viteza de compactare trebuie să fie între 0.5 și 1.5 mm/min")
|
| 43 |
+
# return v
|
| 44 |
+
|
| 45 |
class SoilInput(BaseModel):
|
| 46 |
cement_perecent: float
|
| 47 |
curing_period: float
|
| 48 |
compaction_rate: float
|
| 49 |
|
| 50 |
+
@root_validator
|
| 51 |
+
def check_cement_and_curing(cls, values):
|
| 52 |
+
cement = values.get('cement_perecent')
|
| 53 |
+
curing = values.get('curing_period')
|
| 54 |
+
# Dacă cement_perecent este 0, forțăm curing_period să fie 0,
|
| 55 |
+
# indiferent de valoarea transmisă.
|
| 56 |
+
if cement == 0:
|
| 57 |
+
values['curing_period'] = 0
|
| 58 |
+
else:
|
| 59 |
+
# În cazul în care cimentul nu e 0, ne asigurăm că curing_period este între 1 și 90.
|
| 60 |
+
if not (1 <= curing <= 90):
|
| 61 |
+
raise ValueError("Perioada de maturare trebuie să fie între 1 și 90 zile dacă cement_perecent > 0")
|
| 62 |
+
return values
|
| 63 |
+
|
| 64 |
@validator('cement_perecent')
|
| 65 |
def validate_cement(cls, v):
|
| 66 |
if not 0 <= v <= 15:
|
| 67 |
raise ValueError("Procentul de ciment trebuie să fie între 0% și 15%")
|
| 68 |
return v
|
| 69 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
@validator('compaction_rate')
|
| 71 |
def validate_compaction(cls, v):
|
| 72 |
if not 0.5 <= v <= 1.5:
|
| 73 |
raise ValueError("Viteza de compactare trebuie să fie între 0.5 și 1.5 mm/min")
|
| 74 |
return v
|
| 75 |
+
|
|
|
|
| 76 |
@app.post("/predict")
|
| 77 |
async def predict(soil_data: SoilInput):
|
| 78 |
"""
|