type
Post
status
Published
date
Aug 30, 2022
slug
sd_tg_bot
summary
Telegram bot以实现给prompt和seed出图
tags
工具
category
技术分享
icon
password
Property
Sep 2, 2022 11:34 AM
想到telegram bot可以实现出的图直接上云,于是就想写一bot实现喂给它prompt和seed直接出图。
用的是telegram.bot 这个R包,在运行以下脚本时要确保自己已经配好conda env可以跑SD的txt2img.py。同时需要在
.Renviron
中加一行:R_TELEGRAM_BOT_your_bot=your_token
为了确保bot不让路人都能运行,请在usr_list里输入你授权可以用的用户的chatid,如不清楚可以在telegram上@userinfobot
Telegram_bot.R
#file.edit(path.expand(file.path("~", ".Renviron"))) library(telegram.bot) library(stringr) # Initiate the bot session using the token from the enviroment variable. bot = Bot(token = bot_token('your_bot')) usr_list <- c(12344566, 12345566) create_image <- function(prompt, chat_id) { if (chat_id %in% usr_list) { if (stringr::str_detect(prompt,"seed=|seed:")) { seed <- as.numeric( str_extract(prompt, "[^=:]*$") ) prompt <- prompt |> str_remove("[^=:]*$") |> str_remove("(seed:)|(seed=)") } else { seed <- sample.int(1e+9, 1) prompt <- prompt } sendtopy <- paste0( 'conda run -n ldm python scripts/txt2img.py --prompt "', prompt, '" --plms --skip_grid --n_samples 1 --seed ', seed ) system(sendtopy) df <- file.info(list.files("~/Projects/stable-diffusion/outputs/txt2img-samples/samples", full.names = T)) most_recent <- rownames(df)[which.max(df$mtime)] bot$sendPhoto(chat_id = chat_id, photo = most_recent) message_to_bot <- paste0( "The above image was generated by stable diffusion using prompt: ", prompt, " and seed:", seed ) bot$sendMessage(chat_id = chat_id, text = message_to_bot) } else { bot$sendMessage(chat_id = chat_id, text = "You are not allowed to use this bot!") } } updater <- Updater(token = bot_token('your_bot')) start <- function(bot, update){ bot$sendMessage(chat_id = update$message$chat_id, text = sprintf("Hello %s!", update$message$from$first_name)) } start_handler <- CommandHandler("start", start) updater <- updater + start_handler prompt <- function(bot, update){ create_image(prompt = stringr::str_remove(update$message$text, "/prompt"), chat_id = update$message$chat_id) } updater <- updater + CommandHandler("prompt", prompt) unknown <- function(bot, update){ bot$sendMessage(chat_id = update$message$chat_id, text = "Sorry, I didn't understand that command.") } updater <- updater + MessageHandler(unknown, MessageFilters$command) echo <- function(bot, update){ bot$sendMessage(chat_id = update$message$chat_id, text = update$message$text) } updater <- updater + MessageHandler(echo, MessageFilters$text) updater$start_polling()
运行和停止脚本
nohup Rscript telegram_bot.R & &> ./nohup.out ps -ax | grep telegram #查找PID kill #对应的PID
可写一个监听的脚本让它自动重启。
定时清空log和图
因为图已经在TG上了,本地就没必要存了,设定和log 一起定时清空
log_clean.sh
#!/bin/bash time=$(date "+%Y%m%d:%H-%M-%S") echo "${time}" echo "--我要开始清理日志啦-----" echo '' > /home/wyih/Projects/stable-diffusion/sysLog.txt find /home/wyih/Projects/stable-diffusion/outputs/txt2img-samples/samples/ -type f | xargs rm -rf echo "---已经清理完毕---"
chmod a+x log_clean.sh
sudo crontab -e
0 0 * * * sh /home/wyih/Projects/stable-diffusion/log_clean.sh
运行效果
反应速度还可以。加seed可以生成一样的图。
- Author:tangly1024
- URL:https://tangly1024.com/article/sd_tg_bot
- Copyright:All articles in this blog, except for special statements, adopt BY-NC-SA agreement. Please indicate the source!