| | | 1 | | using Azure; |
| | | 2 | | using Azure.AI.OpenAI; |
| | | 3 | | using Chronicis.Shared.DTOs; |
| | | 4 | | using Chronicis.Shared.Extensions; |
| | | 5 | | using OpenAI.Chat; |
| | | 6 | | |
| | | 7 | | namespace Chronicis.Api.Services; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// Transcription service that uses Azure OpenAI GPT-4 Vision to convert handwritten note images to text. |
| | | 11 | | /// </summary> |
| | | 12 | | public sealed class TranscriptionService : ITranscriptionService |
| | | 13 | | { |
| | | 14 | | private readonly ChatClient _chatClient; |
| | | 15 | | private readonly ILogger<TranscriptionService> _logger; |
| | | 16 | | private readonly TimeSpan _timeout; |
| | 1 | 17 | | internal static readonly TimeSpan DefaultTimeout = TimeSpan.FromSeconds(60); |
| | | 18 | | |
| | | 19 | | public TranscriptionService(IConfiguration configuration, ILogger<TranscriptionService> logger) |
| | 4 | 20 | | : this(configuration, logger, DefaultTimeout) |
| | | 21 | | { |
| | 1 | 22 | | } |
| | | 23 | | |
| | | 24 | | internal TranscriptionService(IConfiguration configuration, ILogger<TranscriptionService> logger, TimeSpan timeout) |
| | | 25 | | { |
| | 4 | 26 | | _logger = logger; |
| | 4 | 27 | | _timeout = timeout; |
| | | 28 | | |
| | 4 | 29 | | var endpoint = configuration["AzureOpenAI:Endpoint"]; |
| | 4 | 30 | | var apiKey = configuration["AzureOpenAI:ApiKey"]; |
| | 4 | 31 | | var deploymentName = configuration["AzureOpenAI:VisionDeploymentName"] |
| | 4 | 32 | | ?? configuration["AzureOpenAI:DeploymentName"]; |
| | | 33 | | |
| | 4 | 34 | | if (string.IsNullOrEmpty(endpoint) || string.IsNullOrEmpty(apiKey) || string.IsNullOrEmpty(deploymentName)) |
| | | 35 | | { |
| | 3 | 36 | | throw new InvalidOperationException("AzureOpenAI configuration is incomplete for transcription."); |
| | | 37 | | } |
| | | 38 | | |
| | 1 | 39 | | var client = new AzureOpenAIClient(new Uri(endpoint), new AzureKeyCredential(apiKey)); |
| | 1 | 40 | | _chatClient = client.GetChatClient(deploymentName); |
| | 1 | 41 | | } |
| | | 42 | | |
| | | 43 | | // Test constructor |
| | | 44 | | internal TranscriptionService(ChatClient chatClient, ILogger<TranscriptionService> logger, TimeSpan timeout) |
| | | 45 | | { |
| | 2 | 46 | | _chatClient = chatClient; |
| | 2 | 47 | | _logger = logger; |
| | 2 | 48 | | _timeout = timeout; |
| | 2 | 49 | | } |
| | | 50 | | |
| | | 51 | | public async Task<TranscriptionResultDto> TranscribeImageAsync(byte[] imageBytes, CancellationToken cancellationToke |
| | | 52 | | { |
| | | 53 | | using var timeoutCts = new CancellationTokenSource(_timeout); |
| | | 54 | | using var linkedCts = CancellationTokenSource.CreateLinkedTokenSource(timeoutCts.Token, cancellationToken); |
| | | 55 | | |
| | | 56 | | try |
| | | 57 | | { |
| | | 58 | | var imageData = BinaryData.FromBytes(imageBytes); |
| | | 59 | | |
| | | 60 | | var messages = new List<ChatMessage> |
| | | 61 | | { |
| | | 62 | | new SystemChatMessage("You are a transcription assistant. Transcribe the handwritten text in the image e |
| | | 63 | | new UserChatMessage( |
| | | 64 | | ChatMessageContentPart.CreateTextPart("Transcribe the handwritten text in this image:"), |
| | | 65 | | ChatMessageContentPart.CreateImagePart(imageData, "image/png")) |
| | | 66 | | }; |
| | | 67 | | |
| | | 68 | | var options = new ChatCompletionOptions |
| | | 69 | | { |
| | | 70 | | MaxOutputTokenCount = 4000 |
| | | 71 | | }; |
| | | 72 | | |
| | | 73 | | var completion = await _chatClient.CompleteChatAsync(messages, options, linkedCts.Token); |
| | | 74 | | var text = completion.Value.Content[0].Text?.Trim() ?? string.Empty; |
| | | 75 | | |
| | | 76 | | if (string.IsNullOrWhiteSpace(text)) |
| | | 77 | | { |
| | | 78 | | return new TranscriptionResultDto |
| | | 79 | | { |
| | | 80 | | Success = false, |
| | | 81 | | ErrorMessage = "Transcription produced no text." |
| | | 82 | | }; |
| | | 83 | | } |
| | | 84 | | |
| | | 85 | | return new TranscriptionResultDto |
| | | 86 | | { |
| | | 87 | | Success = true, |
| | | 88 | | Text = text |
| | | 89 | | }; |
| | | 90 | | } |
| | | 91 | | catch (OperationCanceledException) when (timeoutCts.IsCancellationRequested && !cancellationToken.IsCancellation |
| | | 92 | | { |
| | | 93 | | _logger.LogErrorSanitized("Transcription request timed out after {Timeout} seconds", _timeout.TotalSeconds); |
| | | 94 | | return new TranscriptionResultDto |
| | | 95 | | { |
| | | 96 | | Success = false, |
| | | 97 | | ErrorMessage = $"Transcription timed out after {(int)_timeout.TotalSeconds} seconds." |
| | | 98 | | }; |
| | | 99 | | } |
| | | 100 | | catch (OperationCanceledException) |
| | | 101 | | { |
| | | 102 | | throw; |
| | | 103 | | } |
| | | 104 | | catch (Exception ex) |
| | | 105 | | { |
| | | 106 | | _logger.LogErrorSanitized(ex, "Transcription failed"); |
| | | 107 | | return new TranscriptionResultDto |
| | | 108 | | { |
| | | 109 | | Success = false, |
| | | 110 | | ErrorMessage = "Transcription service failed." |
| | | 111 | | }; |
| | | 112 | | } |
| | | 113 | | } |
| | | 114 | | } |