Update app.py
Browse files
app.py
CHANGED
|
@@ -1,61 +1,36 @@
|
|
| 1 |
-
import
|
| 2 |
-
import joblib
|
| 3 |
-
import
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
models
|
| 8 |
-
|
| 9 |
-
"
|
| 10 |
-
"
|
| 11 |
-
"
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
predictions
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
'Concrete Material', 'Visual Materials', 'Self-Assessment',
|
| 38 |
-
'Exercises Submit', 'Quiz Submitted', 'Playing', 'Paused',
|
| 39 |
-
'Unstarted', 'Buffering'
|
| 40 |
-
]
|
| 41 |
-
|
| 42 |
-
user_input = []
|
| 43 |
-
for col in columns:
|
| 44 |
-
value = st.number_input(f"{col}", value=0.0)
|
| 45 |
-
user_input.append(value)
|
| 46 |
-
|
| 47 |
-
# Button for making predictions
|
| 48 |
-
if st.button("Predict"):
|
| 49 |
-
# Ensure proper input and predict
|
| 50 |
-
try:
|
| 51 |
-
predictions = predict(user_input)
|
| 52 |
-
st.subheader("Predictions")
|
| 53 |
-
st.json(predictions)
|
| 54 |
-
except Exception as e:
|
| 55 |
-
st.error(f"An error occurred: {e}")
|
| 56 |
-
|
| 57 |
-
# Share instructions for deployment
|
| 58 |
-
st.markdown("""
|
| 59 |
-
- To run the app, execute `streamlit run app.py` in your terminal.
|
| 60 |
-
- Make sure the `scaler.joblib` and model files are in the same directory as this script.
|
| 61 |
-
""")
|
|
|
|
| 1 |
+
from flask import Flask, request, jsonify
|
| 2 |
+
import joblib
|
| 3 |
+
import pandas as pd
|
| 4 |
+
|
| 5 |
+
app = Flask(__name__)
|
| 6 |
+
|
| 7 |
+
# Load models and scaler
|
| 8 |
+
models = {
|
| 9 |
+
"processing": joblib.load("svm_model_processing.joblib"),
|
| 10 |
+
"perception": joblib.load("svm_model_perception.joblib"),
|
| 11 |
+
"input": joblib.load("svm_model_input.joblib"),
|
| 12 |
+
"understanding": joblib.load("svm_model_understanding.joblib"),
|
| 13 |
+
}
|
| 14 |
+
scaler = joblib.load("scaler.joblib")
|
| 15 |
+
|
| 16 |
+
@app.route("/predict", methods=["POST"])
|
| 17 |
+
def predict():
|
| 18 |
+
try:
|
| 19 |
+
# Parse input data from JSON
|
| 20 |
+
input_data = request.json
|
| 21 |
+
df = pd.DataFrame([input_data])
|
| 22 |
+
|
| 23 |
+
# Scale the data
|
| 24 |
+
df_scaled = scaler.transform(df)
|
| 25 |
+
|
| 26 |
+
# Make predictions for all target variables
|
| 27 |
+
predictions = {}
|
| 28 |
+
for target, model in models.items():
|
| 29 |
+
predictions[target] = model.predict(df_scaled)[0]
|
| 30 |
+
|
| 31 |
+
return jsonify({"success": True, "predictions": predictions})
|
| 32 |
+
except Exception as e:
|
| 33 |
+
return jsonify({"success": False, "error": str(e)})
|
| 34 |
+
|
| 35 |
+
if __name__ == "__main__":
|
| 36 |
+
app.run(host="0.0.0.0", port=8000)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|