heaversm commited on
Commit
5cd1557
·
1 Parent(s): cfaeada

random challenge generator

Browse files
Files changed (2) hide show
  1. app.py +27 -7
  2. challenges.txt +20 -0
app.py CHANGED
@@ -50,6 +50,21 @@ mongo_collection = mongo_db["images"]
50
  image_labels_global = []
51
  image_paths_global = []
52
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53
  def update_labels(show_labels):
54
  updated_gallery = [(path, label if show_labels else "") for path, label in zip(image_paths_global, image_labels_global)]
55
  return updated_gallery
@@ -120,8 +135,11 @@ def generate_images(prompts, pw, model):
120
  openai_client = OpenAI(api_key=openai_key)
121
  start_time = time.time()
122
 
 
 
 
123
  response = openai_client.images.generate(
124
- prompt=text,
125
  model=model, # dall-e-2 or dall-e-3
126
  quality="standard", # standard or hd
127
  size="512x512" if model == "dall-e-2" else "1024x1024", # varies for dalle-2 and dalle-3
@@ -152,25 +170,27 @@ def generate_images(prompts, pw, model):
152
  return image_paths, image_labels # Return both image paths and labels
153
 
154
  with gr.Blocks() as demo:
155
- gr.Markdown("# <center> Prompt de Resistance Image Generator</center>")
156
- gr.Markdown("**Instructions**: To use this service, please enter the password. Then generate an image from the prompt field below, then click the download arrow from the top right of the image to save it.")
 
 
 
157
  pw = gr.Textbox(label="Password", type="password",
158
  placeholder="Enter the password to unlock the service")
159
  text = gr.Textbox(label="What do you want to create?",
160
  placeholder="Enter your text and then click on the \"Image Generate\" button")
161
-
162
  model = gr.Dropdown(choices=["dall-e-2", "dall-e-3"], label="Model", value="dall-e-3")
163
  show_labels = gr.Checkbox(label="Show Image Labels", value=False)
164
  btn = gr.Button("Generate Images")
165
  output_images = gr.Gallery(label="Image Outputs", show_label=True, columns=[3], rows=[1], object_fit="contain",
166
  height="auto", allow_preview=False)
167
-
168
  #trigger generation either through hitting enter in the text field, or clicking the button.
169
  text.submit(fn=generate_images_wrapper, inputs=[text, pw, model, show_labels], outputs=output_images, api_name="generate_image") # Generate an api endpoint in Gradio / HF
170
  btn.click(fn=generate_images_wrapper, inputs=[text, pw, model, show_labels], outputs=output_images, api_name=False)
171
-
172
  show_labels.change(fn=update_labels, inputs=[show_labels], outputs=[output_images])
173
-
 
174
  download_all_btn = gr.Button("Download All")
175
  download_link = gr.File(label="Download Zip")
176
  download_all_btn.click(fn=download_all_images, inputs=[], outputs=download_link)
 
50
  image_labels_global = []
51
  image_paths_global = []
52
 
53
+ #load challenges
54
+ challenges = []
55
+ with open('challenges.txt', 'r') as file:
56
+ for line in file:
57
+ challenges.append(line.strip())
58
+
59
+ # pick a random challenge
60
+ def get_challenge():
61
+ global challenge
62
+ challenge = random.choice(challenges)
63
+ return challenge
64
+
65
+ # set initial challenge
66
+ challenge = get_challenge()
67
+
68
  def update_labels(show_labels):
69
  updated_gallery = [(path, label if show_labels else "") for path, label in zip(image_paths_global, image_labels_global)]
70
  return updated_gallery
 
135
  openai_client = OpenAI(api_key=openai_key)
136
  start_time = time.time()
137
 
138
+ #make a prompt with the challenge and text
139
+ prompt_w_challenge = f"{challenge}: {text}"
140
+
141
  response = openai_client.images.generate(
142
+ prompt=prompt_w_challenge,
143
  model=model, # dall-e-2 or dall-e-3
144
  quality="standard", # standard or hd
145
  size="512x512" if model == "dall-e-2" else "1024x1024", # varies for dalle-2 and dalle-3
 
170
  return image_paths, image_labels # Return both image paths and labels
171
 
172
  with gr.Blocks() as demo:
173
+ gr.Markdown("# <center>Prompt de Resistance Image Generator</center>")
174
+ gr.Markdown("**Instructions**: To use this service, please enter the password. Then generate an image from the prompt field below in response to the challenge, then click the download arrow from the top right of the image to save it.")
175
+ challenge_display = gr.Textbox(label="Challenge", value=get_challenge())
176
+ challenge_display.disabled = True
177
+ regenerate_btn = gr.Button("New Challenge")
178
  pw = gr.Textbox(label="Password", type="password",
179
  placeholder="Enter the password to unlock the service")
180
  text = gr.Textbox(label="What do you want to create?",
181
  placeholder="Enter your text and then click on the \"Image Generate\" button")
 
182
  model = gr.Dropdown(choices=["dall-e-2", "dall-e-3"], label="Model", value="dall-e-3")
183
  show_labels = gr.Checkbox(label="Show Image Labels", value=False)
184
  btn = gr.Button("Generate Images")
185
  output_images = gr.Gallery(label="Image Outputs", show_label=True, columns=[3], rows=[1], object_fit="contain",
186
  height="auto", allow_preview=False)
 
187
  #trigger generation either through hitting enter in the text field, or clicking the button.
188
  text.submit(fn=generate_images_wrapper, inputs=[text, pw, model, show_labels], outputs=output_images, api_name="generate_image") # Generate an api endpoint in Gradio / HF
189
  btn.click(fn=generate_images_wrapper, inputs=[text, pw, model, show_labels], outputs=output_images, api_name=False)
190
+ # toggle hiding and showing of labels
191
  show_labels.change(fn=update_labels, inputs=[show_labels], outputs=[output_images])
192
+ # generate new challenge
193
+ regenerate_btn.click(fn=get_challenge, inputs=[], outputs=[challenge_display])
194
  download_all_btn = gr.Button("Download All")
195
  download_link = gr.File(label="Download Zip")
196
  download_all_btn.click(fn=download_all_images, inputs=[], outputs=download_link)
challenges.txt ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ The most unbelievable extreme sports photo ever taken.
2
+ Transport technology has taken a giant leap backwards recently.
3
+ Worst. Supervillian. Costume. ever.
4
+ Tower control, we now have a visual on the unidentified flying object.
5
+ Design a new musical instrument.
6
+ Wow, for an action movie that film looks really boring.
7
+ That’s a big teapot!
8
+ That has to be the best stunt I’ve seen in an action movie.
9
+ Of all the things to spoil a marriage proposal.
10
+ Design a depressing birthday card.
11
+ Looks like the trampoline was too bouncy.
12
+ What happens when you put two metal detectors together?
13
+ An unexpected event occurred today at the athletics high jump.
14
+ They certainly have some juggling talent!
15
+ I’m sorry sir, but you can’t bring that into the cinema.
16
+ You don’t usually see boats there!
17
+ The most breathtaking fantasy landscape ever imagined.
18
+ Introducing my latest invention, it helps you wake up in the morning.
19
+ Do those clouds remind you of something?
20
+ I guess the DIY indoor swimming pool idea didn’t work out.