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 ac3cfe4
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 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,6 +90,10 @@ 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()
}
Expand Down

0 comments on commit ac3cfe4

Please sign in to comment.