1+ import io
2+ import asyncio
13import json
2- from fastapi .responses import JSONResponse
4+ from fastapi .responses import JSONResponse , Response
35from fastapi import HTTPException
46from crud .bias_and_fairness import upload_model , upload_data , insert_metrics , get_metrics_by_id , get_all_metrics_query , delete_metrics_by_id
57from utils .run_bias_and_fairness_check import analyze_fairness
8+ from utils .handle_files_uploads import process_files
69from database .db import get_db
7- from fastapi import UploadFile
10+ from fastapi import UploadFile , BackgroundTasks
11+ from database .redis import get_next_job_id , get_job_status , delete_job_status
812
913async def get_all_metrics ():
1014 """
@@ -60,67 +64,39 @@ async def get_metrics(id: int):
6064 detail = f"Failed to retrieve metrics, { str (e )} "
6165 )
6266
63- async def handle_upload (model : UploadFile , data : UploadFile , target_column : str , sensitive_column : str ):
67+ async def get_upload_status (job_id : int ):
68+ value = await get_job_status (job_id )
69+ if value is None :
70+ return Response (status_code = 204 )
71+ await delete_job_status (job_id )
72+ return JSONResponse (
73+ status_code = 200 ,
74+ content = value ,
75+ media_type = "application/json"
76+ )
77+
78+ async def handle_upload (background_tasks : BackgroundTasks , model : UploadFile , data : UploadFile , target_column : str , sensitive_column : str ):
6479 """
6580 Handle file upload from the client.
6681 """
67- try :
68- async with get_db () as db :
69- transaction = await db .begin ()
70-
71- model_content = await model .read ()
72- model_filename = model .filename
73- data_content = await data .read ()
74- data_filename = data .filename
75-
76- if not model_content or not data_content :
77- raise ValueError ("model or data file is empty" )
78- if not model_filename or not data_filename :
79- raise ValueError ("model or data file name is empty" )
80-
81- upload_model_record = await upload_model (content = model_content , name = model_filename , db = db )
82-
83- if not upload_model_record :
84- raise Exception ("failed to upload model file" )
85-
86- upload_data_record = await upload_data (
87- content = data_content ,
88- name = data_filename ,
89- target_column = target_column ,
90- sensitive_column = sensitive_column ,
91- model_id = upload_model_record .id ,
92- db = db
93- )
94-
95- if not upload_data_record :
96- raise Exception ("failed to upload data file" )
97- result = analyze_fairness (
98- model_content = model_content ,
99- data_content = data_content ,
100- target_column = target_column ,
101- sensitive_column = sensitive_column
102- )
103-
104- metrics = await insert_metrics (json .dumps (result ), upload_data_record .id , db )
105- if not metrics :
106- raise Exception ("failed to insert metrics" )
107-
108- await transaction .commit ()
109-
110- return JSONResponse (
111- status_code = 200 ,
112- content = {
113- "model_id" : upload_model_record .id ,
114- "data_id" : upload_data_record .id ,
115- "metrics_id" : metrics .id ,
116- "metrics" : result
117- }
118- )
119- except Exception as e :
120- raise HTTPException (
121- status_code = 500 ,
122- detail = f"Failed to handle upload, { str (e )} "
123- )
82+ job_id = await get_next_job_id ()
83+ response = JSONResponse (status_code = 202 , content = {
84+ "message" : "Processing started" ,
85+ "job_id" : job_id ,
86+ "model_filename" : model .filename .replace (".gz" , "" ) if model .filename else "" ,
87+ "data_filename" : data .filename .replace (".gz" , "" ) if data .filename else ""
88+ }, media_type = "application/json" )
89+ model_ = {
90+ "filename" : model .filename ,
91+ "content" : await model .read ()
92+ }
93+ data_ = {
94+ "filename" : data .filename ,
95+ "content" : await data .read ()
96+ }
97+ # create a job ID or use a unique identifier for the task
98+ background_tasks .add_task (process_files , job_id , model_ , data_ , target_column , sensitive_column )
99+ return response
124100
125101async def delete_metrics (id : int ):
126102 """
@@ -135,10 +111,7 @@ async def delete_metrics(id: int):
135111 detail = f"Metrics with ID { id } not found"
136112 )
137113 await db .commit ()
138- return JSONResponse (
139- status_code = 204 ,
140- content = None
141- )
114+ return Response (status_code = 204 )
142115 except HTTPException as he :
143116 raise he
144117 except Exception as e :
0 commit comments