Update app.py
Browse files
app.py
CHANGED
|
@@ -1,73 +1,36 @@
|
|
|
|
|
|
|
|
| 1 |
import numpy as np
|
| 2 |
import joblib
|
| 3 |
-
import json
|
| 4 |
-
from typing import List, Dict
|
| 5 |
|
| 6 |
-
|
| 7 |
-
scaler = joblib.load('scaler.joblib')
|
| 8 |
models = {
|
| 9 |
-
"processing": joblib.load(
|
| 10 |
-
"perception": joblib.load(
|
| 11 |
-
"input": joblib.load(
|
| 12 |
-
"understanding": joblib.load(
|
| 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 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
Returns:
|
| 53 |
-
List[Dict[str, float]]: A list of predictions for each input.
|
| 54 |
-
"""
|
| 55 |
outputs = []
|
| 56 |
for features in inputs:
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
|
|
|
| 1 |
+
from typing import List, Dict
|
| 2 |
+
import pandas as pd
|
| 3 |
import numpy as np
|
| 4 |
import joblib
|
|
|
|
|
|
|
| 5 |
|
| 6 |
+
scaler = joblib.load("scaler.joblib")
|
|
|
|
| 7 |
models = {
|
| 8 |
+
"processing": joblib.load("svm_model_processing.joblib"),
|
| 9 |
+
"perception": joblib.load("svm_model_perception.joblib"),
|
| 10 |
+
"input": joblib.load("svm_model_input.joblib"),
|
| 11 |
+
"understanding": joblib.load("svm_model_understanding.joblib"),
|
| 12 |
}
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
class Model:
|
| 15 |
def __init__(self):
|
| 16 |
self.scaler = scaler
|
| 17 |
self.models = models
|
| 18 |
|
| 19 |
def __call__(self, inputs: List[List[float]]) -> List[Dict[str, float]]:
|
| 20 |
+
feature_names = [
|
| 21 |
+
"course overview", "reading file", "abstract materiale",
|
| 22 |
+
"concrete material", "visual materials", "self-assessment",
|
| 23 |
+
"exercises submit", "quiz submitted", "playing", "paused",
|
| 24 |
+
"unstarted", "buffering"
|
| 25 |
+
]
|
|
|
|
|
|
|
|
|
|
| 26 |
outputs = []
|
| 27 |
for features in inputs:
|
| 28 |
+
input_df = pd.DataFrame([features], columns=feature_names)
|
| 29 |
+
scaled_input = self.scaler.transform(input_df)
|
| 30 |
+
predictions = {}
|
| 31 |
+
for target, model in self.models.items():
|
| 32 |
+
predictions[target] = model.predict(scaled_input)[0]
|
| 33 |
outputs.append(predictions)
|
| 34 |
return outputs
|
| 35 |
|
|
|
|
|
|
|
| 36 |
model = Model()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|