From 405872da39711fd1a98641eff9a6aa80b3ac992f Mon Sep 17 00:00:00 2001 From: Derek Holloway Date: Thu, 5 Mar 2026 20:14:51 -0800 Subject: [PATCH] Add error handling for the predictor --- WebServer/Components/Pages/Home.razor | 12 +++++++++++- WebServer/Controllers/PythonInterop.cs | 17 +++++++++++------ 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/WebServer/Components/Pages/Home.razor b/WebServer/Components/Pages/Home.razor index bf19c33f..46c594cf 100644 --- a/WebServer/Components/Pages/Home.razor +++ b/WebServer/Components/Pages/Home.razor @@ -46,6 +46,7 @@ + @PredictError } @@ -124,9 +125,11 @@ string pullButtonText = "Pull Data"; string trainButtonText = "Train AI"; string predictButtonText = "Predict AI"; + string PredictError = ""; bool pullDebounce = true; async Task pull(){ + PredictError = ""; if (pullDebounce){ pullDebounce = false; pullButtonText = "Do not refresh the page. The data is pulling."; @@ -146,6 +149,7 @@ bool trainDebounce = true; async Task train(){ + PredictError = ""; if (trainDebounce){ trainDebounce = false; trainButtonText = "Do not refresh the page. The AI is training."; @@ -164,12 +168,18 @@ } async Task predict(){ + PredictError = ""; predictButtonText = "Do not refresh the page. The AI is predicting"; await Task.Delay(1); List threadpool = new List(); foreach(stockPredictionPair cur in predictions){ Task thread = new Task(() => { - cur.Movement = aiModule.PredictAI(cur.Symbol); + (string, int)Result = aiModule.PredictAI(cur.Symbol); + if (string.IsNullOrEmpty(Result.Item1)){ + cur.Movement = Result.Item2; + }else{ + PredictError = Result.Item1; + } Console.WriteLine("Received Signal [" + cur.Symbol + "] : " + cur.Movement); }); thread.Start(); diff --git a/WebServer/Controllers/PythonInterop.cs b/WebServer/Controllers/PythonInterop.cs index 406ccd49..022f51b1 100644 --- a/WebServer/Controllers/PythonInterop.cs +++ b/WebServer/Controllers/PythonInterop.cs @@ -35,13 +35,18 @@ namespace PythonInterop { } } - public int PredictAI(string StockSymbol) { - using (Py.GIL()) { - dynamic predictor = Py.Import("ai-predictor"); - using (dynamic x = predictor.Predict(StockSymbol)) { - int result = (int)x; - return result; + // Return ( Error, Signal ) + public (string, int) PredictAI(string StockSymbol) { + try { + using (Py.GIL()) { + dynamic predictor = Py.Import("ai-predictor"); + using (dynamic x = predictor.Predict(StockSymbol)) { + int result = (int)x; + return ("", result); + } } + } catch (Exception ex) { + return (ex.ToString(), 0); } }