Pijush2023 commited on
Commit
3c82a74
·
verified ·
1 Parent(s): 376f0d5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -4
app.py CHANGED
@@ -70,6 +70,7 @@ current_date1 = get_current_date1()
70
  # Set environment variables for CUDA
71
  os.environ['PYTORCH_USE_CUDA_DSA'] = '1'
72
  os.environ['CUDA_LAUNCH_BLOCKING'] = '1'
 
73
 
74
 
75
  hf_token = os.getenv("HF_TOKEN")
@@ -711,6 +712,13 @@ def generate_map(location_names):
711
  # image_3 = generate_image(hardcoded_prompt_3)
712
  # return image_1, image_2, image_3
713
 
 
 
 
 
 
 
 
714
  import gradio as gr
715
  import torch
716
  from diffusers import FluxPipeline
@@ -720,8 +728,14 @@ device = "cuda:0" if torch.cuda.is_available() else "cpu"
720
 
721
  # Function to initialize Flux bot model
722
  def initialize_flux_bot():
723
- pipe = FluxPipeline.from_pretrained("black-forest-labs/FLUX.1-schnell", torch_dtype=torch.bfloat16)
724
- pipe.to(device) # Move the model to the correct device (GPU/CPU)
 
 
 
 
 
 
725
  return pipe
726
 
727
  # Function to generate image using Flux bot on the specified device
@@ -730,8 +744,8 @@ def generate_image_flux(prompt):
730
  image = pipe(
731
  prompt,
732
  guidance_scale=0.0,
733
- num_inference_steps=4,
734
- max_sequence_length=256,
735
  generator=torch.Generator(device).manual_seed(0)
736
  ).images[0]
737
  return image
 
70
  # Set environment variables for CUDA
71
  os.environ['PYTORCH_USE_CUDA_DSA'] = '1'
72
  os.environ['CUDA_LAUNCH_BLOCKING'] = '1'
73
+ os.environ['PYTORCH_CUDA_ALLOC_CONF'] = 'expandable_segments:True'
74
 
75
 
76
  hf_token = os.getenv("HF_TOKEN")
 
712
  # image_3 = generate_image(hardcoded_prompt_3)
713
  # return image_1, image_2, image_3
714
 
715
+
716
+
717
+
718
+ # Clear any cached memory
719
+ torch.cuda.empty_cache()
720
+
721
+
722
  import gradio as gr
723
  import torch
724
  from diffusers import FluxPipeline
 
728
 
729
  # Function to initialize Flux bot model
730
  def initialize_flux_bot():
731
+ try:
732
+ torch.cuda.empty_cache() # Clear GPU memory cache
733
+ pipe = FluxPipeline.from_pretrained("black-forest-labs/FLUX.1-schnell", torch_dtype=torch.float16) # Use FP16
734
+ pipe.to(device) # Move the model to the correct device (GPU/CPU)
735
+ except torch.cuda.OutOfMemoryError:
736
+ print("CUDA out of memory, switching to CPU.")
737
+ pipe = FluxPipeline.from_pretrained("black-forest-labs/FLUX.1-schnell", torch_dtype=torch.float32) # Use FP32 for CPU
738
+ pipe.to("cpu")
739
  return pipe
740
 
741
  # Function to generate image using Flux bot on the specified device
 
744
  image = pipe(
745
  prompt,
746
  guidance_scale=0.0,
747
+ num_inference_steps=2,
748
+ max_sequence_length=128,
749
  generator=torch.Generator(device).manual_seed(0)
750
  ).images[0]
751
  return image