MAZEN00 commited on
Commit
94947fe
·
verified ·
1 Parent(s): 0b1e8e6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -26
app.py CHANGED
@@ -1,39 +1,43 @@
1
  from fastapi import FastAPI
2
  from pydantic import BaseModel
3
- import pandas as pd
4
- import pickle
5
  from fastapi.middleware.cors import CORSMiddleware
 
 
 
 
 
 
 
 
6
 
7
- # Load pipeline and columns
8
- with open("modeel.joblib", "rb") as f:
9
- model = pickle.load(f)
10
 
11
- with open("columns.pkl", "rb") as f:
12
- model_columns = pickle.load(f)
13
 
 
 
 
14
  app = FastAPI()
15
 
16
- # Enable CORS (needed for frontend to connect)
17
  app.add_middleware(
18
  CORSMiddleware,
19
- allow_origins=["*"],
20
  allow_credentials=True,
21
  allow_methods=["*"],
22
  allow_headers=["*"],
23
  )
24
 
25
- # Health check route
26
- @app.get("/")
27
- def read_root():
28
- return {"status": "ok", "message": "FastAPI is running on Hugging Face 🚀"}
29
-
30
  # Input schema
31
- class InputModel(BaseModel):
 
32
  Age: int
33
  Potential: int
34
  Club: str
35
- Value: int
36
- Wage: int
37
  Special: int
38
  Preferred_Foot: str
39
  International_Reputation: int
@@ -42,7 +46,7 @@ class InputModel(BaseModel):
42
  Body_Type: str
43
  Position: str
44
  Height: float
45
- Weight: int
46
  Release_Clause: float
47
  Role: str
48
  Contract_years: int
@@ -50,13 +54,12 @@ class InputModel(BaseModel):
50
  Attacking_WorkRate: str
51
  Defensive_WorkRate: str
52
 
53
- # Prediction route
 
 
54
  @app.post("/predict")
55
- def predict(player: InputModel):
56
  data = pd.DataFrame([player.dict()])
57
- data = data[model_columns]
58
-
59
- prediction = model.predict(data)
60
- prediction_int = int(prediction[0]) # make sure it's an integer
61
-
62
- return {"prediction": prediction_int}
 
1
  from fastapi import FastAPI
2
  from pydantic import BaseModel
 
 
3
  from fastapi.middleware.cors import CORSMiddleware
4
+ from huggingface_hub import hf_hub_download
5
+ import joblib
6
+ import pandas as pd
7
+
8
+ # -----------------------------
9
+ # Download model + columns from Hugging Face Hub
10
+ # -----------------------------
11
+ REPO_ID = "MAZEN00/Player"
12
 
13
+ model_path = hf_hub_download(repo_id=REPO_ID, filename="modeel.joblib")
14
+ columns_path = hf_hub_download(repo_id=REPO_ID, filename="columns.pkl")
 
15
 
16
+ model = joblib.load(model_path)
17
+ model_columns = joblib.load(columns_path)
18
 
19
+ # -----------------------------
20
+ # FastAPI setup
21
+ # -----------------------------
22
  app = FastAPI()
23
 
 
24
  app.add_middleware(
25
  CORSMiddleware,
26
+ allow_origins=["*"],
27
  allow_credentials=True,
28
  allow_methods=["*"],
29
  allow_headers=["*"],
30
  )
31
 
32
+ # -----------------------------
 
 
 
 
33
  # Input schema
34
+ # -----------------------------
35
+ class PlayerInput(BaseModel):
36
  Age: int
37
  Potential: int
38
  Club: str
39
+ Value: float
40
+ Wage: float
41
  Special: int
42
  Preferred_Foot: str
43
  International_Reputation: int
 
46
  Body_Type: str
47
  Position: str
48
  Height: float
49
+ Weight: float
50
  Release_Clause: float
51
  Role: str
52
  Contract_years: int
 
54
  Attacking_WorkRate: str
55
  Defensive_WorkRate: str
56
 
57
+ # -----------------------------
58
+ # Prediction endpoint
59
+ # -----------------------------
60
  @app.post("/predict")
61
+ def predict(player: PlayerInput):
62
  data = pd.DataFrame([player.dict()])
63
+ data = data.reindex(columns=model_columns, fill_value=0)
64
+ prediction = model.predict(data)[0]
65
+ return {"prediction": int(prediction)}