Advertisement
kopyl

Untitled

Mar 23rd, 2023
622
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.11 KB | None | 0 0
  1. import runpod
  2. import os
  3. import torch
  4. import io
  5. from PIL import Image
  6. from diffusers import StableDiffusionImg2ImgPipeline
  7. import os
  8. import telegram
  9.  
  10.  
  11. BOT_TOKEN = os.environ.get("BOT_TOKEN")
  12. bot = telegram.Bot(token=BOT_TOKEN)
  13.  
  14. device = "cuda"
  15. pipe = StableDiffusionImg2ImgPipeline.from_pretrained(
  16.     "logo-marks-6-1_5", torch_dtype=torch.float16, safety_checker=None
  17. ).to(device)
  18.  
  19.  
  20. def generate_image(user_id, seed, image_order, prompt=""):
  21.  
  22.     init_image = Image.open(
  23.         f"logo-marks-6-1_5/reference-images/{image_order}.png"
  24.     ).convert("RGB")
  25.     init_image.thumbnail((768, 768))
  26.  
  27.     folder_path = f"images/{user_id}"
  28.     os.makedirs(folder_path, exist_ok=True)
  29.  
  30.     generator = torch.Generator(device="cuda").manual_seed(seed)
  31.     images = pipe(
  32.         prompt=f"{prompt} logo mark in zvxsbj style",
  33.         image=init_image,
  34.         strength=0.95,
  35.         guidance_scale=30,
  36.         generator=generator,
  37.     ).images
  38.     print(f"Image with seed {seed} and image_order {image_order} saved")
  39.     return images[0]
  40.  
  41.  
  42. def send_ranom_ask(chat_id):
  43.     bot.send_message(
  44.         chat_id=chat_id,
  45.         text=f"Press /random to get random logo or send a short prompt (1-2 word) and i will generate 10 logotypes for you",
  46.         reply_markup=telegram.ReplyKeyboardMarkup(
  47.             [
  48.                 ["/random"],
  49.             ],
  50.             resize_keyboard=True,
  51.             one_time_keyboard=True,
  52.         ),
  53.     )
  54.  
  55.  
  56. def handler(event):
  57.     user_id = event["input"]["user_id"]
  58.     prompt = event["input"]["prompt"]
  59.  
  60.     image_requests = event["input"]["image_requests"]  # example: [ [1, 0], [1, 1] ]
  61.  
  62.     image_bytes = []
  63.  
  64.     for image_request in image_requests:
  65.         image = generate_image(user_id, image_request[0], image_request[1], prompt)
  66.         bytes_image = io.BytesIO()
  67.         image.save(bytes_image, "PNG")
  68.         bytes_image.seek(0)
  69.         image_bytes.append(telegram.InputMediaPhoto(bytes_image))
  70.  
  71.     bot.send_media_group(user_id, image_bytes)
  72.  
  73.     send_ranom_ask(user_id)
  74.  
  75.     return "Hello World"
  76.  
  77.  
  78. runpod.serverless.start({"handler": handler})
  79.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement