Pijush2023 commited on
Commit
85c787a
·
verified ·
1 Parent(s): d790f10

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -18
app.py CHANGED
@@ -713,39 +713,75 @@ def generate_map(location_names):
713
  # return image_1, image_2, image_3
714
 
715
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
716
  import torch
717
  from diffusers import DiffusionPipeline
718
- import numpy as np
719
- import random
720
- import gradio as gr
721
 
722
- # Constants for device and dtype
723
- dtype = torch.bfloat16
 
 
 
 
 
 
724
  device = "cuda" if torch.cuda.is_available() else "cpu"
725
 
726
- # Load the Flux pipeline model
727
  pipe = DiffusionPipeline.from_pretrained("black-forest-labs/FLUX.1-schnell", torch_dtype=dtype).to(device)
728
 
729
- MAX_SEED = np.iinfo(np.int32).max
730
- MAX_IMAGE_SIZE = 2048
731
-
732
- # Function for inference using the Flux pipeline
733
- def infer(prompt, seed=42, randomize_seed=False, width=1024, height=1024, num_inference_steps=4):
734
- if randomize_seed:
735
- seed = random.randint(0, MAX_SEED)
736
  generator = torch.Generator(device).manual_seed(seed)
737
-
738
- # Generate the image using the pipeline
739
  image = pipe(
740
  prompt=prompt,
741
  width=width,
742
  height=height,
743
- num_inference_steps=num_inference_steps,
744
  generator=generator,
745
  guidance_scale=0.0
746
  ).images[0]
747
-
748
- return image, seed
749
 
750
  # Existing image generation function, updated to use the new Flux pipeline
751
  def update_images():
 
713
  # return image_1, image_2, image_3
714
 
715
 
716
+ # import torch
717
+ # from diffusers import DiffusionPipeline
718
+ # import numpy as np
719
+ # import random
720
+ # import gradio as gr
721
+
722
+ # # Constants for device and dtype
723
+ # dtype = torch.bfloat16
724
+ # device = "cuda" if torch.cuda.is_available() else "cpu"
725
+
726
+ # # Clear CUDA memory
727
+ # torch.cuda.empty_cache()
728
+
729
+ # # Load the Flux pipeline model
730
+ # pipe = DiffusionPipeline.from_pretrained("black-forest-labs/FLUX.1-schnell", torch_dtype=dtype).to(device)
731
+
732
+ # MAX_SEED = np.iinfo(np.int32).max
733
+ # MAX_IMAGE_SIZE = 2048
734
+
735
+ # # Function for inference using the Flux pipeline
736
+ # def infer(prompt, seed=42, randomize_seed=False, width=1024, height=1024, num_inference_steps=4):
737
+ # if randomize_seed:
738
+ # seed = random.randint(0, MAX_SEED)
739
+ # generator = torch.Generator(device).manual_seed(seed)
740
+
741
+ # # Generate the image using the pipeline
742
+ # image = pipe(
743
+ # prompt=prompt,
744
+ # width=width,
745
+ # height=height,
746
+ # num_inference_steps=num_inference_steps,
747
+ # generator=generator,
748
+ # guidance_scale=0.0
749
+ # ).images[0]
750
+
751
+ # return image, seed
752
+
753
+
754
+
755
  import torch
756
  from diffusers import DiffusionPipeline
757
+ import os
 
 
758
 
759
+ # Set PYTORCH_CUDA_ALLOC_CONF to avoid memory fragmentation
760
+ os.environ['PYTORCH_CUDA_ALLOC_CONF'] = 'expandable_segments:True'
761
+
762
+ # Clear CUDA cache before loading the model
763
+ torch.cuda.empty_cache()
764
+
765
+ # Use a smaller dtype (e.g., torch.float16)
766
+ dtype = torch.float16 if torch.cuda.is_available() else torch.float32
767
  device = "cuda" if torch.cuda.is_available() else "cpu"
768
 
769
+ # Load the model with a smaller precision
770
  pipe = DiffusionPipeline.from_pretrained("black-forest-labs/FLUX.1-schnell", torch_dtype=dtype).to(device)
771
 
772
+ # Reduce inference steps to save memory
773
+ def generate_image_flux(prompt, seed=42, width=1024, height=1024, num_inference_steps=2):
 
 
 
 
 
774
  generator = torch.Generator(device).manual_seed(seed)
 
 
775
  image = pipe(
776
  prompt=prompt,
777
  width=width,
778
  height=height,
779
+ num_inference_steps=num_inference_steps, # Reduce the number of inference steps
780
  generator=generator,
781
  guidance_scale=0.0
782
  ).images[0]
783
+ return image
784
+
785
 
786
  # Existing image generation function, updated to use the new Flux pipeline
787
  def update_images():