fixed inference bugs

add DONE during inference, correct handling on C# side
This commit is contained in:
Alex Bezdieniezhnykh
2025-02-01 02:09:11 +02:00
parent e7afa96a0b
commit 739759628a
23 changed files with 324 additions and 95 deletions
@@ -1,4 +1,5 @@
using System.Text;
using System.Diagnostics;
using System.Text;
using Azaion.CommonSecurity.DTO;
using Azaion.CommonSecurity.DTO.Commands;
using MessagePack;
@@ -9,7 +10,7 @@ namespace Azaion.CommonSecurity.Services;
public interface IResourceLoader
{
Task<MemoryStream> LoadFile(string fileName, CancellationToken ct = default);
MemoryStream LoadFileFromPython(string fileName);
}
public interface IAuthProvider
@@ -20,27 +21,64 @@ public interface IAuthProvider
public class PythonResourceLoader : IResourceLoader, IAuthProvider
{
private readonly ApiCredentials _credentials;
private readonly AzaionApiClient _api;
private readonly DealerSocket _dealer = new();
private readonly Guid _clientId = Guid.NewGuid();
public User CurrentUser { get; }
public PythonResourceLoader(ApiCredentials credentials)
public PythonResourceLoader(ApiConfig apiConfig, ApiCredentials credentials, AzaionApiClient api)
{
//Run python by credentials
_credentials = credentials;
_api = api;
//StartPython(apiConfig, credentials);
_dealer.Options.Identity = Encoding.UTF8.GetBytes(_clientId.ToString("N"));
_dealer.Connect($"tcp://{SecurityConstants.ZMQ_HOST}:{SecurityConstants.ZMQ_PORT}");
_dealer.SendFrame(MessagePackSerializer.Serialize(new RemoteCommand(CommandType.GetUser)));
var user = _dealer.Get<User>(out _);
var user = _dealer.Get<User>();
if (user == null)
throw new Exception("Can't get user from Auth provider");
CurrentUser = user;
}
private void StartPython( ApiConfig apiConfig, ApiCredentials credentials)
{
//var inferenceExe = LoadPythonFile().GetAwaiter().GetResult();
string outputProcess = "";
string errorProcess = "";
public async Task<MemoryStream> LoadFile(string fileName, CancellationToken ct = default)
var path = "azaion-inference.exe";
var arguments = $"-e {credentials.Email} -p {credentials.Password} -f {apiConfig.ResourcesFolder}";
using var process = new Process();
process.StartInfo.FileName = path;
process.StartInfo.Arguments = arguments;
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
//process.StartInfo.CreateNoWindow = true;
process.OutputDataReceived += (sender, e) => { if (e.Data != null) Console.WriteLine(e.Data); };
process.ErrorDataReceived += (sender, e) => { if (e.Data != null) Console.WriteLine(e.Data); };
process.Start();
}
public async Task LoadFileFromApi(CancellationToken cancellationToken = default)
{
var hardwareService = new HardwareService();
var hardwareInfo = hardwareService.GetHardware();
var encryptedStream = await _api.GetResource("azaion_inference.exe", _credentials.Password, hardwareInfo, cancellationToken);
var key = Security.MakeEncryptionKey(_credentials.Email, _credentials.Password, hardwareInfo.Hash);
var stream = new MemoryStream();
await encryptedStream.DecryptTo(stream, key, cancellationToken);
stream.Seek(0, SeekOrigin.Begin);
}
public MemoryStream LoadFileFromPython(string fileName)
{
try
{
@@ -49,7 +87,7 @@ public class PythonResourceLoader : IResourceLoader, IAuthProvider
if (!_dealer.TryReceiveFrameBytes(TimeSpan.FromMilliseconds(1000), out var bytes))
throw new Exception($"Unable to receive {fileName}");
return await Task.FromResult(new MemoryStream(bytes));
return new MemoryStream(bytes);
}
catch (Exception ex)
{