pushpikaLiyanagama commited on
Commit
7a2af10
1 Parent(s): d9b78c0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -22
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(user_input):
16
- # Ensure the input is in the same order as your model expects
17
- user_input_array = np.array(user_input).reshape(1, -1)
 
 
 
18
 
19
- # Scale the input using the saved scaler
20
- user_input_scaled = scaler.transform(user_input_array)
 
 
 
21
 
22
- # Predict outcomes for all target variables
 
 
 
23
  predictions = {}
24
  for target, model in models.items():
25
- prediction = model.predict(user_input_scaled)
26
- predictions[target] = prediction[0]
27
 
28
  return predictions
29
 
30
- # Define Gradio interface
31
- interface = gr.Interface(fn=predict,
32
- inputs=gr.Dataframe(type="numpy", row_count=1, col_count=12,
33
- headers=['course overview', 'reading file', 'abstract materiale',
34
- 'concrete material', 'visual materials', 'self-assessment',
35
- 'exercises submit', 'quiz submitted', 'playing', 'paused',
36
- 'unstarted', 'buffering']),
37
- outputs=gr.JSON(),
38
- live=True)
39
-
40
- # Launch the interface
41
- interface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+