Add error handling for the predictor

This commit is contained in:
2026-03-05 20:14:51 -08:00
parent 308076bfa6
commit 405872da39
2 changed files with 22 additions and 7 deletions
+11 -1
View File
@@ -46,6 +46,7 @@
<button @onclick="pull">@pullButtonText</button> <button @onclick="pull">@pullButtonText</button>
<button @onclick="train">@trainButtonText</button> <button @onclick="train">@trainButtonText</button>
<button @onclick="predict">@predictButtonText</button> <button @onclick="predict">@predictButtonText</button>
<span>@PredictError</span>
</div> </div>
</div> </div>
} }
@@ -124,9 +125,11 @@
string pullButtonText = "Pull Data"; string pullButtonText = "Pull Data";
string trainButtonText = "Train AI"; string trainButtonText = "Train AI";
string predictButtonText = "Predict AI"; string predictButtonText = "Predict AI";
string PredictError = "";
bool pullDebounce = true; bool pullDebounce = true;
async Task pull(){ async Task pull(){
PredictError = "";
if (pullDebounce){ if (pullDebounce){
pullDebounce = false; pullDebounce = false;
pullButtonText = "Do not refresh the page. The data is pulling."; pullButtonText = "Do not refresh the page. The data is pulling.";
@@ -146,6 +149,7 @@
bool trainDebounce = true; bool trainDebounce = true;
async Task train(){ async Task train(){
PredictError = "";
if (trainDebounce){ if (trainDebounce){
trainDebounce = false; trainDebounce = false;
trainButtonText = "Do not refresh the page. The AI is training."; trainButtonText = "Do not refresh the page. The AI is training.";
@@ -164,12 +168,18 @@
} }
async Task predict(){ async Task predict(){
PredictError = "";
predictButtonText = "Do not refresh the page. The AI is predicting"; predictButtonText = "Do not refresh the page. The AI is predicting";
await Task.Delay(1); await Task.Delay(1);
List<Task> threadpool = new List<Task>(); List<Task> threadpool = new List<Task>();
foreach(stockPredictionPair cur in predictions){ foreach(stockPredictionPair cur in predictions){
Task thread = new Task(() => { 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); Console.WriteLine("Received Signal [" + cur.Symbol + "] : " + cur.Movement);
}); });
thread.Start(); thread.Start();
+7 -2
View File
@@ -35,14 +35,19 @@ namespace PythonInterop {
} }
} }
public int PredictAI(string StockSymbol) { // Return ( Error, Signal )
public (string, int) PredictAI(string StockSymbol) {
try {
using (Py.GIL()) { using (Py.GIL()) {
dynamic predictor = Py.Import("ai-predictor"); dynamic predictor = Py.Import("ai-predictor");
using (dynamic x = predictor.Predict(StockSymbol)) { using (dynamic x = predictor.Predict(StockSymbol)) {
int result = (int)x; int result = (int)x;
return result; return ("", result);
} }
} }
} catch (Exception ex) {
return (ex.ToString(), 0);
}
} }
} }