Services and schemas
Here is implemented the main functionality for API services.
Services
Main business logic implementation
Source code in app/services.py
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 | |
__init__()
Initiate preprocessor, validator, spacy validator and rule based validator
Source code in app/services.py
16 17 18 19 20 21 22 23 24 25 | |
post_process_predictions(sentences, predictions, probabilities, locations)
staticmethod
Post process model predictions
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sentences |
list
|
The sentences of the remark |
required |
predictions |
list
|
List of booleans each of which shows whether a sentence is valid or not |
required |
probabilities |
list
|
Probabilities of each sentence prediction |
required |
locations |
list
|
Position of each sentence in remark |
required |
Returns:
| Name | Type | Description |
|---|---|---|
sentences_prediction |
list
|
Prediction for each sentence |
is_all_valid |
bool
|
False if any of sentences in remark contain violation |
Source code in app/services.py
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | |
check_competitors(text, urduc)
Main logic for check_competitors endpoint
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
urduc |
int
|
enum for urduc |
required |
text |
str
|
input string |
required |
Returns:
| Name | Type | Description |
|---|---|---|
pred |
bool
|
0 for violation |
violations |
set[str]
|
set of violations |
Source code in app/services.py
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | |
validate_text(text, threshold, urduc)
Main logic for validate_text endpoint.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text |
str
|
Remark of a house |
required |
threshold |
float
|
Threshold for violation detection |
required |
Returns:
| Name | Type | Description |
|---|---|---|
sentences_predictions |
list
|
Predictions for each sentence |
is_all_valid |
bool
|
False if any of sentences in remark contain violation |
violations |
List[str]
|
violations |
Source code in app/services.py
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 | |
post_process_hud_predictions(sentences, probabilities)
staticmethod
Post process HuggingFace model predictions
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sentences |
list
|
The sentences of the remark |
required |
probabilities |
list
|
Probabilities of each sentence prediction |
required |
Returns:
| Name | Type | Description |
|---|---|---|
sentences_predictions |
list
|
Prediction for each sentence |
Source code in app/services.py
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 | |
validate_hud(text)
Main logic for validate_hud endpoint.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text |
str
|
Remark of a house |
required |
Returns:
| Name | Type | Description |
|---|---|---|
sentences_predictions |
list
|
Predictions for each sentence |
Source code in app/services.py
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 | |
Schemas for endpoints
ValidateSentencesRequest
Bases: BaseModel
Request model for /validate_text endpoint
Source code in app/schemas.py
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | |
Config
Additional configs for model
Source code in app/schemas.py
20 21 22 23 24 25 26 27 28 29 30 31 32 | |
SentenceLocation
Bases: BaseModel
Model for single sentence location
Source code in app/schemas.py
35 36 37 38 39 40 | |
SentencePredictions
Bases: BaseModel
Model for single sentence prediction
Source code in app/schemas.py
43 44 45 46 47 48 49 50 | |
ValidateSentencesResponse
Bases: BaseModel
Response model for /validate_text endpoint
Source code in app/schemas.py
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 | |
Config
Additional configs for model
Source code in app/schemas.py
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 | |
ValidateCheckCompetitorsResponse
Bases: BaseModel
Response model for /validate_sentences endpoint
Source code in app/schemas.py
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 | |
Config
Additional configs for model
Source code in app/schemas.py
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 | |
SentenceViolationPredictions
Bases: BaseModel
Model for single sentence fair housing violation prediction
Source code in app/schemas.py
127 128 129 130 131 132 | |
ValidateHUDRequest
Bases: BaseModel
Request model for /validate_hud endpoint
Source code in app/schemas.py
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 | |
Config
Additional configs for model
Source code in app/schemas.py
143 144 145 146 147 148 149 150 151 152 153 154 | |
ValidateHUDResponse
Bases: BaseModel
Response model for /validate_hud endpoint
Source code in app/schemas.py
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 | |
Config
Additional configs for model
Source code in app/schemas.py
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 | |
Middlewares for pre- and post- processing of requests/responses
LoggingMiddleware
Bases: BaseHTTPMiddleware
Basic logging middleware inherited from starlette.BaseHTTPMiddleware
Source code in app/middlewares.py
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 | |
__init__(app, logger)
Init object with app and logger
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
app |
FastAPI
|
application object where middleware need to be added |
required |
logger |
loger
|
already configured logger for logging requests |
required |
Source code in app/middlewares.py
26 27 28 29 30 31 32 33 34 35 36 37 | |
dispatch(request, call_next)
async
Overriding BaseHTTPMiddleware.dispatch method to implement logging logic
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
request |
Request
|
current request |
required |
call_next |
RequestResponseEndpoint
|
call function |
required |
Returns:
| Name | Type | Description |
|---|---|---|
streaming_response |
StreamingResponse
|
streaming response for the endpoint |
Source code in app/middlewares.py
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 | |
ExceptionHandlerMiddleware
Bases: BaseHTTPMiddleware
A middleware to handle errors
Source code in app/middlewares.py
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 | |
dispatch(request, call_next)
async
Try to process the request. If failed, return details about the exception
Source code in app/middlewares.py
89 90 91 92 93 94 95 96 97 98 | |
extract_info(error)
staticmethod
Extract the type and the message of an error and return as a dict
Source code in app/middlewares.py
100 101 102 103 104 | |