From 26c995877ef19997877295cc8e1777ea170bc551 Mon Sep 17 00:00:00 2001 From: Derek Holloway Date: Tue, 17 Feb 2026 21:14:20 -0800 Subject: [PATCH] Allow pull data from python all the way through to the UI --- WebServer/AIPython/ai-predictor.py | 6 ++++-- WebServer/Components/Pages/Home.razor | 13 ++++++++++--- WebServer/Controllers/PythonInterop.cs | 8 ++++++-- 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/WebServer/AIPython/ai-predictor.py b/WebServer/AIPython/ai-predictor.py index de9e27fb..df229552 100644 --- a/WebServer/AIPython/ai-predictor.py +++ b/WebServer/AIPython/ai-predictor.py @@ -1,4 +1,6 @@ import os +import json +import numpy as np os.environ["CUDA_VISIBLE_DEVICES"] = "-1" import yfinance as yf import features @@ -33,8 +35,8 @@ def Predict(): predictions = reconstructed_model.predict(df) # 'predictions' will be a 2D array, flatten it if you want a simple list - flat_predictions = predictions.flatten() + flat_predictions = predictions.flatten().tolist() print(f"Predicted Target_Close_Tomorrow: {flat_predictions}") - return flat_predictions \ No newline at end of file + return json.dumps(flat_predictions) \ No newline at end of file diff --git a/WebServer/Components/Pages/Home.razor b/WebServer/Components/Pages/Home.razor index d12dc1d1..801b9559 100644 --- a/WebServer/Components/Pages/Home.razor +++ b/WebServer/Components/Pages/Home.razor @@ -21,6 +21,11 @@
+ + @foreach(double cur in predictions){ +

@cur


+ } +
@@ -29,6 +34,8 @@ string button1Text = "Train AI"; string button2Text = "Predict AI"; + double[] predictions = {}; + async Task pullandtrain(){ button1Text = "Do not refresh the page. The data is refreshing."; await Task.Delay(1); @@ -37,10 +44,10 @@ } async Task predict(){ - button1Text = "Do not refresh the page. The data is refreshing."; + button2Text = "Do not refresh the page. The data is refreshing."; await Task.Delay(1); - aiModule.TrainAI(); - button1Text = "Refresh Completed"; + predictions = aiModule.PredictAI(); + button2Text = "Refresh Completed"; } } \ No newline at end of file diff --git a/WebServer/Controllers/PythonInterop.cs b/WebServer/Controllers/PythonInterop.cs index 10b82c0b..c9c080ef 100644 --- a/WebServer/Controllers/PythonInterop.cs +++ b/WebServer/Controllers/PythonInterop.cs @@ -1,4 +1,5 @@ using Python.Runtime; +using System.Text.Json; namespace PythonInterop { @@ -29,10 +30,13 @@ namespace PythonInterop { } } - public void PredictAI() { + public double[] PredictAI() { using (Py.GIL()) { dynamic main = Py.Import("ai-predictor"); - dynamic result = main.Predict(); + string result = main.Predict(); + double[]? predictions = JsonSerializer.Deserialize(result); + double[] nullCasted = predictions != null ? predictions : []; + return nullCasted; } }