From 0a2380e60b261afad8846991b8dbd5f2289e73c0 Mon Sep 17 00:00:00 2001 From: Rose Liverman Date: Sat, 22 Aug 2026 21:32:13 -0600 Subject: [PATCH] finished? --- lib/timing.lua | 26 ++++++++++++++++------ rewrite.lua | 58 ++++++++++++++++++++++++++++++++++++-------------- 2 files changed, 61 insertions(+), 23 deletions(-) diff --git a/lib/timing.lua b/lib/timing.lua index 60ff108..ad4139c 100644 --- a/lib/timing.lua +++ b/lib/timing.lua @@ -1,9 +1,9 @@ -local timing = {} +local timing = { + entries = {} +} -function timing.timer() - return setmetatable({ - entries = {}, - }, timing) +local function to_minutes(t) -- floored to tenths + return math.floor( t / 60 * 10 ) / 10 end function timing:start() @@ -15,8 +15,20 @@ function timing:stop() local entry = self.entries[#self.entries] entry.stop_time = os.time() - -- delta in minutes, floored to tenths - return math.floor( (entry.stop_time - entry.start_time) / 60 * 10 ) / 10 + return to_minutes(entry.stop_time - entry.start_time) -- return delta +end + +function timing:print_report() + local minimum, maximum, sum = math.huge, -math.huge, 0 + for _, data in pairs(self.entries) do + local delta = data.stop_time - data.start_time + if delta > maximum then maximum = delta end + if delta < minimum then minimum = delta end + sum = sum + delta + end + print("Total: " .. to_minutes(sum) .. " minutes.") + print("Averge: " .. to_minutes(sum / #self.entries) .. " minutes. Fastest: " + .. to_minutes(minimum) .. " minutes. Slowest: " .. to_minutes(maximum) .. " minutes.") end return timing diff --git a/rewrite.lua b/rewrite.lua index 3d953bc..678cc1f 100644 --- a/rewrite.lua +++ b/rewrite.lua @@ -2,7 +2,8 @@ package.path = (arg[0]:match("@?(.*/)") or arg[0]:match("@?(.*\\)")) .. "lib" .. package.config:sub(1, 1) .. "?.lua;" .. package.path local utility = require "utility" --- TODO import timing library +local prompts = require "prompts" +local timing = require "timing" local config = utility.get_config("read-only") if not config.llm_covers then config.llm_covers = {} end @@ -10,7 +11,17 @@ if not config.llm_covers.calibre_location then config.llm_covers.calibre_locatio if not config.llm_covers.default_model then config.llm_covers.default_model = "gemma4:12b-mlx" end if not config.llm_covers.maximum_bytes then config.llm_covers.maximum_bytes = 40000 end --- TODO import prompts, use new names from fork +local read_all = function(file_name) + return utility.open(file_name, "r", function(file) + return file:read("*all") + end) +end +local write_all = function(file_name, text) + return utility.open(file_name, "w", function(file) + file:write(text) + file:write("\n") + end) +end local strip_reasoning = function(text, reasoning_lines) if not reasoning_lines then reasoning_lines = {} end @@ -31,18 +42,27 @@ local strip_reasoning = function(text, reasoning_lines) end end --- TODO import and rewrite send_prompt to use new timing library +local send_prompt = function(text, model) + if type(text) == "table" then text = table.concat(text, "\n") end -local read_all = function(file_name) - return utility.open(file_name, "r", function(file) - return file:read("*all") - end) -end -local write_all = function(file_name, text) - return utility.open(file_name, "w", function(file) + local tmp_file_name = utility.tmp_file_name() + utility.open(tmp_file_name, "w", function(file) file:write(text) - file:write("\n") end) + + timing:start() + -- word wrap breaks the raw output badly, so I need to implement my own for terminal output somehow + local output = utility.capture_safe("cat " .. tmp_file_name:enquote() .. " | ollama run " .. (model or default_model) .. " --nowordwrap") + os.execute("ollama stop " .. (model or default_model)) -- NOTE this makes things slower, but more stable + local delta = timing:stop() + + print("Took " .. delta .. " minutes.") + os.execute("rm " .. tmp_file_name) + if not output then error("ollama failed to generate output") end + + output = output:sub(1, -2) -- strip extra newline from utility.capture_safe + output = strip_reasoning(output) + return output end local make_directories = function() @@ -166,7 +186,7 @@ local condense_summary = function(name, data, sections) print("Loaded condensed summary segment. " .. #sections .. " sections remaining.") else print("Condensing summary. " .. #sections .. " sections remaining.") - local text = send_prompt(squish_partials_prompt .. table.concat(sections_slice, "\n\n")) + local text = send_prompt(prompts.combine_summaries .. table.concat(sections_slice, "\n\n")) condensed_sections[#condensed_sections + 1] = text print(#text .. " characters added to final context.") write_all(intermediate_condensation_file, text) @@ -199,7 +219,7 @@ local generate_summary = function(name, data) else print("Processing section " .. (i + 1) .. " of " .. math.floor(data.extracted_file_size/config.llm_covers.maximum_bytes + 1)) local section = text:sub(i * config.llm_covers.maximum_bytes, (i + 1) * config.llm_covers.maximum_bytes - 1) - section = send_prompt(partial_prompt .. section) + section = send_prompt(prompts.section_summarization .. section) sections[#sections + 1] = section print(#section .. " characters added to intermediate context.") write_all(intermediate_section_file, section) @@ -238,17 +258,23 @@ local generate_cover_briefs = function(books) end print("Generating cover brief.") - text = send_prompt(cover_brief_prompt .. text) + text = send_prompt(prompts.cover_brief .. text) local cover_brief_file = "cover_briefs/" .. name .. ".txt" write_all(cover_brief_file, text) - write_all("for_gemini/" .. name .. ".txt", "Title: " .. name .. gemini_prompt .. text) + write_all("for_gemini/" .. name .. ".txt", "Title: " .. name .. prompts.for_gemini .. text) data.cover_brief_file = cover_brief_file end end make_directories() -generate_cover_briefs(get_books()) + +local books = get_books() +convert_ebooks(books) +generate_cover_briefs(books) + +print("") +timing:print_report() -- ebook_file, extracted_file, extracted_file_size, -- intermediate_file, condensed_intermediate_file, cover_brief_file