price-builder-backend/controllers/controller.py

29 lines
1.4 KiB
Python
Raw Normal View History

2024-10-13 17:09:43 +04:00
from fastapi import APIRouter, HTTPException
2024-10-13 16:53:07 +04:00
from schemas.schemas import LaptopCreate, LaptopResponse, PredictPriceResponse
from services.service import LaptopService
import os
router = APIRouter()
# Инициализация сервиса
MODEL_PATH = os.getenv("MODEL_PATH", "services/ml/laptop_price_model.pkl")
FEATURE_COLUMNS_PATH = os.getenv("FEATURE_COLUMNS_PATH", "services/ml/feature_columns.pkl")
POLY_PATH = os.getenv("POLY_PATH", "services/ml/poly_transformer.pkl")
SCALER_PATH = os.getenv("SCALER_PATH", "services/ml/scaler.pkl")
laptop_service = LaptopService(model_path=MODEL_PATH, feature_columns_path=FEATURE_COLUMNS_PATH, poly_path=POLY_PATH, scaler_path=SCALER_PATH)
2024-10-13 16:53:07 +04:00
2024-10-13 17:09:43 +04:00
@router.post("/predict_price/", 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())
except Exception as e:
raise HTTPException(status_code=400, detail=str(e))