| import gradio as gr |
|
|
| import numpy as np |
| import pandas as pd |
|
|
| from utils import compute_features |
|
|
| import json |
|
|
| import joblib |
|
|
|
|
| |
| filename = 'finalized_linear_model.joblib' |
|
|
| |
| loaded_model = joblib.load(filename) |
|
|
|
|
| REQUIRED_COLUMN_ORDER = [ |
| 'num_hotel', 'num_attraction', 'num_restaurant', |
| 'num_convenience_store', 'num_pharmacy', 'num_cafe', 'num_bookstore', |
| 'num_school', 'num_co_working', 'num_clinic', 'num_bank', |
| 'num_supermarket', 'num_gym', 'num_fast_food', 'num_shopping_mall', |
| 'num_bakery', 'num_university', 'num_hospital', 'num_dentist', |
| 'num_clothing_store', 'num_department_store', 'num_college', |
| 'num_electronics_store', 'num_hostel', 'num_charging_station', |
| 'num_viewpoint', 'num_jewelry_store' |
| ] |
|
|
| def atm_score(num_atm, num_prediction_atm, k=7, pct=0.15): |
| |
| |
|
|
| |
| eps = 1e-8 |
| |
| num_prediction_atm = num_prediction_atm*(1 + pct) |
|
|
| |
| delta = (num_prediction_atm - num_atm) / (num_atm + eps) |
|
|
| score = 100.0 / (1.0 + np.exp(-k * (delta))) |
| |
| return score |
|
|
|
|
| def predict_score(lat, lon, api_key): |
| |
| |
| |
| inputs = compute_features((lat,lon), api_key, 500) |
| print("[INPUTS]", inputs) |
| num_banks = inputs.pop("num_banks_in_radius", 0) |
|
|
| input_dict = inputs.copy() |
|
|
| input_dict_new = {} |
|
|
| for col in REQUIRED_COLUMN_ORDER: |
| input_dict_new[col] = input_dict[col] |
|
|
| mu_pred = loaded_model.predict([list(input_dict_new.values())])[0] |
|
|
| |
| |
|
|
| |
| |
| |
| |
|
|
| |
| |
| |
|
|
| |
| |
|
|
| |
|
|
| |
| |
|
|
| |
|
|
|
|
| |
|
|
| |
|
|
| |
| score = atm_score(num_banks, mu_pred) |
|
|
| |
| return ( |
| round(float(score), 3), |
| num_banks, |
| round(float(mu_pred), 3), |
| |
| |
| |
|
|
| *[v for k,v in input_dict_new.items() if k[:3] == "num"] |
|
|
| ) |
|
|
| |
| interface = gr.Interface( |
| fn=predict_score, |
| inputs=[ |
| gr.Number(label="Latitude"), |
| gr.Number(label="Longitude"), |
| gr.Text(label="Google Api Key") |
| ], |
| outputs=[ |
| gr.Number(label="Score (0 - 100)"), |
| gr.Number(label="Current ATMs"), |
| gr.Number(label="Ideal ATMs"), |
| |
|
|
| |
|
|
| *[gr.Number(label=x) for x in REQUIRED_COLUMN_ORDER] |
| ], |
| title="Bank Location Scoring Model", |
| description="Enter latitude and longitude to get the predicted score, number of banks, and normalized score.", |
| ) |
|
|
|
|
| interface.launch() |
|
|