pushpikaLiyanagama
commited on
Commit
•
7a2af10
1
Parent(s):
d9b78c0
Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,7 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
import joblib
|
3 |
import numpy as np
|
|
|
|
|
|
|
4 |
|
5 |
# Load the scaler and models
|
6 |
scaler = joblib.load('scaler.joblib')
|
@@ -12,30 +13,61 @@ models = {
|
|
12 |
}
|
13 |
|
14 |
# Define the prediction function
|
15 |
-
def predict(
|
16 |
-
|
17 |
-
|
|
|
|
|
|
|
18 |
|
19 |
-
|
20 |
-
|
|
|
|
|
|
|
21 |
|
22 |
-
#
|
|
|
|
|
|
|
23 |
predictions = {}
|
24 |
for target, model in models.items():
|
25 |
-
|
26 |
-
predictions[target] = prediction[0]
|
27 |
|
28 |
return predictions
|
29 |
|
30 |
-
# Define
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import numpy as np
|
2 |
+
import joblib
|
3 |
+
import json
|
4 |
+
from typing import List, Dict
|
5 |
|
6 |
# Load the scaler and models
|
7 |
scaler = joblib.load('scaler.joblib')
|
|
|
13 |
}
|
14 |
|
15 |
# Define the prediction function
|
16 |
+
def predict(features: List[float]) -> Dict[str, float]:
|
17 |
+
"""
|
18 |
+
Predict outcomes for all target variables based on input features.
|
19 |
+
|
20 |
+
Args:
|
21 |
+
features (List[float]): A list of 12 numeric features in the correct order.
|
22 |
|
23 |
+
Returns:
|
24 |
+
Dict[str, float]: A dictionary with predictions for each target variable.
|
25 |
+
"""
|
26 |
+
# Ensure the input is a NumPy array
|
27 |
+
input_array = np.array(features).reshape(1, -1)
|
28 |
|
29 |
+
# Scale the input
|
30 |
+
scaled_input = scaler.transform(input_array)
|
31 |
+
|
32 |
+
# Predict outcomes
|
33 |
predictions = {}
|
34 |
for target, model in models.items():
|
35 |
+
predictions[target] = model.predict(scaled_input)[0] # Get single prediction
|
|
|
36 |
|
37 |
return predictions
|
38 |
|
39 |
+
# Define a callable class for Hugging Face
|
40 |
+
class Model:
|
41 |
+
def __init__(self):
|
42 |
+
self.scaler = scaler
|
43 |
+
self.models = models
|
44 |
+
|
45 |
+
def __call__(self, inputs: List[List[float]]) -> List[Dict[str, float]]:
|
46 |
+
"""
|
47 |
+
Hugging Face expects the model to handle a batch of inputs.
|
48 |
+
|
49 |
+
Args:
|
50 |
+
inputs (List[List[float]]): A batch of feature vectors.
|
51 |
+
|
52 |
+
Returns:
|
53 |
+
List[Dict[str, float]]: A list of predictions for each input.
|
54 |
+
"""
|
55 |
+
outputs = []
|
56 |
+
for features in inputs:
|
57 |
+
predictions = predict(features)
|
58 |
+
outputs.append(predictions)
|
59 |
+
return outputs
|
60 |
+
|
61 |
+
|
62 |
+
# Instantiate the model
|
63 |
+
model = Model()
|
64 |
+
|
65 |
+
# Hugging Face Inference API expects `model` to be callable
|
66 |
+
if __name__ == "__main__":
|
67 |
+
# For local testing or debugging
|
68 |
+
test_input = [
|
69 |
+
[0.5, 1.0, 0.0, 1.0, 0.5, 0.0, 1.0, 0.5, 1.0, 0.0, 0.0, 0.5] # Example input
|
70 |
+
]
|
71 |
+
output = model(test_input)
|
72 |
+
print(json.dumps(output, indent=4))
|
73 |
+
|