bteodoru commited on
Commit
4fc617b
·
verified ·
1 Parent(s): b0af23d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -16
app.py CHANGED
@@ -1,6 +1,6 @@
1
  from fastapi import FastAPI, HTTPException
2
  from pydantic import BaseModel, validator, model_validator
3
- from pydantic.errors import PydanticValueError
4
  import pandas as pd
5
  import joblib
6
  import numpy as np
@@ -18,18 +18,18 @@ except Exception as e:
18
  model = None # Setăm modelul ca None în caz de eroare
19
  FEATURE_ORDER = [] # Inițializăm o listă goală pentru a evita erorile ulterioare
20
 
21
- # Definim clase personalizate pentru erori
22
- class CementPercentError(PydanticValueError):
23
- msg_template = "Cement percentage must be between 0% and 15%"
24
 
25
- class CuringPeriodError(PydanticValueError):
26
- msg_template = "Curing period must be between 1 and 90 days"
27
 
28
- class CompactionRateError(PydanticValueError):
29
- msg_template = "Compaction velocity must be between 0.5 and 1.5 mm/min"
30
 
31
 
32
- class SoilInput(BaseModel):
33
  cement_perecent: float
34
  curing_period: float
35
  compaction_rate: float
@@ -41,24 +41,58 @@ class SoilInput(BaseModel):
41
  self.curing_period = 0
42
  else:
43
  if not (1 <= self.curing_period <= 90):
44
- raise CuringPeriodError()
45
- # raise ValueError("Perioada de maturare trebuie fie între 1 și 90 zile dacă cement_perecent > 0")
46
  return self
47
 
48
  @validator('cement_perecent')
49
  def validate_cement(cls, v):
50
  if not 0 <= v <= 15:
51
- raise CementPercentError()
52
- # raise ValueError("Procentul de ciment trebuie fie între 0% și 15%")
53
  return v
54
 
55
  @validator('compaction_rate')
56
  def validate_compaction(cls, v):
57
  if not 0.5 <= v <= 1.5:
58
- raise CompactionRateError()
59
- # raise ValueError("Viteza de compactare trebuie fie între 0.5 și 1.5 mm/min")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60
  return v
61
 
 
 
 
 
 
 
62
 
63
  @app.post("/predict")
64
  async def predict(soil_data: SoilInput):
@@ -69,8 +103,23 @@ async def predict(soil_data: SoilInput):
69
  raise HTTPException(status_code=500, detail="Modelul nu a fost încărcat corect")
70
 
71
  try:
72
- # Construim DataFrame-ul pentru predicție
73
  input_data = soil_data.dict()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74
  input_df = pd.DataFrame([input_data])
75
 
76
  # Ne asigurăm că ordinea caracteristicilor este corectă
 
1
  from fastapi import FastAPI, HTTPException
2
  from pydantic import BaseModel, validator, model_validator
3
+ # from pydantic.errors import PydanticValueError
4
  import pandas as pd
5
  import joblib
6
  import numpy as np
 
18
  model = None # Setăm modelul ca None în caz de eroare
19
  FEATURE_ORDER = [] # Inițializăm o listă goală pentru a evita erorile ulterioare
20
 
21
+ # # Definim clase personalizate pentru erori
22
+ # class CementPercentError(PydanticValueError):
23
+ # msg_template = "Cement percentage must be between 0% and 15%"
24
 
25
+ # class CuringPeriodError(PydanticValueError):
26
+ # msg_template = "Curing period must be between 1 and 90 days"
27
 
28
+ # class CompactionRateError(PydanticValueError):
29
+ # msg_template = "Compaction velocity must be between 0.5 and 1.5 mm/min"
30
 
31
 
32
+ class SoilInput__(BaseModel):
33
  cement_perecent: float
34
  curing_period: float
35
  compaction_rate: float
 
41
  self.curing_period = 0
42
  else:
43
  if not (1 <= self.curing_period <= 90):
44
+ # raise CuringPeriodError()
45
+ raise ValueError("Curing period must be between 1 and 90 days")
46
  return self
47
 
48
  @validator('cement_perecent')
49
  def validate_cement(cls, v):
50
  if not 0 <= v <= 15:
51
+ # raise CementPercentError()
52
+ raise ValueError("Cement percentage must be between 0% and 15%")
53
  return v
54
 
55
  @validator('compaction_rate')
56
  def validate_compaction(cls, v):
57
  if not 0.5 <= v <= 1.5:
58
+ # raise CompactionRateError()
59
+ raise ValueError("Compaction velocity must be between 0.5 and 1.5 mm/min")
60
+ return v
61
+
62
+ class SoilInput(BaseModel):
63
+ cement_perecent: float = Field(
64
+ ..., # This means the field is required
65
+ description="Cement percentage in the mixture"
66
+ )
67
+ curing_period: float = Field(
68
+ ...,
69
+ description="Number of days for curing"
70
+ )
71
+ compaction_rate: float = Field(
72
+ ...,
73
+ description="Rate of compaction in mm/min"
74
+ )
75
+
76
+ @field_validator('cement_perecent')
77
+ @classmethod
78
+ def validate_cement(cls, v: float) -> float:
79
+ if not 0 <= v <= 15:
80
+ raise ValueError("Cement percentage must be between 0% and 15%")
81
+ return v
82
+
83
+ @field_validator('curing_period')
84
+ @classmethod
85
+ def validate_curing(cls, v: float) -> float:
86
+ if not 1 <= v <= 90:
87
+ raise ValueError("Curing period must be between 1 and 90 days")
88
  return v
89
 
90
+ @field_validator('compaction_rate')
91
+ @classmethod
92
+ def validate_compaction(cls, v: float) -> float:
93
+ if not 0.5 <= v <= 1.5:
94
+ raise ValueError("Compaction rate must be between 0.5 and 1.5 mm/min")
95
+ return v
96
 
97
  @app.post("/predict")
98
  async def predict(soil_data: SoilInput):
 
103
  raise HTTPException(status_code=500, detail="Modelul nu a fost încărcat corect")
104
 
105
  try:
 
106
  input_data = soil_data.dict()
107
+
108
+ # Aplicăm regula fizică: dacă nu avem ciment, perioada de maturare este 0
109
+ if input_data['cement_perecent'] == 0:
110
+ # Păstrăm datele originale pentru răspuns
111
+ original_data = input_data.copy()
112
+ # Modificăm perioada de maturare pentru predicție
113
+ input_data['curing_period'] = 0
114
+
115
+ # Adăugăm o notă explicativă în răspuns
116
+ explanation = "Pentru amestecuri fără ciment, perioada de maturare nu influențează rezistența."
117
+ else:
118
+ original_data = input_data
119
+ explanation = None
120
+
121
+ # Construim DataFrame-ul pentru predicție
122
+ # input_data = soil_data.dict()
123
  input_df = pd.DataFrame([input_data])
124
 
125
  # Ne asigurăm că ordinea caracteristicilor este corectă