awacke1 commited on
Commit
60bf8bb
1 Parent(s): bb1b13b

Update backup.app.py

Browse files
Files changed (1) hide show
  1. backup.app.py +40 -19
backup.app.py CHANGED
@@ -1,26 +1,26 @@
 
 
1
  import gradio as gr
2
- from diffusers import DiffusionPipeline, LCMScheduler, AutoencoderTiny
3
- import torch
4
  import os
5
- import datetime
 
 
 
 
6
  import time
 
 
7
  from PIL import Image
8
- import re
9
- import base64
10
  from io import BytesIO
11
- import pytz
 
12
 
13
  try:
14
  import intel_extension_for_pytorch as ipex
15
  except:
16
  pass
17
 
18
- from PIL import Image
19
- import numpy as np
20
- import gradio as gr
21
- import psutil
22
- import time
23
-
24
  SAFETY_CHECKER = os.environ.get("SAFETY_CHECKER", None)
25
  TORCH_COMPILE = os.environ.get("TORCH_COMPILE", None)
26
  HF_TOKEN = os.environ.get("HF_TOKEN", None)
@@ -76,6 +76,12 @@ def encode_image(image):
76
  buffered = BytesIO()
77
  #image.save(buffered, format="PNG")
78
  return base64.b64encode(buffered.getvalue()).decode()
 
 
 
 
 
 
79
 
80
  def predict(prompt, guidance, steps, seed=1231231):
81
  generator = torch.manual_seed(seed)
@@ -99,11 +105,6 @@ def predict(prompt, guidance, steps, seed=1231231):
99
  if nsfw_content_detected:
100
  nsfw=gr.Button("🕹️NSFW🎨", scale=1)
101
 
102
- # Generate file name
103
- #date_str = datetime.datetime.now().strftime("%Y%m%d")
104
- #safe_prompt = prompt.replace(" ", "_")[:50] # Truncate long prompts
105
- #filename = f"{date_str}_{safe_prompt}.png"
106
-
107
  central = pytz.timezone('US/Central')
108
  safe_date_time = datetime.datetime.now().strftime("%Y%m%d")
109
  replaced_prompt = prompt.replace(" ", "_").replace("\n", "_")
@@ -116,8 +117,6 @@ def predict(prompt, guidance, steps, seed=1231231):
116
  image_path = os.path.join("", filename) # Specify your directory
117
  results.images[0].save(image_path)
118
  print(f"#Image saved as {image_path}")
119
- #filename = safe_filename(prompt)
120
- #image.save(filename)
121
  encoded_image = encode_image(image)
122
  html_link = f'<a href="data:image/png;base64,{encoded_image}" download="{filename}">Download Image</a>'
123
  gr.Markdown(html_link)
@@ -151,7 +150,23 @@ with gr.Blocks(css=css) as demo:
151
  )
152
  generate_bt = gr.Button("Generate", scale=1)
153
 
 
154
  image = gr.Image(type="filepath")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
155
  with gr.Accordion("Advanced options", open=False):
