jonathanjordan21 commited on
Commit
b820014
·
verified ·
1 Parent(s): d7ceb42

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +136 -0
app.py CHANGED
@@ -1,7 +1,143 @@
1
  from fastapi import FastAPI
 
 
 
 
2
 
3
  app = FastAPI()
4
 
5
  @app.get("/")
6
  def greet_json():
7
  return {"Hello": "World!"}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  from fastapi import FastAPI
2
+ from fastapi import Depends, FastAPI, HTTPException, status
3
+ from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
4
+
5
+ from pydantic import BaseModel
6
 
7
  app = FastAPI()
8
 
9
  @app.get("/")
10
  def greet_json():
11
  return {"Hello": "World!"}
12
+
13
+
14
+ import numpy as np
15
+ import pandas as pd
16
+
17
+ from utils import compute_features
18
+
19
+ import json
20
+
21
+ import joblib
22
+
23
+
24
+ # Define the same file path
25
+ filename = 'finalized_linear_model.joblib'
26
+
27
+ # Load the model from disk
28
+ loaded_model = joblib.load(filename)
29
+
30
+
31
+ REQUIRED_COLUMN_ORDER = [
32
+ 'num_hotel', 'num_attraction', 'num_restaurant',
33
+ 'num_convenience_store', 'num_pharmacy', 'num_cafe', 'num_bookstore',
34
+ 'num_school', 'num_co_working', 'num_clinic', 'num_bank',
35
+ 'num_supermarket', 'num_gym', 'num_fast_food', 'num_shopping_mall',
36
+ 'num_bakery', 'num_university', 'num_hospital', 'num_dentist',
37
+ 'num_clothing_store', 'num_department_store', 'num_college',
38
+ 'num_electronics_store', 'num_hostel', 'num_charging_station',
39
+ 'num_viewpoint', 'num_jewelry_store'
40
+ ]
41
+
42
+ # @app.post('/predict')
43
+ # def predict_score(lat, lon):
44
+
45
+ class Location(BaseModel):
46
+ lat: float
47
+ lon: float
48
+
49
+ @app.post("/predict")
50
+ async def predict_score(location: Location, request: Request):
51
+ lat, lon = location.lat, location.lon
52
+ auth_header = request.headers.get("Authorization")
53
+
54
+ api_key = auth_header.split(" ")[1] if auth_header else None
55
+
56
+ inputs = compute_features((lat,lon), api_key, 500)
57
+ print("[INPUTS]", inputs)
58
+ num_banks = inputs.pop("num_banks_in_radius", 0)
59
+
60
+ input_dict = inputs.copy()
61
+
62
+ input_dict_new = {}
63
+
64
+ for col in REQUIRED_COLUMN_ORDER:
65
+ input_dict_new[col] = input_dict[col]
66
+
67
+ mu_pred = loaded_model.predict([list(input_dict_new.values())])[0]
68
+
69
+ # r = 1/alpha
70
+ # p = r / (r + mu_pred)
71
+
72
+ # # Compute pmf and mode
73
+ # k_mode = int((r - 1) * (1 - p) / p) # mode of NB
74
+ # p_k = nbinom.pmf(num_banks, r, p)
75
+ # p_mode = nbinom.pmf(k_mode, r, p)
76
+
77
+ # # Score normalized 0–100
78
+ # score = (p_k / p_mode) * 100
79
+ # score = np.clip(score, 0, 100)
80
+
81
+ # diff = (num_banks - mu_pred) / (mu_pred + 1e-6)
82
+ # # score = (1 - np.tanh(diff))
83
+
84
+ # print("[TANH]", np.tanh(diff))
85
+
86
+ # diff = mu_pred2 - num_banks
87
+ # score = 100 / (1 + np.exp(-alpha * diff))
88
+
89
+ # score = np.abs(1 + np.tanh(diff)) / 2 * 100
90
+
91
+
92
+ # score = (1 * np.abs(mu_pred2 + 0.1)) * 100
93
+
94
+ # score = np.sigmoid(mu_pred2 - num_banks + 0.1) * 100
95
+
96
+ score = 100 / (1 + np.exp(num_banks - mu_pred))
97
+
98
+ input_dict_new.update({
99
+ "score":round(float(score), 3),
100
+ "num_atm":num_banks,
101
+ "num_prediction_atm":round(float(mu_pred), 3),
102
+ "lat":lat,
103
+ "lon":lon
104
+ })
105
+
106
+ return input_dict_new
107
+
108
+ # return (
109
+ # round(float(score), 3),
110
+ # num_banks,
111
+ # round(float(mu_pred), 3),
112
+ # # round(float(log_score),3)
113
+ # # "Normal Score": round(float(normal_score), 3),
114
+ # # input_dict["total_amenities"],
115
+
116
+ # *[v for k,v in input_dict_new.items() if k[:3] == "num"]
117
+
118
+ # )
119
+
120
+ # # ======== Gradio Interface ========
121
+ # interface = gr.Interface(
122
+ # fn=predict_score,
123
+ # inputs=[
124
+ # gr.Number(label="Latitude"),
125
+ # gr.Number(label="Longitude"),
126
+ # gr.Text(label="Google Api Key")
127
+ # ],
128
+ # outputs=[
129
+ # gr.Number(label="Score (0 - 100)"),
130
+ # gr.Number(label="Current ATMs"),
131
+ # gr.Number(label="Ideal ATMs"),
132
+ # # gr.Number(label="Log Score Probability"),
133
+
134
+ # # gr.Number(label="Total Amenities"),
135
+
136
+ # *[gr.Number(label=x) for x in REQUIRED_COLUMN_ORDER]
137
+ # ],
138
+ # title="Bank Location Scoring Model",
139
+ # description="Enter latitude and longitude to get the predicted score, number of banks, and normalized score.",
140
+ # )
141
+
142
+
143
+ # interface.launch()