Spaces:
Sleeping
Sleeping
Umair khan
commited on
Commit
·
3c2e205
1
Parent(s):
0fdc7b3
Add Gradio app and requirements file
Browse files- app.py +80 -0
- requirements.txt +0 -0
app.py
ADDED
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import pandas as pd
|
3 |
+
import numpy as np
|
4 |
+
from sklearn.model_selection import train_test_split
|
5 |
+
import torch
|
6 |
+
|
7 |
+
|
8 |
+
batting = pd.read_csv('moneyball project\Batting.csv')
|
9 |
+
salaries = pd.read_csv('moneyball project\Salaries.csv')
|
10 |
+
|
11 |
+
df = pd.merge(batting, salaries, on=['playerID', 'yearID', 'teamID'])
|
12 |
+
|
13 |
+
|
14 |
+
df['BA'] = df['H'] / df['AB']
|
15 |
+
|
16 |
+
|
17 |
+
df['OBP'] = (df['H'] + df['BB'] + df['HBP']) / (df['AB'] + df['BB'] + df['HBP'] + df['SF'])
|
18 |
+
|
19 |
+
# Slugging percentage
|
20 |
+
df['SLG'] = (df['H'] + 2*df['2B'] + 3*df['3B'] + 4*df['HR']) / df['AB']
|
21 |
+
|
22 |
+
df = df.dropna()
|
23 |
+
|
24 |
+
features = ['BA', 'OBP', 'SLG', 'HR', 'RBI', 'SB']
|
25 |
+
X = df[features]
|
26 |
+
y = df['salary']
|
27 |
+
|
28 |
+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
|
29 |
+
|
30 |
+
X_train_tensor = torch.tensor(X_train.fillna(0).values, dtype=torch.float32)
|
31 |
+
y_train_tensor = torch.tensor(y_train.fillna(0).values, dtype=torch.float32)
|
32 |
+
X_test_tensor = torch.FloatTensor(X_test.values)
|
33 |
+
y_test_tensor = torch.FloatTensor(y_test.values)
|
34 |
+
|
35 |
+
class LinearRegression(torch.nn.Module):
|
36 |
+
def __init__(self, input_dim):
|
37 |
+
super(LinearRegression, self).__init__()
|
38 |
+
self.linear = torch.nn.Linear(input_dim, 1)
|
39 |
+
|
40 |
+
def forward(self, x):
|
41 |
+
return self.linear(x)
|
42 |
+
|
43 |
+
model = LinearRegression(len(features))
|
44 |
+
criterion = torch.nn.MSELoss()
|
45 |
+
optimizer = torch.optim.SGD(model.parameters(), lr=0.0001)
|
46 |
+
|
47 |
+
num_epochs = 1000
|
48 |
+
for epoch in range(num_epochs):
|
49 |
+
outputs = model(X_train_tensor)
|
50 |
+
loss = criterion(outputs, y_train_tensor.unsqueeze(1))
|
51 |
+
|
52 |
+
optimizer.zero_grad()
|
53 |
+
loss.backward()
|
54 |
+
optimizer.step()
|
55 |
+
|
56 |
+
# Define prediction function for Gradio
|
57 |
+
def predict_salary(BA, OBP, SLG, HR, RBI, SB):
|
58 |
+
stats = [BA, OBP, SLG, HR, RBI, SB]
|
59 |
+
with torch.no_grad():
|
60 |
+
stats_tensor = torch.FloatTensor([stats])
|
61 |
+
predicted_salary = model(stats_tensor).item()
|
62 |
+
return f'${predicted_salary:,.2f}'
|
63 |
+
|
64 |
+
# Gradio interface
|
65 |
+
demo = gr.Interface(
|
66 |
+
fn=predict_salary,
|
67 |
+
inputs=[
|
68 |
+
gr.components.Number(label="Batting Average (BA)"),
|
69 |
+
gr.components.Number(label="On-base Percentage (OBP)"),
|
70 |
+
gr.components.Number(label="Slugging Percentage (SLG)"),
|
71 |
+
gr.components.Number(label="Home Runs (HR)"),
|
72 |
+
gr.components.Number(label="Runs Batted In (RBI)"),
|
73 |
+
gr.components.Number(label="Stolen Bases (SB)")
|
74 |
+
],
|
75 |
+
outputs="text",
|
76 |
+
title="Baseball Player Salary Predictor"
|
77 |
+
)
|
78 |
+
|
79 |
+
# Launch the app
|
80 |
+
demo.launch(share=True)
|
requirements.txt
ADDED
Binary file (90 Bytes). View file
|
|