heaversm commited on
Commit
bae56f1
2 Parent(s): 9b36efd 6c73e7a

Merge branch 'dev' into mh/009-timer-toggles

Browse files
Files changed (1) hide show
  1. app.py +100 -17
app.py CHANGED
@@ -21,6 +21,8 @@ from pymongo.mongo_client import MongoClient
21
  from pymongo.server_api import ServerApi
22
  import time
23
 
 
 
24
 
25
 
26
  load_dotenv()
@@ -151,7 +153,7 @@ def generate_images(prompts, pw, model):
151
 
152
  image_url = response.data[0].url
153
  # conditionally render the user to the label with the prompt
154
- image_label = f"Prompt {i}: {text}" if user_initials == "" else f"Prompt {i}: {text}, User: {user_initials}"
155
 
156
  try:
157
  # Save the prompt, model, image URL, generation time and creation timestamp to the database
@@ -170,30 +172,111 @@ def generate_images(prompts, pw, model):
170
 
171
  return image_paths, image_labels # Return both image paths and labels
172
 
173
- with gr.Blocks() as demo:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
174
  gr.Markdown("# <center>Prompt de Resistance Image Generator</center>")
175
- with gr.Accordion("Instructions & Tips",label="Instructions & Tips",open=True):
176
- 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.")
177
- gr.Markdown("**Tips**: Use adjectives (size,color,mood), specify the visual style (realistic,cartoon,8-bit), explain the point of view (from above,first person,wide angle) ")
 
 
 
 
 
 
 
178
  challenge_display = gr.Textbox(label="Challenge", value=get_challenge())
179
  challenge_display.disabled = True
180
  regenerate_btn = gr.Button("New Challenge")
181
- pw = gr.Textbox(label="Password", type="password", placeholder="Enter the password to unlock the service")
 
 
 
 
 
 
 
 
 
 
 
 
 
182
  with gr.Accordion("Prompts",label="Prompts",open=True):
183
- text = gr.Textbox(label="What do you want to create?", placeholder="Enter your text and then click on the \"Image Generate\" button")
184
- model = gr.Dropdown(choices=["dall-e-2", "dall-e-3"], label="Model", value="dall-e-3")
185
- show_labels = gr.Checkbox(label="Show Image Labels", value=False)
186
- btn = gr.Button("Generate Images")
187
- output_images = gr.Gallery(label="Image Outputs", show_label=True, columns=[3], rows=[1], object_fit="contain", height="auto", allow_preview=False)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
188
  #trigger generation either through hitting enter in the text field, or clicking the button.
189
- 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
190
  btn.click(fn=generate_images_wrapper, inputs=[text, pw, model, show_labels], outputs=output_images, api_name=False)
191
- # toggle hiding and showing of labels
192
  show_labels.change(fn=update_labels, inputs=[show_labels], outputs=[output_images])
193
- # generate new challenge
194
- regenerate_btn.click(fn=get_challenge, inputs=[], outputs=[challenge_display])
195
- download_all_btn = gr.Button("Download All")
196
- download_link = gr.File(label="Download Zip")
197
  download_all_btn.click(fn=download_all_images, inputs=[], outputs=download_link)
198
 
199
  demo.launch(share=False)
 
21
  from pymongo.server_api import ServerApi
22
  import time
23
 
24
+ # countdown stuff
25
+ from datetime import datetime, timedelta
26
 
27
 
28
  load_dotenv()
 
153
 
154
  image_url = response.data[0].url
155
  # conditionally render the user to the label with the prompt
156
+ image_label = f"{i}: {text}" if user_initials == "" else f"{i}: {user_initials}-{text}, "
157
 
158
  try:
159
  # Save the prompt, model, image URL, generation time and creation timestamp to the database
 
172
 
173
  return image_paths, image_labels # Return both image paths and labels
174
 
