Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
L
LearnJoy-ML
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
TMP-2023-24-059
LearnJoy-ML
Commits
e12df198
Commit
e12df198
authored
Mar 18, 2024
by
Bandara K.M.A.P.
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Upload function 4 model
parent
bf57cadd
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
84 additions
and
0 deletions
+84
-0
API.py
API.py
+84
-0
No files found.
API.py
0 → 100644
View file @
e12df198
from
flask
import
Flask
,
request
,
jsonify
import
numpy
as
np
import
joblib
app
=
Flask
(
__name__
)
time_model_card
=
joblib
.
load
(
'time_model_card.pkl'
)
score_model_card
=
joblib
.
load
(
'score_model_card.pkl'
)
# Endpoint to predict score and time for the next 3 months
@
app
.
route
(
'/predict'
,
methods
=
[
'POST'
])
def
predict
():
data
=
request
.
get_json
()
score_data
=
data
[
'score_data'
]
time_data
=
data
[
'time_data'
]
# Convert lists to arrays
new_score_array
=
np
.
array
([
score_data
])
new_time_array
=
np
.
array
([
time_data
])
# Predict using the loaded models
new_time_predictions
=
time_model_card
.
predict
(
new_time_array
)
new_score_predictions
=
score_model_card
.
predict
(
new_score_array
)
# Prepare output
output
=
{
'predicted_score'
:
new_score_predictions
.
tolist
(),
'predicted_time'
:
new_time_predictions
.
tolist
()
}
return
jsonify
(
output
)
# Endpoint to predict score and time for the next 3 months with steps
@
app
.
route
(
'/predict_with_steps'
,
methods
=
[
'POST'
])
def
predict_with_steps
():
data
=
request
.
get_json
()
score_data
=
data
[
'score_data'
]
time_data
=
data
[
'time_data'
]
num_steps
=
3
# Initialize lists
x_new_score
=
[]
x_new_time
=
[]
x_new_2_score
=
score_data
.
copy
()
x_new_2_time
=
time_data
.
copy
()
score_predictions
=
[]
time_predictions
=
[]
for
_
in
range
(
num_steps
):
if
len
(
x_new_score
)
>
0
:
x_new_2_score
=
x_new_score
x_new_2_time
=
x_new_time
# Predict score
y_pred_score
=
score_model_card
.
predict
(
np
.
array
([
x_new_2_score
]))
score_predictions
.
append
(
y_pred_score
.
tolist
())
# Update score data
temp_score
=
np
.
where
(
y_pred_score
>
x_new_2_score
[
-
1
],
2
,
np
.
where
(
y_pred_score
==
x_new_2_score
[
-
1
],
1
,
0
))
x_new_2_score
=
np
.
append
(
x_new_2_score
[
1
:
3
],
x_new_2_score
[
3
:])
x_new_2_score
[
2
]
=
temp_score
x_new_2_score
=
np
.
append
(
x_new_2_score
,
y_pred_score
,
axis
=
0
)
# Predict time
y_pred_time
=
time_model_card
.
predict
(
np
.
array
([
x_new_2_time
]))
time_predictions
.
append
(
y_pred_time
.
tolist
())
# Update time data
temp_time
=
np
.
where
(
y_pred_time
>
x_new_2_time
[
-
1
],
2
,
np
.
where
(
y_pred_time
==
x_new_2_time
[
-
1
],
1
,
0
))
x_new_2_time
=
np
.
append
(
x_new_2_time
[
1
:
3
],
x_new_2_time
[
3
:])
x_new_2_time
[
2
]
=
temp_time
x_new_2_time
=
np
.
append
(
x_new_2_time
,
y_pred_time
,
axis
=
0
)
# Prepare output
output
=
{
'score_predictions'
:
score_predictions
,
'time_predictions'
:
time_predictions
}
return
jsonify
(
output
)
if
__name__
==
'__main__'
:
app
.
run
(
debug
=
True
)
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment