pushpikaLiyanagama commited on
Commit
eb4b258
1 Parent(s): 8e7894e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -27
app.py CHANGED
@@ -1,36 +1,41 @@
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)
 
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')
 
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
+ # 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(share=True)