diff --git a/user_session_data/Adam_Drake/fig.html b/media/Adam_Drake/fig.html similarity index 100% rename from user_session_data/Adam_Drake/fig.html rename to media/Adam_Drake/fig.html diff --git a/prediction_service/__pycache__/views.cpython-310.pyc b/prediction_service/__pycache__/views.cpython-310.pyc index f0175546cd6781327d8e94b0c0bfd5957f696352..437076524b6908ca7b4458e7e053daadbcbc497f 100644 Binary files a/prediction_service/__pycache__/views.cpython-310.pyc and b/prediction_service/__pycache__/views.cpython-310.pyc differ diff --git a/prediction_service/templates/prediction_service/model.html b/prediction_service/templates/prediction_service/model.html index 53244124d5bb31cb0a69644613fea25b93542a80..28dad7b7ab1e8cb7a239035ca4d220bd70465b39 100644 --- a/prediction_service/templates/prediction_service/model.html +++ b/prediction_service/templates/prediction_service/model.html @@ -7,7 +7,7 @@ <h1>ML-Model</h1> - {{ html_content | safe }} + <img src="data:image/png;base64,{{ img_str }}" alt="PIL Image"> </div> diff --git a/prediction_service/views.py b/prediction_service/views.py index 7805c687915c7128286bfde454101ba4902e2679..9baf72e1c7d0ea85345480d34902482d5452247b 100644 --- a/prediction_service/views.py +++ b/prediction_service/views.py @@ -46,26 +46,57 @@ def register(request): return render(request, 'prediction_service/register.html', {'form': form}) import os +import io import numpy as np import matplotlib.pyplot as plt import mpld3 +from PIL import Image +import base64 @login_required def mlmodel(request): - # re generating the file is too slow for the display - # x = np.arange(0, 2*np.pi, 0.1) - # y = np.sin(x) - # fig, ax = plt.subplots() - # ax.plot(x, y) - # ax.set_xlabel('x') - # ax.set_ylabel('y') - # ax.set_title('Sinusoid') - - path = "user_session_data/Adam_Drake/fig.html" - # mpld3.save_html(fig, path) - - with open(path, 'r') as file: - html_content = file.read() - - return render(request, "prediction_service/model.html", {'html_content': html_content}) + matrix = np.asarray([[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], + [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], + [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], + [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,67,232,39,0,0], + [0,0,0,0,62,81,0,0,0,0,0,0,0,0,0,0,0,0,0,0,120,180,39,0,0], + [0,0,0,0,126,163,0,0,0,0,0,0,0,0,0,0,0,0,0,2,153,210,40,0,0], + [0,0,0,0,220,163,0,0,0,0,0,0,0,0,0,0,0,0,0,27,254,162,0,0,0], + [0,0,0,0,222,163,0,0,0,0,0,0,0,0,0,0,0,0,0,183,254,125,0,0,0], + [0,0,0,0,46,245,0,0,0,0,0,0,0,0,0,0,0,0,0,198,254,56,0,0,0], + [0,0,0,0,120,254,0,0,0,0,0,0,0,0,0,0,0,0,23,231,254,29,0,0,0], + [0,0,0,0,159,254,0,0,0,0,0,0,0,0,0,0,0,0,163,254,216,16,0,0,0], + [0,0,0,0,159,254,0,0,0,0,0,0,0,0,0,14,86,178,248,254,91,0,0,0,0], + [0,0,0,0,159,254,35,0,0,47,49,116,144,150,241,243,234,179,241,252,40,0,0,0,0], + [0,0,0,0,150,253,237,207,207,207,253,254,250,240,198,143,91,28,233,250,0,0,0,0,0], + [0,0,0,0,119,177,177,177,177,177,98,56,0,0,0,0,0,102,254,220,0,0,0,0,0], + [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,169,254,137,0,0,0,0,0], + [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,169,254,57,0,0,0,0,0], + [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,169,254,57,0,0,0,0,0], + [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,169,255,94,0,0,0,0,0], + [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,169,254,96,0,0,0,0,0], + [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,169,254,153,0,0,0,0,0], + [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,169,255,153,0,0,0,0,0], + [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,96,254,153,0,0,0,0,0], + [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], + [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]]) + + # Matrix dimensions + raw_height = matrix.shape[0] + raw_width = matrix.shape[1] + + fig, ax = plt.subplots(1, 1, figsize=(4, 4)) + ax.imshow(matrix, cmap='Greys_r') + + buf = io.BytesIO() + fig.savefig(buf, format='png') + buf.seek(0) + img = Image.open(buf) + + # Convert PIL image to base64 string + buffered = io.BytesIO() + img.save(buffered, format='PNG') + img_str = base64.b64encode(buffered.getvalue()).decode('utf-8') + + return render(request, "prediction_service/model.html", {'img_str': img_str})