Guest User

very janky code talking with Automatic1111 stable diffusion repository

a guest
Oct 16th, 2022
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.52 KB | Source Code | 0 0
  1. def decode_base64_to_image(encoding):
  2.     content = encoding.split(";")[1]
  3.     print(content)
  4.     image_encoded = content.split(",")[1]
  5.     return Image.open(BytesIO(base64.b64decode(image_encoded)))
  6.  
  7. backupPath = os.path.join(os.getcwd(), "backups")
  8. if os.path.exists(backupPath) == False:
  9.     os.mkdir(backupPath)
  10.  
  11. txt2ImgURL = "http://your ip here/api/submit"
  12. extrasURL = "http://your ip here/api/submit_extras"
  13. def constructTxt2Img(prompt):
  14.     return {
  15.             "data": [
  16.                     prompt,
  17.                     "person, man, woman, robo, car, vehicle, people", #negative prompt
  18.                     "None",
  19.                     "None",
  20.                     30,
  21.                     "Euler a",
  22.                     False,
  23.                     False,
  24.                     1,
  25.                     1,
  26.                     7,
  27.                     -1,
  28.                     -1,
  29.                     0,
  30.                     0,
  31.                     0,
  32.                     False,
  33.                     384,
  34.                     640,
  35.                     False,
  36.                     0.7,
  37.                     0,
  38.                     0,
  39.                     "None",
  40.                     False,
  41.                     False,
  42.                     None,
  43.                     "",
  44.                     "Seed",
  45.                     "",
  46.                     "Nothing",
  47.                     "",
  48.                     True,
  49.                     True,
  50.                     False,
  51.                     None,
  52.                     "",
  53.                     ""
  54.                 ]
  55.             }
  56.            
  57. def convertBytesToPng(bytes):
  58.     s = "data:image/png;base64," + str(base64.b64encode(bytes), "utf-8")
  59.     return s
  60.  
  61. def constructExtras(bytes):
  62.     return {
  63.   "data": [
  64.     0,
  65.     0,
  66.     convertBytesToPng(bytes),
  67.     None,
  68.     0,
  69.     0,
  70.     0,
  71.     3,
  72.     512,
  73.     512,
  74.     True,
  75.     "None",
  76.     "None",
  77.     0,
  78.     [],
  79.     "",
  80.     ""
  81.   ]
  82. }
  83.  
  84. def constructFileGet(file):
  85.     return "http://your ip here/file=" + file
  86.  
  87. backups = []
  88. def getRandomBackup():
  89.     global backups
  90.     global backupPath
  91.     if (len(backups) == 0):
  92.         for filename in os.listdir(backupPath):
  93.             backups.append(os.path.join(backupPath, filename))
  94.         random.shuffle(backups)    
  95.         if (len(backups) > 1000):
  96.             os.remove(backups.pop(0))
  97.    
  98.     if len(backups) == 0:
  99.         return None
  100.        
  101.     return Image.open(backups.pop(0)).convert("RGB")
  102.                
  103. def generateNewImage(prompt):
  104.     global backupPath
  105.     data = constructTxt2Img(prompt)
  106.     try:
  107.         r = requests.post(url = txt2ImgURL, data = json.dumps(data, indent=4))
  108.         if (r.status_code == 200):
  109.             ret = json.loads(r.text)
  110.             r2 = requests.get(url= constructFileGet(ret["data"][0][0]["name"]))
  111.             if (r2.status_code == 200):
  112.                 extras = constructExtras(r2.content)
  113.                 r3 = requests.post(url = extrasURL, data = json.dumps(extras, indent = 4))
  114.                 if (r3.status_code == 200):
  115.                     r4 = requests.get(url=constructFileGet(json.loads(r3.text)["data"][0][0]["name"]))
  116.                     if r4.status_code == 200:
  117.                         i = Image.open(BytesIO(r4.content))
  118.                         i.save(os.path.join(backupPath, str(time.time()).replace(".", "")+ ".png"), "PNG")
  119.                         return i
  120.     except:
  121.         print("Couldn't connect to the processing server, using a backup")
  122.    
  123.     return getRandomBackup()
  124.    
Tags: janky
Add Comment
Please, Sign In to add comment