Skip to content

Commit

Permalink
Add creation transcription method that returns string
Browse files Browse the repository at this point in the history
  • Loading branch information
nikitabumblebee committed Sep 10, 2024
1 parent d925c29 commit 38e8ce3
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions Sources/ExyteOpenAI/OpenAI+Audio.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,41 @@ public extension OpenAI {
.eraseToAnyPublisher()
}

func createTranscription(from payload: CreateTranscriptionPayload) -> AnyPublisher<String, OpenAIError> {
audioProvider.requestPublisher(for: .createTranscription(payload: payload))
.flatMap {
guard let stringData = String(data: $0.data, encoding: .utf8) else {
return Fail<String, OpenAIError>(error: .requestCreationFailed)
.eraseToAnyPublisher()
}
return Just<String>(stringData)
.setFailureType(to: OpenAIError.self)
.eraseToAnyPublisher()
}
.eraseToAnyPublisher()
}

func createTranslation(from payload: CreateTranslationPayload) -> AnyPublisher<Translation, OpenAIError> {
audioProvider.requestPublisher(for: .createTranslation(payload: payload))
.map { $0.data }
.map(to: Translation.self, decoder: OpenAI.defaultDecoder)
.eraseToAnyPublisher()
}

func createTranslation(from payload: CreateTranslationPayload) -> AnyPublisher<String, OpenAIError> {
audioProvider.requestPublisher(for: .createTranslation(payload: payload))
.flatMap {
guard let stringData = String(data: $0.data, encoding: .utf8) else {
return Fail<String, OpenAIError>(error: .requestCreationFailed)
.eraseToAnyPublisher()
}
return Just<String>(stringData)
.setFailureType(to: OpenAIError.self)
.eraseToAnyPublisher()
}
.eraseToAnyPublisher()
}

}

// MARK: - Concurrency
Expand All @@ -62,8 +90,16 @@ public extension OpenAI {
try await createTranscription(from: payload).async()
}

func createTranscription(from payload: CreateTranscriptionPayload) async throws -> String {
try await createTranscription(from: payload).async()
}

func createTranslation(from payload: CreateTranslationPayload) async throws -> Translation {
try await createTranslation(from: payload).async()
}

func createTranslation(from payload: CreateTranslationPayload) async throws -> String {
try await createTranslation(from: payload).async()
}

}

0 comments on commit 38e8ce3

Please sign in to comment.