Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -11,25 +11,25 @@ from scipy.stats import nbinom
|
|
| 11 |
from xgboost import XGBRegressor
|
| 12 |
import json
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
#
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
|
| 26 |
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
| 33 |
|
| 34 |
|
| 35 |
# with open("xgb_model(1).json", "r") as f:
|
|
@@ -49,12 +49,12 @@ def predict_score(lat, lon, api_key):
|
|
| 49 |
|
| 50 |
inputs = torch.tensor(list(inputs.values()), dtype=torch.float32)
|
| 51 |
|
| 52 |
-
#
|
| 53 |
-
|
| 54 |
-
|
| 55 |
|
| 56 |
-
#
|
| 57 |
-
|
| 58 |
|
| 59 |
mu_pred2 = xgb_model.predict(inputs.unsqueeze(0).numpy())
|
| 60 |
|
|
@@ -91,7 +91,7 @@ def predict_score(lat, lon, api_key):
|
|
| 91 |
return (
|
| 92 |
round(float(score), 3),
|
| 93 |
num_banks,
|
| 94 |
-
|
| 95 |
round(float(mu_pred2), 3),
|
| 96 |
# round(float(log_score),3)
|
| 97 |
# "Normal Score": round(float(normal_score), 3),
|
|
@@ -112,7 +112,7 @@ interface = gr.Interface(
|
|
| 112 |
outputs=[
|
| 113 |
gr.Number(label="Score (0 - 100)"),
|
| 114 |
gr.Number(label="Current ATMs"),
|
| 115 |
-
|
| 116 |
gr.Number(label="Ideal ATMs (XGBoost)"),
|
| 117 |
# gr.Number(label="Log Score Probability"),
|
| 118 |
|
|
|
|
| 11 |
from xgboost import XGBRegressor
|
| 12 |
import json
|
| 13 |
|
| 14 |
+
class NegBinomialModel(nn.Module):
|
| 15 |
+
def __init__(self, in_features):
|
| 16 |
+
super().__init__()
|
| 17 |
+
self.linear = nn.Linear(in_features, 1)
|
| 18 |
+
self.alpha = nn.Parameter(torch.tensor(0.5))
|
| 19 |
+
|
| 20 |
+
def forward(self, x):
|
| 21 |
+
# safer activation than exp()
|
| 22 |
+
mu = torch.exp(torch.clamp(self.linear(x), min=-5, max=5))
|
| 23 |
+
alpha = torch.clamp(self.alpha, min=1e-3, max=10)
|
| 24 |
+
return mu.squeeze(), alpha
|
| 25 |
|
| 26 |
|
| 27 |
+
model = NegBinomialModel(12)
|
| 28 |
+
model.load_state_dict(torch.load("model_weights.pt", map_location='cpu'))
|
| 29 |
+
model.eval()
|
| 30 |
|
| 31 |
+
MU_BANKS = 2.6035915713614286
|
| 32 |
+
STD_BANKS = 3.0158890435512125
|
| 33 |
|
| 34 |
|
| 35 |
# with open("xgb_model(1).json", "r") as f:
|
|
|
|
| 49 |
|
| 50 |
inputs = torch.tensor(list(inputs.values()), dtype=torch.float32)
|
| 51 |
|
| 52 |
+
# Get model output
|
| 53 |
+
with torch.no_grad():
|
| 54 |
+
mu_pred, alpha = model(inputs)
|
| 55 |
|
| 56 |
+
# Unpack into respective values
|
| 57 |
+
mu_pred = mu_pred.numpy().flatten()
|
| 58 |
|
| 59 |
mu_pred2 = xgb_model.predict(inputs.unsqueeze(0).numpy())
|
| 60 |
|
|
|
|
| 91 |
return (
|
| 92 |
round(float(score), 3),
|
| 93 |
num_banks,
|
| 94 |
+
round(float(mu_pred), 3),
|
| 95 |
round(float(mu_pred2), 3),
|
| 96 |
# round(float(log_score),3)
|
| 97 |
# "Normal Score": round(float(normal_score), 3),
|
|
|
|
| 112 |
outputs=[
|
| 113 |
gr.Number(label="Score (0 - 100)"),
|
| 114 |
gr.Number(label="Current ATMs"),
|
| 115 |
+
gr.Number(label="Ideal ATMs (Negative Binomial)"),
|
| 116 |
gr.Number(label="Ideal ATMs (XGBoost)"),
|
| 117 |
# gr.Number(label="Log Score Probability"),
|
| 118 |
|