mirror of
https://github.com/azaion/annotations.git
synced 2026-04-22 12:36:31 +00:00
Errors sending to UI
notifying client of AI model conversion
This commit is contained in:
@@ -1,11 +1,11 @@
|
||||
using System.Text;
|
||||
using Azaion.Common.Database;
|
||||
using Azaion.Common.Database;
|
||||
using Azaion.Common.DTO.Config;
|
||||
using Azaion.CommonSecurity;
|
||||
using Azaion.Common.Events;
|
||||
using Azaion.Common.Extensions;
|
||||
using Azaion.CommonSecurity.DTO.Commands;
|
||||
using Azaion.CommonSecurity.Services;
|
||||
using MediatR;
|
||||
using MessagePack;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
@@ -13,45 +13,74 @@ namespace Azaion.Common.Services;
|
||||
|
||||
public interface IInferenceService
|
||||
{
|
||||
Task RunInference(List<string> mediaPaths, Func<AnnotationImage, Task> processAnnotation, CancellationToken detectToken = default);
|
||||
Task RunInference(List<string> mediaPaths, CancellationToken ct = default);
|
||||
void StopInference();
|
||||
}
|
||||
|
||||
public class InferenceService(ILogger<InferenceService> logger, IInferenceClient client, IAzaionApi azaionApi, IOptions<AIRecognitionConfig> aiConfigOptions) : IInferenceService
|
||||
public class InferenceService : IInferenceService
|
||||
{
|
||||
public async Task RunInference(List<string> mediaPaths, Func<AnnotationImage, Task> processAnnotation, CancellationToken detectToken = default)
|
||||
private readonly IInferenceClient _client;
|
||||
private readonly IAzaionApi _azaionApi;
|
||||
private readonly IOptions<AIRecognitionConfig> _aiConfigOptions;
|
||||
private readonly IAnnotationService _annotationService;
|
||||
private readonly IMediator _mediator;
|
||||
private CancellationTokenSource _inferenceCancelTokenSource = new();
|
||||
|
||||
public InferenceService(
|
||||
ILogger<InferenceService> logger,
|
||||
IInferenceClient client,
|
||||
IAzaionApi azaionApi,
|
||||
IOptions<AIRecognitionConfig> aiConfigOptions,
|
||||
IAnnotationService annotationService,
|
||||
IMediator mediator)
|
||||
{
|
||||
client.Send(RemoteCommand.Create(CommandType.Login, azaionApi.Credentials));
|
||||
var aiConfig = aiConfigOptions.Value;
|
||||
_client = client;
|
||||
_azaionApi = azaionApi;
|
||||
_aiConfigOptions = aiConfigOptions;
|
||||
_annotationService = annotationService;
|
||||
_mediator = mediator;
|
||||
|
||||
aiConfig.Paths = mediaPaths;
|
||||
client.Send(RemoteCommand.Create(CommandType.Inference, aiConfig));
|
||||
|
||||
while (!detectToken.IsCancellationRequested)
|
||||
client.InferenceDataReceived += async (sender, command) =>
|
||||
{
|
||||
try
|
||||
{
|
||||
var bytes = client.GetBytes(ct: detectToken);
|
||||
if (bytes == null)
|
||||
throw new Exception("Can't get bytes from inference client");
|
||||
|
||||
if (bytes.Length == 4 && Encoding.UTF8.GetString(bytes) == "DONE")
|
||||
if (command.Message == "DONE")
|
||||
{
|
||||
_inferenceCancelTokenSource?.Cancel();
|
||||
return;
|
||||
}
|
||||
|
||||
var annotationImage = MessagePackSerializer.Deserialize<AnnotationImage>(bytes, cancellationToken: detectToken);
|
||||
|
||||
await processAnnotation(annotationImage);
|
||||
var annImage = MessagePackSerializer.Deserialize<AnnotationImage>(command.Data);
|
||||
await ProcessDetection(annImage);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
logger.LogError(e, e.Message);
|
||||
break;
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private async Task ProcessDetection(AnnotationImage annotationImage, CancellationToken ct = default)
|
||||
{
|
||||
var annotation = await _annotationService.SaveAnnotation(annotationImage, ct);
|
||||
await _mediator.Publish(new AnnotationAddedEvent(annotation), ct);
|
||||
}
|
||||
|
||||
public async Task RunInference(List<string> mediaPaths, CancellationToken ct = default)
|
||||
{
|
||||
_inferenceCancelTokenSource = new CancellationTokenSource();
|
||||
_client.Send(RemoteCommand.Create(CommandType.Login, _azaionApi.Credentials));
|
||||
|
||||
var aiConfig = _aiConfigOptions.Value;
|
||||
aiConfig.Paths = mediaPaths;
|
||||
_client.Send(RemoteCommand.Create(CommandType.Inference, aiConfig));
|
||||
|
||||
using var combinedTokenSource = CancellationTokenSource.CreateLinkedTokenSource(ct, _inferenceCancelTokenSource.Token);
|
||||
await combinedTokenSource.Token.AsTask();
|
||||
}
|
||||
|
||||
public void StopInference()
|
||||
{
|
||||
client.Send(RemoteCommand.Create(CommandType.StopInference));
|
||||
_client.Send(RemoteCommand.Create(CommandType.StopInference));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user