1inkusFace commited on
Commit
37e2aa5
·
verified ·
1 Parent(s): 5116401

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -30
app.py CHANGED
@@ -110,7 +110,7 @@ def apply_style(style_name: str, positive: str, negative: str = "") -> Tuple[str
110
  if not negative:
111
  negative = ""
112
  return p.replace("{prompt}", positive), n + negative
113
-
114
  #unetX = UNet2DConditionModel.from_pretrained('ford442/RealVisXL_V5.0_BF16', subfolder='unet', low_cpu_mem_usage=False, token=True) #.to(device).to(torch.bfloat16) #.to(device=device, dtype=torch.bfloat16)
115
 
116
  def load_and_prepare_model():
@@ -128,7 +128,7 @@ def load_and_prepare_model():
128
  vae=None,
129
  # unet=None,
130
  )
131
-
132
  '''
133
  scaling_factor (`float`, *optional*, defaults to 0.18215):
134
  The component-wise standard deviation of the trained latent space computed using the first batch of the
@@ -141,7 +141,7 @@ def load_and_prepare_model():
141
  If enabled it will force the VAE to run in float32 for high image resolution pipelines, such as SD-XL. VAE
142
  can be fine-tuned / trained to a lower range without loosing too much precision in which case
143
  `force_upcast` can be set to `False` - see: https://huggingface.co/madebyollin/sdxl-vae-fp16-fix
144
-
145
  '''
146
  #pipe.vae=vaeX
147
  pipe.vae=vaeX.to(device)
@@ -163,9 +163,9 @@ def load_and_prepare_model():
163
  print(f'init noise scale: {pipe.scheduler.init_noise_sigma}')
164
  #print(f'UNET: {pipe.unet}')
165
  pipe.watermark=None
166
- pipe.safety_checker=None
167
  return pipe
168
-
169
  # Preload and compile both models
170
  pipe = load_and_prepare_model()
171
 
@@ -181,13 +181,13 @@ checkpoint = "ford442/Phi-3.5-mini-instruct-bf16"
181
  #txt_tokenizer.tokenizer_legacy=False
182
  #model = Phi3ForCausalLM.from_pretrained(checkpoint).to('cuda:0')
183
  #model = AutoModelForCausalLM.from_pretrained(checkpoint, device_map='cuda') #.to('cuda')
184
- model5 = InstructBlipForConditionalGeneration.from_pretrained("Salesforce/instructblip-vicuna-7b").to('cuda')
185
  processor5 = InstructBlipProcessor.from_pretrained("Salesforce/instructblip-vicuna-7b")
186
 
187
  ip_model = IPAdapterXL(pipe, local_folder, ip_ckpt, device)
188
  text_encoder_1=CLIPTextModel.from_pretrained('ford442/RealVisXL_V5.0_BF16', subfolder='text_encoder',token=True) #.to(device=device, dtype=torch.bfloat16)
189
  text_encoder_2=CLIPTextModelWithProjection.from_pretrained('ford442/RealVisXL_V5.0_BF16', subfolder='text_encoder_2',token=True) #.to(device=device, dtype=torch.bfloat16)
190
-
191
  MAX_SEED = np.iinfo(np.int32).max
192
 
193
  neg_prompt_2 = " 'non-photorealistic':1.5, 'unrealistic skin','unattractive face':1.3, 'low quality':1.1, ('dull color scheme', 'dull colors', 'digital noise':1.2),'amateurish', 'poorly drawn face':1.3, 'poorly drawn', 'distorted face', 'low resolution', 'simplistic' "
@@ -212,7 +212,7 @@ def filter_text(text,phraseC):
212
  else:
213
  # Handle the case where no match is found
214
  return text
215
-
216
  def upload_to_ftp(filename):
217
  try:
218
  transport = paramiko.Transport((FTP_HOST, 22))
@@ -225,7 +225,7 @@ def upload_to_ftp(filename):
225
  print(f"Uploaded {filename} to FTP server")