156
  guidance = gr.Slider(
157
  label="Guidance", minimum=0.0, maximum=5, value=0.3, step=0.001
@@ -160,6 +175,8 @@ with gr.Blocks(css=css) as demo:
160
  seed = gr.Slider(
161
  randomize=True, minimum=0, maximum=12013012031030, label="Seed", step=1
162
  )
 
 
163
  with gr.Accordion("Run with diffusers"):
164
  gr.Markdown(
165
  """## Running LCM-LoRAs it with `diffusers`
@@ -169,9 +186,11 @@ with gr.Blocks(css=css) as demo:
169
 
170
  ```py
171
  from diffusers import DiffusionPipeline, LCMScheduler
 
172
  pipe = DiffusionPipeline.from_pretrained("Lykon/dreamshaper-7").to("cuda")
173
  pipe.scheduler = LCMScheduler.from_config(pipe.scheduler.config)
174
  pipe.load_lora_weights("latent-consistency/lcm-lora-sdv1-5") #yes, it's a normal LoRA
 
175
  results = pipe(
176
  prompt="ImageEditor",
177
  num_inference_steps=4,
@@ -182,8 +201,10 @@ with gr.Blocks(css=css) as demo:
182
  """
183
  )
184
 
 
185
  inputs = [prompt, guidance, steps, seed]
186
  generate_bt.click(fn=predict, inputs=inputs, outputs=image, show_progress=False)
 
187
  prompt.input(fn=predict, inputs=inputs, outputs=image, show_progress=False)
188
  guidance.change(fn=predict, inputs=inputs, outputs=image, show_progress=False)
189
  steps.change(fn=predict, inputs=inputs, outputs=image, show_progress=False)
 
1
+ import base64
2
+ import datetime
3
  import gradio as gr
4
+ import numpy as np
 
5
  import os
6
+ import pytz
7
+ import psutil
8
+ import re
9
+ import random
10
+ import torch
11
  import time
12
+ import time
13
+
14
  from PIL import Image
 
 
15
  from io import BytesIO
16
+ from PIL import Image
17
+ from diffusers import DiffusionPipeline, LCMScheduler, AutoencoderTiny
18
 
19
  try:
20
  import intel_extension_for_pytorch as ipex
21
  except:
22
  pass
23
 
 
 
 
 
 
 
24
  SAFETY_CHECKER = os.environ.get("SAFETY_CHECKER", None)
25
  TORCH_COMPILE = os.environ.get("TORCH_COMPILE", None)
26
  HF_TOKEN = os.environ.get("HF_TOKEN", None)
 
76
  buffered = BytesIO()
77
  #image.save(buffered, format="PNG")
78
  return base64.b64encode(buffered.getvalue()).decode()
79
+
80
+ def fake_gan():
81
+ base_dir = os.getcwd() # Get the current base directory
82
+ img_files = [file for file in os.listdir(base_dir) if file.lower().endswith((".png", ".jpg", ".jpeg"))] # List all files ending with ".jpg" or ".jpeg"
83
+ images = [(random.choice(img_files), os.path.splitext(file)[0]) for file in img_files]
84
+ return images
85
 
86
  def predict(prompt, guidance, steps, seed=1231231):
87
  generator = torch.manual_seed(seed)
 
105
  if nsfw_content_detected:
106
  nsfw=gr.Button("🕹️NSFW🎨", scale=1)
107
 
 
 
 
 
 
108
  central = pytz.timezone('US/Central')
109
  safe_date_time = datetime.datetime.now().strftime("%Y%m%d")
110
  replaced_prompt = prompt.replace(" ", "_").replace("\n", "_")
 
117
  image_path = os.path.join("", filename) # Specify your directory
118
  results.images[0].save(image_path)
119
  print(f"#Image saved as {image_path}")
 
 
120
  encoded_image = encode_image(image)
121
  html_link = f'<a href="data:image/png;base64,{encoded_image}" download="{filename}">Download Image</a>'
122
  gr.Markdown(html_link)
 
150
  )
151
  generate_bt = gr.Button("Generate", scale=1)
152
 
153
+ # Image Result from last prompt
154
  image = gr.Image(type="filepath")
155
+
156
+ # Gallery of Generated Images with Image Names in Random Set to Download
157
+ with gr.Row(variant="compact"):
158
+ text = gr.Textbox(
159
+ label="Image Sets",
160
+ show_label=False,
161
+ max_lines=1,
162
+ placeholder="Enter your prompt",
163
+ )
164
+ btn = gr.Button("Generate Gallery of Saved Images")
165
+ gallery = gr.Gallery(
166
+ label="Generated Images", show_label=False, elem_id="gallery"
167
+ )
168
+
169
+ # Advanced Generate Options
170
  with gr.Accordion("Advanced options", open=False):
171
  guidance = gr.Slider(
172
  label="Guidance", minimum=0.0, maximum=5, value=0.3, step=0.001
 
175
  seed = gr.Slider(
176
  randomize=True, minimum=0, maximum=12013012031030, label="Seed", step=1
177
  )
178
+
179
+ # Diffusers
180
  with gr.Accordion("Run with diffusers"):
181
  gr.Markdown(
182
  """## Running LCM-LoRAs it with `diffusers`
 
186
 
187
  ```py
188
  from diffusers import DiffusionPipeline, LCMScheduler
189
+
190
  pipe = DiffusionPipeline.from_pretrained("Lykon/dreamshaper-7").to("cuda")
191
  pipe.scheduler = LCMScheduler.from_config(pipe.scheduler.config)
192
  pipe.load_lora_weights("latent-consistency/lcm-lora-sdv1-5") #yes, it's a normal LoRA
193
+
194
  results = pipe(
195
  prompt="ImageEditor",
196
  num_inference_steps=4,
 
201
  """
202
  )
203
 
204
+ # Function IO Eventing and Controls
205
  inputs = [prompt, guidance, steps, seed]
206
  generate_bt.click(fn=predict, inputs=inputs, outputs=image, show_progress=False)
207
+ btn.click(fake_gan, None, gallery)
208
  prompt.input(fn=predict, inputs=inputs, outputs=image, show_progress=False)
209
  guidance.change(fn=predict, inputs=inputs, outputs=image, show_progress=False)
210
  steps.change(fn=predict, inputs=inputs, outputs=image, show_progress=False)