Spaces:
Sleeping
Sleeping
madamanastasia commited on
Commit ·
430001f
1
Parent(s): c9588fd
Initial deployment
Browse files- Dockerfile +12 -0
- main.py +79 -0
- pricing_model.joblib +3 -0
- requirements.txt +8 -0
Dockerfile
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.11-slim
|
| 2 |
+
|
| 3 |
+
WORKDIR /app
|
| 4 |
+
|
| 5 |
+
COPY requirements.txt .
|
| 6 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 7 |
+
|
| 8 |
+
COPY . .
|
| 9 |
+
|
| 10 |
+
EXPOSE 7860
|
| 11 |
+
|
| 12 |
+
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
|
main.py
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI
|
| 2 |
+
from fastapi.responses import HTMLResponse, JSONResponse
|
| 3 |
+
from pydantic import BaseModel
|
| 4 |
+
from typing import List, Any, Dict, Union
|
| 5 |
+
import pandas as pd
|
| 6 |
+
import joblib
|
| 7 |
+
from pathlib import Path
|
| 8 |
+
|
| 9 |
+
APP_DIR = Path(__file__).resolve().parent
|
| 10 |
+
MODEL_PATH = APP_DIR / "pricing_model.joblib"
|
| 11 |
+
|
| 12 |
+
FEATURE_ORDER = [
|
| 13 |
+
"model_key",
|
| 14 |
+
"mileage",
|
| 15 |
+
"engine_power",
|
| 16 |
+
"fuel",
|
| 17 |
+
"paint_color",
|
| 18 |
+
"car_type",
|
| 19 |
+
"private_parking_available",
|
| 20 |
+
"has_gps",
|
| 21 |
+
"has_air_conditioning",
|
| 22 |
+
"automatic_car",
|
| 23 |
+
"has_getaround_connect",
|
| 24 |
+
"has_speed_regulator",
|
| 25 |
+
"winter_tires",
|
| 26 |
+
]
|
| 27 |
+
|
| 28 |
+
app = FastAPI(
|
| 29 |
+
title="Getaround API",
|
| 30 |
+
version="1.0.0",
|
| 31 |
+
docs_url=None, # we serve a custom /docs page to match the project requirement
|
| 32 |
+
redoc_url=None
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
+
model = joblib.load(MODEL_PATH)
|
| 36 |
+
|
| 37 |
+
class PredictIn(BaseModel):
|
| 38 |
+
input: List[Any] # accepts list[dict] OR list[list] (see docs)
|
| 39 |
+
|
| 40 |
+
@app.get("/health")
|
| 41 |
+
def health():
|
| 42 |
+
return {"status": "ok"}
|
| 43 |
+
|
| 44 |
+
@app.post("/predict")
|
| 45 |
+
def predict(payload: PredictIn):
|
| 46 |
+
rows = payload.input
|
| 47 |
+
|
| 48 |
+
# Accept either:
|
| 49 |
+
# 1) list[dict] with keys matching FEATURE_ORDER
|
| 50 |
+
# 2) list[list] with values in FEATURE_ORDER order
|
| 51 |
+
if len(rows) == 0:
|
| 52 |
+
return JSONResponse({"prediction": []})
|
| 53 |
+
|
| 54 |
+
first = rows[0]
|
| 55 |
+
if isinstance(first, dict):
|
| 56 |
+
X = pd.DataFrame(rows)
|
| 57 |
+
# Ensure column order & missing columns are handled
|
| 58 |
+
for c in FEATURE_ORDER:
|
| 59 |
+
if c not in X.columns:
|
| 60 |
+
X[c] = None
|
| 61 |
+
X = X[FEATURE_ORDER]
|
| 62 |
+
else:
|
| 63 |
+
# list-like
|
| 64 |
+
X = pd.DataFrame(rows, columns=FEATURE_ORDER)
|
| 65 |
+
|
| 66 |
+
preds = model.predict(X)
|
| 67 |
+
return {"prediction": [float(p) for p in preds]}
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
from fastapi.responses import HTMLResponse
|
| 71 |
+
from pathlib import Path
|
| 72 |
+
|
| 73 |
+
BASE_DIR = Path(__file__).resolve().parent
|
| 74 |
+
|
| 75 |
+
@app.get("/docs", response_class=HTMLResponse)
|
| 76 |
+
def docs():
|
| 77 |
+
html = (BASE_DIR / "docs.html").read_text(encoding="utf-8")
|
| 78 |
+
return HTMLResponse(content=html)
|
| 79 |
+
|
pricing_model.joblib
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:de412ea007542517311520a5c4ec04548e33870de58e9435e74e7099913c34e1
|
| 3 |
+
size 61410759
|
requirements.txt
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi==0.111.0
|
| 2 |
+
uvicorn[standard]==0.30.1
|
| 3 |
+
pandas==2.2.2
|
| 4 |
+
numpy==2.0.0
|
| 5 |
+
scikit-learn==1.5.1
|
| 6 |
+
joblib==1.4.2
|
| 7 |
+
streamlit==1.36.0
|
| 8 |
+
openpyxl==3.1.5
|