226
  except Exception as e:
227
  print(f"FTP upload error: {e}")
228
-
229
  def save_image(img):
230
  unique_name = str(uuid.uuid4()) + ".png"
231
  img.save(unique_name,optimize=False,compress_level=0)
@@ -241,7 +241,7 @@ def uploadNote(prompt,num_inference_steps,guidance_scale,timestamp):
241
  f.write(f"Guidance Scale: {guidance_scale} \n")
242
  f.write(f"SPACE SETUP: \n")
243
  f.write(f"Model UNET: ford442/RealVisXL_V5.0_BF16 \n")
244
- upload_to_ftp(filename)
245
 
246
  def captioning(img):
247
  prompts_array = [
@@ -252,7 +252,7 @@ def captioning(img):
252
  "The larger details in this scene include",
253
  "The smaller details in this scene include",
254
  # "The feeling this scene seems like",
255
- "The setting of this scene must be located",
256
  # Add more prompts here
257
  ]
258
  output_prompt=[]
@@ -271,11 +271,11 @@ def captioning(img):
271
  length_penalty=1.0,
272
  temperature=1,
273
  )
274
-
275
  generated_text = processor5.batch_decode(generated_ids, skip_special_tokens=True)[0].strip()
276
  generated_text = generated_text.replace(cap_prompt, "").strip() #Or could try .split(prompt, 1)[-1].strip()
277
  output_prompt.append(generated_text)
278
- print(generated_text)
279
  # Loop through prompts array:
280
  for prompt in prompts_array:
281
  inputs = processor5(images=img, text=prompt, return_tensors="pt").to('cuda')
@@ -317,7 +317,7 @@ def captioning(img):
317
  output_prompt.append(response_text)
318
  output_prompt = " ".join(output_prompt)
319
  return output_prompt
320
-
321
  def flatten_and_stringify(data):
322
  return [str(item) for sublist in data if isinstance(sublist, list) for item in flatten_and_stringify(sublist) ] + [str(item) for item in data if not isinstance(item, list)]
323
 
@@ -381,8 +381,8 @@ def expand_prompt(prompt):
381
  print(enhanced_prompt_2)
382
  enh_prompt=[enhanced_prompt,enhanced_prompt_2]
