File size: 1,521 Bytes
8e152f0 |
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 |
from fastapi import FastAPI, HTTPException, File, UploadFile
from fastapi.responses import FileResponse
from app.services.preprocessing import data_quality, standardize_data_types, handle_missing_data, handle_outliers, generate_final_report, save_cleaned_data
import pandas as pd
import io
import os
app = FastAPI(title="Data Preprocessing")
os.makedirs("output", exist_ok=True)
@app.get("/")
async def root():
return {"message": "Welcome to the Data Preprocessing API!"}
@app.post("/preprocess_data/")
async def upload_csv(upload_file: UploadFile = File(...)):
try:
if not upload_file.filename.endswith('.csv'):
raise HTTPException(status_code=400, detail="File must be in CSV format!")
content = await upload_file.read()
df = pd.read_csv(io.BytesIO(content), encoding_errors="replace")
if df.empty:
raise HTTPException(status_code=400, detail="File is empty, upload the correct file")
data_quality(df)
df = standardize_data_types(df)
df = handle_missing_data(df)
df = handle_outliers(df)
REPORT_PATH = "output/preprocessing_report.txt"
generate_final_report(df, REPORT_PATH)
CLEANED_DATA_PATH = "output/cleaned_dataset.csv"
save_cleaned_data(df, CLEANED_DATA_PATH)
return FileResponse(CLEANED_DATA_PATH, media_type="text/csv", filename="cleaned_dataset.csv")
except Exception as e:
raise HTTPException(status_code=400, detail=f"Error processing file: {str(e)}")
|