finalizare 1.0

This commit is contained in:
andrei-mihnea-cerbu
2024-05-21 12:10:53 +03:00
parent f7795f7519
commit 1cc1d34003
11268 changed files with 2102399 additions and 10909 deletions
@@ -1,55 +1,97 @@
using System.Diagnostics;
using System.Runtime.InteropServices;
using Newtonsoft.Json;
namespace Application.Endpoints.SicknessPrediction;
public class PythonScriptRunner
{
public async Task<BaseResponse> RunPythonScript(string scriptPath, string args)
public static async Task<BaseResponse> RunPythonScript(string scriptPath, string args)
{
var response = new BaseResponse();
using (Process process = new Process())
var solutionRoot = PathHelper.GetSolutionRoot();
var pythonExecutablePath = Path.Combine(solutionRoot, "venv", "Scripts", "python.exe");
using var process = new Process();
process.StartInfo = new ProcessStartInfo(pythonExecutablePath, $"\"{scriptPath}\" \"{args}\"")
{
process.StartInfo = new ProcessStartInfo("python", $"\"{scriptPath}\" \"{args}\"")
{
RedirectStandardOutput = true,
RedirectStandardError = true, // Redirect standard error to capture any errors.
UseShellExecute = false,
CreateNoWindow = true,
};
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true
};
try
{
process.Start();
// Read output and error streams.
var output = await process.StandardOutput.ReadToEndAsync();
var error = await process.StandardError.ReadToEndAsync();
process.WaitForExit();
try
{
process.Start();
var output = await process.StandardOutput.ReadToEndAsync();
var error = await process.StandardError.ReadToEndAsync();
await process.WaitForExitAsync();
if (process.ExitCode == 0) // Assuming exit code 0 as a success.
if (process.ExitCode == 0) // Assuming exit code 0 as a success.
try
{
Console.WriteLine(output);
response.StatusCode = HttpStatusCodes.OK;
response.Message = "Success";
response.Data = output;
var response = JsonConvert.DeserializeObject<BaseResponsePython>(output);
return new BaseResponse()
{
StatusCode = response.StatusCode,
Message = response.Message,
Data = response.Data
};
}
else
catch (JsonException ex)
{
Console.WriteLine(error);
response.StatusCode = HttpStatusCodes.InternalServerError;
response.Message = error; // Using the error output as the message if not successful.
response.Data = null;
Console.WriteLine($"JSON Error: {ex.Message}");
return new BaseResponse { StatusCode = 500, Message = "Error parsing JSON response." };
}
}
catch (Exception ex)
{
response.StatusCode = HttpStatusCodes.InternalServerError;
response.Message = $"An error occurred while executing the Python script: {ex.Message}";
response.Data = null;
}
return response;
Console.WriteLine(error);
return new BaseResponse { StatusCode = 500, Message = error };
}
catch (Exception ex)
{
Console.WriteLine($"Execution Error: {ex.Message}");
return new BaseResponse
{ StatusCode = 500, Message = $"An error occurred while executing the Python script: {ex.Message}" };
}
}
}
public static class PathHelper
{
public static string GetSolutionRoot()
{
var currentDir = Directory.GetCurrentDirectory();
var solutionRoot = Directory.GetParent(currentDir)?.FullName;
if (solutionRoot == null)
throw new InvalidOperationException("Failed to find the solution root directory.");
return solutionRoot;
}
public static string GetPythonExecutablePath()
{
var solutionRoot = GetSolutionRoot();
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
return Path.Combine(solutionRoot, "venv", "Scripts", "python.exe");
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux) || RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
return Path.Combine(solutionRoot, "venv", "bin", "python");
throw new InvalidOperationException("Unsupported operating system.");
}
}
public class BaseResponsePython
{
public int StatusCode { get; set; }
public string Message { get; set; } = string.Empty;
public List<PredictionData> Data { get; set; } = []; // Correct initialization and make it a property
}
public class PredictionData // Removed abstract if no inheritance is required
{
public string Disease { get; set; } = string.Empty;
public double Probability { get; set; }
}