383
  '''
384
- return enhanced_prompt
385
-
386
  @spaces.GPU(duration=40)
387
  def generate_30(
388
  prompt: str = "",
@@ -506,8 +506,8 @@ def generate_30(
506
  downscale1 = upscale.resize((upscale.width // 4, upscale.height // 4), Image.LANCZOS)
507
  downscale_path = f"rvIP_upscale_{timestamp}.png"
508
  downscale1.save(downscale_path,optimize=False,compress_level=0)
509
- upload_to_ftp(downscale_path)
510
- image_paths = [save_image(downscale1)]
511
  else:
512
  print('-- IMAGE REQUIRED --')
513
  return image_paths
@@ -635,8 +635,8 @@ def generate_60(
635
  downscale1 = upscale.resize((upscale.width // 4, upscale.height // 4), Image.LANCZOS)
636
  downscale_path = f"rvIP_upscale_{timestamp}.png"
637
  downscale1.save(downscale_path,optimize=False,compress_level=0)
638
- upload_to_ftp(downscale_path)
639
- image_paths = [save_image(downscale1)]
640
  else:
641
  print('-- IMAGE REQUIRED --')
642
  return image_paths
@@ -764,8 +764,8 @@ def generate_90(
764
  downscale1 = upscale.resize((upscale.width // 4, upscale.height // 4), Image.LANCZOS)
765
  downscale_path = f"rvIP_upscale_{timestamp}.png"
766
  downscale1.save(downscale_path,optimize=False,compress_level=0)
767
- upload_to_ftp(downscale_path)
768
- image_paths = [save_image(downscale1)]
769
  else:
770
  print('-- IMAGE REQUIRED --')
771
  return image_paths
@@ -818,7 +818,7 @@ with gr.Blocks(theme=gr.themes.Origin(),css=css) as demo:
818
  run_button_30 = gr.Button("Run 30 Seconds", scale=0)
819
  run_button_60 = gr.Button("Run 60 Seconds", scale=0)
820
  run_button_90 = gr.Button("Run 90 Seconds", scale=0)
821
- result = gr.Gallery(label="Result", columns=1, show_label=False)
822
  ip_strength = gr.Slider(
823
  label="Image Strength",
824
  minimum=0.0,
@@ -938,7 +938,7 @@ with gr.Blocks(theme=gr.themes.Origin(),css=css) as demo:
938
  outputs=negative_prompt,
939
  api_name=False,
940
  )
941
-
942
  gr.on(
943
  triggers=[
944
  run_button_30.click,
@@ -970,7 +970,7 @@ with gr.Blocks(theme=gr.themes.Origin(),css=css) as demo:
970
  ],
971
  outputs=[result],
972
  )
973
-
974
  gr.on(
975
  triggers=[
976
  run_button_60.click,
@@ -1002,7 +1002,7 @@ with gr.Blocks(theme=gr.themes.Origin(),css=css) as demo:
1002
  ],
1003
  outputs=[result],
1004
  )
1005
-
1006
  gr.on(
1007
  triggers=[
1008
  run_button_90.click,
@@ -1051,7 +1051,7 @@ with gr.Blocks(theme=gr.themes.Origin(),css=css) as demo:
1051
  gr.Markdown(
1052
  """
1053
  <div style="text-align: justify;">
1054
- ⚡This is the demo space for generating images using Stable Diffusion XL with quality styles, different models, and types. Try the sample prompts to generate higher quality images. Try the sample prompts for generating higher quality images.
1055
  <a href='https://huggingface.co/spaces/prithivMLmods/Top-Prompt-Collection' target='_blank'>Try prompts</a>.
1056
  </div>
1057
  """)
@@ -1061,12 +1061,12 @@ with gr.Blocks(theme=gr.themes.Origin(),css=css) as demo:
1061
  <div style="text-align: justify;">
1062
  ⚠️ Users are accountable for the content they generate and are responsible for ensuring it meets appropriate ethical standards.
1063
  </div>
1064
- """)
1065
 
1066
  def text_generation(input_text, seed):
1067
  full_prompt = "Text Generator Application by ecarbo"
1068
  return full_prompt
1069
-
1070
  title = "Text Generator Demo GPT-Neo"
1071
  description = "Text Generator Application by ecarbo"
1072
 
@@ -1083,4 +1083,4 @@ if __name__ == "__main__":
1083
  description=description,
1084
  )
1085
  combined_interface = gr.TabbedInterface([demo_interface, text_gen_interface], ["Image Generation", "Text Generation"])
1086
- combined_interface.launch(show_api=False)
 
110
  if not negative:
111
  negative = ""
112
  return p.replace("{prompt}", positive), n + negative
113
+
114
  #unetX = UNet2DConditionModel.from_pretrained('ford442/RealVisXL_V5.0_BF16', subfolder='unet', low_cpu_mem_usage=False, token=True) #.to(device).to(torch.bfloat16) #.to(device=device, dtype=torch.bfloat16)
115
 
116
  def load_and_prepare_model():
 
128
  vae=None,
129
  # unet=None,
130
  )
131
+
132
  '''
133
  scaling_factor (`float`, *optional*, defaults to 0.18215):
134
  The component-wise standard deviation of the trained latent space computed using the first batch of the
 
141
  If enabled it will force the VAE to run in float32 for high image resolution pipelines, such as SD-XL. VAE
142
  can be fine-tuned / trained to a lower range without loosing too much precision in which case
143
  `force_upcast` can be set to `False` - see: https://huggingface.co/madebyollin/sdxl-vae-fp16-fix
