Skip to content

Commit

Permalink
Merge pull request #233 from harmony-one/dev
Browse files Browse the repository at this point in the history
Merge to Master 9/1 1.0
  • Loading branch information
theofandrich authored Sep 1, 2023
2 parents f4ac3e9 + cb7d7d0 commit d8e5bb6
Show file tree
Hide file tree
Showing 22 changed files with 2,129 additions and 191 deletions.
26 changes: 26 additions & 0 deletions src/bot.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import {TranslateBot} from "./modules/translate/TranslateBot";

require("events").EventEmitter.defaultMaxListeners = 30;
import express from "express";
import {
Expand Down Expand Up @@ -93,6 +95,10 @@ function createInitialSessionData(): BotSessionData {
oneCountry: {
lastDomain: "",
},
translate: {
languages: [],
enable: false
}
};
}

Expand All @@ -116,6 +122,7 @@ const payments = new BotPayments();
const schedule = new BotSchedule(bot);
const openAiBot = new OpenAIBot(payments);
const oneCountryBot = new OneCountryBot();
const translateBot = new TranslateBot();

bot.on("message:new_chat_members:me", async (ctx) => {
try {
Expand Down Expand Up @@ -257,6 +264,25 @@ const onMessage = async (ctx: OnMessageContext) => {
}
return;
}

if (translateBot.isSupportedEvent(ctx)) {
const price = translateBot.getEstimatedPrice(ctx);
const isPaid = await payments.pay(ctx, price);

if(isPaid) {
const response = await translateBot.onEvent(ctx, (reason?: string) => {
payments.refundPayment(reason, ctx, price);
}).catch((e) => {
payments.refundPayment(e.message || "Unknown error", ctx, price);
return {next: false};
});

if (!response.next) {
return;
}
}
}

if (openAiBot.isSupportedEvent(ctx)) {
if (ctx.session.openAi.imageGen.isEnabled) {
const price = openAiBot.getEstimatedPrice(ctx);
Expand Down
6 changes: 3 additions & 3 deletions src/database/stats.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ export class StatsService {
const currentTime = moment();
const dateStart = moment()
.tz('America/Los_Angeles')
.set({ hour: 23, minute: 59, second: 0 })
.subtract(1 + daysPeriod,'days')
.set({ hour: 0, minute: 0, second: 0 })
.subtract(daysPeriod,'days')
.unix()

const dateEnd = currentTime.unix();
Expand All @@ -67,7 +67,7 @@ export class StatsService {
const currentTime = moment();
const dateStart = moment()
.tz('America/Los_Angeles')
.set({ hour: 23, minute: 59, second: 0 })
.set({ hour: 0, minute: 0, second: 0 })
.subtract(daysPeriod,'days')
.unix()

Expand Down
85 changes: 82 additions & 3 deletions src/modules/sd-images/SDImagesBotBase.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { SDNodeApi, IModel } from "./api";
import { OnMessageContext, OnCallBackQueryData } from "../types";
import { sleep, uuidv4 } from "./utils";
import { getTelegramFileUrl, loadFile, sleep, uuidv4 } from "./utils";
import { InputFile } from "grammy";
import { COMMAND } from './helpers';

Expand Down Expand Up @@ -91,11 +91,11 @@ export class SDImagesBotBase {

ctx.chatAction = "upload_photo";

const imageBuffer = await this.sdNodeApi.generateImage(
const imageBuffer = await this.sdNodeApi.generateImage({
prompt,
model,
seed
);
});

const reqMessage = session.message ?
session.message.split(' ').length > 1 ?
Expand All @@ -119,4 +119,83 @@ export class SDImagesBotBase {

this.queue = this.queue.filter((v) => v !== uuid);
}

generateImageByImage = async (
ctx: OnMessageContext | OnCallBackQueryData,
refundCallback: (reason?: string) => void,
session: ISession
) => {
const { model, prompt, seed } = session;
const uuid = uuidv4();

try {
const queueMessageId = await this.waitingQueue(uuid, ctx);

ctx.chatAction = "upload_photo";

let fileBuffer: Buffer;
let width, height;
let fileName;

const photos = ctx.message?.photo || ctx.message?.reply_to_message?.photo;

if (photos) {
const photo = photos[photos.length - 1];

width = photo.width;
height = photo.height;

const file = await ctx.api.getFile(photo.file_id);

if (file?.file_path) {
const url = getTelegramFileUrl(file?.file_path);
fileName = file?.file_path;

fileBuffer = await loadFile(url);
} else {
throw new Error("File not found");
}
} else {
throw new Error("User image not found");
}

const imageBuffer = await this.sdNodeApi.generateImageByImage({
fileBuffer,
fileName,
prompt,
model,
seed,
});

const reqMessage = session.message ?
session.message.split(' ').length > 1 ?
session.message :
`${session.message} ${prompt}`
:
`/${model.aliases[0]} ${prompt}`;

await ctx.replyWithMediaGroup([
{
type: "photo",
media: new InputFile(fileBuffer),
caption: reqMessage,
},
{
type: "photo",
media: new InputFile(imageBuffer),
// caption: reqMessage,
}
]);

if (ctx.chat?.id && queueMessageId) {
await ctx.api.deleteMessage(ctx.chat?.id, queueMessageId);
}
} catch (e) {
console.error(e);
ctx.reply(`Error: something went wrong... Refunding payments`);
refundCallback();
}

this.queue = this.queue.filter((v) => v !== uuid);
}
}
114 changes: 114 additions & 0 deletions src/modules/sd-images/api/configs/img2img_config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import { Txt2ImgOptions } from './types';

export interface Img2ImgOptions extends Txt2ImgOptions {
denoise?: number;
fileName: string;
controlnetVersion: number;
}

export function buildImg2ImgPrompt(options: Img2ImgOptions & {
clientId: string;
fileName: string;
}) {
return {
client_id: options.clientId,
"prompt": {
"3": {
"inputs": {
seed: options.seed,
steps: options.steps,
cfg: options.cfgScale,
"sampler_name": "euler",
"scheduler": "normal",
"denoise": options.denoise || 0.5,
"model": [
"4",
0
],
"positive": [
"6",
0
],
"negative": [
"7",
0
],
"latent_image": [
"10",
0
]
},
"class_type": "KSampler"
},
"4": {
"inputs": {
"ckpt_name": options.model
},
"class_type": "CheckpointLoaderSimple"
},
"6": {
"inputs": {
"text": options.prompt,
"clip": [
"4",
1
]
},
"class_type": "CLIPTextEncode"
},
"7": {
"inputs": {
"text": options.negativePrompt,
"clip": [
"4",
1
]
},
"class_type": "CLIPTextEncode"
},
"8": {
"inputs": {
"samples": [
"3",
0
],
"vae": [
"4",
2
]
},
"class_type": "VAEDecode"
},
"9": {
"inputs": {
"filename_prefix": "ComfyUI",
"images": [
"8",
0
]
},
"class_type": "SaveImage"
},
"10": {
"inputs": {
"pixels": [
"11",
0
],
"vae": [
"4",
2
]
},
"class_type": "VAEEncode"
},
"11": {
"inputs": {
"image": options.fileName,
"choose file to upload": "image"
},
"class_type": "LoadImage"
}
}
}
}
Loading

0 comments on commit d8e5bb6

Please sign in to comment.