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

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -0
app.py ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
40
+ Weak_Foot: int
41
+ Skill_Moves: int
42
+ Body_Type: str
43
+ Position: str
44
+ Height: float
45
+ Weight: int
46
+ Release_Clause: float
47
+ Role: str
48
+ Contract_years: int
49
+ Fitness_level: str
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}