144
+
145
  '''
146
  #pipe.vae=vaeX
147
  pipe.vae=vaeX.to(device)
 
163
  print(f'init noise scale: {pipe.scheduler.init_noise_sigma}')
164
  #print(f'UNET: {pipe.unet}')
165
  pipe.watermark=None
166
+ pipe.safety_checker=None
167
  return pipe
168
+
169
  # Preload and compile both models
170
  pipe = load_and_prepare_model()
171
 
 
181
  #txt_tokenizer.tokenizer_legacy=False
182
  #model = Phi3ForCausalLM.from_pretrained(checkpoint).to('cuda:0')
183
  #model = AutoModelForCausalLM.from_pretrained(checkpoint, device_map='cuda') #.to('cuda')
184
+ model5 = InstructBlipForConditionalGeneration.from_pretrained("Salesforce/instructblip-vicuna-7b").to('cuda',torch.bfloat16)
185
  processor5 = InstructBlipProcessor.from_pretrained("Salesforce/instructblip-vicuna-7b")
186
 
187
  ip_model = IPAdapterXL(pipe, local_folder, ip_ckpt, device)
188
  text_encoder_1=CLIPTextModel.from_pretrained('ford442/RealVisXL_V5.0_BF16', subfolder='text_encoder',token=True) #.to(device=device, dtype=torch.bfloat16)
189
  text_encoder_2=CLIPTextModelWithProjection.from_pretrained('ford442/RealVisXL_V5.0_BF16', subfolder='text_encoder_2',token=True) #.to(device=device, dtype=torch.bfloat16)
190
+
191
  MAX_SEED = np.iinfo(np.int32).max
192
 
193
  neg_prompt_2 = " 'non-photorealistic':1.5, 'unrealistic skin','unattractive face':1.3, 'low quality':1.1, ('dull color scheme', 'dull colors', 'digital noise':1.2),'amateurish', 'poorly drawn face':1.3, 'poorly drawn', 'distorted face', 'low resolution', 'simplistic' "
 
212
  else:
213
  # Handle the case where no match is found
214
  return text
215
+
216
  def upload_to_ftp(filename):
217
  try:
218
  transport = paramiko.Transport((FTP_HOST, 22))
 
225
  print(f"Uploaded {filename} to FTP server")
226
  except Exception as e:
227
  print(f"FTP upload error: {e}")
228
+
229
  def save_image(img):
230
  unique_name = str(uuid.uuid4()) + ".png"
231
  img.save(unique_name,optimize=False,compress_level=0)
 
241
  f.write(f"Guidance Scale: {guidance_scale} \n")
242
  f.write(f"SPACE SETUP: \n")
243
  f.write(f"Model UNET: ford442/RealVisXL_V5.0_BF16 \n")
244
+ upload_to_ftp(filename)
245
 
246
  def captioning(img):
247
  prompts_array = [
 
252
  "The larger details in this scene include",
253
  "The smaller details in this scene include",
254
  # "The feeling this scene seems like",
255
+ "The setting of this scene must be located",
256
  # Add more prompts here
257
  ]
258
  output_prompt=[]
 
271
  length_penalty=1.0,
272
  temperature=1,
273
  )
274
+
275
  generated_text = processor5.batch_decode(generated_ids, skip_special_tokens=True)[0].strip()
276
  generated_text = generated_text.replace(cap_prompt, "").strip() #Or could try .split(prompt, 1)[-1].strip()
277
  output_prompt.append(generated_text)
278
+ print(generated_text)
279
  # Loop through prompts array:
280
  for prompt in prompts_array:
281
  inputs = processor5(images=img, text=prompt, return_tensors="pt").to('cuda')
 
317
  output_prompt.append(response_text)
318
  output_prompt = " ".join(output_prompt)
319
  return output_prompt
320
+
321
  def flatten_and_stringify(data):
322
  return [str(item) for sublist in data if isinstance(sublist, list) for item in flatten_and_stringify(sublist) ] + [str(item) for item in data if not isinstance(item, list)]
323
 
 
381
  print(enhanced_prompt_2)
382
  enh_prompt=[enhanced_prompt,enhanced_prompt_2]
383
  '''
