Skip to content

Commit 1967c65

Browse files
committed
Fix: improve FastAPI basemodel underscore handling
1 parent ddb2154 commit 1967c65

2 files changed

Lines changed: 61 additions & 16 deletions

File tree

README.md

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
[image12]: ./screenshots/MLOps_proj3_Render_webservice_live.PNG "render web service life"
1515
[image13]: ./screenshots/MLOps_proj3_Render_webservice_live_test_status.PNG "render web service test"
1616
[image14]: ./screenshots/live_post.png "render web service script result"
17+
[image15]: ./screenshots/render_auto-deploy_settings.PNG "render deploy settings"
18+
[image16]: ./screenshots/render_deployed_censusproject.PNG "render app deployed"
19+
[image17]: ./screenshots/render_deployed-app_browser-homepage.PNG "render app welcome"
1720

1821

1922
# US Census Data - Creating and Deploying a Classifier Pipeline as Web Service
@@ -152,7 +155,27 @@ As an examples regarding the use case of having a person earning <=50K as income
152155

153156
* Because default render Python version is 3.7 and this version has issues with dvc, the environment variable PYTHON_VERSION has to be configured being version 3.10.9.
154157
* After selection, render starts its advanced deployment configuation, some parameters are already set, some have to be set manually appropriately. Render guides you through with easy to handle UI's.
155-
* That's it. Implement coding changes, push to the GitHub repository, and the app will automatically redeploy each time, but it will only deploy if your continuous integration action passes.
158+
159+
![render deploy settings][image15]
160+
161+
<br>
162+
163+
* That's it. Implement coding changes, push to the GitHub repository, and the app will automatically redeploy each time, but it will only deploy if your continuous integration action passes.
164+
165+
![render app deployed][image16]
166+
167+
<br>
168+
169+
* Regarding the automatically created render census-project app link used as browser link
170+
```
171+
https://census-project-xki0.onrender.com
172+
```
173+
we get the welcome page message
174+
175+
![render app welcome][image17]
176+
177+
<br>
178+
156179
* Have in mind: if you rely on your CI/CD to fail before fixing an issue, it slows down your deployment. Fix issues early, e.g. by running an ensemble linter like flake8 locally before committing changes.
157180
* For checking the render deployment, a python file exists that uses the httpx module to do one GET and POST on the live render web service and prints its results.
158181

src/app/schemas.py

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
# Imports
1111
###################
1212

13-
from pydantic import BaseModel
13+
from pydantic import BaseModel, Field
1414
from typing import Optional
1515
from enum import Enum
1616

@@ -19,6 +19,9 @@
1919
# Coding
2020
###################
2121

22+
def hyphen_to_underscore(field_name):
23+
return f"{field_name}".replace("_", "-")
24+
2225
class FeatureLabels(str, Enum):
2326
''' Delivers the feature names as needed in Python '''
2427
age = "age"
@@ -39,17 +42,36 @@ class FeatureLabels(str, Enum):
3942

4043
class Person(BaseModel):
4144
''' Delivers the type hints for feature attributes '''
42-
age: int
43-
workclass: Optional[str] = None
44-
fnlgt: int
45-
education: Optional[str] = None
46-
education_num: int
47-
marital_status: Optional[str] = None
48-
occupation: Optional[str] = None
49-
relationship: Optional[str] = None
50-
race: Optional[str] = None
51-
sex: Optional[str] = None
52-
capital_gain: int
53-
capital_loss: int
54-
hours_per_week: int
55-
native_country: Optional[str] = None
45+
# age: int
46+
# workclass: Optional[str] = None
47+
# fnlgt: int
48+
# education: Optional[str] = None
49+
# education_num: int
50+
# marital_status: Optional[str] = None
51+
# occupation: Optional[str] = None
52+
# relationship: Optional[str] = None
53+
# race: Optional[str] = None
54+
# sex: Optional[str] = None
55+
# capital_gain: int
56+
# capital_loss: int
57+
# hours_per_week: int
58+
# native_country: Optional[str] = None
59+
60+
age: int = Field(..., example=45)
61+
capital_gain: int = Field(..., example=2174)
62+
capital_loss: int = Field(..., example=0)
63+
education: str = Field(..., example="Bachelors")
64+
education_num: int = Field(..., example=13)
65+
fnlgt: int = Field(..., example=2334)
66+
hours_per_week: int = Field(..., example=60)
67+
marital_status: str = Field(..., example="Never-married")
68+
native_country: str = Field(..., example="Cuba")
69+
occupation: str = Field(..., example="Prof-specialty")
70+
race: str = Field(..., example="Black")
71+
relationship: str = Field(..., example="Wife")
72+
sex: str = Field(..., example="Female")
73+
workclass: str = Field(..., example="State-gov")
74+
75+
class Config:
76+
alias_generator = hyphen_to_underscore
77+
allow_population_by_field_name = True

0 commit comments

Comments
 (0)