forked from pinks/eris
1
0
Fork 0

move files

This commit is contained in:
pinks 2023-09-13 11:50:22 +02:00
parent 0ab1f89579
commit fcb655ea09
17 changed files with 253 additions and 246 deletions

View File

@ -1,7 +1,7 @@
import { Collections, Grammy, GrammyStatelessQ } from "../deps.ts"; import { Collections, Grammy, GrammyStatelessQ } from "../deps.ts";
import { formatUserChat } from "../utils.ts"; import { formatUserChat } from "../common/utils.ts";
import { jobStore } from "../db/jobStore.ts"; import { jobStore } from "../db/jobStore.ts";
import { parsePngInfo, PngInfo } from "../sd.ts"; import { parsePngInfo, PngInfo } from "../common/parsePngInfo.ts";
import { Context, logger } from "./mod.ts"; import { Context, logger } from "./mod.ts";
export const img2imgQuestion = new GrammyStatelessQ.StatelessQuestion<Context>( export const img2imgQuestion = new GrammyStatelessQ.StatelessQuestion<Context>(

View File

@ -1,5 +1,5 @@
import { Grammy, GrammyAutoQuote, GrammyFiles, GrammyParseMode, Log } from "../deps.ts"; import { Grammy, GrammyAutoQuote, GrammyFiles, GrammyParseMode, Log } from "../deps.ts";
import { formatUserChat } from "../utils.ts"; import { formatUserChat } from "../common/utils.ts";
import { session, SessionFlavor } from "./session.ts"; import { session, SessionFlavor } from "./session.ts";
import { queueCommand } from "./queueCommand.ts"; import { queueCommand } from "./queueCommand.ts";
import { txt2imgCommand, txt2imgQuestion } from "./txt2imgCommand.ts"; import { txt2imgCommand, txt2imgQuestion } from "./txt2imgCommand.ts";

View File

@ -1,7 +1,6 @@
import { Grammy, GrammyParseMode, GrammyStatelessQ } from "../deps.ts"; import { Grammy, GrammyParseMode, GrammyStatelessQ } from "../deps.ts";
import { fmt } from "../utils.ts"; import { fmt } from "../common/utils.ts";
import { getPngInfo, parsePngInfo } from "../common/parsePngInfo.ts";
import { getPngInfo, parsePngInfo } from "../sd.ts";
import { Context } from "./mod.ts"; import { Context } from "./mod.ts";
export const pnginfoQuestion = new GrammyStatelessQ.StatelessQuestion<Context>( export const pnginfoQuestion = new GrammyStatelessQ.StatelessQuestion<Context>(

View File

@ -1,8 +1,9 @@
import { Grammy, GrammyParseMode } from "../deps.ts"; import { Grammy, GrammyParseMode } from "../deps.ts";
import { fmt, getFlagEmoji } from "../utils.ts"; import { fmt } from "../common/utils.ts";
import { runningWorkers } from "../tasks/pingWorkers.ts"; import { runningWorkers } from "../tasks/pingWorkers.ts";
import { jobStore } from "../db/jobStore.ts"; import { jobStore } from "../db/jobStore.ts";
import { Context, logger } from "./mod.ts"; import { Context, logger } from "./mod.ts";
import { getFlagEmoji } from "../common/getFlagEmoji.ts";
export async function queueCommand(ctx: Grammy.CommandContext<Context>) { export async function queueCommand(ctx: Grammy.CommandContext<Context>) {
let formattedMessage = await getMessageText(); let formattedMessage = await getMessageText();

View File

@ -1,6 +1,6 @@
import { db } from "../db/db.ts"; import { db } from "../db/db.ts";
import { Grammy, GrammyKvStorage } from "../deps.ts"; import { Grammy, GrammyKvStorage } from "../deps.ts";
import { SdApi, SdTxt2ImgRequest } from "../sd.ts"; import { SdApi, SdTxt2ImgRequest } from "../common/sdApi.ts";
export type SessionFlavor = Grammy.SessionFlavor<SessionData>; export type SessionFlavor = Grammy.SessionFlavor<SessionData>;

View File

@ -1,7 +1,7 @@
import { Grammy, GrammyStatelessQ } from "../deps.ts"; import { Grammy, GrammyStatelessQ } from "../deps.ts";
import { formatUserChat } from "../utils.ts"; import { formatUserChat } from "../common/utils.ts";
import { jobStore } from "../db/jobStore.ts"; import { jobStore } from "../db/jobStore.ts";
import { getPngInfo, parsePngInfo, PngInfo } from "../sd.ts"; import { getPngInfo, parsePngInfo, PngInfo } from "../common/parsePngInfo.ts";
import { Context, logger } from "./mod.ts"; import { Context, logger } from "./mod.ts";
export const txt2imgQuestion = new GrammyStatelessQ.StatelessQuestion<Context>( export const txt2imgQuestion = new GrammyStatelessQ.StatelessQuestion<Context>(

51
common/getFlagEmoji.ts Normal file
View File

@ -0,0 +1,51 @@
/** Language to biggest country emoji map */
const languageToFlagMap: Record<string, string> = {
"en": "🇺🇸",
"zh": "🇨🇳",
"es": "🇪🇸",
"hi": "🇮🇳",
"ar": "🇪🇬",
"pt": "🇧🇷",
"bn": "🇧🇩",
"ru": "🇷🇺",
"ja": "🇯🇵",
"pa": "🇮🇳",
"de": "🇩🇪",
"ko": "🇰🇷",
"fr": "🇫🇷",
"tr": "🇹🇷",
"ur": "🇵🇰",
"it": "🇮🇹",
"th": "🇹🇭",
"vi": "🇻🇳",
"pl": "🇵🇱",
"uk": "🇺🇦",
"uz": "🇺🇿",
"su": "🇮🇩",
"sw": "🇹🇿",
"nl": "🇳🇱",
"fi": "🇫🇮",
"el": "🇬🇷",
"da": "🇩🇰",
"cs": "🇨🇿",
"sk": "🇸🇰",
"bg": "🇧🇬",
"sv": "🇸🇪",
"be": "🇧🇾",
"hu": "🇭🇺",
"lt": "🇱🇹",
"lv": "🇱🇻",
"et": "🇪🇪",
"sl": "🇸🇮",
"hr": "🇭🇷",
"zu": "🇿🇦",
"id": "🇮🇩",
"is": "🇮🇸",
"lb": "🇱🇺", // Luxembourgish - Luxembourg
};
export function getFlagEmoji(languageCode?: string): string | undefined {
const language = languageCode?.split("-").pop()?.toLowerCase();
if (!language) return;
return languageToFlagMap[language];
}

View File

@ -3,7 +3,7 @@ import {
assertEquals, assertEquals,
assertMatch, assertMatch,
} from "https://deno.land/std@0.135.0/testing/asserts.ts"; } from "https://deno.land/std@0.135.0/testing/asserts.ts";
import { parsePngInfo } from "./sd.ts"; import { parsePngInfo } from "./parsePngInfo.ts";
Deno.test("parses pnginfo", async (t) => { Deno.test("parses pnginfo", async (t) => {
await t.step("1", () => { await t.step("1", () => {

117
common/parsePngInfo.ts Normal file
View File

@ -0,0 +1,117 @@
import { PngChunksExtract, PngChunkText } from "../deps.ts";
export function getPngInfo(pngData: Uint8Array): string | undefined {
return PngChunksExtract.default(pngData)
.filter((chunk) => chunk.name === "tEXt")
.map((chunk) => PngChunkText.decode(chunk.data))
.find((textChunk) => textChunk.keyword === "parameters")
?.text;
}
export interface PngInfo {
prompt: string;
negative_prompt: string;
steps: number;
cfg_scale: number;
width: number;
height: number;
sampler_name: string;
seed: number;
denoising_strength: number;
}
export function parsePngInfo(pngInfo: string): Partial<PngInfo> {
const tags = pngInfo.split(/[,;]+|\.+\s|\n/u);
let part: "prompt" | "negative_prompt" | "params" = "prompt";
const params: Partial<PngInfo> = {};
const prompt: string[] = [];
const negativePrompt: string[] = [];
for (const tag of tags) {
const paramValuePair = tag.trim().match(/^(\w+\s*\w*):\s+(.*)$/u);
if (paramValuePair) {
const [, param, value] = paramValuePair;
switch (param.replace(/\s+/u, "").toLowerCase()) {
case "positiveprompt":
case "positive":
case "prompt":
case "pos":
part = "prompt";
prompt.push(value.trim());
break;
case "negativeprompt":
case "negative":
case "neg":
part = "negative_prompt";
negativePrompt.push(value.trim());
break;
case "steps":
case "cycles": {
part = "params";
const steps = Number(value.trim());
if (steps > 0) params.steps = Math.min(steps, 50);
break;
}
case "cfgscale":
case "cfg":
case "detail": {
part = "params";
const cfgScale = Number(value.trim());
if (cfgScale > 0) params.cfg_scale = Math.min(cfgScale, 20);
break;
}
case "size":
case "resolution": {
part = "params";
const [width, height] = value.trim()
.split(/\s*[x,]\s*/u, 2)
.map((v) => v.trim())
.map(Number);
if (width > 0 && height > 0) {
params.width = Math.min(width, 2048);
params.height = Math.min(height, 2048);
}
break;
}
case "denoisingstrength":
case "denoising":
case "denoise": {
part = "params";
// allow percent or decimal
let denoisingStrength: number;
if (value.trim().endsWith("%")) {
denoisingStrength = Number(value.trim().slice(0, -1).trim()) / 100;
} else {
denoisingStrength = Number(value.trim());
}
denoisingStrength = Math.min(Math.max(denoisingStrength, 0), 1);
params.denoising_strength = denoisingStrength;
break;
}
case "seed":
case "model":
case "modelhash":
case "modelname":
case "sampler":
part = "params";
// ignore for now
break;
default:
break;
}
} else if (tag.trim().length > 0) {
switch (part) {
case "prompt":
prompt.push(tag.trim());
break;
case "negative_prompt":
negativePrompt.push(tag.trim());
break;
default:
break;
}
}
}
if (prompt.length > 0) params.prompt = prompt.join(", ");
if (negativePrompt.length > 0) params.negative_prompt = negativePrompt.join(", ");
return params;
}

View File

@ -1,4 +1,4 @@
import { Async, AsyncX, PngChunksExtract, PngChunkText } from "./deps.ts"; import { Async, AsyncX } from "../deps.ts";
export interface SdApi { export interface SdApi {
url: string; url: string;
@ -298,119 +298,3 @@ export class SdApiError extends Error {
super(message); super(message);
} }
} }
export function getPngInfo(pngData: Uint8Array): string | undefined {
return PngChunksExtract.default(pngData)
.filter((chunk) => chunk.name === "tEXt")
.map((chunk) => PngChunkText.decode(chunk.data))
.find((textChunk) => textChunk.keyword === "parameters")
?.text;
}
export interface PngInfo {
prompt: string;
negative_prompt: string;
steps: number;
cfg_scale: number;
width: number;
height: number;
sampler_name: string;
seed: number;
denoising_strength: number;
}
export function parsePngInfo(pngInfo: string): Partial<PngInfo> {
const tags = pngInfo.split(/[,;]+|\.+\s|\n/u);
let part: "prompt" | "negative_prompt" | "params" = "prompt";
const params: Partial<PngInfo> = {};
const prompt: string[] = [];
const negativePrompt: string[] = [];
for (const tag of tags) {
const paramValuePair = tag.trim().match(/^(\w+\s*\w*):\s+(.*)$/u);
if (paramValuePair) {
const [, param, value] = paramValuePair;
switch (param.replace(/\s+/u, "").toLowerCase()) {
case "positiveprompt":
case "positive":
case "prompt":
case "pos":
part = "prompt";
prompt.push(value.trim());
break;
case "negativeprompt":
case "negative":
case "neg":
part = "negative_prompt";
negativePrompt.push(value.trim());
break;
case "steps":
case "cycles": {
part = "params";
const steps = Number(value.trim());
if (steps > 0) params.steps = Math.min(steps, 50);
break;
}
case "cfgscale":
case "cfg":
case "detail": {
part = "params";
const cfgScale = Number(value.trim());
if (cfgScale > 0) params.cfg_scale = Math.min(cfgScale, 20);
break;
}
case "size":
case "resolution": {
part = "params";
const [width, height] = value.trim()
.split(/\s*[x,]\s*/u, 2)
.map((v) => v.trim())
.map(Number);
if (width > 0 && height > 0) {
params.width = Math.min(width, 2048);
params.height = Math.min(height, 2048);
}
break;
}
case "denoisingstrength":
case "denoising":
case "denoise": {
part = "params";
// allow percent or decimal
let denoisingStrength: number;
if (value.trim().endsWith("%")) {
denoisingStrength = Number(value.trim().slice(0, -1).trim()) / 100;
} else {
denoisingStrength = Number(value.trim());
}
denoisingStrength = Math.min(Math.max(denoisingStrength, 0), 1);
params.denoising_strength = denoisingStrength;
break;
}
case "seed":
case "model":
case "modelhash":
case "modelname":
case "sampler":
part = "params";
// ignore for now
break;
default:
break;
}
} else if (tag.trim().length > 0) {
switch (part) {
case "prompt":
prompt.push(tag.trim());
break;
case "negative_prompt":
negativePrompt.push(tag.trim());
break;
default:
break;
}
}
}
if (prompt.length > 0) params.prompt = prompt.join(", ");
if (negativePrompt.length > 0) params.negative_prompt = negativePrompt.join(", ");
return params;
}

60
common/utils.ts Normal file
View File

@ -0,0 +1,60 @@
import { GrammyParseMode, GrammyTypes } from "../deps.ts";
export function formatOrdinal(n: number) {
if (n % 100 === 11 || n % 100 === 12 || n % 100 === 13) return `${n}th`;
if (n % 10 === 1) return `${n}st`;
if (n % 10 === 2) return `${n}nd`;
if (n % 10 === 3) return `${n}rd`;
return `${n}th`;
}
export const fmt = (
rawStringParts: TemplateStringsArray | GrammyParseMode.Stringable[],
...stringLikes: GrammyParseMode.Stringable[]
): GrammyParseMode.FormattedString => {
let text = "";
const entities: GrammyTypes.MessageEntity[] = [];
const length = Math.max(rawStringParts.length, stringLikes.length);
for (let i = 0; i < length; i++) {
for (const stringLike of [rawStringParts[i], stringLikes[i]]) {
if (stringLike instanceof GrammyParseMode.FormattedString) {
entities.push(
...stringLike.entities.map((e) => ({
...e,
offset: e.offset + text.length,
})),
);
}
if (stringLike != null) text += stringLike.toString();
}
}
return new GrammyParseMode.FormattedString(text, entities);
};
export function formatUserChat(ctx: { from?: GrammyTypes.User; chat?: GrammyTypes.Chat }) {
const msg: string[] = [];
if (ctx.from) {
msg.push(ctx.from.first_name);
if (ctx.from.last_name) msg.push(ctx.from.last_name);
if (ctx.from.username) msg.push(`(@${ctx.from.username})`);
if (ctx.from.language_code) msg.push(`(${ctx.from.language_code.toUpperCase()})`);
}
if (ctx.chat) {
if (
ctx.chat.type === "group" ||
ctx.chat.type === "supergroup" ||
ctx.chat.type === "channel"
) {
msg.push("in");
msg.push(ctx.chat.title);
if (
(ctx.chat.type === "supergroup" || ctx.chat.type === "channel") &&
ctx.chat.username
) {
msg.push(`(@${ctx.chat.username})`);
}
}
}
return msg.join(" ");
}

View File

@ -1,5 +1,6 @@
import { GrammyTypes, IKV } from "../deps.ts"; import { GrammyTypes, IKV } from "../deps.ts";
import { PngInfo, SdTxt2ImgInfo } from "../sd.ts"; import { SdTxt2ImgInfo } from "../common/sdApi.ts";
import { PngInfo } from "../common/parsePngInfo.ts";
import { db } from "./db.ts"; import { db } from "./db.ts";
export interface JobSchema { export interface JobSchema {

View File

@ -1,6 +1,6 @@
import { Async, Log } from "../deps.ts"; import { Async, Log } from "../deps.ts";
import { getGlobalSession } from "../bot/session.ts"; import { getGlobalSession } from "../bot/session.ts";
import { sdGetConfig } from "../sd.ts"; import { sdGetConfig } from "../common/sdApi.ts";
const logger = () => Log.getLogger(); const logger = () => Log.getLogger();

View File

@ -11,8 +11,14 @@ import {
} from "../deps.ts"; } from "../deps.ts";
import { bot } from "../bot/mod.ts"; import { bot } from "../bot/mod.ts";
import { getGlobalSession, GlobalData, WorkerData } from "../bot/session.ts"; import { getGlobalSession, GlobalData, WorkerData } from "../bot/session.ts";
import { fmt, formatUserChat } from "../utils.ts"; import { fmt, formatUserChat } from "../common/utils.ts";
import { SdApiError, sdImg2Img, SdProgressResponse, SdResponse, sdTxt2Img } from "../sd.ts"; import {
SdApiError,
sdImg2Img,
SdProgressResponse,
SdResponse,
sdTxt2Img,
} from "../common/sdApi.ts";
import { JobSchema, jobStore } from "../db/jobStore.ts"; import { JobSchema, jobStore } from "../db/jobStore.ts";
import { runningWorkers } from "./pingWorkers.ts"; import { runningWorkers } from "./pingWorkers.ts";

View File

@ -1,5 +1,5 @@
import { FmtDuration, Log } from "../deps.ts"; import { FmtDuration, Log } from "../deps.ts";
import { formatUserChat } from "../utils.ts"; import { formatUserChat } from "../common/utils.ts";
import { jobStore } from "../db/jobStore.ts"; import { jobStore } from "../db/jobStore.ts";
const logger = () => Log.getLogger(); const logger = () => Log.getLogger();

View File

@ -1,6 +1,6 @@
import { Log } from "../deps.ts"; import { Log } from "../deps.ts";
import { bot } from "../bot/mod.ts"; import { bot } from "../bot/mod.ts";
import { formatOrdinal } from "../utils.ts"; import { formatOrdinal } from "../common/utils.ts";
import { jobStore } from "../db/jobStore.ts"; import { jobStore } from "../db/jobStore.ts";
const logger = () => Log.getLogger(); const logger = () => Log.getLogger();

112
utils.ts
View File

@ -1,112 +0,0 @@
import { GrammyParseMode, GrammyTypes } from "./deps.ts";
export function formatOrdinal(n: number) {
if (n % 100 === 11 || n % 100 === 12 || n % 100 === 13) return `${n}th`;
if (n % 10 === 1) return `${n}st`;
if (n % 10 === 2) return `${n}nd`;
if (n % 10 === 3) return `${n}rd`;
return `${n}th`;
}
export const fmt = (
rawStringParts: TemplateStringsArray | GrammyParseMode.Stringable[],
...stringLikes: GrammyParseMode.Stringable[]
): GrammyParseMode.FormattedString => {
let text = "";
const entities: GrammyTypes.MessageEntity[] = [];
const length = Math.max(rawStringParts.length, stringLikes.length);
for (let i = 0; i < length; i++) {
for (const stringLike of [rawStringParts[i], stringLikes[i]]) {
if (stringLike instanceof GrammyParseMode.FormattedString) {
entities.push(
...stringLike.entities.map((e) => ({
...e,
offset: e.offset + text.length,
})),
);
}
if (stringLike != null) text += stringLike.toString();
}
}
return new GrammyParseMode.FormattedString(text, entities);
};
export function formatUserChat(ctx: { from?: GrammyTypes.User; chat?: GrammyTypes.Chat }) {
const msg: string[] = [];
if (ctx.from) {
msg.push(ctx.from.first_name);
if (ctx.from.last_name) msg.push(ctx.from.last_name);
if (ctx.from.username) msg.push(`(@${ctx.from.username})`);
if (ctx.from.language_code) msg.push(`(${ctx.from.language_code.toUpperCase()})`);
}
if (ctx.chat) {
if (
ctx.chat.type === "group" ||
ctx.chat.type === "supergroup" ||
ctx.chat.type === "channel"
) {
msg.push("in");
msg.push(ctx.chat.title);
if (
(ctx.chat.type === "supergroup" || ctx.chat.type === "channel") &&
ctx.chat.username
) {
msg.push(`(@${ctx.chat.username})`);
}
}
}
return msg.join(" ");
}
/** Language to biggest country emoji map */
const languageToFlagMap: Record<string, string> = {
"en": "🇺🇸", // English - United States
"zh": "🇨🇳", // Chinese - China
"es": "🇪🇸", // Spanish - Spain
"hi": "🇮🇳", // Hindi - India
"ar": "🇪🇬", // Arabic - Egypt
"pt": "🇧🇷", // Portuguese - Brazil
"bn": "🇧🇩", // Bengali - Bangladesh
"ru": "🇷🇺", // Russian - Russia
"ja": "🇯🇵", // Japanese - Japan
"pa": "🇮🇳", // Punjabi - India
"de": "🇩🇪", // German - Germany
"ko": "🇰🇷", // Korean - South Korea
"fr": "🇫🇷", // French - France
"tr": "🇹🇷", // Turkish - Turkey
"ur": "🇵🇰", // Urdu - Pakistan
"it": "🇮🇹", // Italian - Italy
"th": "🇹🇭", // Thai - Thailand
"vi": "🇻🇳", // Vietnamese - Vietnam
"pl": "🇵🇱", // Polish - Poland
"uk": "🇺🇦", // Ukrainian - Ukraine
"uz": "🇺🇿", // Uzbek - Uzbekistan
"su": "🇮🇩", // Sundanese - Indonesia
"sw": "🇹🇿", // Swahili - Tanzania
"nl": "🇳🇱", // Dutch - Netherlands
"fi": "🇫🇮", // Finnish - Finland
"el": "🇬🇷", // Greek - Greece
"da": "🇩🇰", // Danish - Denmark
"cs": "🇨🇿", // Czech - Czech Republic
"sk": "🇸🇰", // Slovak - Slovakia
"bg": "🇧🇬", // Bulgarian - Bulgaria
"sv": "🇸🇪", // Swedish - Sweden
"be": "🇧🇾", // Belarusian - Belarus
"hu": "🇭🇺", // Hungarian - Hungary
"lt": "🇱🇹", // Lithuanian - Lithuania
"lv": "🇱🇻", // Latvian - Latvia
"et": "🇪🇪", // Estonian - Estonia
"sl": "🇸🇮", // Slovenian - Slovenia
"hr": "🇭🇷", // Croatian - Croatia
"zu": "🇿🇦", // Zulu - South Africa
"id": "🇮🇩", // Indonesian - Indonesia
"is": "🇮🇸", // Icelandic - Iceland
"lb": "🇱🇺", // Luxembourgish - Luxembourg
};
export function getFlagEmoji(languageCode?: string): string | undefined {
const language = languageCode?.split("-").pop()?.toLowerCase();
if (!language) return;
return languageToFlagMap[language];
}