65 lines
2.2 KiB
C#
65 lines
2.2 KiB
C#
using Python.Runtime;
|
|
|
|
namespace Controllers.PythonInterop {
|
|
|
|
public class AIModule {
|
|
|
|
public AIModule(string PythonPathBase = "/usr/local/", string PythonVersion = "python3.11") {
|
|
// Use the user provided python runner
|
|
Runtime.PythonDLL = PythonPathBase + $"lib/lib{PythonVersion}.so";
|
|
|
|
// Use our local environment for the python libraries
|
|
PythonEngine.PythonHome = PythonPathBase;
|
|
|
|
// Include all the paths for python packages most importantly our venv
|
|
PythonEngine.PythonPath = $"{PythonPathBase}lib/{PythonVersion}:{PythonPathBase}lib/{PythonVersion}/lib-dynload:{PythonPathBase}lib/{PythonVersion}/site-packages:{Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "AIPython")}";
|
|
|
|
// Initiilize python
|
|
PythonEngine.Initialize();
|
|
|
|
// Needed because C# calls the python from each connections worker thread
|
|
PythonEngine.BeginAllowThreads();
|
|
}
|
|
|
|
public void PullAI() {
|
|
using (Py.GIL()) {
|
|
dynamic datapuller = Py.Import("datapuller");
|
|
using (datapuller.pull()){ }
|
|
}
|
|
}
|
|
|
|
public void TrainAI() {
|
|
using (Py.GIL()) {
|
|
dynamic trainer = Py.Import("ai-trainer");
|
|
using (trainer.TrainAI()){ }
|
|
}
|
|
}
|
|
|
|
// 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);
|
|
}
|
|
}
|
|
|
|
public float GetCurrentPrice(string StockSymbol) {
|
|
using (Py.GIL()) {
|
|
dynamic price = Py.Import("currentprice");
|
|
using (dynamic x = price.getCurrentPrice(StockSymbol)) {
|
|
float CurrentPrice = (float)x;
|
|
return x;
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
} |