jonathanjordan21 commited on
Commit
69d9c01
·
verified ·
1 Parent(s): 6a481a3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -11
app.py CHANGED
@@ -1,19 +1,35 @@
1
  import gradio as gr
2
  import torch
3
  import numpy as np
 
4
 
5
- # ======== Example Dummy Model ========
6
- # Replace this with your own model loading
7
- class DummyModel(torch.nn.Module):
8
- def __init__(self):
 
9
  super().__init__()
10
- self.linear = torch.nn.Linear(2, 3) # 2 inputs (lat, lon) → 3 outputs
 
11
 
12
  def forward(self, x):
13
- return self.linear(x)
 
 
 
14
 
15
- # load your trained model (replace with torch.load if you have .pt file)
16
- model = DummyModel()
 
 
 
 
 
 
 
 
 
 
17
  model.eval()
18
 
19
  # ======== Prediction Function ========
@@ -26,13 +42,15 @@ def predict_score(lat, lon):
26
  outputs = model(inputs).numpy().flatten()
27
 
28
  # Unpack into respective values
29
- score, num_banks, normal_score = outputs
 
 
30
 
31
  # You can apply any post-processing here
32
  return {
33
  "Score": round(float(score), 3),
34
- "Num Banks": round(float(num_banks), 3),
35
- "Normal Score": round(float(normal_score), 3),
36
  }
37
 
38
  # ======== Gradio Interface ========
 
1
  import gradio as gr
2
  import torch
3
  import numpy as np
4
+ import pandas as pd
5
 
6
+ dataset = pd.read_csv("cleaned_df_noindex.csv")
7
+ df_amenities = pd.read_csv("df_amenities.csv")
8
+
9
+ class NegBinomialModel(nn.Module):
10
+ def __init__(self, in_features):
11
  super().__init__()
12
+ self.linear = nn.Linear(in_features, 1)
13
+ self.log_alpha = nn.Parameter(torch.tensor(0.0))
14
 
15
  def forward(self, x):
16
+ eta = self.linear(x)
17
+ mu = torch.exp(eta).squeeze(-1)
18
+ alpha = torch.exp(self.log_alpha)
19
+ return mu, alpha
20
 
21
+ def negbinom_loss(y, mu, alpha):
22
+ log_prob = (
23
+ torch.lgamma(y + 1/alpha)
24
+ - torch.lgamma(1/alpha)
25
+ - torch.lgamma(y + 1)
26
+ + (1/alpha) * torch.log(1 / (1 + alpha * mu))
27
+ + y * torch.log((alpha * mu) / (1 + alpha * mu))
28
+ )
29
+ return -torch.mean(log_prob)
30
+
31
+ model = NegBinomialModel(len(dataset.columns)-2)
32
+ model.load_state_dict(torch.load("model_weights.pt"))
33
  model.eval()
34
 
35
  # ======== Prediction Function ========
 
42
  outputs = model(inputs).numpy().flatten()
43
 
44
  # Unpack into respective values
45
+ mu_pred, alpha = outputs
46
+
47
+ score = (1 * np.abs(mu_pred + 0.1)) * 100
48
 
49
  # You can apply any post-processing here
50
  return {
51
  "Score": round(float(score), 3),
52
+ "Num Banks": round(float(mu_pred), 3),
53
+ # "Normal Score": round(float(normal_score), 3),
54
  }
55
 
56
  # ======== Gradio Interface ========