Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,6 +8,8 @@ import pandas as pd
|
|
| 8 |
from utils import compute_features
|
| 9 |
from scipy.stats import nbinom
|
| 10 |
|
|
|
|
|
|
|
| 11 |
|
| 12 |
class NegBinomialModel(nn.Module):
|
| 13 |
def __init__(self, in_features):
|
|
@@ -22,14 +24,20 @@ class NegBinomialModel(nn.Module):
|
|
| 22 |
return mu.squeeze(), alpha
|
| 23 |
|
| 24 |
|
| 25 |
-
model = NegBinomialModel(
|
| 26 |
-
model.load_state_dict(torch.load("model_weights.pt", map_location='cpu'))
|
| 27 |
model.eval()
|
| 28 |
|
| 29 |
# MU_BANKS = 2.6035915713614286
|
| 30 |
# STD_BANKS = 3.0158890435512125
|
| 31 |
|
| 32 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
def predict_score(lat, lon):
|
| 34 |
# Convert input to tensor
|
| 35 |
# inputs = torch.tensor([[lat, lon]], dtype=torch.float32)
|
|
@@ -37,7 +45,7 @@ def predict_score(lat, lon):
|
|
| 37 |
print("[INPUTS]", inputs)
|
| 38 |
num_banks = inputs.pop("num_banks_in_radius", 0)
|
| 39 |
|
| 40 |
-
inputs = torch.tensor(
|
| 41 |
|
| 42 |
# Get model output
|
| 43 |
with torch.no_grad():
|
|
@@ -46,6 +54,8 @@ def predict_score(lat, lon):
|
|
| 46 |
# Unpack into respective values
|
| 47 |
mu_pred = mu_pred.numpy().flatten()
|
| 48 |
|
|
|
|
|
|
|
| 49 |
# r = 1/alpha
|
| 50 |
# p = r / (r + mu_pred)
|
| 51 |
|
|
@@ -76,6 +86,7 @@ def predict_score(lat, lon):
|
|
| 76 |
round(float(score), 3),
|
| 77 |
num_banks,
|
| 78 |
round(float(mu_pred), 3),
|
|
|
|
| 79 |
# round(float(log_score),3)
|
| 80 |
# "Normal Score": round(float(normal_score), 3),
|
| 81 |
)
|
|
@@ -90,7 +101,8 @@ interface = gr.Interface(
|
|
| 90 |
outputs=[
|
| 91 |
gr.Number(label="Score (0 - 100)"),
|
| 92 |
gr.Number(label="Number of Current Banks"),
|
| 93 |
-
gr.Number(label="Number of Ideal Banks"),
|
|
|
|
| 94 |
# gr.Number(label="Log Score Probability"),
|
| 95 |
],
|
| 96 |
title="Bank Location Scoring Model",
|
|
|
|
| 8 |
from utils import compute_features
|
| 9 |
from scipy.stats import nbinom
|
| 10 |
|
| 11 |
+
from xgboost import XGBRegressor
|
| 12 |
+
|
| 13 |
|
| 14 |
class NegBinomialModel(nn.Module):
|
| 15 |
def __init__(self, in_features):
|
|
|
|
| 24 |
return mu.squeeze(), alpha
|
| 25 |
|
| 26 |
|
| 27 |
+
model = NegBinomialModel(12)
|
| 28 |
+
model.load_state_dict(torch.load("model_weights(1).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(**params)
|
| 39 |
+
|
| 40 |
+
|
| 41 |
def predict_score(lat, lon):
|
| 42 |
# Convert input to tensor
|
| 43 |
# inputs = torch.tensor([[lat, lon]], dtype=torch.float32)
|
|
|
|
| 45 |
print("[INPUTS]", inputs)
|
| 46 |
num_banks = inputs.pop("num_banks_in_radius", 0)
|
| 47 |
|
| 48 |
+
inputs = torch.tensor(list(inputs.values()), dtype=torch.float32)
|
| 49 |
|
| 50 |
# Get model output
|
| 51 |
with torch.no_grad():
|
|
|
|
| 54 |
# Unpack into respective values
|
| 55 |
mu_pred = mu_pred.numpy().flatten()
|
| 56 |
|
| 57 |
+
mu_pred2 = xgb_model.predict(inputs.numpy())
|
| 58 |
+
|
| 59 |
# r = 1/alpha
|
| 60 |
# p = r / (r + mu_pred)
|
| 61 |
|
|
|
|
| 86 |
round(float(score), 3),
|
| 87 |
num_banks,
|
| 88 |
round(float(mu_pred), 3),
|
| 89 |
+
round(float(mu_pred2), 3),
|
| 90 |
# round(float(log_score),3)
|
| 91 |
# "Normal Score": round(float(normal_score), 3),
|
| 92 |
)
|
|
|
|
| 101 |
outputs=[
|
| 102 |
gr.Number(label="Score (0 - 100)"),
|
| 103 |
gr.Number(label="Number of Current Banks"),
|
| 104 |
+
gr.Number(label="Number of Ideal Banks (Negative Binomial)"),
|
| 105 |
+
gr.Number(label="Number of Ideal Banks (XGBoost)"),
|
| 106 |
# gr.Number(label="Log Score Probability"),
|
| 107 |
],
|
| 108 |
title="Bank Location Scoring Model",
|