Skip to content

Fix for non English language response from Whisper #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,5 @@ OpenAPI_key.txt
image_edit_mask_.png
image_edit_original_.png
instructions.txt
/Playgrounds/Transcription/Male voices
/Playgrounds/Transcription/Male voices.zip
4 changes: 2 additions & 2 deletions Forge.OpenAI/Forge.OpenAI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@

<PropertyGroup>
<!--TargetFrameworks>net6.0</TargetFrameworks-->
<TargetFrameworks>net461;netstandard2.0;netcoreapp3.1;net6.0;net7.0</TargetFrameworks>
<TargetFrameworks>netstandard2.0;net6.0;net7.0</TargetFrameworks>
<SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>..\..\.Documents\Forge.pfx</AssemblyOriginatorKeyFile>
<!--<AssemblyOriginatorKeyFile>..\..\.Documents\Forge.pfx</AssemblyOriginatorKeyFile>-->
<ProjectGuid>{D1DDE02E-1865-4173-8F3C-153D99AB394F}</ProjectGuid>
<DocumentationFile>Forge.OpenAI.xml</DocumentationFile>
</PropertyGroup>
Expand Down
13 changes: 11 additions & 2 deletions Forge.OpenAI/Infrastructure/ApiHttpService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Net.Http;
Expand Down Expand Up @@ -411,16 +412,20 @@ private async Task<HttpOperationResult<TResult>> ApiCall<TData, TResult>(HttpMet
{
if (typeof(string).IsAssignableFrom(typeof(TResult)))
{
//var jsonResult = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
result = new HttpOperationResult<TResult>(jsonResult as TResult);
}
else
{
result = new HttpOperationResult<TResult>(JsonSerializer.Deserialize<TResult>(jsonResult, _options.JsonSerializerOptions));
var contentAsBytes = UTF8Encoding.UTF8.GetBytes(jsonResult);
var jsonObj = GetUTF8Object<TResult>(contentAsBytes);
result = new HttpOperationResult<TResult>(jsonObj);
SetResponseData(response, result.Result);
}
}
else
{
// var jsonResult = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
_logger?.LogDebug($"ApiCall, response indicates an unsuccessful operation from {httpClient.BaseAddress}{uri}, method: {httpMethod.Method}");

result = new HttpOperationResult<TResult>(new Exception(response.StatusCode.ToString(), new Exception(jsonResult)), response.StatusCode, jsonResult);
Expand All @@ -434,7 +439,11 @@ private async Task<HttpOperationResult<TResult>> ApiCall<TData, TResult>(HttpMet
return result;
});
}

private TResult GetUTF8Object<TResult>(byte[] responseBytes)
{
Utf8JsonReader reader = new Utf8JsonReader(responseBytes);
return JsonSerializer.Deserialize<TResult>(ref reader, _options.JsonSerializerOptions);
}
/// <summary>Perform the API call in streaming mode the asynchronously.</summary>
/// <typeparam name="TData">The type of the data.</typeparam>
/// <typeparam name="TResult">The type of the result.</typeparam>
Expand Down
7 changes: 7 additions & 0 deletions Forge.OpenAI/Interfaces/Services/ITranscriptionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ public interface ITranscriptionService
/// TranscriptionResponse
/// </returns>
Task<HttpOperationResult<TranscriptionResponse>> GetAsync(TranscriptionRequest request, CancellationToken cancellationToken);
/// <summary>
/// This method is useful
/// </summary>
/// <param name="request"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
//Task<HttpOperationResult<TranscriptionResponse>> GetResponseAsStringAsync(TranscriptionRequest request, CancellationToken cancellationToken);

}

Expand Down
2 changes: 1 addition & 1 deletion Forge.OpenAI/Services/TranscriptionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ private async Task<HttpContent> TranscriptHttpContentFactoryAsync(TranscriptionR

if (!string.IsNullOrWhiteSpace(request.Prompt)) content.Add(new StringContent(request.Prompt), "prompt");
if (!string.IsNullOrWhiteSpace(request.ResponseFormat)) content.Add(new StringContent(request.ResponseFormat), "response_format");
if (!string.IsNullOrWhiteSpace(request.Language)) content.Add(new StringContent(request.ResponseFormat), "language");
if (!string.IsNullOrWhiteSpace(request.Language)) content.Add(new StringContent(request.Language), "language");
if (request.Temperature.HasValue) content.Add(new StringContent(request.Temperature.Value.ToString()), "temperature");

return content;
Expand Down
4 changes: 3 additions & 1 deletion Forge.OpenAI/Settings/OpenAIDefaultOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,9 @@ public static class OpenAIDefaultOptions
/// <summary>The default json serializer options</summary>
public static JsonSerializerOptions DefaultJsonSerializerOptions { get; set; } = new JsonSerializerOptions
{
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping

};

}
Expand Down
13 changes: 12 additions & 1 deletion Playgrounds/Transcription/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,22 @@
using Forge.OpenAI.Models.Common;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System.Text;

namespace Transcription
{

internal class Program
{
public static string DecodeFromUtf16ToUtf8(string utf16String)
{
// copy the string as UTF-8 bytes.
byte[] utf8Bytes = new byte[utf16String.Length];
for (int i = 0; i < utf16String.Length; ++i)
utf8Bytes[i] = (byte)utf16String[i];

return Encoding.UTF8.GetString(utf8Bytes, 0, utf8Bytes.Length);
}

static async Task Main(string[] args)
{
Expand All @@ -27,10 +37,11 @@ static async Task Main(string[] args)

TranscriptionRequest request = new TranscriptionRequest();
request.AudioFile = new BinaryContentData() { ContentName = "audio.mp3", SourceStream = File.OpenRead("audio.mp3") };

//request.Language = "ta";
HttpOperationResult<TranscriptionResponse> response = await openAi.TranscriptionService.GetAsync(request, CancellationToken.None).ConfigureAwait(false);
if (response.IsSuccess)
{
Console.OutputEncoding = Encoding.UTF8;
Console.WriteLine(response.Result?.Text);
}
else
Expand Down
Binary file added Playgrounds/Transcription/Sample.mp3
Binary file not shown.
8 changes: 7 additions & 1 deletion Playgrounds/Transcription/Transcription.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
</PropertyGroup>

<ItemGroup>
<!--<PackageReference Include="Forge.OpenAI" Version="1.1.1" />-->
<PackageReference Include="Microsoft.Extensions.Configuration" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="7.0.2" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="7.0.0" />
Expand All @@ -24,16 +25,21 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\Forge.OpenAI\Forge.OpenAI.csproj" />
<ProjectReference Include="..\..\Forge.OpenAI\Forge.OpenAI.csproj" />
</ItemGroup>



<ItemGroup>
<None Update="appsettings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="audio.mp3">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Sample.mp3">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>