pushpikaLiyanagama commited on
Commit
83074cb
·
verified ·
1 Parent(s): a7b659a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +110 -53
app.py CHANGED
@@ -1,6 +1,76 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import joblib
2
  import pandas as pd
3
- import gradio as gr
4
 
5
  # Load the scaler and models
6
  scaler = joblib.load("models/scaler.joblib")
@@ -8,61 +78,48 @@ models = {
8
  "processing": joblib.load("models/svm_model_processing.joblib"),
9
  "perception": joblib.load("models/svm_model_perception.joblib"),
10
  "input": joblib.load("models/svm_model_input.joblib"),
11
- "understanding": joblib.load("models/svm_model_understanding.joblib")
12
  }
13
 
14
- def predict(course_overview, reading_file, abstract_materiale, concrete_material, visual_materials,
15
- self_assessment, exercises_submit, quiz_submitted, playing, paused, unstarted, buffering):
16
- try:
17
- input_data = {
18
- "course overview": [course_overview],
19
- "reading file": [reading_file],
20
- "abstract materiale": [abstract_materiale],
21
- "concrete material": [concrete_material],
22
- "visual materials": [visual_materials],
23
- "self-assessment": [self_assessment],
24
- "exercises submit": [exercises_submit],
25
- "quiz submitted": [quiz_submitted],
26
- "playing": [playing],
27
- "paused": [paused],
28
- "unstarted": [unstarted],
29
- "buffering": [buffering]
30
- }
31
 
32
- input_df = pd.DataFrame(input_data)
33
- input_scaled = scaler.transform(input_df)
 
 
 
 
 
 
 
 
 
 
 
 
34
 
35
- predictions = {}
36
- for target, model in models.items():
37
- pred = model.predict(input_scaled)
38
- predictions[target] = pred[0] # Return as is, without converting to int
39
-
40
- return predictions
 
 
 
 
 
 
 
 
 
 
 
 
 
41
 
 
 
42
  except Exception as e:
43
- return {"error": str(e)}
44
-
45
- # Define Gradio interface using the latest syntax
46
- iface = gr.Interface(
47
- fn=predict,
48
- inputs=[
49
- gr.Number(label="Course Overview"),
50
- gr.Number(label="Reading File"),
51
- gr.Number(label="Abstract Materiale"),
52
- gr.Number(label="Concrete Material"),
53
- gr.Number(label="Visual Materials"),
54
- gr.Number(label="Self Assessment"),
55
- gr.Number(label="Exercises Submit"),
56
- gr.Number(label="Quiz Submitted"),
57
- gr.Number(label="Playing"),
58
- gr.Number(label="Paused"),
59
- gr.Number(label="Unstarted"),
60
- gr.Number(label="Buffering")
61
- ],
62
- outputs=gr.JSON(),
63
- title="SVM Multi-Target Prediction",
64
- description="Enter the feature values to get predictions for processing, perception, input, and understanding."
65
- )
66
-
67
- if __name__ == "__main__":
68
- iface.launch()
 
