Skip to content

Commit

Permalink
Fix whipser setup (#7)
Browse files Browse the repository at this point in the history
* Create new PodcastBuddy.cache_dir, fix whisper setup, and use the cache dir for all whisper calls
* Move ShowAssistant and CoHost initialization into CLI.run so that it doesn't setup the session before it's configured
  • Loading branch information
codenamev authored Dec 19, 2024
1 parent a1d979a commit 5539ca2
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 14 deletions.
5 changes: 5 additions & 0 deletions lib/podcast_buddy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,12 @@ def start_session(name: nil)
@session = Session.new(name: name)
end

def cache_dir
@cache_dir ||= "#{ENV["HOME"]}/.buddy"
end

def setup
Dir.mkdir PodcastBuddy.cache_dir unless Dir.exist?(PodcastBuddy.cache_dir)
SystemDependency.auto_install!(:git)
SystemDependency.auto_install!(:sdl2)
SystemDependency.auto_install!(:whisper)
Expand Down
11 changes: 7 additions & 4 deletions lib/podcast_buddy/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@ class CLI
def initialize(argv)
@options = parse_options(argv)
@tasks = []
@show_assistant = PodcastBuddy::ShowAssistant.new
end

def run
configure_logger
configure_session
setup_dependencies
@show_assistant = PodcastBuddy::ShowAssistant.new
@co_host = PodcastBuddy::CoHost.new(listener: @show_assistant.listener)
start_recording
rescue Interrupt
handle_shutdown
Expand Down Expand Up @@ -77,9 +78,12 @@ def configure_session
if @options[:name]
base_path = "#{PodcastBuddy.root}/tmp/#{@options[:name]}"
FileUtils.mkdir_p base_path
PodcastBuddy.session = @options[:name]
PodcastBuddy.start_session(name: @options[:name])
PodcastBuddy.logger.info PodcastBuddy.to_human("Using custom session name: #{@options[:name]}", :info)
PodcastBuddy.logger.info PodcastBuddy.to_human(" Saving files to: #{PodcastBuddy.session}", :info)
PodcastBuddy.logger.info PodcastBuddy.to_human(" Saving files to: #{PodcastBuddy.session.base_path}", :info)
else
PodcastBuddy.start_session
PodcastBuddy.logger.info PodcastBuddy.to_human("Saving files to: #{PodcastBuddy.session.base_path}", :info)
end
end

Expand All @@ -92,7 +96,6 @@ def setup_dependencies

def start_recording
Sync do |task|
@co_host = PodcastBuddy::CoHost.new(listener: @show_assistant.listener)
show_assistant_task = task.async { @show_assistant.start }
co_host_task = task.async { @co_host.start }
@tasks = [
Expand Down
2 changes: 1 addition & 1 deletion lib/podcast_buddy/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def openai_client

# @return [String]
def whisper_command
"./whisper.cpp/stream -m ./whisper.cpp/models/ggml-#{whisper_model}.bin -t 8 --step 0 --length 5000 --keep 500 --vad-thold 0.75 --audio-ctx 0 --keep-context -c 1 -l en"
"#{PodcastBuddy.cache_dir}/whisper.cpp/build/bin/stream -m #{PodcastBuddy.cache_dir}/whisper.cpp/models/ggml-#{whisper_model}.bin -t 8 --step 0 --length 5000 --keep 500 --vad-thold 0.75 --audio-ctx 0 --keep-context -c 1 -l en"
end
end
end
14 changes: 8 additions & 6 deletions lib/podcast_buddy/system_dependency.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ def self.resolve_whisper_model(model)
end

def self.model_downloaded?(model)
File.exist?(File.join(PodcastBuddy.root, "whisper.cpp", "models", "ggml-#{model}.bin"))
File.exist?(File.join(PodcastBuddy.cache_dir, "whisper.cpp", "models", "ggml-#{model}.bin"))
end

def self.download_model(model)
originating_directory = Dir.pwd
Dir.chdir("whisper.cpp")
Dir.chdir("#{PodcastBuddy.cache_dir}/whisper.cpp")
PodcastBuddy.logger.info "Downloading GGML model: #{PodcastBuddy.whisper_model}"
PodcastBuddy.logger.info `bash ./models/download-ggml-model.sh #{PodcastBuddy.whisper_model}`
ensure
Expand All @@ -36,7 +36,7 @@ def self.download_model(model)
def installed?
PodcastBuddy.logger.info "Checking for system dependency: #{name}..."
if name.to_s == "whisper"
Dir.exist?("whisper.cpp")
Dir.exist?("#{PodcastBuddy.cache_dir}/whisper.cpp")
else
system("brew list -1 #{name} > /dev/null") || system("type -a #{name}")
end
Expand All @@ -46,12 +46,14 @@ def install
PodcastBuddy.logger.info "Installing #{name}..."
originating_directory = Dir.pwd
if name.to_s == "whisper"
Dir.chdir(PodcastBuddy.root)
Dir.chdir(PodcastBuddy.cache_dir)
PodcastBuddy.logger.info "Setting up whipser.cpp in #{PodcastBuddy.cache_dir}/whipser.cpp"
PodcastBuddy.logger.info `git clone https://github.com/ggerganov/whisper.cpp`
Dir.chdir("whisper.cpp")
PodcastBuddy.logger.info "Downloading GGML model: #{PodcastBuddy.whisper_model}"
PodcastBuddy.logger.info `bash ./models/download-ggml-model.sh #{PodcastBuddy.whisper_model}`
PodcastBuddy.logger.info `make && make stream`
PodcastBuddy.logger.info `bash #{PodcastBuddy.cache_dir}/whisper.cpp/models/download-ggml-model.sh #{PodcastBuddy.whisper_model}`
PodcastBuddy.logger.info "Building whipser.cpp with streaming support..."
PodcastBuddy.logger.info `cmake -B build -DWHISPER_SDL2=ON && cmake --build build --config Release`
else
`brew list #{name} || brew install #{name}`
end
Expand Down
3 changes: 3 additions & 0 deletions spec/cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

describe "#run" do
let(:show_assistant) { instance_double(PodcastBuddy::ShowAssistant) }
let(:listener) { instance_double(PodcastBuddy::Listener) }

before do
allow(PodcastBuddy).to receive(:logger).and_return(Logger.new(nil))
Expand All @@ -33,6 +34,8 @@
allow(show_assistant).to receive(:start)
allow(show_assistant).to receive(:stop)
allow(show_assistant).to receive(:generate_show_notes)
allow(show_assistant).to receive(:listener).and_return(listener)
allow(listener).to receive(:subscribe)
end

it "configures logger and session" do
Expand Down
4 changes: 2 additions & 2 deletions spec/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,13 @@

describe "#whisper_command" do
it "returns the expected command string" do
expected_command = "./whisper.cpp/stream -m ./whisper.cpp/models/ggml-small.en-q5_1.bin -t 8 --step 0 --length 5000 --keep 500 --vad-thold 0.75 --audio-ctx 0 --keep-context -c 1 -l en"
expected_command = "#{PodcastBuddy.cache_dir}/whisper.cpp/build/bin/stream -m #{PodcastBuddy.cache_dir}/whisper.cpp/models/ggml-small.en-q5_1.bin -t 8 --step 0 --length 5000 --keep 500 --vad-thold 0.75 --audio-ctx 0 --keep-context -c 1 -l en"
expect(config.whisper_command).to eq(expected_command)
end

it "incorporates changes to whisper_model" do
config.whisper_model = "test_model"
expected_command = "./whisper.cpp/stream -m ./whisper.cpp/models/ggml-test_model.bin -t 8 --step 0 --length 5000 --keep 500 --vad-thold 0.75 --audio-ctx 0 --keep-context -c 1 -l en"
expected_command = "#{PodcastBuddy.cache_dir}/whisper.cpp/build/bin/stream -m #{PodcastBuddy.cache_dir}/whisper.cpp/models/ggml-test_model.bin -t 8 --step 0 --length 5000 --keep 500 --vad-thold 0.75 --audio-ctx 0 --keep-context -c 1 -l en"
expect(config.whisper_command).to eq(expected_command)
end
end
Expand Down
2 changes: 2 additions & 0 deletions spec/fixtures/tmp/2024-12-19_13-39-47/transcript.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
test transcription
test transcription
3 changes: 2 additions & 1 deletion spec/fixtures/tmp/session/transcript.log
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
Existing transcript
Existing transcripttest transcription
test transcription

0 comments on commit 5539ca2

Please sign in to comment.