Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,28 +5,28 @@ from torch import nn
|
|
| 5 |
import numpy as np
|
| 6 |
import pandas as pd
|
| 7 |
|
| 8 |
-
from
|
| 9 |
from scipy.stats import nbinom
|
| 10 |
|
| 11 |
from xgboost import XGBRegressor
|
| 12 |
import json
|
| 13 |
|
| 14 |
-
class NegBinomialModel(nn.Module):
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
|
| 26 |
|
| 27 |
-
model = NegBinomialModel(12)
|
| 28 |
-
model.load_state_dict(torch.load("model_weights
|
| 29 |
-
model.eval()
|
| 30 |
|
| 31 |
# MU_BANKS = 2.6035915713614286
|
| 32 |
# STD_BANKS = 3.0158890435512125
|
|
@@ -45,14 +45,16 @@ def predict_score(lat, lon):
|
|
| 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():
|
| 52 |
-
|
| 53 |
|
| 54 |
-
# Unpack into respective values
|
| 55 |
-
mu_pred = mu_pred.numpy().flatten()
|
| 56 |
|
| 57 |
mu_pred2 = xgb_model.predict(inputs.unsqueeze(0).numpy())
|
| 58 |
|
|
@@ -73,22 +75,30 @@ def predict_score(lat, lon):
|
|
| 73 |
|
| 74 |
# print("[TANH]", np.tanh(diff))
|
| 75 |
|
| 76 |
-
diff =
|
| 77 |
-
score = 100 / (1 + np.exp(-alpha * diff))
|
|
|
|
|
|
|
|
|
|
| 78 |
|
| 79 |
-
score = np.abs(
|
| 80 |
|
|
|
|
| 81 |
|
| 82 |
-
|
| 83 |
|
| 84 |
# You can apply any post-processing here
|
| 85 |
return (
|
| 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 |
)
|
| 93 |
|
| 94 |
# ======== Gradio Interface ========
|
|
@@ -100,10 +110,23 @@ interface = gr.Interface(
|
|
| 100 |
],
|
| 101 |
outputs=[
|
| 102 |
gr.Number(label="Score (0 - 100)"),
|
| 103 |
-
gr.Number(label="
|
| 104 |
-
gr.Number(label="Number of Ideal Banks (Negative Binomial)"),
|
| 105 |
-
gr.Number(label="
|
| 106 |
# gr.Number(label="Log Score Probability"),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
],
|
| 108 |
title="Bank Location Scoring Model",
|
| 109 |
description="Enter latitude and longitude to get the predicted score, number of banks, and normalized score.",
|
|
@@ -111,3 +134,118 @@ interface = gr.Interface(
|
|
| 111 |
|
| 112 |
|
| 113 |
interface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
import numpy as np
|
| 6 |
import pandas as pd
|
| 7 |
|
| 8 |
+
from utils2 import compute_features
|
| 9 |
from scipy.stats import nbinom
|
| 10 |
|
| 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
|
|
|
|
| 45 |
print("[INPUTS]", inputs)
|
| 46 |
num_banks = inputs.pop("num_banks_in_radius", 0)
|
| 47 |
|
| 48 |
+
input_dict = inputs.copy()
|
| 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 |
|
|
|
|
| 75 |
|
| 76 |
# print("[TANH]", np.tanh(diff))
|
| 77 |
|
| 78 |
+
# diff = mu_pred2 - num_banks
|
| 79 |
+
# score = 100 / (1 + np.exp(-alpha * diff))
|
| 80 |
+
|
| 81 |
+
# score = np.abs(1 + np.tanh(diff)) / 2 * 100
|
| 82 |
+
|
| 83 |
|
| 84 |
+
# score = (1 * np.abs(mu_pred2 + 0.1)) * 100
|
| 85 |
|
| 86 |
+
# score = np.sigmoid(mu_pred2 - num_banks + 0.1) * 100
|
| 87 |
|
| 88 |
+
score = 100 / (1 + np.exp(mu_pred2 - num_banks))
|
| 89 |
|
| 90 |
# You can apply any post-processing here
|
| 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),
|
| 98 |
+
input_dict["total_amenities"],
|
| 99 |
+
|
| 100 |
+
*[v for k,v in input_dict.items() if k[:3] == "num"]
|
| 101 |
+
|
| 102 |
)
|
| 103 |
|
| 104 |
# ======== Gradio Interface ========
|
|
|
|
| 110 |
],
|
| 111 |
outputs=[
|
| 112 |
gr.Number(label="Score (0 - 100)"),
|
| 113 |
+
gr.Number(label="Current ATMs"),
|
| 114 |
+
# gr.Number(label="Number of Ideal Banks (Negative Binomial)"),
|
| 115 |
+
gr.Number(label="Ideal ATMs (XGBoost)"),
|
| 116 |
# gr.Number(label="Log Score Probability"),
|
| 117 |
+
|
| 118 |
+
gr.Number(label="Total Amenities"),
|
| 119 |
+
|
| 120 |
+
gr.Number(label="Dining and Drinking"),
|
| 121 |
+
gr.Number(label="Community and Government"),
|
| 122 |
+
gr.Number(label="Retail"),
|
| 123 |
+
gr.Number(label="Business and Professional Services"),
|
| 124 |
+
gr.Number(label="Landmarks and Outdoors"),
|
| 125 |
+
gr.Number(label="Arts and Entertainment"),
|
| 126 |
+
gr.Number(label="Health and Medicine"),
|
| 127 |
+
gr.Number(label="Travel and Transportation"),
|
| 128 |
+
gr.Number(label="Sports and Recreation"),
|
| 129 |
+
gr.Number(label="Event"),
|
| 130 |
],
|
| 131 |
title="Bank Location Scoring Model",
|
| 132 |
description="Enter latitude and longitude to get the predicted score, number of banks, and normalized score.",
|
|
|
|
| 134 |
|
| 135 |
|
| 136 |
interface.launch()
|
| 137 |
+
|
| 138 |
+
|
| 139 |
+
# import gradio as gr
|
| 140 |
+
# import torch
|
| 141 |
+
# from torch import nn
|
| 142 |
+
|
| 143 |
+
# import numpy as np
|
| 144 |
+
# import pandas as pd
|
| 145 |
+
|
| 146 |
+
# from utils import compute_features
|
| 147 |
+
# from scipy.stats import nbinom
|
| 148 |
+
|
| 149 |
+
# from xgboost import XGBRegressor
|
| 150 |
+
# import json
|
| 151 |
+
|
| 152 |
+
# class NegBinomialModel(nn.Module):
|
| 153 |
+
# def __init__(self, in_features):
|
| 154 |
+
# super().__init__()
|
| 155 |
+
# self.linear = nn.Linear(in_features, 1)
|
| 156 |
+
# self.alpha = nn.Parameter(torch.tensor(0.5))
|
| 157 |
+
|
| 158 |
+
# def forward(self, x):
|
| 159 |
+
# # safer activation than exp()
|
| 160 |
+
# mu = torch.exp(torch.clamp(self.linear(x), min=-5, max=5))
|
| 161 |
+
# alpha = torch.clamp(self.alpha, min=1e-3, max=10)
|
| 162 |
+
# return mu.squeeze(), alpha
|
| 163 |
+
|
| 164 |
+
|
| 165 |
+
# model = NegBinomialModel(12)
|
| 166 |
+
# model.load_state_dict(torch.load("model_weights(1).pt", map_location='cpu'))
|
| 167 |
+
# model.eval()
|
| 168 |
+
|
| 169 |
+
# # MU_BANKS = 2.6035915713614286
|
| 170 |
+
# # STD_BANKS = 3.0158890435512125
|
| 171 |
+
|
| 172 |
+
|
| 173 |
+
# # with open("xgb_model(1).json", "r") as f:
|
| 174 |
+
# # params = json.load(f)
|
| 175 |
+
|
| 176 |
+
# xgb_model = XGBRegressor()
|
| 177 |
+
# xgb_model.load_model("xgb_model(1).json")
|
| 178 |
+
|
| 179 |
+
# def predict_score(lat, lon):
|
| 180 |
+
# # Convert input to tensor
|
| 181 |
+
# # inputs = torch.tensor([[lat, lon]], dtype=torch.float32)
|
| 182 |
+
# inputs = compute_features((lat,lon))
|
| 183 |
+
# print("[INPUTS]", inputs)
|
| 184 |
+
# num_banks = inputs.pop("num_banks_in_radius", 0)
|
| 185 |
+
|
| 186 |
+
# inputs = torch.tensor(list(inputs.values()), dtype=torch.float32)
|
| 187 |
+
|
| 188 |
+
# # Get model output
|
| 189 |
+
# with torch.no_grad():
|
| 190 |
+
# mu_pred, alpha = model(inputs)
|
| 191 |
+
|
| 192 |
+
# # Unpack into respective values
|
| 193 |
+
# mu_pred = mu_pred.numpy().flatten()
|
| 194 |
+
|
| 195 |
+
# mu_pred2 = xgb_model.predict(inputs.unsqueeze(0).numpy())
|
| 196 |
+
|
| 197 |
+
# # r = 1/alpha
|
| 198 |
+
# # p = r / (r + mu_pred)
|
| 199 |
+
|
| 200 |
+
# # # Compute pmf and mode
|
| 201 |
+
# # k_mode = int((r - 1) * (1 - p) / p) # mode of NB
|
| 202 |
+
# # p_k = nbinom.pmf(num_banks, r, p)
|
| 203 |
+
# # p_mode = nbinom.pmf(k_mode, r, p)
|
| 204 |
+
|
| 205 |
+
# # # Score normalized 0–100
|
| 206 |
+
# # score = (p_k / p_mode) * 100
|
| 207 |
+
# # score = np.clip(score, 0, 100)
|
| 208 |
+
|
| 209 |
+
# # diff = (num_banks - mu_pred) / (mu_pred + 1e-6)
|
| 210 |
+
# # # score = (1 - np.tanh(diff))
|
| 211 |
+
|
| 212 |
+
# # print("[TANH]", np.tanh(diff))
|
| 213 |
+
|
| 214 |
+
# diff = mu_pred - num_banks
|
| 215 |
+
# score = 100 / (1 + np.exp(-alpha * diff))
|
| 216 |
+
|
| 217 |
+
# score = np.abs(1 + np.tanh(diff)) / 2 * 100
|
| 218 |
+
|
| 219 |
+
|
| 220 |
+
# # score = (1 * np.abs(mu_pred + 0.1)) * 100
|
| 221 |
+
|
| 222 |
+
# # You can apply any post-processing here
|
| 223 |
+
# return (
|
| 224 |
+
# round(float(score), 3),
|
| 225 |
+
# num_banks,
|
| 226 |
+
# round(float(mu_pred), 3),
|
| 227 |
+
# round(float(mu_pred2), 3),
|
| 228 |
+
# # round(float(log_score),3)
|
| 229 |
+
# # "Normal Score": round(float(normal_score), 3),
|
| 230 |
+
# )
|
| 231 |
+
|
| 232 |
+
# # ======== Gradio Interface ========
|
| 233 |
+
# interface = gr.Interface(
|
| 234 |
+
# fn=predict_score,
|
| 235 |
+
# inputs=[
|
| 236 |
+
# gr.Number(label="Latitude"),
|
| 237 |
+
# gr.Number(label="Longitude"),
|
| 238 |
+
# ],
|
| 239 |
+
# outputs=[
|
| 240 |
+
# gr.Number(label="Score (0 - 100)"),
|
| 241 |
+
# gr.Number(label="Number of Current Banks"),
|
| 242 |
+
# gr.Number(label="Number of Ideal Banks (Negative Binomial)"),
|
| 243 |
+
# gr.Number(label="Number of Ideal Banks (XGBoost)"),
|
| 244 |
+
# # gr.Number(label="Log Score Probability"),
|
| 245 |
+
# ],
|
| 246 |
+
# title="Bank Location Scoring Model",
|
| 247 |
+
# description="Enter latitude and longitude to get the predicted score, number of banks, and normalized score.",
|
| 248 |
+
# )
|
| 249 |
+
|
| 250 |
+
|
| 251 |
+
# interface.launch()
|