Skip to content

Commit

Permalink
Removed progress bar due inconsistencies with logging
Browse files Browse the repository at this point in the history
  • Loading branch information
Neo-Ciber94 committed Jun 25, 2022
1 parent 358f528 commit 20f98cc
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 98 deletions.
17 changes: 9 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,23 @@ A CLI utility for generate images using DALL·E mini.

```bash
Usage: dallemini generate <prompt>
Version: 0.1.0
Version: 0.2.0

DALL-E Mini: https://huggingface.co/spaces/dalle-mini/dalle-mini

Description:

Generates an image from the given prompt using DALL-E Mini
Generates an image from the given prompt using DALL-E Mini

Options:

-h, --help - Show this help.
-v, --verbose [verbose] - Show additional information (Default: false)
-o, --output <output> - Output path of the images
-n, --name <name> - Name of the generate files
-r, --retry <retry> - Number of retries if the operation fails. (Default: 3)
-b, --batch <batch> - Number of times to generate images (Default: 1)
-h, --help - Show this help.
-s, --silent - Don't show any output
-l, --log [log] - Minimum log level (Default: "debug", Values: "debug", "info", "warn", "error")
-o, --output <output> - Output path of the images
-n, --name <name> - Name of the generate files
-r, --retry <retry> - Number of retries if the operation fails. (Default: 3)
-b, --batch <batch> - Number of times to generate images (Default: 1)
Examples:
Expand Down
3 changes: 1 addition & 2 deletions import-map.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
"std/colors": "https://deno.land/std@0.145.0/fmt/colors.ts",
"std/fs": "https://deno.land/std@0.144.0/fs/mod.ts",
"imagescript": "https://deno.land/x/imagescript@v1.2.13/mod.ts",
"cliffy": "https://deno.land/x/cliffy@v0.24.2/mod.ts",
"progress": "https://deno.land/x/progress@v1.2.8/mod.ts"
"cliffy": "https://deno.land/x/cliffy@v0.24.2/mod.ts"
}
}
102 changes: 32 additions & 70 deletions src/cli/generate.command.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
import { Command } from "cliffy";
import { MultiProgressBar } from "progress";
import { Command, EnumType } from "cliffy";
import {
DalleImageGenerator,
DEFAULT_BATCH,
DEFAULT_RETRY,
} from "../core/DalleImageGenerator.ts";
import { createConsoleLogger } from "../utils/logger.ts";
import { createConsoleLogger, LogLevel } from "../utils/logger.ts";

const logLevel = new EnumType<LogLevel>(["debug", "info", "warn", "error"]);

const generate = new Command()
.name("generate")
.description("Generates an image from the given prompt using DALL-E Mini")
.arguments("<prompt:string>")
.option("-v, --verbose", "Show additional information", {
.option("-s, --silent", "Don't show any output", {
collect: true,
value: (val: boolean) => val,
})
.type("logLevel", logLevel)
.option("-l, --log [log:logLevel]", "Minimum log level", {
default: "debug",
})
.option("-o, --output <output:string>", "Output path of the images")
.option("-n, --name <name:string>", "Name of the generate files")
.option(
Expand All @@ -27,72 +32,29 @@ const generate = new Command()
.option("-b, --batch <batch:number>", "Number of times to generate images", {
default: DEFAULT_BATCH,
})
.action(async ({ name, output, verbose, retry, batch }, prompt: string) => {
const logger = createConsoleLogger("debug");
const logging = verbose
? {
debug: logger.debug,
info: logger.info,
warn: logger.warn,
error: logger.error,
}
: undefined;

const abortController = new AbortController();
const _bars = renderProgressBar(abortController.signal);

const imageGenerator = new DalleImageGenerator({
filename: name,
output,
retry,
batch,
prompt,
logging,
});

await imageGenerator.generate();
abortController.abort();
});

function renderProgressBar(signal: AbortSignal): MultiProgressBar {
const MAX = 100;
const MS = 100;

const progressBar = new MultiProgressBar({
title: "Generating images with DALL-E Mini\n",
complete: "#",
incomplete: "-",
display: "[:bar] :text",
});

let current = 0;
let ms = 0;

const render = (seconds: number, completed: number) => {
progressBar.render([
{
text: `${seconds} seconds`,
completed,
total: MAX,
},
]);
};

const intervalId = setInterval(() => {
const seconds = Math.floor(ms / 1000);
if (signal.aborted) {
render(seconds, MAX);
clearInterval(intervalId);
return;
.action(
async ({ name, output, retry, silent, batch, log }, prompt: string) => {
const logger = createConsoleLogger(log as LogLevel);
const logging = !silent
? {
debug: logger.debug,
info: logger.info,
warn: logger.warn,
error: logger.error,
}
: undefined;

const imageGenerator = new DalleImageGenerator({
filename: name,
output,
retry,
batch,
prompt,
logging,
});

await imageGenerator.generate();
}

render(seconds, current % MAX);

current += 5;
ms += MS;
}, MS);

return progressBar;
}
);

export default generate;
4 changes: 4 additions & 0 deletions src/core/DalleImageGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,15 @@ export class DalleImageGenerator {
this.logger.debug(`With ${retry} ${retry > 1 ? "retries" : "retry"}`);
this.logger.debug(`With output path: ${this.getOutputPath()}`);

const startTime = Date.now();
for (let i = 0; i < batch; i++) {
generateBatchPromises.push(this.newBatch(i + 1, counter));
}

await Promise.all(generateBatchPromises);
const totalTime = Date.now() - startTime;
this.logger.info("Done");
this.logger.debug(`Duration ${totalTime / 1000} seconds`);
}

private async newBatch(batchId: number, counter: Counter) {
Expand Down
2 changes: 1 addition & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import generate from "./cli/generate.command.ts";
const command = new Command()
.name("dallemini")
.description("A CLI utility to generate images with DALL-E Mini")
.version("0.1.0")
.version("0.2.0")
.command("generate", generate)
.example("Generate image", "\n\ndallemini generate --output ./out pizza")
.meta("DALL-E Mini", "https://huggingface.co/spaces/dalle-mini/dalle-mini");
Expand Down
74 changes: 57 additions & 17 deletions src/utils/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,26 @@ import * as colors from "std/colors";

export type LogLevel = "debug" | "info" | "warn" | "error";

export type LogAdapter = (level: LogLevel, message: string) => boolean | void;

export interface Logger {
level: LogLevel;
adapters: LogAdapter[];
debug: (message: string) => void;
info: (message: string) => void;
warn: (message: string) => void;
error: (message: string) => void;
}

export function createConsoleLogger(level: LogLevel = "debug"): Logger {
const adapters: LogAdapter[] = [];
return {
level,
debug: (args) => log("debug", level, args),
info: (args) => log("info", level, args),
warn: (args) => log("warn", level, args),
error: (args) => log("error", level, args),
adapters,
debug: (args) => log("debug", level, adapters, args),
info: (args) => log("info", level, adapters, args),
warn: (args) => log("warn", level, adapters, args),
error: (args) => log("error", level, adapters, args),
};
}

Expand All @@ -37,26 +42,61 @@ function levelToNumber(level: LogLevel): number {
return 3;
case "error":
return 4;
default:
return 0;
}
}

function log(level: LogLevel, minLevel: LogLevel, message: string) {
function log(
level: LogLevel,
minLevel: LogLevel,
adapters: LogAdapter[],
message: string
) {
if (levelToNumber(minLevel) > levelToNumber(level)) {
return;
}

const runAdapters = (msg: string) => {
let run = true;
for (const adapter of adapters) {
const result = adapter(level, msg);
if (result === false) {
run = false;
}
}

return run;
};

switch (level) {
case "debug":
console.log(`${colors.magenta(LEVEL_LABEL.debug)} ${message}`);
break;
case "info":
console.info(`${colors.brightBlue(LEVEL_LABEL.info)} ${message}`);
break;
case "warn":
console.warn(`${colors.yellow(LEVEL_LABEL.warn)} ${message}`);
break;
case "error":
console.error(`${colors.red(LEVEL_LABEL.error)} ${message}`);
break;
case "debug": {
const msg = `${colors.magenta(LEVEL_LABEL.debug)} ${message}`;
if (runAdapters(msg)) {
console.log(msg);
}
return msg;
}
case "info": {
const msg = `${colors.brightBlue(LEVEL_LABEL.info)} ${message}`;
if (runAdapters(msg)) {
console.info(msg);
}
return msg;
}
case "warn": {
const msg = `${colors.yellow(LEVEL_LABEL.warn)} ${message}`;
if (runAdapters(msg)) {
console.warn(msg);
}
return msg;
}
case "error": {
const msg = `${colors.red(LEVEL_LABEL.error)} ${message}`;
if (runAdapters(msg)) {
console.error(msg);
}
return msg;
}
}
}

0 comments on commit 20f98cc

Please sign in to comment.