argmaxinc / WhisperKit

On-device Speech Recognition for Apple Silicon
https://takeargmax.com/blog/whisperkit
MIT License
3.17k stars 267 forks source link

how to fix "Ambiguous use of 'transcribe(audioPath:decodeOptions:callback:)'" #142

Closed zacfire closed 3 months ago

zacfire commented 4 months ago

i try many way, but still show:Ambiguous use of 'transcribe(audioPath:decodeOptions:callback:)

Here is code:

 func stopRecording() {
        audioRecorder?.stop()
        if let url = audioRecorder?.url {
            transcribeAudio(audioPath: url.path)
        }
    }

func transcribeAudio(audioPath: String) {
        Task {
            do {
                let whisper = try await WhisperKit()
                let results = try await whisper.transcribe(audioPath: audioPath)
                DispatchQueue.main.async {
                    if let result = results.first, !result.text.isEmpty {
                        self.transcription = result.text
                        let newRecording = Recording(
                            name: "Recording \(self.recordings.count + 1)",
                            createdAt: Date(),
                            fileURL: URL(fileURLWithPath: audioPath),
                            transcript: result.text
                        )
                        self.recordings.append(newRecording)
                    }
                }
            } catch {
                DispatchQueue.main.async {
                    print("Transcription failed: \(error)")
                }
            }
        }
    } 
jkrukowski commented 4 months ago

In WhisperKit there are 2 methods with the same arguments but different return type

  1. func transcribe(audioPath:decodeOptions:callback:) async throws -> TranscriptionResult?
  2. func transcribe(audioPath:decodeOptions:callback:) async throws -> [TranscriptionResult]

The 1st one is deprecated and is subject to removal in a future version.

To suppress the warning, please specify the return type explicitly, e.g.

// ...
let whisper = try await WhisperKit()
let results: [TranscriptionResult] = try await whisper.transcribe(audioPath: audioPath)
// ...
ZachNagengast commented 4 months ago

Looking into what changes we can do to reduce the ambiguous errors here, good callout

ZachNagengast commented 4 months ago

@zacfire Please pull the latest and try it out with the changes from #143 thanks to @jkrukowski (it should allow your current code to work without ambiguity errors)