Pijush2023 commited on
Commit
d790f10
·
verified ·
1 Parent(s): 2985c37

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -38
app.py CHANGED
@@ -713,51 +713,52 @@ def generate_map(location_names):
713
  # return image_1, image_2, image_3
714
 
715
 
716
- import gradio as gr
717
  import torch
718
- from diffusers import FluxPipeline
719
- import os
 
 
720
 
721
- # Set PYTORCH_CUDA_ALLOC_CONF to handle memory fragmentation
722
- os.environ['PYTORCH_CUDA_ALLOC_CONF'] = 'expandable_segments:True'
 
723
 
724
- # Check if CUDA (GPU) is available, otherwise fallback to CPU
725
- device = "cuda:0" if torch.cuda.is_available() else "cpu"
726
 
727
- # Function to initialize Flux bot model with GPU memory management
728
- def initialize_flux_bot():
729
- try:
730
- torch.cuda.empty_cache() # Clear GPU memory cache
731
- pipe = FluxPipeline.from_pretrained("black-forest-labs/FLUX.1-schnell", torch_dtype=torch.float16) # Use FP16
732
- pipe.to(device) # Move the model to the correct device (GPU/CPU)
733
- except torch.cuda.OutOfMemoryError:
734
- print("CUDA out of memory, switching to CPU.")
735
- pipe = FluxPipeline.from_pretrained("black-forest-labs/FLUX.1-schnell", torch_dtype=torch.float32) # Use FP32 for CPU
736
- pipe.to("cpu")
737
- return pipe
738
-
739
- # Function to generate image using Flux bot on the specified device
740
- def generate_image_flux(prompt):
741
- pipe = initialize_flux_bot()
742
  image = pipe(
743
- prompt,
744
- guidance_scale=0.0,
745
- num_inference_steps=2, # Reduced steps to save memory
746
- max_sequence_length=128, # Reduced sequence length to save memory
747
- generator=torch.Generator(device).manual_seed(0)
 
748
  ).images[0]
749
- return image
750
-
751
- # Hardcoded prompts for the images
752
- hardcoded_prompt_1 = "A high quality cinematic image for Toyota Truck in Birmingham skyline shot in the style of Michael Mann"
753
- hardcoded_prompt_2 = "A high quality cinematic image for Alabama Quarterback close up emotional shot in the style of Michael Mann"
754
- hardcoded_prompt_3 = "A high quality cinematic image for Taylor Swift concert in Birmingham skyline style of Michael Mann"
755
 
756
- # Function to update images
757
  def update_images():
758
- image_1 = generate_image_flux(hardcoded_prompt_1)
759
- image_2 = generate_image_flux(hardcoded_prompt_2)
760
- image_3 = generate_image_flux(hardcoded_prompt_3)
 
 
 
 
 
 
 
761
  return image_1, image_2, image_3
762
 
763
 
@@ -1505,7 +1506,7 @@ with gr.Blocks(theme='Pijush2023/scikit-learn-pijush') as demo:
1505
 
1506
  with gr.Column():
1507
 
1508
- # Display images
1509
  image_output_1 = gr.Image(value=generate_image_flux(hardcoded_prompt_1), width=400, height=400)
1510
  image_output_2 = gr.Image(value=generate_image_flux(hardcoded_prompt_2), width=400, height=400)
1511
  image_output_3 = gr.Image(value=generate_image_flux(hardcoded_prompt_3), width=400, height=400)
 
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():
752
+ # Hardcoded prompts for generating images
753
+ hardcoded_prompt_1 = "A high quality cinematic image for Toyota Truck in Birmingham skyline shot in the style of Michael Mann"
754
+ hardcoded_prompt_2 = "A high quality cinematic image for Alabama Quarterback close up emotional shot in the style of Michael Mann"
755
+ hardcoded_prompt_3 = "A high quality cinematic image for Taylor Swift concert in Birmingham skyline style of Michael Mann"
756
+
757
+ # Use the Flux pipeline to generate images based on hardcoded prompts
758
+ image_1, _ = infer(hardcoded_prompt_1)
759
+ image_2, _ = infer(hardcoded_prompt_2)
760
+ image_3, _ = infer(hardcoded_prompt_3)
761
+
762
  return image_1, image_2, image_3
763
 
764
 
 
1506
 
1507
  with gr.Column():
1508
 
1509
+ # Display generated images
1510
  image_output_1 = gr.Image(value=generate_image_flux(hardcoded_prompt_1), width=400, height=400)
1511
  image_output_2 = gr.Image(value=generate_image_flux(hardcoded_prompt_2), width=400, height=400)
1512
  image_output_3 = gr.Image(value=generate_image_flux(hardcoded_prompt_3), width=400, height=400)