Update app.py
Browse files
app.py
CHANGED
|
@@ -6,6 +6,7 @@ import numpy as np
|
|
| 6 |
import pandas as pd
|
| 7 |
|
| 8 |
from utils import compute_features
|
|
|
|
| 9 |
|
| 10 |
|
| 11 |
class NegBinomialModel(nn.Module):
|
|
@@ -25,10 +26,15 @@ model = NegBinomialModel(16)
|
|
| 25 |
model.load_state_dict(torch.load("model_weights.pt", map_location='cpu'))
|
| 26 |
model.eval()
|
| 27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
def predict_score(lat, lon):
|
| 29 |
# Convert input to tensor
|
| 30 |
# inputs = torch.tensor([[lat, lon]], dtype=torch.float32)
|
| 31 |
inputs = compute_features((lat,lon))
|
|
|
|
| 32 |
num_banks = inputs.pop("num_banks_in_radius", 0)
|
| 33 |
|
| 34 |
inputs = torch.tensor([lat,lon] + list(inputs.values()), dtype=torch.float32)
|
|
@@ -40,13 +46,37 @@ def predict_score(lat, lon):
|
|
| 40 |
# Unpack into respective values
|
| 41 |
mu_pred = mu_pred.numpy().flatten()
|
| 42 |
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
# You can apply any post-processing here
|
| 46 |
return (
|
| 47 |
round(float(score), 3),
|
| 48 |
num_banks,
|
| 49 |
round(float(mu_pred), 3),
|
|
|
|
| 50 |
# "Normal Score": round(float(normal_score), 3),
|
| 51 |
)
|
| 52 |
|
|
@@ -60,8 +90,8 @@ interface = gr.Interface(
|
|
| 60 |
outputs=[
|
| 61 |
gr.Number(label="Score"),
|
| 62 |
gr.Number(label="Num Current Banks"),
|
| 63 |
-
gr.Number(label="Num Ideal Banks")
|
| 64 |
-
# gr.Number(label="
|
| 65 |
],
|
| 66 |
title="Bank Location Scoring Model",
|
| 67 |
description="Enter latitude and longitude to get the predicted score, number of banks, and normalized score.",
|
|
|
|
| 6 |
import pandas as pd
|
| 7 |
|
| 8 |
from utils import compute_features
|
| 9 |
+
from scipy.stats import nbinom
|
| 10 |
|
| 11 |
|
| 12 |
class NegBinomialModel(nn.Module):
|
|
|
|
| 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)
|
| 36 |
inputs = compute_features((lat,lon))
|
| 37 |
+
print("[INPUTS]", inputs)
|
| 38 |
num_banks = inputs.pop("num_banks_in_radius", 0)
|
| 39 |
|
| 40 |
inputs = torch.tensor([lat,lon] + list(inputs.values()), dtype=torch.float32)
|
|
|
|
| 46 |
# Unpack into respective values
|
| 47 |
mu_pred = mu_pred.numpy().flatten()
|
| 48 |
|
| 49 |
+
# r = 1/alpha
|
| 50 |
+
# p = r / (r + mu_pred)
|
| 51 |
+
|
| 52 |
+
# # Compute pmf and mode
|
| 53 |
+
# k_mode = int((r - 1) * (1 - p) / p) # mode of NB
|
| 54 |
+
# p_k = nbinom.pmf(num_banks, r, p)
|
| 55 |
+
# p_mode = nbinom.pmf(k_mode, r, p)
|
| 56 |
+
|
| 57 |
+
# # Score normalized 0–100
|
| 58 |
+
# score = (p_k / p_mode) * 100
|
| 59 |
+
# score = np.clip(score, 0, 100)
|
| 60 |
+
|
| 61 |
+
# diff = (num_banks - mu_pred) / (mu_pred + 1e-6)
|
| 62 |
+
# # score = (1 - np.tanh(diff))
|
| 63 |
+
|
| 64 |
+
# print("[TANH]", np.tanh(diff))
|
| 65 |
+
|
| 66 |
+
diff = mu_pred - num_banks
|
| 67 |
+
score = 100 / (1 + np.exp(-alpha * diff))
|
| 68 |
+
|
| 69 |
+
score = np.abs(1 + np.tanh(diff)) / 2 * 100
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
# score = (1 * np.abs(mu_pred + 0.1)) * 100
|
| 73 |
|
| 74 |
# You can apply any post-processing here
|
| 75 |
return (
|
| 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 |
)
|
| 82 |
|
|
|
|
| 90 |
outputs=[
|
| 91 |
gr.Number(label="Score"),
|
| 92 |
gr.Number(label="Num Current Banks"),
|
| 93 |
+
gr.Number(label="Num Ideal Banks"),
|
| 94 |
+
# gr.Number(label="Log Score Probability"),
|
| 95 |
],
|
| 96 |
title="Bank Location Scoring Model",
|
| 97 |
description="Enter latitude and longitude to get the predicted score, number of banks, and normalized score.",
|