1
+ # import joblib
2
+ # import pandas as pd
3
+ # import gradio as gr
4
+
5
+ # # Load the scaler and models
6
+ # scaler = joblib.load("models/scaler.joblib")
7
+ # models = {
8
+ # "processing": joblib.load("models/svm_model_processing.joblib"),
9
+ # "perception": joblib.load("models/svm_model_perception.joblib"),
10
+ # "input": joblib.load("models/svm_model_input.joblib"),
11
+ # "understanding": joblib.load("models/svm_model_understanding.joblib")
12
+ # }
13
+
14
+ # def predict(course_overview, reading_file, abstract_materiale, concrete_material, visual_materials,
15
+ # self_assessment, exercises_submit, quiz_submitted, playing, paused, unstarted, buffering):
16
+ # try:
17
+ # input_data = {
18
+ # "course overview": [course_overview],
19
+ # "reading file": [reading_file],
20
+ # "abstract materiale": [abstract_materiale],
21
+ # "concrete material": [concrete_material],
22
+ # "visual materials": [visual_materials],
23
+ # "self-assessment": [self_assessment],
24
+ # "exercises submit": [exercises_submit],
25
+ # "quiz submitted": [quiz_submitted],
26
+ # "playing": [playing],
27
+ # "paused": [paused],
28
+ # "unstarted": [unstarted],
29
+ # "buffering": [buffering]
30
+ # }
31
+
32
+ # input_df = pd.DataFrame(input_data)
33
+ # input_scaled = scaler.transform(input_df)
34
+
35
+ # predictions = {}
36
+ # for target, model in models.items():
37
+ # pred = model.predict(input_scaled)
38
+ # predictions[target] = pred[0] # Return as is, without converting to int
39
+
40
+ # return predictions
41
+
42
+ # except Exception as e:
43
+ # return {"error": str(e)}
44
+
45
+ # # Define Gradio interface using the latest syntax
46
+ # iface = gr.Interface(
47
+ # fn=predict,
48
+ # inputs=[
49
+ # gr.Number(label="Course Overview"),
50
+ # gr.Number(label="Reading File"),
51
+ # gr.Number(label="Abstract Materiale"),
52
+ # gr.Number(label="Concrete Material"),
53
+ # gr.Number(label="Visual Materials"),
54
+ # gr.Number(label="Self Assessment"),
55
+ # gr.Number(label="Exercises Submit"),
56
+ # gr.Number(label="Quiz Submitted"),
57
+ # gr.Number(label="Playing"),
58
+ # gr.Number(label="Paused"),
59
+ # gr.Number(label="Unstarted"),
60
+ # gr.Number(label="Buffering")
61
+ # ],
62
+ # outputs=gr.JSON(),
63
+ # title="SVM Multi-Target Prediction",
64
+ # description="Enter the feature values to get predictions for processing, perception, input, and understanding."
65
+ # )
66
+
67
+ # if __name__ == "__main__":
68
+ # iface.launch()
69
+
70
+ from fastapi import FastAPI, HTTPException
71
+ from pydantic import BaseModel
72
  import joblib
73
  import pandas as pd
 
74
 
75
  # Load the scaler and models
76
  scaler = joblib.load("models/scaler.joblib")
 
78
  "processing": joblib.load("models/svm_model_processing.joblib"),
79
  "perception": joblib.load("models/svm_model_perception.joblib"),
80
  "input": joblib.load("models/svm_model_input.joblib"),
81
+ "understanding": joblib.load("models/svm_model_understanding.joblib"),
82
  }
83
 
84
+ # Initialize the FastAPI app
85
+ app = FastAPI(title="SVM Multi-Target Prediction API")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
86
 
87
+ # Define the input data model
88
+ class InputData(BaseModel):
89
+ course_overview: float
90
+ reading_file: float
91
+ abstract_materiale: float
92
+ concrete_material: float
93
+ visual_materials: float
94
+ self_assessment: float
95
+ exercises_submit: float
96
+ quiz_submitted: float
97
+ playing: float
98
+ paused: float
99
+ unstarted: float
100
+ buffering: float
101
 
102
+ # Define the prediction endpoint
103
+ @app.post("/predict")
104
+ def predict(input_data: InputData):
105
+ """
106
+ Predict target values based on input features using pre-trained SVM models.
107
+ """
108
+ try:
109
+ # Convert the input data to a DataFrame
110
+ input_df = pd.DataFrame([input_data.dict()])
111
+
112
+ # Scale the input data
113
+ input_scaled = scaler.transform(input_df)
114
+
115
+ # Generate predictions for each target
116
+ predictions = {
117
+ target: model.predict(input_scaled)[0]
118
+ for target, model in models.items()
119
+ }
120
+ return {"predictions": predictions}
121
 
122
+ except ValueError as ve:
123
+ raise HTTPException(status_code=400, detail=f"Input value error: {ve}")
124
  except Exception as e:
125
+ raise HTTPException(status_code=500, detail=f"Unexpected error: {e}")