-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
63 changed files
with
3,133 additions
and
30 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
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
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 @@ | ||
import Foundation | ||
|
||
public struct API { | ||
public let scheme: Scheme | ||
public let host: String | ||
public let path: String? | ||
|
||
public init( | ||
scheme: API.Scheme, | ||
host: String, | ||
pathPrefix path: String? = nil | ||
) { | ||
self.scheme = scheme | ||
self.host = host | ||
self.path = path | ||
} | ||
} | ||
|
||
extension API { | ||
public enum Scheme { | ||
case http | ||
case https | ||
case custom(String) | ||
|
||
var value: String { | ||
switch self { | ||
case .http: | ||
return "http" | ||
case .https: | ||
return "https" | ||
case .custom(let scheme): | ||
return scheme | ||
} | ||
} | ||
} | ||
} |
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 @@ | ||
// | ||
// AudioProvider.swift | ||
// | ||
// | ||
// Created by Dylan Shine on 3/19/23. | ||
// | ||
|
||
import Foundation | ||
|
||
public struct AudioProvider { | ||
|
||
private let requestHandler: RequestHandler | ||
|
||
init(requestHandler: RequestHandler) { | ||
self.requestHandler = requestHandler | ||
} | ||
|
||
/** | ||
Create transcription BETA | ||
POST | ||
|
||
https://api.openai.com/v1/audio/transcriptions | ||
|
||
Transcribes audio into the input language. | ||
*/ | ||
public func transcribe( | ||
file: Data, | ||
fileName: String, | ||
mimeType: MIMEType.Audio, | ||
model: ModelID = Model.Whisper.whisper1, | ||
prompt: String? = nil, | ||
responseFormat: String? = nil, | ||
temperature: Double? = nil, | ||
language: Language? = nil | ||
) async throws -> Transcription { | ||
|
||
let request = CreateTranscriptionRequest( | ||
file: file, | ||
fileName: fileName, | ||
mimeType: mimeType, | ||
model: model, | ||
prompt: prompt, | ||
responseFormat: responseFormat, | ||
temperature: temperature, | ||
language: language | ||
) | ||
|
||
return try await requestHandler.perform(request: request) | ||
} | ||
|
||
/** | ||
Create translation BETA | ||
POST | ||
|
||
https://api.openai.com/v1/audio/translations | ||
|
||
Translates audio into into English. | ||
*/ | ||
public func translate( | ||
file: Data, | ||
fileName: String, | ||
mimeType: MIMEType.Audio, | ||
model: ModelID = Model.Whisper.whisper1, | ||
prompt: String? = nil, | ||
responseFormat: String? = nil, | ||
temperature: Double? = nil | ||
) async throws -> Translation { | ||
|
||
let request = CreateTranslationRequest( | ||
file: file, | ||
fileName: fileName, | ||
mimeType: mimeType, | ||
model: model, | ||
prompt: prompt, | ||
responseFormat: responseFormat, | ||
temperature: temperature | ||
) | ||
|
||
return try await requestHandler.perform(request: request) | ||
} | ||
} |
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,55 @@ | ||
import Foundation | ||
|
||
struct CreateTranscriptionRequest: Request { | ||
let method: HTTPMethod = .post | ||
let path = "/v1/audio/transcriptions" | ||
let body: Data? | ||
private let boundary = UUID().uuidString | ||
|
||
var headers: HTTPHeaders { | ||
var headers = HTTPHeaders() | ||
headers.add(name: .contentType, value: "multipart/form-data; boundary=\(boundary)") | ||
return headers | ||
} | ||
|
||
init( | ||
file: Data, | ||
fileName: String, | ||
mimeType: MIMEType.Audio, | ||
model: ModelID, | ||
prompt: String?, | ||
responseFormat: String?, | ||
temperature: Double?, | ||
language: Language? | ||
) { | ||
let builder = MultipartFormDataBuilder(boundary: boundary) | ||
|
||
builder.addDataField( | ||
fieldName: "file", | ||
fileName: fileName, | ||
data: file, | ||
mimeType: mimeType.rawValue | ||
) | ||
|
||
builder.addTextField(named: "model", value: model.id) | ||
|
||
if let prompt = prompt { | ||
builder.addTextField(named: "prompt", value: prompt) | ||
} | ||
|
||
if let responseFormat = responseFormat { | ||
builder.addTextField(named: "response_format", value: responseFormat) | ||
} | ||
|
||
if let temperature = temperature { | ||
builder.addTextField(named: "temperature", value: String(temperature)) | ||
} | ||
|
||
if let language = language { | ||
builder.addTextField(named: "language", value: language.rawValue) | ||
} | ||
|
||
self.body = builder.build() | ||
} | ||
} | ||
|
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 @@ | ||
import Foundation | ||
|
||
struct CreateTranslationRequest: Request { | ||
let method: HTTPMethod = .post | ||
let path = "/v1/audio/translations" | ||
let body: Data? | ||
private let boundary = UUID().uuidString | ||
|
||
var headers: HTTPHeaders { | ||
var headers = HTTPHeaders() | ||
headers.add(name: .contentType, value: "multipart/form-data; boundary=\(boundary)") | ||
return headers | ||
} | ||
|
||
init( | ||
file: Data, | ||
fileName: String, | ||
mimeType: MIMEType.Audio, | ||
model: ModelID, | ||
prompt: String?, | ||
responseFormat: String?, | ||
temperature: Double? | ||
) { | ||
let builder = MultipartFormDataBuilder(boundary: boundary) | ||
|
||
builder.addDataField( | ||
fieldName: "file", | ||
fileName: fileName, | ||
data: file, | ||
mimeType: mimeType.rawValue | ||
) | ||
|
||
builder.addTextField(named: "model", value: model.id) | ||
|
||
if let prompt = prompt { | ||
builder.addTextField(named: "prompt", value: prompt) | ||
} | ||
|
||
if let responseFormat = responseFormat { | ||
builder.addTextField(named: "response_format", value: responseFormat) | ||
} | ||
|
||
if let temperature = temperature { | ||
builder.addTextField(named: "temperature", value: String(temperature)) | ||
} | ||
|
||
self.body = builder.build() | ||
} | ||
} |
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,22 @@ | ||
// | ||
// Transcription.swift | ||
// | ||
// | ||
// Created by Joshua Galvan on 6/12/23. | ||
// | ||
|
||
|
||
import Foundation | ||
|
||
/** | ||
Audio | ||
Learn how to turn audio into text. | ||
|
||
Related guide: https://platform.openai.com/docs/guides/speech-to-text | ||
*/ | ||
|
||
public struct Transcription { | ||
public let text: String | ||
} | ||
|
||
extension Transcription: Codable {} |
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,22 @@ | ||
// | ||
// Translation.swift | ||
// | ||
// | ||
// Created by Joshua Galvan on 6/12/23. | ||
// | ||
|
||
|
||
import Foundation | ||
|
||
/** | ||
Audio | ||
Learn how to turn audio into text. | ||
|
||
Related guide: https://platform.openai.com/docs/guides/speech-to-text | ||
*/ | ||
|
||
public struct Translation { | ||
public let text: String | ||
} | ||
|
||
extension Translation: Codable {} |
Oops, something went wrong.