175
+
176
+ #timer stuff
177
+ timer_cancelled_global = False # when true, timer does not tick down
178
+
179
+ def countdown(seconds):
180
+ """
181
+ This function takes the number of seconds as input and returns a string displaying the remaining time.
182
+ """
183
+ target_time = datetime.now() + timedelta(seconds=int(seconds))
184
+ while target_time > datetime.now():
185
+ remaining_time = target_time - datetime.now()
186
+ remaining_seconds = int(remaining_time.total_seconds())
187
+ yield f"{remaining_seconds:02d}"
188
+ # Check if the countdown was cancelled
189
+ if timer_cancelled_global:
190
+ break
191
+
192
+ def stop_countdown():
193
+ """
194
+ This function stops the countdown.
195
+ """
196
+ global timer_cancelled_global
197
+ timer_cancelled_global = True
198
+
199
+ def reset_countdown(slider):
200
+ """
201
+ This function resets the countdown.
202
+ """
203
+ global timer_cancelled_global
204
+ timer_cancelled_global = False
205
+ return 60
206
+
207
+
208
+ #custom css
209
+ css = """
210
+ #gallery-images .caption-label {
211
+ white-space: normal !important;
212
+ }
213
+ """
214
+
215
+
216
+ with gr.Blocks(css=css) as demo:
217
+
218
  gr.Markdown("# <center>Prompt de Resistance Image Generator</center>")
219
+
220
+ pw = gr.Textbox(label="Password", type="password", placeholder="Enter the password to unlock the service")
221
+
222
+ #instructions
223
+ with gr.Accordion("Instructions & Tips",label="instructions",open=False):
224
+ with gr.Row():
225
+ 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.")
226
+ gr.Markdown("**Tips**: Use adjectives (size,color,mood), specify the visual style (realistic,cartoon,8-bit), explain the point of view (from above,first person,wide angle) ")
227
+
228
+ #challenge
229
  challenge_display = gr.Textbox(label="Challenge", value=get_challenge())
230
  challenge_display.disabled = True
231
  regenerate_btn = gr.Button("New Challenge")
232
+
233
+
234
+ #countdown
235
+ with gr.Accordion("Countdown",label="Countdown",open=False):
236
+ with gr.Row():
237
+ with gr.Column(scale=3):
238
+ slider = gr.Slider(minimum=1, maximum=120, value=60,label="Countdown",info="Select duration in seconds")
239
+ with gr.Column(scale=1):
240
+ countdown_button = gr.Button("Start")
241
+ stop_countdown_button = gr.Button("Stop")
242
+ reset_countdown_button = gr.Button("Reset")
243
+
244
+
245
+ #prompts
246
  with gr.Accordion("Prompts",label="Prompts",open=True):
247
+ with gr.Row():
248
+ with gr.Column(scale=3):
249
+ text = gr.Textbox(label="What do you want to create?", placeholder="Enter your text and then click on the \"Image Generate\" button")
250
+ with gr.Column(scale=1):
251
+ model = gr.Dropdown(choices=["dall-e-2", "dall-e-3"], label="Model", value="dall-e-3")
252
+ with gr.Row():
253
+ btn = gr.Button("Generate Images")
254
+
255
+ #output
256
+ with gr.Accordion("Image Outputs",label="Image Outputs",open=True):
257
+ output_images = gr.Gallery(label="Image Outputs", elem_id="gallery-images", show_label=True, columns=[3], rows=[1], object_fit="contain", height="auto", allow_preview=False)
258
+ show_labels = gr.Checkbox(label="Show Labels", value=False)
259
+
260
+
261
+ with gr.Accordion("Downloads",label="download",open=False):
262
+ download_all_btn = gr.Button("Download All")
263
+ download_link = gr.File(label="Download Zip")
264
+
265
+ # generate new challenge
266
+ regenerate_btn.click(fn=get_challenge, inputs=[], outputs=[challenge_display])
267
+
268
+ #countdown
269
+ countdown_button.click(fn=countdown, inputs=[slider], outputs=[slider])
270
+ stop_countdown_button.click(fn=stop_countdown)
271
+ reset_countdown_button.click(fn=reset_countdown,inputs=[slider],outputs=[slider])
272
+
273
+ #submissions
274
  #trigger generation either through hitting enter in the text field, or clicking the button.
 
275
  btn.click(fn=generate_images_wrapper, inputs=[text, pw, model, show_labels], outputs=output_images, api_name=False)
276
+ 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
277
  show_labels.change(fn=update_labels, inputs=[show_labels], outputs=[output_images])
278
+
279
+ #downloads
 
 
280
  download_all_btn.click(fn=download_all_images, inputs=[], outputs=download_link)
281
 
282
  demo.launch(share=False)