384
+ return enhanced_prompt
385
+
386
  @spaces.GPU(duration=40)
387
  def generate_30(
388
  prompt: str = "",
 
506
  downscale1 = upscale.resize((upscale.width // 4, upscale.height // 4), Image.LANCZOS)
507
  downscale_path = f"rvIP_upscale_{timestamp}.png"
508
  downscale1.save(downscale_path,optimize=False,compress_level=0)
509
+ upload_to_ftp(downscale_path)
510
+ image_paths = [save_image(downscale1)]
511
  else:
512
  print('-- IMAGE REQUIRED --')
513
  return image_paths
 
635
  downscale1 = upscale.resize((upscale.width // 4, upscale.height // 4), Image.LANCZOS)
636
  downscale_path = f"rvIP_upscale_{timestamp}.png"
637
  downscale1.save(downscale_path,optimize=False,compress_level=0)
638
+ upload_to_ftp(downscale_path)
639
+ image_paths = [save_image(downscale1)]
640
  else:
641
  print('-- IMAGE REQUIRED --')
642
  return image_paths
 
764
  downscale1 = upscale.resize((upscale.width // 4, upscale.height // 4), Image.LANCZOS)
765
  downscale_path = f"rvIP_upscale_{timestamp}.png"
766
  downscale1.save(downscale_path,optimize=False,compress_level=0)
767
+ upload_to_ftp(downscale_path)
768
+ image_paths = [save_image(downscale1)]
769
  else:
770
  print('-- IMAGE REQUIRED --')
771
  return image_paths
 
818
  run_button_30 = gr.Button("Run 30 Seconds", scale=0)
819
  run_button_60 = gr.Button("Run 60 Seconds", scale=0)
820
  run_button_90 = gr.Button("Run 90 Seconds", scale=0)
821
+ result = gr.Gallery(label="Result", columns=1, show_label=False)
822
  ip_strength = gr.Slider(
823
  label="Image Strength",
824
  minimum=0.0,
 
938
  outputs=negative_prompt,
939
  api_name=False,
940
  )
941
+
942
  gr.on(
943
  triggers=[
944
  run_button_30.click,
 
970
  ],
971
  outputs=[result],
972
  )
973
+
974
  gr.on(
975
  triggers=[
976
  run_button_60.click,
 
1002
  ],
1003
  outputs=[result],
1004
  )
1005
+
1006
  gr.on(
1007
  triggers=[
1008
  run_button_90.click,
 
1051
  gr.Markdown(
1052
  """
1053
  <div style="text-align: justify;">
1054
+ ⚡This is the demo space for generating images using Stable Diffusion XL with quality styles, different models, and types. Try the sample prompts to generate higher quality images. Try the sample prompts for generating higher quality images.
1055
  <a href='https://huggingface.co/spaces/prithivMLmods/Top-Prompt-Collection' target='_blank'>Try prompts</a>.
1056
  </div>
1057
  """)
 
1061
  <div style="text-align: justify;">
1062
  ⚠️ Users are accountable for the content they generate and are responsible for ensuring it meets appropriate ethical standards.
1063
  </div>
1064
+ """)
1065
 
1066
  def text_generation(input_text, seed):
1067
  full_prompt = "Text Generator Application by ecarbo"
1068
  return full_prompt
1069
+
1070
  title = "Text Generator Demo GPT-Neo"
1071
  description = "Text Generator Application by ecarbo"
1072
 
 
1083
  description=description,
1084
  )
1085
  combined_interface = gr.TabbedInterface([demo_interface, text_gen_interface], ["Image Generation", "Text Generation"])
1086
+ combined_interface.launch(show_api=False)