Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,9 +7,19 @@ from flask import Flask, request, jsonify
|
|
| 7 |
|
| 8 |
# Step 1: Data Collection
|
| 9 |
def fetch_data():
|
| 10 |
-
api_key = '
|
| 11 |
response = requests.get(f"https://api.sportsdata.io/v3/nba/stats/json/PlayerSeasonStats/2023?key={api_key}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
data = response.json()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
return pd.DataFrame(data)
|
| 14 |
|
| 15 |
# Step 2: Data Preprocessing
|
|
@@ -43,6 +53,33 @@ def train_model(df):
|
|
| 43 |
# Step 5: Deployment with Flask
|
| 44 |
app = Flask(__name__)
|
| 45 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
@app.route('/predict', methods=['POST'])
|
| 47 |
def predict():
|
| 48 |
data = request.json
|
|
|
|
| 7 |
|
| 8 |
# Step 1: Data Collection
|
| 9 |
def fetch_data():
|
| 10 |
+
api_key = 'itku7CjwJv5bfrwAvGlwR3nYv' # Replace with your SportsData.io API key
|
| 11 |
response = requests.get(f"https://api.sportsdata.io/v3/nba/stats/json/PlayerSeasonStats/2023?key={api_key}")
|
| 12 |
+
|
| 13 |
+
# Check if the response is valid
|
| 14 |
+
if response.status_code != 200:
|
| 15 |
+
raise ValueError(f"Error fetching data: {response.status_code}, {response.text}")
|
| 16 |
+
|
| 17 |
data = response.json()
|
| 18 |
+
|
| 19 |
+
# Check if the response is a list of dictionaries
|
| 20 |
+
if not isinstance(data, list) or not all(isinstance(i, dict) for i in data):
|
| 21 |
+
raise ValueError("API response is not in the expected format (list of dictionaries)")
|
| 22 |
+
|
| 23 |
return pd.DataFrame(data)
|
| 24 |
|
| 25 |
# Step 2: Data Preprocessing
|
|
|
|
| 53 |
# Step 5: Deployment with Flask
|
| 54 |
app = Flask(__name__)
|
| 55 |
|
| 56 |
+
@app.route('/predict', methods=['POST'])
|
| 57 |
+
def predict():
|
| 58 |
+
data = request.json
|
| 59 |
+
input_features = [data['RecentForm'], data['HomeAdvantage']]
|
| 60 |
+
prediction = model.predict([input_features])[0]
|
| 61 |
+
return jsonify({'prediction': prediction})
|
| 62 |
+
|
| 63 |
+
if __name__ == '__main__':
|
| 64 |
+
# Fetch and preprocess data
|
| 65 |
+
df = fetch_data()
|
| 66 |
+
df = preprocess_data(df)
|
| 67 |
+
df = engineer_features(df)
|
| 68 |
+
|
| 69 |
+
# Train the model
|
| 70 |
+
model = train_model(df)
|
| 71 |
+
|
| 72 |
+
# Run the Flask app
|
| 73 |
+
app.run(debug=True, host='0.0.0.0', port=5000)
|
| 74 |
+
y_pred = model.predict(X_test)
|
| 75 |
+
mae = mean_absolute_error(y_test, y_pred)
|
| 76 |
+
print(f"Mean Absolute Error: {mae}")
|
| 77 |
+
|
| 78 |
+
return model
|
| 79 |
+
|
| 80 |
+
# Step 5: Deployment with Flask
|
| 81 |
+
app = Flask(__name__)
|
| 82 |
+
|
| 83 |
@app.route('/predict', methods=['POST'])
|
| 84 |
def predict():
|
| 85 |
data = request.json
|