Bryan Wade commited on
Commit
ae10612
1 Parent(s): 150be19

app updates

Browse files
Files changed (3) hide show
  1. app.py +72 -40
  2. requirements.txt +2 -1
  3. styles.txt +102 -0
app.py CHANGED
@@ -13,7 +13,6 @@ from contextlib import nullcontext
13
  #from PIL import Image
14
  #from torchvision import transforms
15
 
16
- #from diffusers import StableDiffusionImageVariationPipeline
17
 
18
 
19
  openai.api_key = os.getenv('openaikey')
@@ -25,15 +24,62 @@ dtype = torch.float16 if device == "cuda" else torch.float32
25
  pipe = StableDiffusionPipeline.from_pretrained("stale2000/sd-dnditem", torch_dtype=dtype, use_auth_token=authtoken)
26
  pipe = pipe.to(device)
27
 
28
- def predict(input, manual_query_repacement, history=[]):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
 
30
  # gpt3
31
- if manual_query_repacement != "":
32
- input = manual_query_repacement
 
 
 
 
 
 
 
 
 
 
 
 
33
 
34
  response = openai.Completion.create(
35
  model="text-davinci-003",
36
- prompt=input,
37
  temperature=0.9,
38
  max_tokens=150,
39
  top_p=1,
@@ -42,61 +88,47 @@ def predict(input, manual_query_repacement, history=[]):
42
 
43
  # tokenize the new input sentence
44
  responseText = response["choices"][0]["text"]
45
- history.append((input, responseText))
46
 
47
 
48
  #img generation
49
- prompt = "Yoda"
50
- scale = 10
51
- n_samples = 4
52
-
53
- # Sometimes the nsfw checker is confused by the Naruto images, you can disable
54
- # it at your own risk here
55
- #disable_safety = False
56
 
57
- #if disable_safety:
58
- # def null_safety(images, **kwargs):
59
- # return images, False
60
- # pipe.safety_checker = null_safety
61
-
62
-
63
  #with autocast("cuda"):
64
  # images = pipe(n_samples*[prompt], guidance_scale=scale).images
65
 
66
  with context("cuda"):
67
- images = pipe(n_samples*[prompt], guidance_scale=scale, num_inference_steps=5).images
 
68
 
69
- for idx, im in enumerate(images):
70
- im.save(f"{idx:06}.png")
71
 
72
- images_list = pipe(
73
- inp.tile(n_samples, 1, 1, 1),
74
- guidance_scale=scale,
75
- num_inference_steps=steps,
76
- generator=generator,
77
- )
78
 
79
- images = []
80
- for i, image in enumerate(images_list["images"]):
81
- if(images_list["nsfw_content_detected"][i]):
82
- safe_image = Image.open(r"unsafe.png")
83
- images.append(safe_image)
84
- else:
85
- images.append(image)
 
 
86
 
87
 
88
-
89
- return history, history, images
90
 
 
 
 
91
 
92
 
93
- inputText = gr.Textbox(value="tmp")
94
- manual_query = gr.Textbox(placeholder="Input any query here, to replace the image generation query builder entirely.")
95
 
96
  output_img = gr.Gallery(label="Generated image")
97
  output_img.style(grid=2)
98
 
 
 
99
  gr.Interface(fn=predict,
100
- inputs=[inputText,manual_query,'state'],
101
 
102
  outputs=["chatbot",'state', output_img]).launch()
 
13
  #from PIL import Image
14
  #from torchvision import transforms
15
 
 
16
 
17
 
18
  openai.api_key = os.getenv('openaikey')
 
24
  pipe = StableDiffusionPipeline.from_pretrained("stale2000/sd-dnditem", torch_dtype=dtype, use_auth_token=authtoken)
25
  pipe = pipe.to(device)
26
 
27
+ disable_safety = True
28
+
29
+ if disable_safety:
30
+ def null_safety(images, **kwargs):
31
+ return images, False
32
+ pipe.safety_checker = null_safety
33
+
34
+ def create_files():
35
+ directory = 'C:\\Users\\brcwa\\OneDrive\\Desktop\\destinyCaptures\\dnd\\fullcaptionsimple\\full'
36
+ for filename2 in os.listdir(directory):
37
+ if not filename2.endswith('txt'):
38
+ continue
39
+ f = os.path.join(directory, filename2)
40
+ # checking if it is a file
41
+ if os.path.isfile(f):
42
+ text_file = open(f, "r")
43
+ lines = text_file.read()
44
+ print(lines.split(',')[1] + "," + lines.split(',')[1])
45
+
46
+ #create_files()
47
+
48
+ def createGPTPrompt(item_type, description):
49
+ return item_type.split(",")[0].split(" ")[-1] + " of " + description
50
+
51
+ def convert_lines(lines):
52
+ key_arr = []
53
+ key_hash = {}
54
+ for line in lines:
55
+ key = line.split(",")[0]
56
+ val = line.split(",")[1]
57
+ key_arr.append(key)
58
+ key_hash[key] = val
59
+
60
+ return key_arr, key_hash
61
+
62
+ def predict(dropdown, style_dropdown, manual_gpt_replacement, manual_sd_prompt, n_samples, history=[]):
63
 
64
  # gpt3
65
+ sd_input = ""
66
+ gpt_input = ""
67
+
68
+ description = style_dropdown
69
+ if manual_sd_prompt != '':
70
+ gpt_input = manual_gpt_replacement
71
+ else:
72
+ gpt_input = "Describe the mechanics of a 5th Edition DnD item called '" + createGPTPrompt(dropdown, description) + "' :"
73
+
74
+ if manual_sd_prompt != '':
75
+ sd_input = manual_sd_prompt
76
+ else:
77
+ sd_input = "dnditem, " + dropdown + style_hashmap[style_dropdown] + ", circle inner background and white outerbackground"
78
+
79
 
80
  response = openai.Completion.create(
81
  model="text-davinci-003",
82
+ prompt=gpt_input,
83
  temperature=0.9,
84
  max_tokens=150,
85
  top_p=1,
 
88
 
89
  # tokenize the new input sentence
90
  responseText = response["choices"][0]["text"]
91
+ history.append((sd_input, responseText))
92
 
93
 
94
  #img generation
95
+ scale = 5.5
 
 
 
 
 
 
96
 
 
 
 
 
 
 
97
  #with autocast("cuda"):
98
  # images = pipe(n_samples*[prompt], guidance_scale=scale).images
99
 
100
  with context("cuda"):
101
+ images = pipe(n_samples*[sd_input], guidance_scale=scale, num_inference_steps=40).images
102
+
103
 
104
+ return history, history, images
 
105
 
 
 
 
 
 
 
106
 
107
+
108
+ #inputText = gr.Textbox(placeholder="input query")
109
+ manual_gpt_query = gr.Textbox(placeholder="Input any query here, to replace the gpt query builder entirely.")
110
+ manual_sd_prompt = gr.Textbox(placeholder="Input any query here, to replace the gpt query builder entirely.")
111
+
112
+
113
+
114
+ choiceArr = ["none", "a pair of boots, ", "a cloak, ", "a pair of gloves, ", "a helmet, ", "a necklace, ", "a ring, ", "a robe, ", "a rod, ", "a shield, ", "a staff, ", "a sword, ", "a wand, "]
115
+ dropdown = gr.Dropdown(label= "Item Type", choices=choiceArr)
116
 
117
 
 
 
118
 
119
+ text_file = open("styles.txt", "r")
120
+ lines = text_file.read().split('\n')
121
+ dropdown_arr, style_hashmap = convert_lines(lines)
122
 
123
 
124
+ style_dropdown = gr.Dropdown(label= "Item Ability and Style", choices=dropdown_arr)
 
125
 
126
  output_img = gr.Gallery(label="Generated image")
127
  output_img.style(grid=2)
128
 
129
+ step_slide = gr.Slider(1, 4, value=2, step=1),
130
+ slide = gr.Slider(label="Number of Images Generated", minimum=1, maximum=4, value=2, step=1)
131
  gr.Interface(fn=predict,
132
+ inputs=[dropdown, style_dropdown, manual_gpt_query,manual_sd_prompt,slide,'state'],
133
 
134
  outputs=["chatbot",'state', output_img]).launch()
requirements.txt CHANGED
@@ -4,4 +4,5 @@ openai
4
  gradio
5
  diffusers
6
  transformers
7
- accelerate
 
 
4
  gradio
5
  diffusers
6
  transformers
7
+ accelerate
8
+ tensorboard
styles.txt ADDED
@@ -0,0 +1,102 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ elvenkind,green elvenkind style
2
+ speed,green striped speed style
3
+ striding and springing,cloth striding and springing style
4
+ winterlands,blue leather winterlands style
5
+ spider climbing,purple black spider climbing style
6
+ winged,winged style
7
+ displacement,silverr green displacement style
8
+ protection,blue protection style
9
+ manta ray,manta ray style
10
+ many fashions,purple feathered many fashions style
11
+ archery,bronze archery style
12
+ defense,gold defense style
13
+ ogre strength,silver and gold ogre strength style
14
+ missile snaring,purple spider missile snaring style
15
+ brilliance,gem brilliance style
16
+ comprehend language,metal green comprehend language style
17
+ telepathy,brain telepathy style
18
+ teleportation,black and gold teleportation style
19
+ heart wound closure,red gem heart wound closure style
20
+ fireball,bead fireball style
21
+ prayer beads,multicolor gem prayer beads style
22
+ health,red heart gem health style
23
+ the sphere,bronze horned sphere style
24
+ ultimate evil,black green demon ultimate evil style
25
+ the drunkard,metal leaf drunkard style
26
+ the guardian,metal wood shield guardian style
27
+ proof against detection and location,proof against detection and location style
28
+ the planes,black circle planes style
29
+ adaption,gold adaption style
30
+ evasion,green bird evasion style
31
+ xray,eye xray style
32
+ resistence,gold and red resistence style
33
+ winter,frosted winter style
34
+ obscuring,black skull obscuring style
35
+ red fury,silver red fury style
36
+ air elemental control,dual air elemental control style
37
+ animal influence,wood animal influence style
38
+ featherfall,silver gold featherfall style
39
+ free action,blue gem silver free action style
40
+ invisibility,plain silver invisibility style
41
+ mind shielding,purple mind shielding style
42
+ shooting stars,purple shooting stars style
43
+ spell storing,yellow spell storing style
44
+ three wishes,jeweled three wishes style
45
+ regeneration,green wood regeneration style
46
+ telekinesis,yellow gem telekinesis style
47
+ the ram,silver ram style
48
+ water walking,silver blue water walking style
49
+ eyes,red eyes style
50
+ the stars,blue and black stars style
51
+ the archmage,white archmage style
52
+ useful things,multi-color patches useful things style
53
+ the immoveable,silver immoveable style
54
+ absorption,yellow absorption style
55
+ lordly might,black silver lordly might style
56
+ ressurection,silver green red ressurection style
57
+ rulership,brown red gold rulership style
58
+ the tentacle,real silver gold tentacle style
59
+ the hidden lords,gold hidden lords style
60
+ ruidium,red and brown ruidium style
61
+ the animated,blue and green transparent animated style
62
+ missile attraction,black and silver missile attraction style
63
+ spellguard,gold jeweled spellguard style
64
+ charm,gold and multicolor charm style
65
+ the magi,bronze magi style
66
+ the python,white python style
67
+ withering,yellow hand withering style
68
+ gulthias,black branch gulthias style
69
+ fire,flaming style
70
+ ice,ice style
71
+ the woodland,brown and green woodland style
72
+ lighting,silver lighting style
73
+ healing,tiki totem healing style
74
+ power,metal power style
75
+ swarming insects,swarming insects style
76
+ the adder snake,black snake adder style
77
+ the sun,bright yellow sunblade style
78
+ life stealing,silver life stealing style
79
+ sharpness,silver sharpness style
80
+ viciousness,silver vicious style
81
+ vorpal,red vorpal style
82
+ the moon,moonblade style
83
+ zariel,blue white silver zariel style
84
+ dancing,silver dancing style
85
+ defender,silver blue defender style
86
+ the dragon slayer,silver dragon slayer style
87
+ frost brand,frost brand style
88
+ holy avenger,silver holy avenger style
89
+ luck,green silver luck blade style
90
+ nine lives stealing,silver nine lives stealer style
91
+ secrets,secretys style
92
+ web,purple metal web style
93
+ wonder,multicolor wonder style
94
+ winter,dark blue winter style
95
+ stone binding,chain stone binding style
96
+ magic detection,gem magic detection style
97
+ fear,skull fear style
98
+ fireball,bright fireball style
99
+ magic detection,lamp magic detection style
100
+ magic missile,blue silver magic missile style
101
+ paralysis,paralysis style
102
+ polymorph,brown polymorph style