Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -11,32 +11,32 @@ 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:
|
| 36 |
# params = json.load(f)
|
| 37 |
|
| 38 |
xgb_model = XGBRegressor()
|
| 39 |
-
xgb_model.load_model("
|
| 40 |
|
| 41 |
def predict_score(lat, lon, api_key):
|
| 42 |
# Convert input to tensor
|
|
@@ -49,10 +49,10 @@ 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 |
mu_pred2 = xgb_model.predict(inputs.unsqueeze(0).numpy())
|
| 58 |
|
|
@@ -89,7 +89,7 @@ def predict_score(lat, lon, api_key):
|
|
| 89 |
return (
|
| 90 |
round(float(score), 3),
|
| 91 |
num_banks,
|
| 92 |
-
|
| 93 |
round(float(mu_pred2), 3),
|
| 94 |
# round(float(log_score),3)
|
| 95 |
# "Normal Score": round(float(normal_score), 3),
|
|
@@ -110,7 +110,7 @@ interface = gr.Interface(
|
|
| 110 |
outputs=[
|
| 111 |
gr.Number(label="Score (0 - 100)"),
|
| 112 |
gr.Number(label="Current ATMs"),
|
| 113 |
-
|
| 114 |
gr.Number(label="Ideal ATMs (XGBoost)"),
|
| 115 |
# gr.Number(label="Log Score Probability"),
|
| 116 |
|
|
|
|
| 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(10)
|
| 28 |
+
model.load_state_dict(torch.load("model_weights_2.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:
|
| 36 |
# params = json.load(f)
|
| 37 |
|
| 38 |
xgb_model = XGBRegressor()
|
| 39 |
+
xgb_model.load_model("xgb_model_2.json")
|
| 40 |
|
| 41 |
def predict_score(lat, lon, api_key):
|
| 42 |
# Convert input to tensor
|
|
|
|
| 49 |
|
| 50 |
inputs = torch.tensor(list(inputs.values()), dtype=torch.float32)
|
| 51 |
|
| 52 |
+
with torch.no_grad():
|
| 53 |
+
mu_pred, alpha = model(inputs)
|
| 54 |
|
| 55 |
+
mu_pred = mu_pred.numpy().flatten()
|
| 56 |
|
| 57 |
mu_pred2 = xgb_model.predict(inputs.unsqueeze(0).numpy())
|
| 58 |
|
|
|
|
| 89 |
return (
|
| 90 |
round(float(score), 3),
|
| 91 |
num_banks,
|
| 92 |
+
round(float(mu_pred), 3),
|
| 93 |
round(float(mu_pred2), 3),
|
| 94 |
# round(float(log_score),3)
|
| 95 |
# "Normal Score": round(float(normal_score), 3),
|
|
|
|
| 110 |
outputs=[
|
| 111 |
gr.Number(label="Score (0 - 100)"),
|
| 112 |
gr.Number(label="Current ATMs"),
|
| 113 |
+
gr.Number(label="Ideal ATMs (Negative Binomial)"),
|
| 114 |
gr.Number(label="Ideal ATMs (XGBoost)"),
|
| 115 |
# gr.Number(label="Log Score Probability"),
|
| 116 |
|