AlphaQuark commited on
Commit
b453c51
·
verified ·
1 Parent(s): 5a236e1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -11
app.py CHANGED
@@ -1,20 +1,38 @@
1
  import torch
2
  from diffusers import AutoPipelineForImage2Image
3
- from diffusers.utils import make_image_grid, load_image
 
4
 
 
5
  pipeline = AutoPipelineForImage2Image.from_pretrained(
6
- "runwayml/stable-diffusion-v1-5", torch_dtype=torch.float16, variant="fp16", use_safetensors=True
 
 
 
7
  )
 
 
8
  pipeline.enable_model_cpu_offload()
9
- # remove following line if xFormers is not installed or you have PyTorch 2.0 or higher installed
10
- # pipeline.enable_xformers_memory_efficient_attention()
11
 
12
- # prepare image
13
- url = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/img2img-init.png"
14
- init_image = load_image(url)
 
 
 
 
 
15
 
16
- prompt = "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k"
 
 
 
 
 
 
 
 
 
17
 
18
- # pass prompt and image to pipeline
19
- image = pipeline(prompt, image=init_image).images[0]
20
- make_image_grid([init_image, image], rows=1, cols=2)
 
1
  import torch
2
  from diffusers import AutoPipelineForImage2Image
3
+ from PIL import Image
4
+ import gradio as gr
5
 
6
+ # Initialize the image-to-image pipeline
7
  pipeline = AutoPipelineForImage2Image.from_pretrained(
8
+ "runwayml/stable-diffusion-v1-5",
9
+ torch_dtype=torch.float16,
10
+ variant="fp16",
11
+ use_safetensors=True
12
  )
13
+
14
+ # Enable CPU offloading to manage memory usage
15
  pipeline.enable_model_cpu_offload()
 
 
16
 
17
+ # Function to process the image and prompt, and generate a new image
18
+ def process_image(prompt, init_image):
19
+ # Convert the uploaded file to an image
20
+ init_image = Image.open(init_image).convert("RGB")
21
+
22
+ # Generate an image based on the prompt and initial image
23
+ with torch.no_grad(): # This ensures that no gradients are calculated, saving memory
24
+ generated_image = pipeline(prompt, init_image=init_image, num_inference_steps=50).images[0]
25
 
26
+ return generated_image
27
+
28
+ # Create the Gradio interface
29
+ interface = gr.Interface(
30
+ fn=process_image,
31
+ inputs=[gr.inputs.Textbox(label="Prompt"), gr.inputs.Image(label="Initial Image", type="pil")],
32
+ outputs=gr.outputs.Image(type="pil", label="Generated Image"),
33
+ title="Image-to-Image Transformation",
34
+ description="Upload an image and enter a prompt to transform the image accordingly."
35
+ )
36
 
37
+ # Launch the interface
38
+ interface.launch()