Spaces:
Running
on
T4
Running
on
T4
Update app.py
Browse files
app.py
CHANGED
@@ -1,20 +1,38 @@
|
|
1 |
import torch
|
2 |
from diffusers import AutoPipelineForImage2Image
|
3 |
-
from
|
|
|
4 |
|
|
|
5 |
pipeline = AutoPipelineForImage2Image.from_pretrained(
|
6 |
-
"runwayml/stable-diffusion-v1-5",
|
|
|
|
|
|
|
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 |
-
#
|
13 |
-
|
14 |
-
|
|
|
|
|
|
|
|
|
|
|
15 |
|
16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
|
18 |
-
#
|
19 |
-
|
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()
|
|