File size: 24,776 Bytes
38d6a33 |
1 2 3 4 5 6 7 8 9 10 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 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 |
"""
Copyright 2024 Infosys Ltd.”
Use of this source code is governed by MIT license that can be found in the LICENSE file or at
MIT license https://opensource.org/licenses/MIT
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""
from typing import Annotated, Any, Dict, List
from fairness.config.logger import CustomLogger
from fairness.exception.exception import FairnessException
from fairness.mappers.mappers import AuditRequest, BatchId, MonitoringRequest
from fairness.service.service import FairnessService
from fairness.service.service import FairnessUIservice
from fairness.service.service_latest import FairnessServiceUpload
from fairness.service.service_latest import FairnessUIserviceUpload
from fairness.service.preprocessing import FairnessServicePreproc
from fairness.service.preprocessing import FairnessUIservicePreproc
from fairness.service.inprocessing import InprocessingService
from fairness.service.wrapper import FairnessWorkbench
from fairness.service.bart import BartService
from fairness.service.service_success_rates import SuccessRateService
from fairness.service.service_monitoring import FairnessAudit
from fastapi import APIRouter, HTTPException, UploadFile, Form,Body
from requests.exceptions import ChunkedEncodingError
from fastapi.responses import FileResponse
from fairness.mappers.mappers import FairnessAnalysisRequest,MitigationRequest,IndividualRequest,GetDataRequest
from fairness.exception.custom_exception import CustomHTTPException
from fairness.Telemetry.Telemetry_call import workbench_preprocess_labels, workbench_preprocess_get_batchId, workbenchAnalyse_get_data, workbench_Analyse, inprocess_get_data, inprocess_uploadfile, Wrapper_download, wraper_batch
from fairness.Telemetry.Telemetry_call import image_openai_keys,text_openai_keys
from fastapi import APIRouter, HTTPException, Depends
from fairness.auth.auth_client_id import get_auth_client_id
from fairness.auth.auth_jwt import get_auth_jwt
from fairness.auth.auth_none import get_auth_none
from fairness.mappers.mappers import GetDataRequest
import traceback
import os
from openai import AuthenticationError
llm_router = APIRouter()
workbench_router = APIRouter()
standalone_apis_router = APIRouter()
monitoring_apis=APIRouter()
log = CustomLogger()
uiService = FairnessUIservice()
service = FairnessService()
analyse = FairnessServiceUpload()
analyseUpload = FairnessUIserviceUpload()
servicepreproc = FairnessServicePreproc()
uiServicepreproc = FairnessUIservicePreproc()
fairnessWorkbench = FairnessWorkbench()
inprocessing = InprocessingService()
fairnesstelemetryurl = os.getenv("FAIRNESS_TELEMETRY_URL")
telemetry_flag = os.getenv("tel_Falg")
auth_type = os.environ.get("AUTH_TYPE")
if auth_type == "azure":
auth = get_auth_client_id()
elif auth_type == "jwt":
auth = get_auth_jwt()
elif auth_type == 'none':
auth = get_auth_none()
else:
raise HTTPException(status_code=500, detail="Invalid authentication type configured")
@llm_router.post('/fairness/analysis/llm')
def bias_analysis_in_text(
response: Annotated[str, Form()],
evaluator: Annotated[str, Form()], auth= Depends(auth)
):
try:
payload={
'response': response,
'evaluator': evaluator
}
log.info('before invoking get analysis llm service')
log.debug('request payload: '+str(payload))
response=uiService.get_analysis_llm(payload)
log.debug('response: '+str(response))
return response
except HTTPException as cie:
log.error(cie.__dict__)
log.info("inside exception data")
log.info("exit create usecase routing method")
traceback.print_exc()
tb = traceback.TracebackException.from_exception(cie)
last_traceback = list(tb.format())[-2]
errormsg = str(last_traceback)
raise CustomHTTPException(text_openai_keys, "OPEN AI KEY WAS NOT FOUND", "open ai keys are not found in db")
except AuthenticationError as cie:
log.error(cie.__dict__)
log.info("inside exception data")
log.info("exit create usecase routing method")
traceback.print_exc()
tb = traceback.TracebackException.from_exception(cie)
last_traceback = list(tb.format())[-2]
errormsg = str(last_traceback)
raise CustomHTTPException(text_openai_keys, "OPEN AI KEY is Invalid", "open ai keys are not found in db")
@llm_router.post('/fairness/analysis/image')
def bias_analysis_in_image(
prompt: Annotated[str, Form()],
image: UploadFile,
evaluator: Annotated[str, Form()], auth= Depends(auth)
):
try:
payload={
'prompt': prompt,
'image': image,
'evaluator': evaluator
}
log.info('before invoking get analysis image service')
log.debug('request payload: '+str(payload))
response=uiService.get_analysis_image(payload)
log.debug('response: '+str(response))
return response
except HTTPException as cie:
log.error(cie.__dict__)
log.info("inside exception data")
log.info("exit create usecase routing method")
traceback.print_exc()
tb = traceback.TracebackException.from_exception(cie)
last_traceback = list(tb.format())[-2]
errormsg = str(last_traceback)
raise CustomHTTPException(image_openai_keys, "OPEN AI KEY WAS NOT FOUND", "open ai keys are not found in db")
except AuthenticationError as cie:
log.error(cie.__dict__)
log.info("inside exception data")
log.info("exit create usecase routing method")
traceback.print_exc()
tb = traceback.TracebackException.from_exception(cie)
last_traceback = list(tb.format())[-2]
errormsg = str(last_traceback)
raise CustomHTTPException(image_openai_keys, "OPEN AI KEY is Invalid", "open ai keys are not found in db")
@llm_router.post('/fairness/bart/response')
def sterotype_classification_using_bart(
text:Annotated[str, Form()], auth= Depends(auth)
):
try:
bartService=BartService()
log.info("before invoking service ")
log.debug("request payload: " + str(text))
log.debug(str(text))
response = bartService.predict(text)
log.debug(response)
return response
except FairnessException as cie:
log.error(cie.__dict__)
log.info("exit method")
raise HTTPException(**cie.__dict__)
@standalone_apis_router.post('/fairness/analyse')
def analyze_classification_dataset(payload:FairnessAnalysisRequest=Body(...),file:GetDataRequest= Depends(), auth= Depends(auth)):
payload = {
"biasType": payload.biasType,
"methodType": payload.methodType,
"taskType": payload.taskType,
"label": payload.label,
"predLabel":payload.predLabel,
"FavourableOutcome": payload.favourableOutcome,
"ProtectedAttribute": payload.protectedAttribute,
"priviledged": payload.priviledgedGroups,
"file": file.file}
log.info("before invoking create get attribute mitigation usecase service ")
log.debug("request payload: " + str(payload))
log.debug(str(payload))
response = analyseUpload.upload_file(payload)
log.debug(response)
return response
@standalone_apis_router.post('/fairness/pretrainMitigate')
def mitigate_bias_in_classification_Dataset(payload:MitigationRequest=Body(...),file:GetDataRequest= Depends(), auth= Depends(auth)):
payload = {
"MitigationType": payload.mitigationType,
"MitigationTechnique": payload.mitigationTechnique,
"taskType": payload.taskType,
"label": payload.label,
"FavourableOutcome": payload.favourableOutcome,
"ProtectedAttribute": payload.protectedAttribute,
"priviledged": payload.priviledgedGroups,
"file": file.file}
log.info("before invoking create get attribute mitigation usecase service ")
log.debug("request payload: " + str(payload))
log.debug(str(payload))
response = analyseUpload.upload_file_Premitigation(payload)
log.debug(response)
return response
@standalone_apis_router.get('/fairness/download/mitigatedData/{fileName}')
def download_mitigated_file(fileName: str, auth= Depends(auth)):
try:
log.debug(fileName)
headers = {
'Content-Disposition': f'attachment; filename={fileName}'
}
response = analyseUpload.get_mitigated_data(fileName)
return FileResponse(response, media_type="application/octet-stream", headers=headers)
except Exception as cie:
log.error(cie.__dict__)
log.info("exit create usecase routing method")
raise HTTPException(**cie.__dict__)
@standalone_apis_router.post('/fairness/individualMetrics',response_model=List[Dict[str, Any]])
def individual_metric(payload:IndividualRequest=Body(...),file:GetDataRequest= Depends(), auth= Depends(auth)):
payload = {
"label": payload.label,
"k":payload.k,
"file": file.file}
log.info("before invoking create get attribute mitigation usecase service ")
log.debug("request payload: " + str(payload))
log.debug(str(payload))
response = analyseUpload.getLabels_Individual(payload)
log.debug(response)
return response
@workbench_router.post('/fairness/preprocessing/mitigation/get_data')
def preprocessing_mitigation_api_returning_atrributes(MitigationType: Annotated[str, Form()],
MitigationTechnique: Annotated[str, Form()],
taskType: Annotated[str, Form()],
fileId: Annotated[str, Form()], auth= Depends(auth)):
payload = {"MitigationType": MitigationType,
"MitigationTechnique": MitigationTechnique,
"taskType": taskType,
"fileId": fileId}
try:
log.info("before invoking create usecase service ")
log.debug("request payload: " + str(payload))
log.debug(str(payload))
response = uiServicepreproc.upload_file_pretrainMitigation(payload)
log.debug(response)
return response
except HTTPException as cie:
log.error(cie.__dict__)
log.info("inside exception data")
log.info("exit create usecase routing method")
traceback.print_exc()
tb = traceback.TracebackException.from_exception(cie)
last_traceback = list(tb.format())[-2]
log.info(f"{last_traceback}last_line")
errormsg = str(last_traceback)
raise CustomHTTPException(workbench_preprocess_labels, "filenot found error"+ " Line no:" + str(errormsg), cie.__dict__.get("detail"))
except ChunkedEncodingError as cie:
log.error(cie.__dict__)
log.info("inside exception data")
log.info("exit create usecase routing method")
traceback.print_exc()
tb = traceback.TracebackException.from_exception(cie)
last_traceback = list(tb.format())[-2]
log.info(f"{last_traceback}last_line")
errormsg = str(last_traceback)
raise CustomHTTPException(workbench_preprocess_labels, "error occured"+ " Line no:" + str(errormsg), "file not found")
@workbench_router.post('/fairness/preprocessing/mitigation/BatchId')
def preprocessing_dataset_mitigation(payload: BatchId, auth= Depends(auth)):
log.info("Entered create usecase routing method")
try:
log.info("before invoking create usecase service ")
log.debug("request payload: " + str(payload))
response = uiServicepreproc.return_pretrainMitigation_protected_attrib(payload)
log.info("after invoking create usecase service ")
log.debug("response : " + str(response))
log.info("exit create usecase routing method")
return response
except HTTPException as cie:
log.error(cie.__dict__)
log.info("inside exception data")
log.info("exit create usecase routing method")
traceback.print_exc()
tb = traceback.TracebackException.from_exception(cie)
last_traceback = list(tb.format())[-2]
log.info(f"{last_traceback}last_line")
errormsg = str(last_traceback)
raise CustomHTTPException(workbench_preprocess_get_batchId, "filenot found error"+ " Line no:" + str(errormsg), cie.__dict__.get("detail"))
except ChunkedEncodingError as cie:
log.error(cie.__dict__)
log.info("inside exception data")
log.info("exit create usecase routing method")
traceback.print_exc()
tb = traceback.TracebackException.from_exception(cie)
last_traceback = list(tb.format())[-2]
log.info(f"{last_traceback}last_line")
errormsg = str(last_traceback)
raise CustomHTTPException(workbench_preprocess_labels, "error occured"+ " Line no:" + str(errormsg), "file not found")
@workbench_router.get('/fairness/mitigation/getMitigatedData/{fileName}')
def download_mitigateFile(fileName: str, auth= Depends(auth)):
try:
log.debug(fileName)
content_type=""
if fileName.endswith(".csv"):
content_type="text/csv"
elif fileName.endswith(".feather"):
content_type="application/octet-stream"
elif fileName.endswith(".parquet"):
content_type="application/octet-stream"
elif fileName.endswith(".json"):
content_type="application/json"
headers = {
'Content-Disposition': f'attachment; filename={fileName}',
"Content-Type": content_type
}
response = service.get_mitigated_data(fileName)
log.info(f"{response}")
# return FileResponse(response, headers=headers)
return response
except FairnessException as cie:
log.error(cie.__dict__)
log.info("exit create usecase routing method")
raise HTTPException(**cie.__dict__)
#analyse with group, individual metrics
@workbench_router.post('/fairness/bias/Workbench/analyse/uploadfile')
def analyze_api_for_returning_attributes(biasType: Annotated[str, Form()],
methodType: Annotated[str, Form()],
taskType: Annotated[str, Form()],
fileId: Annotated[str, Form()], auth= Depends(auth)):
payload = {"biasType": biasType,
"methodType": methodType,
"taskType": taskType,
"fileId": fileId}
try:
log.info("before invoking create usecase service ")
log.debug("request payload: " + str(payload))
log.debug(str(payload))
response = uiServicepreproc.analyse_UploadFile(payload)
log.debug(response)
return response
except HTTPException as cie:
log.error(cie.__dict__)
log.info("inside exception data")
log.info("exit create usecase routing method")
traceback.print_exc()
tb = traceback.TracebackException.from_exception(cie)
last_traceback = list(tb.format())[-2]
log.info(f"{last_traceback}last_line")
errormsg = str(last_traceback)
raise CustomHTTPException(workbenchAnalyse_get_data, "filenot found error"+ " Line no:" + str(errormsg), cie.__dict__.get("detail"))
except ChunkedEncodingError as cie:
log.error(cie.__dict__)
log.info("inside exception data")
log.info("exit create usecase routing method")
traceback.print_exc()
tb = traceback.TracebackException.from_exception(cie)
last_traceback = list(tb.format())[-2]
log.info(f"{last_traceback}last_line")
errormsg = str(last_traceback)
raise CustomHTTPException(workbenchAnalyse_get_data, "error occured"+ " Line no:" + str(errormsg), "file not found")
@workbench_router.post('/fairness/bias/workbench/analyze/batchId/Attributes')
def analyze_bias_in_structured_dataset(payload: BatchId, auth= Depends(auth)):
log.info("Entered create usecase routing method")
try:
log.info("before invoking create usecase service ")
log.debug("request payload: " + str(payload))
response = uiServicepreproc.return_protected_attrib_analyseDB(payload)
log.info("after invoking create usecase service ")
log.debug("response : " + str(response))
log.info("exit create usecase routing method")
return response
except HTTPException as cie:
log.error(cie.__dict__)
log.info("inside exception data")
log.info("exit create usecase routing method")
traceback.print_exc()
tb = traceback.TracebackException.from_exception(cie)
last_traceback = list(tb.format())[-2]
log.info(f"{last_traceback}last_line")
errormsg = str(last_traceback)
raise CustomHTTPException(workbench_Analyse, "filenot found error"+ " Line no:" + str(errormsg), cie.__dict__.get("detail"))
except ChunkedEncodingError as cie:
log.error(cie.__dict__)
log.info("inside exception data")
log.info("exit create usecase routing method")
traceback.print_exc()
tb = traceback.TracebackException.from_exception(cie)
last_traceback = list(tb.format())[-2]
log.info(f"{last_traceback}last_line")
errormsg = str(last_traceback)
raise CustomHTTPException(workbench_Analyse, "error occured"+ " Line no:" + str(errormsg), "file not found")
#inprocessing mitigation upload file
@workbench_router.post('/fairness/Workbench/inprocessing_uploadfile')
def inprocessing_api_for_returning_attributes(fileId: Annotated[str, Form()], auth= Depends(auth)):
log.info("Entered create usecase routing method")
payload={
'fileId': fileId,
}
try:
log.info("before invoking create usecase service ")
log.debug("request payload: " + str(payload))
log.debug(str(payload))
response = inprocessing.upload_inprocess(payload)
log.debug(response)
return response
except HTTPException as cie:
log.error(cie.__dict__)
log.info("inside exception data")
log.info("exit create usecase routing method")
traceback.print_exc()
tb = traceback.TracebackException.from_exception(cie)
last_traceback = list(tb.format())[-2]
log.info(f"{last_traceback}last_line")
errormsg = str(last_traceback)
raise CustomHTTPException(inprocess_uploadfile, "filenot found error"+ " Line no:" + str(errormsg), cie.__dict__.get("detail"))
except ChunkedEncodingError as cie:
log.error(cie.__dict__)
log.info("inside exception data")
log.info("exit create usecase routing method")
traceback.print_exc()
tb = traceback.TracebackException.from_exception(cie)
last_traceback = list(tb.format())[-2]
log.info(f"{last_traceback}last_line")
errormsg = str(last_traceback)
raise CustomHTTPException(inprocess_uploadfile, "error occured"+ " Line no:" + str(errormsg), "file not found")
@standalone_apis_router.post('/fairness/analyse/success_rate')
def get_success_rates(payload:AuditRequest=Body(...),file:GetDataRequest= Depends()):
payload={
"label":payload.label,
"favourable_outcome":payload.favourableOutcome,
"categorical_attributes":payload.categorical_attrbutes,
"file":file.file
}
response = SuccessRateService.analyze(payload)
return response
@standalone_apis_router.get('/fairness/analyse/success_rate/download/{filename}')
def download_report(filename: str):
response = SuccessRateService.download_pdf(filename)
return FileResponse(
response,
media_type="application/octet-stream",
headers={
"Content-Disposition": f'attachment; filename="{filename}"'
}
)
@standalone_apis_router.post('/fairness/audit/fairness_classifier')
def audit_fairness_classifier(payload:MonitoringRequest=Body(...),file:GetDataRequest= Depends()):
payload={
"label":payload.label,
"file":file.file
}
audit=FairnessAudit()
response = audit.audit(payload)
return response
@standalone_apis_router.get('/fairness/audit/fairness_classifier/download/{filename}')
def download_report(filename: str):
response = FairnessAudit.download_file(filename)
return FileResponse(
response,
media_type="application/octet-stream",
headers={
"Content-Disposition": f'attachment; filename="{filename}"'
}
)
@workbench_router.post('/fairness/wrapper/batchId')
def wrapper_endpoint_for_triggering_workbench(payload: BatchId, auth= Depends(auth)):
log.info("Entered create usecase routing method")
try:
log.info("before invoking create usecase service ")
log.debug("request payload: " + str(payload))
response = fairnessWorkbench.wapper_trigger(payload)
log.info("after invoking create usecase service ")
log.debug("response : " + str(response))
log.info("exit create usecase routing method")
return response
except HTTPException as cie:
log.error(cie.__dict__)
log.info("inside exception data")
log.info("exit create usecase routing method")
traceback.print_exc()
tb = traceback.TracebackException.from_exception(cie)
last_traceback = list(tb.format())[-2]
log.info(f"{last_traceback}last_line")
errormsg = str(last_traceback)
raise CustomHTTPException(wraper_batch, "filenot found error"+ " Line no:" + str(errormsg), cie.__dict__.get("detail"))
except ChunkedEncodingError as cie:
log.error(cie.__dict__)
log.info("inside exception data")
log.info("exit create usecase routing method")
traceback.print_exc()
tb = traceback.TracebackException.from_exception(cie)
last_traceback = list(tb.format())[-2]
log.info(f"{last_traceback}last_line")
errormsg = str(last_traceback)
raise CustomHTTPException(wraper_batch, "error occured"+ " Line no:" + str(errormsg), "file not found")
@workbench_router.post('/fairness/wrapper/download')
def wrapper_enpoint_for_downloading_report(payload: BatchId, auth= Depends(auth)):
log.info("Entered create usecase routing method")
try:
log.info("before invoking create usecase service ")
log.debug("request payload: " + str(payload))
response = fairnessWorkbench.wrapper_download(payload)
log.info("after invoking create usecase service ")
log.debug("response : " + str(response))
log.info("exit create usecase routing method")
return response
except HTTPException as cie:
log.error(cie.__dict__)
log.info("inside exception data")
log.info("exit create usecase routing method")
traceback.print_exc()
tb = traceback.TracebackException.from_exception(cie)
last_traceback = list(tb.format())[-2]
log.info(f"{last_traceback}last_line")
errormsg = str(last_traceback)
raise CustomHTTPException(Wrapper_download, "filenot found error"+ " Line no:" + str(errormsg), cie.__dict__.get("detail"))
except ChunkedEncodingError as cie:
log.error(cie.__dict__)
log.info("inside exception data")
log.info("exit create usecase routing method")
traceback.print_exc()
tb = traceback.TracebackException.from_exception(cie)
last_traceback = list(tb.format())[-2]
log.info(f"{last_traceback}last_line")
errormsg = str(last_traceback)
raise CustomHTTPException(Wrapper_download, "error occured"+ " Line no:" + str(errormsg), "file not found")
|