добавить веб-сервис предсказания цен, с dockerfile
Этот коммит содержится в:
64
services/ml_service/app/main.py
Обычный файл
64
services/ml_service/app/main.py
Обычный файл
@@ -0,0 +1,64 @@
|
||||
from os import getenv
|
||||
from pathlib import Path
|
||||
|
||||
from fastapi import FastAPI
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
from ._meta import PACKAGE_PATH
|
||||
from .predictor import (
|
||||
FuelType, SellingType, TransmissionType, PricePredictionFeatures, PricePredictor,
|
||||
)
|
||||
|
||||
|
||||
MODELS_PATH = getenv('MODELS_PATH', None)
|
||||
if MODELS_PATH is not None:
|
||||
MODELS_PATH = Path(MODELS_PATH)
|
||||
else:
|
||||
SERVICES_PATH = PACKAGE_PATH.parents[1]
|
||||
assert SERVICES_PATH.name == 'services'
|
||||
MODELS_PATH = SERVICES_PATH / 'models'
|
||||
|
||||
MODEL_PATH = MODELS_PATH / 'model.pkl'
|
||||
|
||||
|
||||
predictor = PricePredictor(MODEL_PATH)
|
||||
|
||||
|
||||
API_BASE_PATH = '/api'
|
||||
|
||||
|
||||
app = FastAPI(
|
||||
title='Сервис ML',
|
||||
version='0.1.0',
|
||||
root_path=API_BASE_PATH,
|
||||
#redoc_url=None,
|
||||
)
|
||||
|
||||
|
||||
@app.get('/', summary='Тестовый эндпоинт')
|
||||
async def root():
|
||||
return {'Hello': 'World'}
|
||||
|
||||
|
||||
class PricePredictionRequest(BaseModel):
|
||||
|
||||
selling_price: float = Field(..., gt=0)
|
||||
driven_kms: float = Field(..., ge=0)
|
||||
age: float = Field(..., ge=0)
|
||||
fuel_type: FuelType
|
||||
selling_type: SellingType
|
||||
transmission_type: TransmissionType
|
||||
|
||||
|
||||
@app.post('/predict', summary='Предсказать цену подержанного автомобиля')
|
||||
def predict_price(item_id: int, req: PricePredictionRequest):
|
||||
features = PricePredictionFeatures(
|
||||
selling_price=req.selling_price,
|
||||
driven_kms=req.driven_kms,
|
||||
age=req.age,
|
||||
fuel_type=req.fuel_type,
|
||||
selling_type=req.selling_type,
|
||||
transmission_type=req.transmission_type,
|
||||
)
|
||||
pred = predictor.predict(features)
|
||||
return {'item_id': item_id, 'price': pred}
|
||||
Ссылка в новой задаче
Block a user