-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add translation and transcription endpoints
- Loading branch information
1 parent
3a83729
commit 1547a1e
Showing
18 changed files
with
712 additions
and
38 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
142 changes: 142 additions & 0 deletions
142
Sources/ExyteOpenAI/Endpoint Configurations/Audio.swift
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,142 @@ | ||
// | ||
// Audio.swift | ||
// | ||
// Copyright (c) 2024 Exyte | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in | ||
// all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
// THE SOFTWARE. | ||
// | ||
|
||
import Foundation | ||
|
||
enum Audio { | ||
case createTranscriptionPayload(payload: CreateTranscriptionPayload) | ||
case createTranslationPayload(payload: CreateTranslationPayload) | ||
case createSpeechPayload(payload: CreateSpeechPayload, destination: URL) | ||
} | ||
|
||
extension Audio: EndpointConfiguration { | ||
|
||
var method: HTTPRequestMethod { | ||
return .post | ||
} | ||
|
||
var path: String { | ||
switch self { | ||
case .createTranscriptionPayload: | ||
return "/audio/transcriptions" | ||
case .createTranslationPayload: | ||
return "/audio/translations" | ||
case .createSpeechPayload: | ||
return "/audio/speech" | ||
} | ||
} | ||
|
||
var task: RequestTask { | ||
switch self { | ||
case .createTranscriptionPayload(let payload): | ||
var data: [FormBodyPart] = [ | ||
FormBodyPart( | ||
name: "file", | ||
value: .fileURL(payload.file), | ||
fileName: payload.file.lastPathComponent, | ||
mimeType: payload.file.pathExtension | ||
), | ||
FormBodyPart( | ||
name: "model", | ||
value: .plainText(payload.model.rawValue) | ||
), | ||
FormBodyPart( | ||
name: "response_format", | ||
value: .plainText(payload.responseFormat?.rawValue ?? TextResponseFormat.json.rawValue) | ||
) | ||
] | ||
if let temperature = payload.temperature { | ||
data.append( | ||
FormBodyPart( | ||
name: "temperature", | ||
value: .floatingPoint(Float(temperature)) | ||
) | ||
) | ||
} | ||
if let prompt = payload.prompt { | ||
data.append( | ||
FormBodyPart( | ||
name: "prompt", | ||
value: .plainText(prompt) | ||
) | ||
) | ||
} | ||
if let language = payload.language { | ||
data.append( | ||
FormBodyPart( | ||
name: "language", | ||
value: .plainText(language)) | ||
) | ||
} | ||
if let timestampGranularities = payload.timestampGranularities, | ||
payload.responseFormat == .verboseJson { | ||
let timestampGranularitiesData = withUnsafeBytes(of: timestampGranularities) { Data($0) } | ||
data.append( | ||
FormBodyPart( | ||
name: "timestamp_granularities", | ||
value: .data(timestampGranularitiesData) | ||
) | ||
) | ||
} | ||
return .uploadMultipart(data) | ||
case .createTranslationPayload(let payload): | ||
var data: [FormBodyPart] = [ | ||
FormBodyPart( | ||
name: "file", | ||
value: .fileURL(payload.file), | ||
fileName: payload.file.lastPathComponent, | ||
mimeType: payload.file.pathExtension | ||
), | ||
FormBodyPart( | ||
name: "model", | ||
value: .plainText(payload.model.rawValue) | ||
), | ||
FormBodyPart( | ||
name: "response_format", | ||
value: .plainText(payload.responseFormat?.rawValue ?? TextResponseFormat.json.rawValue) | ||
) | ||
] | ||
if let prompt = payload.prompt { | ||
data.append( | ||
FormBodyPart( | ||
name: "prompt", | ||
value: .plainText(prompt) | ||
) | ||
) | ||
} | ||
if let temperature = payload.temperature { | ||
data.append( | ||
FormBodyPart( | ||
name: "temperature", | ||
value: .floatingPoint(Float(temperature)) | ||
) | ||
) | ||
} | ||
return .uploadMultipart(data) | ||
case .createSpeechPayload(let payload, let destination): | ||
return .download(destination) | ||
} | ||
} | ||
|
||
} |
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,49 @@ | ||
// | ||
// Transcription.swift | ||
// | ||
// Copyright (c) 2024 Exyte | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in | ||
// all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
// THE SOFTWARE. | ||
// | ||
|
||
import Foundation | ||
|
||
public struct Transcription: Codable { | ||
|
||
let text: String | ||
let language: String? | ||
let duration: Double? | ||
let words: String? | ||
let segments: [TranscriptionSegment]? | ||
|
||
public init( | ||
text: String, | ||
language: String? = nil, | ||
duration: Double? = nil, | ||
words: String? = nil, | ||
segments: [TranscriptionSegment]? = nil | ||
) { | ||
self.text = text | ||
self.language = language | ||
self.duration = duration | ||
self.words = words | ||
self.segments = segments | ||
} | ||
|
||
} |
40 changes: 40 additions & 0 deletions
40
Sources/ExyteOpenAI/Models/Audio/TranscriptionSegment.swift
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,40 @@ | ||
// | ||
// TranscriptionSegment.swift | ||
// | ||
// Copyright (c) 2024 Exyte | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in | ||
// all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
// THE SOFTWARE. | ||
// | ||
|
||
import Foundation | ||
|
||
public struct TranscriptionSegment: Codable { | ||
|
||
let id: Int | ||
let seek: Int | ||
let start: Double | ||
let end: Double | ||
let text: String | ||
let tokens: [Int] | ||
let temperature: Double | ||
let avgLogprob: Double? | ||
let compressionRatio: Double? | ||
let noSpeechProb: Double? | ||
|
||
} |
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,35 @@ | ||
// | ||
// Translation.swift | ||
// | ||
// Copyright (c) 2024 Exyte | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in | ||
// all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
// THE SOFTWARE. | ||
// | ||
|
||
import Foundation | ||
|
||
public struct Translation: Codable { | ||
|
||
let text: String | ||
|
||
public init(text: String) { | ||
self.text = text | ||
} | ||
|
||
} |
36 changes: 36 additions & 0 deletions
36
Sources/ExyteOpenAI/Models/Enums/AudioResponseFormat.swift
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,36 @@ | ||
// | ||
// AudioResponseFormat.swift | ||
// | ||
// Copyright (c) 2024 Exyte | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in | ||
// all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
// THE SOFTWARE. | ||
// | ||
|
||
import Foundation | ||
|
||
public enum AudioResponseFormat: String, Codable { | ||
|
||
case mp3 = "mp3" | ||
case opus = "opus" | ||
case aac = "aac" | ||
case flac = "flac" | ||
case wav = "wav" | ||
case pcm = "pcm" | ||
|
||
} |
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,29 @@ | ||
// | ||
// STTModel.swift | ||
// | ||
// Copyright (c) 2024 Exyte | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in | ||
// all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
// THE SOFTWARE. | ||
// | ||
|
||
import Foundation | ||
|
||
public enum STTModel: String, Codable { | ||
case whisper1 = "whisper-1" | ||
} |
Oops, something went wrong.