mirror of
https://github.com/5shekel/stable-diffusion-telegram-bot.git
synced 2024-05-22 19:33:14 +03:00
Compare commits
No commits in common. "9c965b497e7a2718a1797d33c231d9ef3cccb043" and "9bce6f989ef07ceb6e7ad5f264d938093d0f331c" have entirely different histories.
9c965b497e
...
9bce6f989e
10
README.md
10
README.md
|
@ -7,26 +7,22 @@ this is a txt2img bot running on tami telegram channel
|
|||
supported invocation:
|
||||
`/draw <text>` - send prompt text to the bot and it will draw an image
|
||||
you can add `negative_prompt` using `ng: <text>`
|
||||
you can add `denoised intermediate steps` using `steps: <text>`
|
||||
|
||||
examples:
|
||||
`/draw a city street`
|
||||
and without people
|
||||
`/draw a city street ng: people`
|
||||
with more steps
|
||||
`/draw a city street ng: people steps: 50`
|
||||
|
||||
to change the model use:
|
||||
`/getmodels` - to get a list of models and then click to set it.
|
||||
|
||||
|
||||
|
||||
- note1: Anything after ng will be considered as nergative prompt. a.k.a things you do not want to see in your diffusion!
|
||||
- note2: on [negative_prompt](https://github.com/AUTOMATIC1111/stable-diffusion-webui/wiki/Negative-prompt) (aka ng):
|
||||
note1: Anything after ng will be considered as nergative prompt. a.k.a things you do not want to see in your diffusion!
|
||||
note2: on [negative_prompt](https://github.com/AUTOMATIC1111/stable-diffusion-webui/wiki/Negative-prompt) (aka ng):
|
||||
thia is a bit of a black art. i took the recommended defaults for the `Deliberate` model from this fun [alt-model spreadsheet](https://docs.google.com/spreadsheets/d/1Q0bYKRfVOTUHQbUsIISCztpdZXzfo9kOoAy17Qhz3hI/edit#gid=797387129).
|
||||
~~and you (currntly) can only ADD to it, not replace.~~
|
||||
- note3: on `steps` - step of 1 will generate only the first "step" of bot hallucinations. the default is 40. higher will take longer and will give "better" image. range is hardcoded 1-70.
|
||||
see ![video](https://user-images.githubusercontent.com/57876960/212490617-f0444799-50e5-485e-bc5d-9c24a9146d38.mp4)
|
||||
|
||||
## Setup
|
||||
|
||||
Install requirements
|
||||
|
|
94
main.py
94
main.py
|
@ -2,7 +2,7 @@ import json
|
|||
import requests
|
||||
import io
|
||||
import os
|
||||
import uuid
|
||||
import random
|
||||
import base64
|
||||
from PIL import Image, PngImagePlugin
|
||||
from pyrogram import Client, filters
|
||||
|
@ -26,81 +26,55 @@ SD_URL = os.environ.get("SD_URL", None)
|
|||
print(SD_URL)
|
||||
app = Client("stable", api_id=API_ID, api_hash=API_HASH, bot_token=TOKEN)
|
||||
|
||||
#default params
|
||||
steps_value_default = 40
|
||||
|
||||
def process_input_string(string):
|
||||
ng_delimiter = "ng:"
|
||||
steps_delimiter = "steps:"
|
||||
|
||||
ng_index = string.find(ng_delimiter)
|
||||
steps_index = string.find(steps_delimiter)
|
||||
|
||||
if ng_index != -1 and steps_index != -1:
|
||||
if ng_index < steps_index:
|
||||
positive = string[:ng_index].strip()
|
||||
negative = string[ng_index + len(ng_delimiter):steps_index].strip()
|
||||
steps_str = string[steps_index + len(steps_delimiter):].strip().split()[0]
|
||||
else:
|
||||
positive = string[:steps_index].strip()
|
||||
negative = string[steps_index + len(steps_delimiter):ng_index].strip()
|
||||
steps_str = string[ng_index + len(ng_delimiter):].strip().split()[0]
|
||||
elif ng_index != -1:
|
||||
positive = string[:ng_index].strip()
|
||||
negative = string[ng_index + len(ng_delimiter):].strip()
|
||||
steps_str = None
|
||||
elif steps_index != -1:
|
||||
positive = string[:steps_index].strip()
|
||||
negative = None
|
||||
steps_str = string[steps_index + len(steps_delimiter):].strip().split()[0]
|
||||
def slice_positive_negative(string):
|
||||
delimiter = "ng:"
|
||||
if delimiter in string:
|
||||
index = string.index(delimiter)
|
||||
positive = string[:index].rstrip()
|
||||
negative = string[index + len(delimiter) :].lstrip()
|
||||
return positive, negative
|
||||
else:
|
||||
positive = string.strip()
|
||||
negative = None
|
||||
steps_str = None
|
||||
|
||||
try:
|
||||
steps_value = int(steps_str)
|
||||
#limit steps to range
|
||||
if not 1 <= steps_value <= 70:
|
||||
steps_value = steps_value_default
|
||||
except (ValueError, TypeError):
|
||||
steps_value = None
|
||||
|
||||
|
||||
return positive, negative, steps_value
|
||||
return string, "null" # if we are missing the ng bad thing happen....
|
||||
#POSSIBLE NG [[[[[[[[[[lowres, low resolution, bad resolution, bad, poor quality, bad quality, blurry, bad art, incomplete, logo, signature, text, jpeg artifacts, compression artifacts,child, childish]]]]]]]]]]
|
||||
|
||||
@app.on_message(filters.command(["draw"]))
|
||||
def draw(client, message):
|
||||
msgs = message.text.split(" ", 1)
|
||||
if len(msgs) == 1:
|
||||
message.reply_text(
|
||||
"Format :\n/draw < text to image >\nng: < negative (optional) >\nsteps: < steps value (1-70, optional) >"
|
||||
"Format :\n/draw < text to image >\nng: < negative (optional) >"
|
||||
)
|
||||
return
|
||||
|
||||
positive, negative, steps_value = process_input_string(msgs[1])
|
||||
msg = slice_positive_negative(msgs[1])
|
||||
print(msg)
|
||||
payload = {
|
||||
"prompt": positive,
|
||||
"prompt": msg[0],
|
||||
"negative_prompt": msg[1],
|
||||
}
|
||||
if negative is not None:
|
||||
payload["negative_prompt"] = negative
|
||||
if steps_value is not None:
|
||||
payload["steps"] = steps_value
|
||||
|
||||
print(payload)
|
||||
|
||||
# The rest of the draw function remains unchanged
|
||||
|
||||
|
||||
K = message.reply_text("Please Wait 10-15 Second")
|
||||
r = requests.post(url=f"{SD_URL}/sdapi/v1/txt2img", json=payload).json()
|
||||
|
||||
def genr():
|
||||
unique_id = str(uuid.uuid4())[:7]
|
||||
return f"{message.from_user.first_name}-{unique_id}"
|
||||
chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||
chars1 = "1234564890"
|
||||
gen1 = random.choice(chars)
|
||||
gen2 = random.choice(chars)
|
||||
gen3 = random.choice(chars1)
|
||||
gen4 = random.choice(chars)
|
||||
gen5 = random.choice(chars)
|
||||
gen6 = random.choice(chars)
|
||||
gen7 = random.choice(chars1)
|
||||
gen8 = random.choice(chars)
|
||||
gen9 = random.choice(chars)
|
||||
gen10 = random.choice(chars1)
|
||||
return f"{message.from_user.id}-MOE{gen1}{gen2}{gen3}{gen4}{gen5}{gen6}{gen7}{gen8}{gen9}{gen10}"
|
||||
|
||||
word = genr()
|
||||
|
||||
for i in r["images"]:
|
||||
image = Image.open(io.BytesIO(base64.b64decode(i.split(",", 1)[0])))
|
||||
|
||||
|
@ -112,15 +86,9 @@ def draw(client, message):
|
|||
image.save(f"{word}.png", pnginfo=pnginfo)
|
||||
|
||||
message.reply_photo(
|
||||
photo=f"{word}.png",
|
||||
caption=(
|
||||
f"Prompt - **{positive}**\n"
|
||||
f"Negative Prompt - **{negative if negative is not None else 'None'}**\n"
|
||||
f"Steps - **{steps_value if steps_value != steps_value_default else 'Default'}**\n"
|
||||
f"**[{message.from_user.first_name}-Kun](tg://user?id={message.from_user.id})**"
|
||||
),
|
||||
)
|
||||
|
||||
photo=f"{word}.png",
|
||||
caption=f"Prompt - **{msg[0]}**\nNegative Prompt - **{msg[1]}**\n**[{message.from_user.first_name}-Kun](tg://user?id={message.from_user.id})**",
|
||||
)
|
||||
# os.remove(f"{word}.png")
|
||||
K.delete()
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user