Spaces:
Sleeping
Sleeping
api
Browse files- src/main.py +113 -0
src/main.py
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, HTTPException
|
| 2 |
+
from pydantic import BaseModel, validator
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import pickle, uvicorn, os, logging
|
| 5 |
+
|
| 6 |
+
app = FastAPI()
|
| 7 |
+
|
| 8 |
+
# Configure logging
|
| 9 |
+
logging.basicConfig(level=logging.INFO)
|
| 10 |
+
|
| 11 |
+
# Define filepath for ml_components.pkl
|
| 12 |
+
ML_COMPONENTS_FILEPATH = os.path.join("assets", "ml", "ml_components.pkl")
|
| 13 |
+
|
| 14 |
+
# Load machine learning model and other components
|
| 15 |
+
with open(ML_COMPONENTS_FILEPATH, "rb") as file:
|
| 16 |
+
ml_components = pickle.load(file)
|
| 17 |
+
|
| 18 |
+
# preprocessor = ml_components["preprocessor"]
|
| 19 |
+
pipeline = ml_components["pipeline"]
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
class DeviceSpecs(BaseModel):
|
| 23 |
+
"""
|
| 24 |
+
Device specifications.
|
| 25 |
+
|
| 26 |
+
- battery_power: Total energy a battery can store in one time measured in mAh
|
| 27 |
+
- blue: Has Bluetooth or not (0 for False, 1 for True)
|
| 28 |
+
- clock_speed: The speed at which the microprocessor executes instructions
|
| 29 |
+
- dual_sim: Has dual sim support or not (0 for False, 1 for True)
|
| 30 |
+
- fc: Front Camera megapixels
|
| 31 |
+
- four_g: Has 4G or not (0 for False, 1 for True)
|
| 32 |
+
- int_memory: Internal Memory in Gigabytes
|
| 33 |
+
- m_dep: Mobile Depth in cm
|
| 34 |
+
- mobile_wt: Weight of mobile phone
|
| 35 |
+
- n_cores: Number of cores of the processor
|
| 36 |
+
- pc: Primary Camera megapixels
|
| 37 |
+
- px_height: Pixel Resolution Height
|
| 38 |
+
- px_width: Pixel Resolution Width
|
| 39 |
+
- ram: Random Access Memory in Megabytes
|
| 40 |
+
- sc_h: Screen Height of mobile in cm
|
| 41 |
+
- sc_w: Screen Width of mobile in cm
|
| 42 |
+
- talk_time: longest time that a single battery charge will last when you are
|
| 43 |
+
- three_g: Has 3G or not (0 for False, 1 for True)
|
| 44 |
+
- touch_screen: Has touch screen or not (0 for False, 1 for True)
|
| 45 |
+
- wifi: Has wifi or not (0 for False, 1 for True)
|
| 46 |
+
"""
|
| 47 |
+
|
| 48 |
+
battery_power: float
|
| 49 |
+
blue: int
|
| 50 |
+
clock_speed: float
|
| 51 |
+
dual_sim: int
|
| 52 |
+
fc: float
|
| 53 |
+
four_g: int
|
| 54 |
+
int_memory: float
|
| 55 |
+
m_dep: float
|
| 56 |
+
mobile_wt: float
|
| 57 |
+
n_cores: float
|
| 58 |
+
pc: float
|
| 59 |
+
px_height: float
|
| 60 |
+
px_width: float
|
| 61 |
+
ram: float
|
| 62 |
+
sc_h: float
|
| 63 |
+
sc_w: float
|
| 64 |
+
talk_time: float
|
| 65 |
+
three_g: int
|
| 66 |
+
touch_screen: int
|
| 67 |
+
wifi: int
|
| 68 |
+
|
| 69 |
+
@validator("blue", "dual_sim", "four_g", "three_g", "touch_screen", "wifi")
|
| 70 |
+
def validate_boolean(cls, v):
|
| 71 |
+
# Ensure the values are either 0 or 1
|
| 72 |
+
if v not in (0, 1):
|
| 73 |
+
raise ValueError("Value must be 0 or 1")
|
| 74 |
+
return v
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
@app.post("/predict/{device_id}")
|
| 78 |
+
async def predict_price(device_id: int, specs: DeviceSpecs):
|
| 79 |
+
"""
|
| 80 |
+
Predict the price of a device based on its specifications.
|
| 81 |
+
|
| 82 |
+
Args:
|
| 83 |
+
device_id (int): The ID of the device.
|
| 84 |
+
specs (DeviceSpecs): The device specifications.
|
| 85 |
+
|
| 86 |
+
Returns:
|
| 87 |
+
dict: A dictionary containing the input data and predicted price.
|
| 88 |
+
"""
|
| 89 |
+
try:
|
| 90 |
+
logging.info(f"Input request received...")
|
| 91 |
+
|
| 92 |
+
# Preprocess the data
|
| 93 |
+
data = pd.DataFrame([{"device_id": device_id, **specs.dict()}])
|
| 94 |
+
logging.info(f"Input as a dataframe\n{data.to_markdown()}\n")
|
| 95 |
+
|
| 96 |
+
# Predict price
|
| 97 |
+
data["predicted_price"] = pipeline.predict(data)
|
| 98 |
+
|
| 99 |
+
logging.info(
|
| 100 |
+
f"Predictions made\n{data[['device_id', 'predicted_price']].to_markdown()}\n"
|
| 101 |
+
)
|
| 102 |
+
|
| 103 |
+
# Return input data and predicted price
|
| 104 |
+
return data.to_dict("records")
|
| 105 |
+
except Exception as e:
|
| 106 |
+
logging.error(
|
| 107 |
+
f"An error occurred while processing prediction for device ID {device_id}: {str(e)}"
|
| 108 |
+
)
|
| 109 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 110 |
+
|
| 111 |
+
|
| 112 |
+
if __name__ == "__main__":
|
| 113 |
+
uvicorn.run(app, host="127.0.0.1", port=8000, reload=True)
|