2024-10-13 17:09:43 +04:00
from fastapi import APIRouter , HTTPException
2024-12-02 21:27:11 +04:00
from schemas . schemas import LaptopCreate , TVCreate , PredictPriceResponse
from services . service import LaptopService , TVService
2024-10-13 16:53:07 +04:00
import os
router = APIRouter ( )
2024-12-04 00:26:49 +04:00
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 ,
)
2024-10-13 16:53:07 +04:00
2024-12-02 21:27:11 +04:00
@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. " )
2024-10-13 16:53:07 +04:00
def predict_price ( data : LaptopCreate ) :
2024-10-13 17:09:43 +04:00
"""
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
"""
2024-10-13 16:53:07 +04:00
try :
return laptop_service . predict_price ( data . dict ( ) )
2024-12-02 21:27:11 +04:00
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 ( ) )
2024-12-08 23:35:28 +04:00
except Exception as e :
raise HTTPException ( status_code = 400 , detail = str ( e ) )
2024-12-09 15:19:40 +04:00
@router.get ( ' /get_unique_data_laptop ' , summary = " Get unique data for laptops species " )
2024-12-08 23:35:28 +04:00
def get_unique_laptops ( ) :
try :
return laptop_service . get_unique_data ( )
2024-12-09 15:19:40 +04:00
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 " )
2024-12-11 00:28:32 +04:00
def get_unique_tvs ( ) :
2024-12-09 15:19:40 +04:00
try :
return tv_service . get_unique_data ( )
2024-10-13 16:53:07 +04:00
except Exception as e :
2024-12-11 00:28:32 +04:00
raise HTTPException ( status_code = 400 , detail = str ( e ) )