-
Notifications
You must be signed in to change notification settings - Fork 18
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
7 changed files
with
252 additions
and
2 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
5 changes: 5 additions & 0 deletions
5
Sources/SwiftOpenAI/OpenAI/DataModels/Audio/CreateTranscriptionDataModel.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,5 @@ | ||
import Foundation | ||
|
||
public struct CreateTranscriptionDataModel: Decodable { | ||
public let text: String | ||
} |
5 changes: 5 additions & 0 deletions
5
Sources/SwiftOpenAI/OpenAI/DataModels/Audio/OpenAITranscriptionModelType.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,5 @@ | ||
import Foundation | ||
|
||
public enum OpenAITranscriptionModelType: String { | ||
case whisper = "whisper-1" | ||
} |
38 changes: 38 additions & 0 deletions
38
Sources/SwiftOpenAI/OpenAI/OpenAIEndpoints/List/CreateTranscriptionEndpoint.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,38 @@ | ||
import Foundation | ||
|
||
struct CreateTranscriptionEndpoint: Endpoint { | ||
private let file: Data | ||
private let model: OpenAITranscriptionModelType | ||
private let language: String | ||
private let prompt: String | ||
private let responseFormat: OpenAIAudioResponseType | ||
private let temperature: Double | ||
|
||
var method: HTTPMethod { | ||
.POST | ||
} | ||
|
||
var path: String = "audio/transcriptions" | ||
|
||
init(file: Data, | ||
model: OpenAITranscriptionModelType, | ||
language: String = "en", | ||
prompt: String = "", | ||
responseFormat: OpenAIAudioResponseType, | ||
temperature: Double = 0.0) { | ||
self.file = file | ||
self.model = model | ||
self.language = language | ||
self.prompt = prompt | ||
self.responseFormat = responseFormat | ||
self.temperature = temperature | ||
} | ||
|
||
var parameters: [String: Any]? { | ||
["model": self.model.rawValue as Any, | ||
"language": self.language as Any, | ||
"prompt": self.prompt as Any, | ||
"response_format": self.responseFormat.rawValue as Any, | ||
"temperature": self.temperature as Any] | ||
} | ||
} |
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
97 changes: 97 additions & 0 deletions
97
Sources/SwiftOpenAI/OpenAI/Requests/Audio/CreateTranscriptionRequest.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,97 @@ | ||
import Foundation | ||
|
||
protocol CreateTranscriptionRequestProtocol { | ||
func execute(api: API, | ||
apiKey: String, | ||
file: Data, | ||
model: OpenAITranscriptionModelType, | ||
language: String, | ||
prompt: String, | ||
responseFormat: OpenAIAudioResponseType, | ||
temperature: Double) async throws -> AsyncThrowingStream<CreateTranscriptionDataModel, Error> | ||
} | ||
|
||
final public class CreateTranscriptionRequest: NSObject, CreateTranscriptionRequestProtocol { | ||
public typealias Init = (_ api: API, | ||
_ apiKey: String, | ||
_ file: Data, | ||
_ model: OpenAITranscriptionModelType, | ||
_ language: String, | ||
_ prompt: String, | ||
_ responseFormat: OpenAIAudioResponseType, | ||
_ temperature: Double) async throws -> AsyncThrowingStream<CreateTranscriptionDataModel, Error> | ||
|
||
private var urlSession: URLSession? | ||
private var dataTask: URLSessionDataTask? | ||
private var continuation: AsyncThrowingStream<CreateTranscriptionDataModel, Error>.Continuation? | ||
|
||
public override init() { | ||
super.init() | ||
} | ||
|
||
public func execute(api: API, | ||
apiKey: String, | ||
file: Data, | ||
model: OpenAITranscriptionModelType, | ||
language: String, | ||
prompt: String, | ||
responseFormat: OpenAIAudioResponseType, | ||
temperature: Double) async throws -> AsyncThrowingStream<CreateTranscriptionDataModel, Error> { | ||
|
||
return AsyncThrowingStream<CreateTranscriptionDataModel, Error> { continuation in | ||
self.continuation = continuation | ||
|
||
var endpoint = OpenAIEndpoints.createTranscription(file: file, model: model, language: language, prompt: prompt, responseFormat: responseFormat, temperature: temperature).endpoint | ||
api.routeEndpoint(&endpoint, environment: OpenAIEnvironmentV1()) | ||
|
||
let boundary = UUID().uuidString | ||
|
||
var urlRequest = api.buildURLRequest(endpoint: endpoint) | ||
api.addHeaders(urlRequest: &urlRequest, | ||
headers: ["Content-Type": "multipart/form-data; boundary=\(boundary)", | ||
"Authorization": "Bearer \(apiKey)"]) | ||
|
||
var body = Data() | ||
|
||
body.append("--\(boundary)\r\n".data(using: .utf8)!) | ||
body.append("Content-Disposition: form-data; name=\"model\"\r\n\r\n".data(using: .utf8)!) | ||
body.append("whisper-1\r\n".data(using: .utf8)!) | ||
|
||
body.append("--\(boundary)\r\n".data(using: .utf8)!) | ||
body.append("Content-Disposition: form-data; name=\"file\"; filename=\"steve.mp4\"\r\n".data(using: .utf8)!) | ||
body.append("Content-Type: audio/mpeg\r\n\r\n".data(using: .utf8)!) | ||
body.append(file) | ||
body.append("\r\n".data(using: .utf8)!) | ||
|
||
body.append("--\(boundary)--\r\n".data(using: .utf8)!) | ||
|
||
urlRequest.httpBody = body | ||
|
||
self.urlSession = URLSession(configuration: .default, | ||
delegate: self, | ||
delegateQueue: OperationQueue()) | ||
|
||
dataTask = urlSession?.dataTask(with: urlRequest) | ||
dataTask?.resume() | ||
} | ||
} | ||
} | ||
|
||
extension CreateTranscriptionRequest: URLSessionDataDelegate { | ||
public func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data) { | ||
do { | ||
let createTranscriptionDataModel = try JSONDecoder().decode(CreateTranscriptionDataModel.self, from: data) | ||
self.continuation?.yield(createTranscriptionDataModel) | ||
} catch { | ||
print("Error al parsear JSON:", error.localizedDescription) | ||
} | ||
} | ||
|
||
public func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) { | ||
guard let error = error else { | ||
continuation?.finish() | ||
return | ||
} | ||
continuation?.finish(throwing: error) | ||
} | ||
} |
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