This repository has been archived by the owner on Aug 31, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
stablediffusion.py
59 lines (52 loc) · 1.7 KB
/
stablediffusion.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# this bot uses the dezgo uncensored stable diffusion api (https://rapidapi.com/dezgo/api/dezgo/)
import os
import requests
from dotenv import load_dotenv
import time
import urllib.parse
load_dotenv()
url = "https://dezgo.p.rapidapi.com/text2image"
prompt = "None"
headers = {
"content-type": "application/x-www-form-urlencoded",
"X-RapidAPI-Key": os.environ['dezgo_sd_key'],
"X-RapidAPI-Host": "dezgo.p.rapidapi.com"
}
def find_seed(prompt):
for n in prompt:
if n == "/":
if prompt[prompt.index("/")+1:].isdigit():
if int(prompt[prompt.index("/")+1:]) < 2147483648:
return "&seed=" + prompt[prompt.index("/")+1:]
else:
return "seed_too_long"
else:
return "seed_no_int"
else:
pass
return ""
def seed_remover(prompt):
for n in prompt:
if n == "/":
return prompt[:prompt.index("/")]
else:
pass
return prompt
def generate_sd_normal(prompt, chat_id):
seed = find_seed(prompt)
if seed == "seed_too_long":
seed = ""
elif seed == "seed_no_int":
seed = ""
prompt = seed_remover(prompt)
prompt = urllib.parse.quote(prompt)
for guidance in range(7, 11):
payload = "guidance=" + str(guidance) + "&steps=50&prompt=" + prompt + "&width=512&height=512" + seed
response = requests.request("POST", url, data=payload, headers=headers)
if response.status_code == 200:
f = open('sd_picture_gd_' + str(guidance) + "_" + str(chat_id) + '.png', 'wb')
f.write(response.content)
f.close()
time.sleep(1)
else:
return None