Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add audio device selector to transcribe + take a stab at Delete/Retry models #54

Merged
merged 5 commits into from
Mar 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
148 changes: 100 additions & 48 deletions Examples/WhisperAX/WhisperAX/Views/ContentView.swift
Original file line number Diff line number Diff line change
@@ -246,6 +246,16 @@ struct ContentView: View {
.progressViewStyle(CircularProgressViewStyle())
.scaleEffect(0.5)
}

Button(action: {
deleteModel()
}, label: {
Image(systemName: "trash")
})
.help("Delete model")
.buttonStyle(BorderlessButtonStyle())
.disabled(localModels.count == 0)
.disabled(!localModels.contains(selectedModel))

#if os(macOS)
Button(action: {
@@ -306,6 +316,33 @@ struct ContentView: View {
}

// MARK: - Controls
var audioDevicesView: some View {
Group {
#if os(macOS)
HStack {
if let audioDevices = audioDevices, audioDevices.count > 0 {
Picker("", selection: $selectedAudioInput) {
ForEach(audioDevices, id: \.self) { device in
Text(device.name).tag(device.name)
}
}
.frame(width: 250)
.disabled(isRecording)
}
}
.onAppear {
audioDevices = AudioProcessor.getAudioDevices()
if let audioDevices = audioDevices,
!audioDevices.isEmpty,
selectedAudioInput == "No Audio Input",
let device = audioDevices.first {
selectedAudioInput = device.name
}
}
#endif
}
}

var controlsView: some View {
VStack {
basicSettingsView
@@ -321,7 +358,13 @@ struct ContentView: View {
Label("Reset", systemImage: "arrow.clockwise")
}
.buttonStyle(.borderless)

Spacer()

audioDevicesView

Spacer()

Button {
showAdvancedOptions.toggle()
} label: {
@@ -395,63 +438,50 @@ struct ContentView: View {
}
}
case "Stream":
HStack {
Button {
resetState()
} label: {
Label("Reset", systemImage: "arrow.clockwise")
}
.frame(minWidth: 0, maxWidth: .infinity)
.buttonStyle(.borderless)

Button {
withAnimation {
toggleRecording(shouldLoop: true)
}
} label: {
Image(systemName: !isRecording ? "record.circle" : "stop.circle.fill")
.resizable()
.scaledToFit()
.frame(width: 70, height: 70)
.padding()
.foregroundColor(modelState != .loaded ? .gray : .red)
}
.contentTransition(.symbolEffect(.replace))
.buttonStyle(BorderlessButtonStyle())
.disabled(modelState != .loaded)
.frame(minWidth: 0, maxWidth: .infinity)

VStack {
VStack {
HStack {
Button {
showAdvancedOptions.toggle()
resetState()
} label: {
Label("Settings", systemImage: "slider.horizontal.3")
Label("Reset", systemImage: "arrow.clockwise")
}
.frame(minWidth: 0, maxWidth: .infinity)
.buttonStyle(.borderless)

#if os(macOS)
HStack {
if let audioDevices = audioDevices, audioDevices.count > 0 {
Picker("", selection: $selectedAudioInput) {
ForEach(audioDevices, id: \.self) { device in
Text(device.name).tag(device.name)
}
}
.frame(minWidth: 80)
.disabled(isRecording)

Spacer()

audioDevicesView

Spacer()

VStack {
Button {
showAdvancedOptions.toggle()
} label: {
Label("Settings", systemImage: "slider.horizontal.3")
}
.frame(minWidth: 0, maxWidth: .infinity)
.buttonStyle(.borderless)
}
.onAppear {
audioDevices = AudioProcessor.getAudioDevices()
if let audioDevices = audioDevices,
!audioDevices.isEmpty,
selectedAudioInput == "No Audio Input",
let device = audioDevices.first {
selectedAudioInput = device.name
}

HStack {
Button {
withAnimation {
toggleRecording(shouldLoop: true)
}
} label: {
Image(systemName: !isRecording ? "record.circle" : "stop.circle.fill")
.resizable()
.scaledToFit()
.frame(width: 70, height: 70)
.padding()
.foregroundColor(modelState != .loaded ? .gray : .red)
}
#endif
.contentTransition(.symbolEffect(.replace))
.buttonStyle(BorderlessButtonStyle())
.disabled(modelState != .loaded)
.frame(minWidth: 0, maxWidth: .infinity)
}
}
default:
@@ -779,13 +809,35 @@ struct ContentView: View {
try await whisperKit.loadModels()

await MainActor.run {
if !localModels.contains(model) {
Copy link
Contributor Author

@cgfarmer4 cgfarmer4 Mar 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

local models doesnt refresh if you redownload a model after deleting without this

localModels.append(model)
}

availableLanguages = whisperKit.tokenizer?.langauges.map { $0.key }.sorted() ?? ["english"]
loadingProgressValue = 1.0
modelState = whisperKit.modelState
}
}
}
}

func deleteModel() {
if localModels.contains(selectedModel) {
let modelFolder = URL(fileURLWithPath: localModelPath).appendingPathComponent("openai_whisper-\(selectedModel)")

do {
try FileManager.default.removeItem(at: modelFolder)

if let index = localModels.firstIndex(of: selectedModel) {
localModels.remove(at: index)
}

modelState = .unloaded
} catch {
print("Error deleting model: \(error)")
}
}
}

func updateProgressBar(targetProgress: Float, maxTime: TimeInterval) async {
let initialProgress = loadingProgressValue