2nzi commited on
Commit
f4bb7d1
·
1 Parent(s): fc9b711

Add application file

Browse files
Files changed (3) hide show
  1. Dockerfile +16 -0
  2. main.py +68 -0
  3. requirements.txt +6 -0
Dockerfile ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # read the doc: https://huggingface.co/docs/hub/spaces-sdks-docker
2
+ # you will also find guides on how best to write your Dockerfile
3
+
4
+ FROM python:3.9
5
+
6
+ RUN useradd -m -u 1000 user
7
+
8
+ WORKDIR /app
9
+
10
+ COPY --chown=user ./requirements.txt requirements.txt
11
+
12
+ RUN pip install --no-cache-dir --upgrade -r requirements.txt
13
+
14
+ COPY --chown=user . /app
15
+
16
+ CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
main.py ADDED
@@ -0,0 +1,68 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import uvicorn
2
+ import pandas as pd
3
+ from pydantic import BaseModel
4
+ from typing import List, Union
5
+ from fastapi import FastAPI
6
+ import joblib
7
+
8
+ description = """
9
+ Welcome to the GetAround Car Value Prediction API. This app provides an endpoint to predict car values based on various features! Try it out 🕹️
10
+
11
+ ## Machine Learning
12
+
13
+ This section includes a Machine Learning endpoint that predicts car values based on various features. Here is the endpoint:
14
+
15
+ * `/predict`: **POST** request that accepts a list of car features and returns a predicted car value.
16
+
17
+ Check out the documentation below 👇 for more information on each endpoint.
18
+ """
19
+
20
+ tags_metadata = [
21
+ {
22
+ "name": "Machine Learning",
23
+ "description": "Endpoint for predicting car values based on provided features."
24
+ }
25
+ ]
26
+
27
+ app = FastAPI(
28
+ title="🚗 GetAround Car Value Prediction API",
29
+ description=description,
30
+ version="0.1",
31
+ contact={
32
+ "name": "Antoine VERDON",
33
+ "email": "[email protected]", # Replace with actual email
34
+ },
35
+ openapi_tags=tags_metadata
36
+ )
37
+
38
+
39
+ class PredictionFeatures(BaseModel):
40
+ CarData: List[Union[str, int, bool]] = ["Renault", 193231, 85, "diesel", "black", "estate", False, True, False, False, False, False, True]
41
+
42
+ @app.get("/", tags=["Introduction Endpoints"])
43
+ async def index():
44
+ return "Hello world! This `/` is the most simple and default endpoint. If you want to learn more, check out documentation of the API at `/docs`"
45
+
46
+ @app.post("/predict", tags=["Machine Learning"])
47
+ async def predict(predictionFeatures: PredictionFeatures):
48
+ columns = [
49
+ 'model_key', 'mileage', 'engine_power', 'fuel', 'paint_color',
50
+ 'car_type', 'private_parking_available', 'has_gps',
51
+ 'has_air_conditioning', 'automatic_car', 'has_getaround_connect',
52
+ 'has_speed_regulator', 'winter_tires'
53
+ ]
54
+
55
+ car_data_dict = {col: [val] for col, val in zip(columns, predictionFeatures.CarData)}
56
+ car_data = pd.DataFrame(car_data_dict)
57
+
58
+ # model_file = hf_hub_download(repo_id="2nzi/GetAround-CarPrediction", filename="best_model_XGBoost.pkl")
59
+ # with open(model_file, 'rb') as f:
60
+ # model = pickle.load(f)
61
+
62
+ model = joblib.load('best_model_XGBoost.pkl')
63
+ prediction = model.predict(car_data)
64
+ response = {"prediction": prediction.tolist()[0]}
65
+ return response
66
+
67
+ if __name__=="__main__":
68
+ uvicorn.run(app, host="0.0.0.0", port=4000)
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ fastapi
2
+ uvicorn[standard]
3
+ pandas
4
+ pydantic
5
+ joblib
6
+ scikit-learn