from fastapi import APIRouter, HTTPException from schemas.schemas import LaptopCreate, TVCreate, PredictPriceResponse from services.service import LaptopService, TVService import os router = APIRouter() LAPTOP_MODEL_PATH = "services/ml/laptopML/laptop_price_model.pkl" LAPTOP_FEATURE_COLUMNS_PATH = "services/ml/laptopML/feature_columns.pkl" LAPTOP_POLY_PATH = "services/ml/laptopML/poly_transformer.pkl" LAPTOP_SCALER_PATH = "services/ml/laptopML/scaler.pkl" laptop_service = LaptopService( model_path=LAPTOP_MODEL_PATH, feature_columns_path=LAPTOP_FEATURE_COLUMNS_PATH, poly_path=LAPTOP_POLY_PATH, scaler_path=LAPTOP_SCALER_PATH, ) TV_MODEL_PATH = "services/ml/tvML/tv_price_model.pkl" TV_FEATURE_COLUMNS_PATH = "services/ml/tvML/feature_columns.pkl" TV_POLY_PATH = "services/ml/tvML/poly_transformer.pkl" TV_SCALER_PATH = "services/ml/tvML/scaler.pkl" tv_service = TVService( model_path=TV_MODEL_PATH, feature_columns_path=TV_FEATURE_COLUMNS_PATH, poly_path=TV_POLY_PATH, scaler_path=TV_SCALER_PATH, ) @router.post("/predict_price/laptop/", response_model=PredictPriceResponse, summary="Predict laptop price", description="Predict the price of a laptop based on its specifications.", response_description="The predicted price of the laptop.") def predict_price(data: LaptopCreate): """ Predict the price of a laptop given its specifications. - **processor**: Type of processor (e.g., i5, i7) - **ram**: Amount of RAM in GB - **os**: Operating system (e.g., Windows, MacOS) - **ssd**: Size of SSD in GB - **display**: Size of the display in inches """ try: return laptop_service.predict_price(data.dict()) except Exception as e: raise HTTPException(status_code=400, detail=str(e)) @router.post("/predict_price/tv/", response_model=PredictPriceResponse, summary="Predict TV price", description="Predict the price of a TV based on its specifications.", response_description="The predicted price of the TV.") def predict_price(data: TVCreate): try: return tv_service.predict_price(data.dict()) except Exception as e: raise HTTPException(status_code=400, detail=str(e)) @router.get('/get_unique_data_laptop', summary="Get unique data for laptops species") def get_unique_laptops(): try: return laptop_service.get_unique_data() except Exception as e: raise HTTPException(status_code=400, detail=str(e)) @router.get('/get_unique_data_tv', summary="Get unique data for tvs species") def get_unique_tvs(): try: return tv_service.get_unique_data() except Exception as e: raise HTTPException(status_code=400, detail=str(e))