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 |
-
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(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()
|
| 39 |
-
xgb_model.load_model("xgb_model(
|
| 40 |
|
| 41 |
def predict_score(lat, lon, api_key):
|
| 42 |
# Convert input to tensor
|
|
@@ -49,12 +49,10 @@ def predict_score(lat, lon, api_key):
|
|
| 49 |
|
| 50 |
inputs = torch.tensor(list(inputs.values()), dtype=torch.float32)
|
| 51 |
|
| 52 |
-
#
|
| 53 |
-
|
| 54 |
-
mu_pred, alpha = model(inputs)
|
| 55 |
|
| 56 |
-
#
|
| 57 |
-
mu_pred = mu_pred.numpy().flatten()
|
| 58 |
|
| 59 |
mu_pred2 = xgb_model.predict(inputs.unsqueeze(0).numpy())
|
| 60 |
|
|
@@ -91,11 +89,11 @@ def predict_score(lat, lon, api_key):
|
|
| 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 |
|
|
@@ -112,11 +110,11 @@ interface = gr.Interface(
|
|
| 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 |
|
| 119 |
-
gr.Number(label="Total Amenities"),
|
| 120 |
|
| 121 |
gr.Number(label="Dining and Drinking"),
|
| 122 |
gr.Number(label="Community and Government"),
|
|
@@ -133,120 +131,3 @@ interface = gr.Interface(
|
|
| 133 |
description="Enter latitude and longitude to get the predicted score, number of banks, and normalized score.",
|
| 134 |
)
|
| 135 |
|
| 136 |
-
|
| 137 |
-
interface.launch()
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
# import gradio as gr
|
| 141 |
-
# import torch
|
| 142 |
-
# from torch import nn
|
| 143 |
-
|
| 144 |
-
# import numpy as np
|
| 145 |
-
# import pandas as pd
|
| 146 |
-
|
| 147 |
-
# from utils import compute_features
|
| 148 |
-
# from scipy.stats import nbinom
|
| 149 |
-
|
| 150 |
-
# from xgboost import XGBRegressor
|
| 151 |
-
# import json
|
| 152 |
-
|
| 153 |
-
# class NegBinomialModel(nn.Module):
|
| 154 |
-
# def __init__(self, in_features):
|
| 155 |
-
# super().__init__()
|
| 156 |
-
# self.linear = nn.Linear(in_features, 1)
|
| 157 |
-
# self.alpha = nn.Parameter(torch.tensor(0.5))
|
| 158 |
-
|
| 159 |
-
# def forward(self, x):
|
| 160 |
-
# # safer activation than exp()
|
| 161 |
-
# mu = torch.exp(torch.clamp(self.linear(x), min=-5, max=5))
|
| 162 |
-
# alpha = torch.clamp(self.alpha, min=1e-3, max=10)
|
| 163 |
-
# return mu.squeeze(), alpha
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
# model = NegBinomialModel(12)
|
| 167 |
-
# model.load_state_dict(torch.load("model_weights(1).pt", map_location='cpu'))
|
| 168 |
-
# model.eval()
|
| 169 |
-
|
| 170 |
-
# # MU_BANKS = 2.6035915713614286
|
| 171 |
-
# # STD_BANKS = 3.0158890435512125
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
# # with open("xgb_model(1).json", "r") as f:
|
| 175 |
-
# # params = json.load(f)
|
| 176 |
-
|
| 177 |
-
# xgb_model = XGBRegressor()
|
| 178 |
-
# xgb_model.load_model("xgb_model(1).json")
|
| 179 |
-
|
| 180 |
-
# def predict_score(lat, lon):
|
| 181 |
-
# # Convert input to tensor
|
| 182 |
-
# # inputs = torch.tensor([[lat, lon]], dtype=torch.float32)
|
| 183 |
-
# inputs = compute_features((lat,lon))
|
| 184 |
-
# print("[INPUTS]", inputs)
|
| 185 |
-
# num_banks = inputs.pop("num_banks_in_radius", 0)
|
| 186 |
-
|
| 187 |
-
# inputs = torch.tensor(list(inputs.values()), dtype=torch.float32)
|
| 188 |
-
|
| 189 |
-
# # Get model output
|
| 190 |
-
# with torch.no_grad():
|
| 191 |
-
# mu_pred, alpha = model(inputs)
|
| 192 |
-
|
| 193 |
-
# # Unpack into respective values
|
| 194 |
-
# mu_pred = mu_pred.numpy().flatten()
|
| 195 |
-
|
| 196 |
-
# mu_pred2 = xgb_model.predict(inputs.unsqueeze(0).numpy())
|
| 197 |
-
|
| 198 |
-
# # r = 1/alpha
|
| 199 |
-
# # p = r / (r + mu_pred)
|
| 200 |
-
|
| 201 |
-
# # # Compute pmf and mode
|
| 202 |
-
# # k_mode = int((r - 1) * (1 - p) / p) # mode of NB
|
| 203 |
-
# # p_k = nbinom.pmf(num_banks, r, p)
|
| 204 |
-
# # p_mode = nbinom.pmf(k_mode, r, p)
|
| 205 |
-
|
| 206 |
-
# # # Score normalized 0–100
|
| 207 |
-
# # score = (p_k / p_mode) * 100
|
| 208 |
-
# # score = np.clip(score, 0, 100)
|
| 209 |
-
|
| 210 |
-
# # diff = (num_banks - mu_pred) / (mu_pred + 1e-6)
|
| 211 |
-
# # # score = (1 - np.tanh(diff))
|
| 212 |
-
|
| 213 |
-
# # print("[TANH]", np.tanh(diff))
|
| 214 |
-
|
| 215 |
-
# diff = mu_pred - num_banks
|
| 216 |
-
# score = 100 / (1 + np.exp(-alpha * diff))
|
| 217 |
-
|
| 218 |
-
# score = np.abs(1 + np.tanh(diff)) / 2 * 100
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
# # score = (1 * np.abs(mu_pred + 0.1)) * 100
|
| 222 |
-
|
| 223 |
-
# # You can apply any post-processing here
|
| 224 |
-
# return (
|
| 225 |
-
# round(float(score), 3),
|
| 226 |
-
# num_banks,
|
| 227 |
-
# round(float(mu_pred), 3),
|
| 228 |
-
# round(float(mu_pred2), 3),
|
| 229 |
-
# # round(float(log_score),3)
|
| 230 |
-
# # "Normal Score": round(float(normal_score), 3),
|
| 231 |
-
# )
|
| 232 |
-
|
| 233 |
-
# # ======== Gradio Interface ========
|
| 234 |
-
# interface = gr.Interface(
|
| 235 |
-
# fn=predict_score,
|
| 236 |
-
# inputs=[
|
| 237 |
-
# gr.Number(label="Latitude"),
|
| 238 |
-
# gr.Number(label="Longitude"),
|
| 239 |
-
# ],
|
| 240 |
-
# outputs=[
|
| 241 |
-
# gr.Number(label="Score (0 - 100)"),
|
| 242 |
-
# gr.Number(label="Number of Current Banks"),
|
| 243 |
-
# gr.Number(label="Number of Ideal Banks (Negative Binomial)"),
|
| 244 |
-
# gr.Number(label="Number of Ideal Banks (XGBoost)"),
|
| 245 |
-
# # gr.Number(label="Log Score Probability"),
|
| 246 |
-
# ],
|
| 247 |
-
# title="Bank Location Scoring Model",
|
| 248 |
-
# description="Enter latitude and longitude to get the predicted score, number of banks, and normalized score.",
|
| 249 |
-
# )
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
# interface.launch()
|
|
|
|
| 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(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()
|
| 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),
|
| 96 |
+
# input_dict["total_amenities"],
|
| 97 |
|
| 98 |
*[v for k,v in input_dict.items() if k[:3] == "num"]
|
| 99 |
|
|
|
|
| 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 |
|
| 117 |
+
# gr.Number(label="Total Amenities"),
|
| 118 |
|
| 119 |
gr.Number(label="Dining and Drinking"),
|
| 120 |
gr.Number(label="Community and Government"),
|
|
|
|
| 131 |
description="Enter latitude and longitude to get the predicted score, number of banks, and normalized score.",
|
| 132 |
)
|
| 133 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|