-
Notifications
You must be signed in to change notification settings - Fork 431
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for audio transcriptions and translations (Whisper)
- Loading branch information
Showing
15 changed files
with
613 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using Newtonsoft.Json; | ||
using static OpenAI_API.Audio.TextToSpeechRequest; | ||
|
||
namespace OpenAI_API.Audio | ||
{ | ||
public class AudioRequest | ||
{ | ||
/// <summary> | ||
/// The model to use for this request. Currently only <see cref="OpenAI_API.Models.Model.Whisper1"/> is supported. | ||
/// </summary> | ||
[JsonProperty("model")] | ||
public string Model { get; set; } = OpenAI_API.Models.Model.DefaultTranscriptionModel; | ||
|
||
/// <summary> | ||
/// An optional text to guide the model's style or continue a previous audio segment. The prompt should match the audio language for transcriptions, or English for translations. | ||
/// </summary> | ||
[JsonProperty("prompt", DefaultValueHandling = DefaultValueHandling.Ignore)] | ||
public string Prompt { get; set; } = null; | ||
|
||
/// <summary> | ||
/// The language of the input audio. Supplying the input language in ISO-639-1 format will improve accuracy and latency. | ||
/// </summary> | ||
[JsonProperty("language", DefaultValueHandling = DefaultValueHandling.Ignore)] | ||
public string Language { get; set; } = null; | ||
|
||
/// <summary> | ||
/// The format of the transcript output, should be one of the options in <see cref="AudioRequest.ResponseFormats"/>. See <seealso href="https://platform.openai.com/docs/api-reference/audio/createTranscription#audio-createtranscription-response_format"/> | ||
/// </summary> | ||
[JsonProperty("response_format", DefaultValueHandling = DefaultValueHandling.Ignore)] | ||
public string ResponseFormat { get; set; } = null; | ||
|
||
/// <summary> | ||
/// The sampling temperature, between 0 and 1. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic. If set to 0, the model will use log probability to automatically increase the temperature until certain thresholds are hit. | ||
/// </summary> | ||
[JsonProperty("temperature", DefaultValueHandling = DefaultValueHandling.Ignore)] | ||
public double Temperature { get; set; } = 0; | ||
|
||
|
||
/// <summary> | ||
/// The format of the transcript output. See <seealso href="https://platform.openai.com/docs/api-reference/audio/createTranscription#audio-createtranscription-response_format"/> | ||
/// </summary> | ||
public static class ResponseFormats | ||
{ | ||
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member | ||
public const string JSON = "json"; | ||
public const string Text = "text"; | ||
public const string SRT = "srt"; | ||
public const string VerboseJson = "verbose_json"; | ||
public const string VTT = "vtt"; | ||
#pragma warning restore CS1591 // Missing XML comment for publicly visible type or member | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace OpenAI_API.Audio | ||
{ | ||
/// <summary> | ||
/// Represents a verbose_json output from the OpenAI Transcribe or Translate endpoints. | ||
/// </summary> | ||
public class AudioResultVerbose : ApiResultBase | ||
{ | ||
public double duration { get; set; } | ||
public string language { get; set; } | ||
public List<Segment> segments { get; set; } | ||
public string task { get; set; } | ||
public string text { get; set; } | ||
|
||
public class Segment | ||
{ | ||
public double avg_logprob { get; set; } | ||
public double compression_ratio { get; set; } | ||
public double end { get; set; } | ||
public int id { get; set; } | ||
public double no_speech_prob { get; set; } | ||
public int seek { get; set; } | ||
public double start { get; set; } | ||
public double temperature { get; set; } | ||
public string text { get; set; } | ||
public List<int> tokens { get; set; } | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
using System.IO; | ||
using System.Threading.Tasks; | ||
|
||
namespace OpenAI_API.Audio | ||
{ | ||
/// <summary> | ||
/// Transcribe audio into text, with optional translation into English. | ||
/// </summary> | ||
public interface ITranscriptionEndpoint | ||
{ | ||
/// <summary> | ||
/// This allows you to set default parameters for every request, for example to set a default language. For every request, if you do not have a parameter set on the request but do have it set here as a default, the request will automatically pick up the default value. | ||
/// </summary> | ||
AudioRequest DefaultRequestArgs { get; set; } | ||
|
||
/// <summary> | ||
/// Gets the transcription of the audio stream, in the specified format | ||
/// </summary> | ||
/// <param name="audioStream">The stream containing audio data, in one of these formats: flac, mp3, mp4, mpeg, mpga, m4a, ogg, wav, or webm.</param> | ||
/// <param name="filename">The name of the audio file in the stream. This does not have to be real, but it must contain the correct file extension. For example, "file.mp3" if you are supplying an mp3 audio stream.</param> | ||
/// <param name="responseFormat">The format of the response. Suggested value are <see cref="AudioRequest.ResponseFormats.SRT"/> or <see cref="AudioRequest.ResponseFormats.VTT"/>. For text and Json formats, try <see cref="GetTranscriptionTextAsync(Stream, string, string, double?)"/> or <see cref="GetTranscriptionDetailsAsync(Stream, string, string, double?)"/> instead.</param> | ||
/// <param name="language">The language of the input audio. Supplying the input language in ISO-639-1 format will improve accuracy and latency.</param> | ||
/// <param name="prompt">An optional text to guide the model's style or continue a previous audio segment. The prompt should match the audio language.</param> | ||
/// <param name="temperature">The sampling temperature, between 0 and 1. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic. If set to 0, the model will use log probability to automatically increase the temperature until certain thresholds are hit.</param> | ||
/// <returns>A string of the transcribed text</returns> | ||
Task<string> GetAsFormatAsync(Stream audioStream, string filename,string responseFormat, string language = null, string prompt = null, double? temperature = null); | ||
|
||
/// <summary> | ||
/// Gets the transcription of the audio file, in the specified format | ||
/// </summary> | ||
/// <param name="audioFilePath">The local path to the audio file, in one of these formats: flac, mp3, mp4, mpeg, mpga, m4a, ogg, wav, or webm.</param> | ||
/// <param name="responseFormat">The format of the response. Suggested value are <see cref="AudioRequest.ResponseFormats.SRT"/> or <see cref="AudioRequest.ResponseFormats.VTT"/>. For text and Json formats, try <see cref="GetTranscriptionTextAsync(Stream, string, string, double?)"/> or <see cref="GetTranscriptionDetailsAsync(Stream, string, string, double?)"/> instead.</param> | ||
/// <param name="language">The language of the input audio. Supplying the input language in ISO-639-1 format will improve accuracy and latency.</param> | ||
/// <param name="prompt">An optional text to guide the model's style or continue a previous audio segment. The prompt should match the audio language.</param> | ||
/// <param name="temperature">The sampling temperature, between 0 and 1. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic. If set to 0, the model will use log probability to automatically increase the temperature until certain thresholds are hit.</param> | ||
/// <returns>A string of the transcribed text</returns> | ||
Task<string> GetAsFormatAsync(string audioFilePath, string responseFormat, string language = null, string prompt = null, double? temperature = null); | ||
|
||
/// <summary> | ||
/// Gets the transcription of the audio stream, with full metadata | ||
/// </summary> | ||
/// <param name="audioStream">The stream containing audio data, in one of these formats: flac, mp3, mp4, mpeg, mpga, m4a, ogg, wav, or webm.</param> | ||
/// <param name="filename">The name of the audio file in the stream. This does not have to be real, but it must contain the correct file extension. For example, "file.mp3" if you are supplying an mp3 audio stream.</param> | ||
/// <param name="language">The language of the input audio. Supplying the input language in ISO-639-1 format will improve accuracy and latency.</param> | ||
/// <param name="prompt">An optional text to guide the model's style or continue a previous audio segment. The prompt should match the audio language.</param> | ||
/// <param name="temperature">The sampling temperature, between 0 and 1. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic. If set to 0, the model will use log probability to automatically increase the temperature until certain thresholds are hit.</param> | ||
/// <returns>A string of the transcribed text</returns> | ||
Task<AudioResultVerbose> GetWithDetailsAsync(Stream audioStream, string filename,string language = null, string prompt = null, double? temperature = null); | ||
|
||
/// <summary> | ||
/// Gets the transcription of the audio file, with full metadata | ||
/// </summary> | ||
/// <param name="audioFilePath">The local path to the audio file, in one of these formats: flac, mp3, mp4, mpeg, mpga, m4a, ogg, wav, or webm.</param> | ||
/// <param name="language">The language of the input audio. Supplying the input language in ISO-639-1 format will improve accuracy and latency.</param> | ||
/// <param name="prompt">An optional text to guide the model's style or continue a previous audio segment. The prompt should match the audio language.</param> | ||
/// <param name="temperature">The sampling temperature, between 0 and 1. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic. If set to 0, the model will use log probability to automatically increase the temperature until certain thresholds are hit.</param> | ||
/// <returns>A string of the transcribed text</returns> | ||
Task<AudioResultVerbose> GetWithDetailsAsync(string audioFilePath, string language = null, string prompt = null, double? temperature = null); | ||
|
||
/// <summary> | ||
/// Gets the transcription of the audio stream as a text string | ||
/// </summary> | ||
/// <param name="audioStream">The stream containing audio data, in one of these formats: flac, mp3, mp4, mpeg, mpga, m4a, ogg, wav, or webm.</param> | ||
/// <param name="filename">The name of the audio file in the stream. This does not have to be real, but it must contain the correct file extension. For example, "file.mp3" if you are supplying an mp3 audio stream.</param> | ||
/// <param name="language">The language of the input audio. Supplying the input language in ISO-639-1 format will improve accuracy and latency.</param> | ||
/// <param name="prompt">An optional text to guide the model's style or continue a previous audio segment. The prompt should match the audio language.</param> | ||
/// <param name="temperature">The sampling temperature, between 0 and 1. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic. If set to 0, the model will use log probability to automatically increase the temperature until certain thresholds are hit.</param> | ||
/// <returns>A string of the transcribed text</returns> | ||
Task<string> GetTextAsync(Stream audioStream, string filename, string language = null, string prompt = null, double? temperature = null); | ||
|
||
/// <summary> | ||
/// Gets the transcription of the audio file as a text string | ||
/// </summary> | ||
/// <param name="audioFilePath">The local path to the audio file, in one of these formats: flac, mp3, mp4, mpeg, mpga, m4a, ogg, wav, or webm.</param> | ||
/// <param name="language">The language of the input audio. Supplying the input language in ISO-639-1 format will improve accuracy and latency.</param> | ||
/// <param name="prompt">An optional text to guide the model's style or continue a previous audio segment. The prompt should match the audio language.</param> | ||
/// <param name="temperature">The sampling temperature, between 0 and 1. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic. If set to 0, the model will use log probability to automatically increase the temperature until certain thresholds are hit.</param> | ||
/// <returns>A string of the transcribed text</returns> | ||
Task<string> GetTextAsync(string audioFilePath, string language = null, string prompt = null, double? temperature = null); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.