Spaces:
Sleeping
Sleeping
Julian Bilcke commited on
Commit ·
9bfb451
1
Parent(s): faf4ba4
add new "fast LLM" optimization
Browse filesFiles changed (6)
6 unmodified lines7891011121374 unmodified lines8889909192939419 unmodified lines1141151161171181191206 unmodified linesimport { fonts } from "@/lib/fonts"import { useStore } from "./store"import { Zoom } from "./interface/zoom"import { getStory } from "./queries/getStory"import { BottomBar } from "./interface/bottom-bar"import { Page } from "./interface/page"import { GeneratedPanel } from "@/types"74 unmodified linesnbPanelsToGenerate,existingPanels,})console.log("LLM generated some new panels:", candidatePanels)existingPanels.push(...candidatePanels)19 unmodified lines}// update the frontendconsole.log("updating the frontend..")setCaptions(newCaptions)setPanels(newPanelsPrompts)6 unmodified lines78910111274 unmodified lines878889909192939419 unmodified lines1141151161171181191206 unmodified linesimport { fonts } from "@/lib/fonts"import { useStore } from "./store"import { Zoom } from "./interface/zoom"import { BottomBar } from "./interface/bottom-bar"import { Page } from "./interface/page"import { GeneratedPanel } from "@/types"74 unmodified linesnbPanelsToGenerate,existingPanels,})console.log("LLM generated some new panels:")console.table(candidatePanels)existingPanels.push(...candidatePanels)19 unmodified lines}// update the frontend// console.log("updating the frontend..")setCaptions(newCaptions)setPanels(newPanelsPrompts)
16 unmodified lines1718192021222316 unmodified linesprompt: string;nbTotalPanels: number;}): Promise<GeneratedPanels> => {// throw new Error("Planned maintenance")// In case you need to quickly debug the RENDERING engine you can uncomment this:// return mockGeneratedPanels16 unmodified lines1718192021222316 unmodified linesprompt: string;nbTotalPanels: number;}): Promise<GeneratedPanels> => {throw new Error("legacy, deprecated")// In case you need to quickly debug the RENDERING engine you can uncomment this:// return mockGeneratedPanels
31 unmodified lines32333435363738394041424344454647484950515253546 unmodified lines6162636465666731 unmodified linesexistingPanels,})console.log("LLM responded with panels:", panelCandidates)// we clean the output from the LLM// most importantly, we need to adjust the panel index,// to start from where we last finishedfor (let p = startAt; p < endAt; p++) {panels.push({panel: p,instructions: `${panelCandidates[p]?.instructions || ""}`,caption: `${panelCandidates[p]?.caption || ""}`,})}} catch (err) {console.log("LLM step failed due to:", err)console.log("we are now switching to a degraded mode, using 4 similar panels")panels = []for (let p = startAt; p < endAt; p++) {panels.push({6 unmodified linescaption: "(Sorry, LLM generation failed: using degraded mode)"})}console.error(err)} finally {return panels}31 unmodified lines323334353637383940414243444546474849505152536 unmodified lines6061626364656631 unmodified linesexistingPanels,})// console.log("LLM responded with panelCandidates:", panelCandidates)// we clean the output from the LLM// most importantly, we need to adjust the panel index,// to start from where we last finishedfor (let i = 0; i < nbPanelsToGenerate; i++) {panels.push({panel: startAt + i,instructions: `${panelCandidates[i]?.instructions || ""}`,caption: `${panelCandidates[i]?.caption || ""}`,})}} catch (err) {// console.log("LLM step failed due to:", err)// console.log("we are now switching to a degraded mode, using 4 similar panels")panels = []for (let p = startAt; p < endAt; p++) {panels.push({6 unmodified linescaption: "(Sorry, LLM generation failed: using degraded mode)"})}// console.error(err)} finally {return panels}
17 unmodified lines18192021222317 unmodified linesnbPanelsToGenerate: number;existingPanels: GeneratedPanel[];}): Promise<GeneratedPanel[]> => {// throw new Error("Planned maintenance")// In case you need to quickly debug the RENDERING engine you can uncomment this:17 unmodified lines1819202122232417 unmodified linesnbPanelsToGenerate: number;existingPanels: GeneratedPanel[];}): Promise<GeneratedPanel[]> => {// console.log("predictNextPanels: ", { prompt, nbPanelsToGenerate })// throw new Error("Planned maintenance")// In case you need to quickly debug the RENDERING engine you can uncomment this:
57 unmodified lines5859606162636413 unmodified lines7879808182838457 unmodified lines}})) {instructions += output.token.textprocess.stdout.write(output.token.text)if (instructions.includes("</s>") ||instructions.includes("<s>") ||13 unmodified lines}}} catch (err) {console.error(`error during generation: ${err}`)// a common issue with Llama-2 might be that the model receives too many requestsif (`${err}` === "Error: Model is overloaded") {57 unmodified lines5859606162636413 unmodified lines7879808182838457 unmodified lines}})) {instructions += output.token.text// process.stdout.write(output.token.text)if (instructions.includes("</s>") ||instructions.includes("<s>") ||13 unmodified lines}}} catch (err) {// console.error(`error during generation: ${err}`)// a common issue with Llama-2 might be that the model receives too many requestsif (`${err}` === "Error: Model is overloaded") {
1 unmodified line23456789101112131 unmodified linelet str = (`${input || ""}`// a summary of all the weird hallucinations I saw it make...replaceAll(`"]`, `"}]`).replaceAll(`" ]`, `"}]`).replaceAll(`" ]`, `"}]`).replaceAll(`"\n]`, `"}]`).replaceAll(`"\n ]`, `"}]`).replaceAll(`"\n ]`, `"}]`).replaceAll("}}", "}").replaceAll("]]", "]").replaceAll("[[", "[")1 unmodified line234567891011121314151 unmodified linelet str = (`${input || ""}`// a summary of all the weird hallucinations I saw it make...replaceAll(`"\n`, `",\n`) // fix missing commas at the end of a line.replaceAll(`"]`, `"}]`).replaceAll(/"\S*,?\S*\]/gi, `"}]`).replaceAll(/"\S*,?\S*\}\S*]/gi, `"}]`)// this removes the trailing commas (which are valid in JS but not JSON).replace(/,(?=\s*?[\}\]])/g, '').replaceAll("}}", "}").replaceAll("]]", "]").replaceAll("[[", "[")
src/app/main.tsx+3-3
src/app/queries/getStory.ts+1-1
src/app/queries/getStoryContinuation.ts+8-9
src/app/queries/predictNextPanels.ts+1
src/app/queries/predictWithHuggingFace.ts+2-2
src/lib/dirtyLLMResponseCleaner.ts+7-5