Logan Zoellner commited on
Commit
7033da3
·
1 Parent(s): ace7020

maybe better fallback?

Browse files
Files changed (1) hide show
  1. app.py +99 -26
app.py CHANGED
@@ -80,7 +80,74 @@ def npc_randomize():
80
 
81
 
82
 
83
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
84
 
85
 
86
  def npc_generate(name,race,characterClass,pronoun):
@@ -88,7 +155,7 @@ def npc_generate(name,race,characterClass,pronoun):
88
  desc="{name} is a {race} {characterClass}, {pronoun}".format(name=name,race=race,characterClass=characterClass,pronoun=pronoun)
89
 
90
  p = prompt + "\n"+desc
91
- print(f"*****Inside poem_generate - Prompt is :{p}")
92
  json_ = {"inputs": p,
93
  "parameters":
94
  {
@@ -101,35 +168,41 @@ def npc_generate(name,race,characterClass,pronoun):
101
  response = requests.post(API_URL, json=json_)
102
  output = response.json()
103
  print(f"If there was an error? Reason is : {output}")
104
- output_tmp = output[0]['generated_text']
105
- print(f"GPTJ response without splits is: {output_tmp}")
106
- #poem = output[0]['generated_text'].split("\n\n")[0] # +"."
107
- if "\n\n" not in output_tmp:
108
- if output_tmp.find('.') != -1:
109
- idx = output_tmp.find('.')
110
- poem = output_tmp[:idx+1]
111
- else:
112
- idx = output_tmp.rfind('\n')
113
- poem = output_tmp[:idx]
114
  else:
115
- poem = output_tmp.split("\n\n")[0] # +"."
116
- poem = poem.replace('?','')
117
- print(f"Poem being returned is: {poem}")
118
- return desc+poem
119
-
120
- def poem_to_image(poem):
121
- print("*****Inside Poem_to_image")
122
- poem = " ".join(poem.split('\n'))
123
- poem = poem + ", character art, concept art, artstation"
 
 
 
 
 
 
 
 
 
 
124
  steps, width, height, images, diversity = '50','256','256','1',15
125
  iface = gr.Interface.load("spaces/multimodalart/latentdiffusion")
126
  print("about to die",iface,dir(iface))
127
 
128
- prompt = re.sub(r'[^a-zA-Z0-9 ,.]', '', poem)
129
  print("about to die",prompt)
130
 
131
 
132
- img=iface(poem, steps, width, height, images, diversity)[0]
133
  return img
134
 
135
  demo = gr.Blocks()
@@ -155,12 +228,12 @@ with demo:
155
  input_pronoun = gr.Textbox(label="pronoun",placeholder="he")
156
 
157
  with gr.Row():
158
- poem_txt = gr.Textbox(label="description",lines=7)
159
  output_image = gr.Image(label="portrait",type="filepath", shape=(256,256))
160
 
161
  b0.click(npc_randomize,inputs=[],outputs=[input_name,input_race,input_class,input_pronoun])
162
- b1.click(npc_generate, inputs=[ input_name,input_race,input_class,input_pronoun], outputs=poem_txt)
163
- b2.click(poem_to_image, poem_txt, output_image)
164
  #examples=examples
165
 
166
  demo.launch(enable_queue=True, debug=True)
 
80
 
81
 
82
 
83
+ def genericDescription():
84
+ colors="""red
85
+ blue
86
+ green
87
+ yellow
88
+ orange
89
+ purple
90
+ pink
91
+ brown
92
+ black
93
+ white""".split("\n")
94
+ colors=[x.strip() for x in colors]
95
+ outfits="""shirt
96
+ pair of pants
97
+ pair of shoes
98
+ hat
99
+ pair of glasses
100
+ backpack
101
+ belt
102
+ tie
103
+ cloak
104
+ robe
105
+ chain mail vest
106
+ suit of plate armor
107
+ suit of leather armor
108
+ suit of studded leather armor
109
+ suit of scale armor
110
+ suit of chain mail armor
111
+ suit of ring mail armor
112
+ """.split("\n")
113
+ outfits=[x.strip() for x in outfits]
114
+ weapons="""sword
115
+ dagger
116
+ mace
117
+ axe
118
+ polearm
119
+ bow
120
+ crossbow
121
+ sling
122
+ club
123
+ flail
124
+ warhammer
125
+ morningstar
126
+ halberd
127
+ war pick
128
+ war sickle
129
+ war hammer""".split("\n")
130
+ weapons=[x.strip() for x in weapons]
131
+
132
+ objects="""shield
133
+ lantern
134
+ sack
135
+ severed head
136
+ crystal""".split("\n")
137
+ objects=[x.strip() for x in objects]
138
+
139
+ desc="wears a {color} {outfit}".format(color=random.choice(colors),outfit=random.choice(outfits))
140
+ if random.random()<0.5:
141
+ desc+="and a {color} {outfit}".format(color=random.choice(colors),outfit=random.choice(outfits))
142
+
143
+ if random.random()<0.5:
144
+ desc+="and carries a {weapon}".format(weapon=random.choice(weapons))
145
+ elif random.random()<0.5:
146
+ desc+="and carries a {weapon} and a {object}".format(weapon=random.choice(weapons),object=random.choice(objects))
147
+ else:
148
+ desc+="and carries two {weapon}s".format(weapon=random.choice(weapons))
149
+
150
+ return desc
151
 
152
 
153
  def npc_generate(name,race,characterClass,pronoun):
 
155
  desc="{name} is a {race} {characterClass}, {pronoun}".format(name=name,race=race,characterClass=characterClass,pronoun=pronoun)
156
 
157
  p = prompt + "\n"+desc
158
+ print(f"*****Inside desc_generate - Prompt is :{p}")
159
  json_ = {"inputs": p,
160
  "parameters":
161
  {
 
168
  response = requests.post(API_URL, json=json_)
169
  output = response.json()
170
  print(f"If there was an error? Reason is : {output}")
171
+
172
+
173
+ #error handling
174
+ if "error" in output:
175
+ #fallback method
176
+ longDescription=genericDescription()
 
 
 
 
177
  else:
178
+ output_tmp = output[0]['generated_text']
179
+ print(f"GPTJ response without splits is: {output_tmp}")
180
+ if "\n\n" not in output_tmp:
181
+ if output_tmp.find('.') != -1:
182
+ idx = output_tmp.find('.')
183
+ longDescription = output_tmp[:idx+1]
184
+ else:
185
+ idx = output_tmp.rfind('\n')
186
+ longDescription = output_tmp[:idx]
187
+ else:
188
+ longDescription = output_tmp.split("\n\n")[0] # +"."
189
+ longDescription = longDescription.replace('?','')
190
+ print(f"longDescription being returned is: {longDescription}")
191
+ return desc+longDescription
192
+
193
+ def desc_to_image(desc):
194
+ print("*****Inside desc_to_image")
195
+ desc = " ".join(desc.split('\n'))
196
+ desc = desc + ", character art, concept art, artstation"
197
  steps, width, height, images, diversity = '50','256','256','1',15
198
  iface = gr.Interface.load("spaces/multimodalart/latentdiffusion")
199
  print("about to die",iface,dir(iface))
200
 
201
+ prompt = re.sub(r'[^a-zA-Z0-9 ,.]', '', desc)
202
  print("about to die",prompt)
203
 
204
 
205
+ img=iface(desc, steps, width, height, images, diversity)[0]
206
  return img
207
 
208
  demo = gr.Blocks()
 
228
  input_pronoun = gr.Textbox(label="pronoun",placeholder="he")
229
 
230
  with gr.Row():
231
+ desc_txt = gr.Textbox(label="description",lines=7)
232
  output_image = gr.Image(label="portrait",type="filepath", shape=(256,256))
233
 
234
  b0.click(npc_randomize,inputs=[],outputs=[input_name,input_race,input_class,input_pronoun])
235
+ b1.click(npc_generate, inputs=[ input_name,input_race,input_class,input_pronoun], outputs=desc_txt)
236
+ b2.click(desc_to_image, desc_txt, output_image)
237
  #examples=examples
238
 
239
  demo.launch(enable_queue=True, debug=True)