nenene
commited on
Commit
·
4969631
1
Parent(s):
6d57fe1
run model
Browse files
app.py
CHANGED
@@ -1,7 +1,67 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
-
|
4 |
-
|
|
|
|
|
5 |
|
6 |
-
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
from diffusers import DiffusionPipeline
|
3 |
+
import torch
|
4 |
+
from PIL import Image, ImageOps
|
5 |
+
import requests
|
6 |
+
from io import BytesIO
|
7 |
+
from transparent_background import Remover
|
8 |
|
9 |
+
# Initialize the Diffusion Pipeline
|
10 |
+
model_id = "yahoo-inc/photo-background-generation"
|
11 |
+
pipeline = DiffusionPipeline.from_pretrained(model_id, custom_pipeline=model_id)
|
12 |
+
pipeline = pipeline.to('cuda')
|
13 |
|
14 |
+
def resize_with_padding(img, expected_size):
|
15 |
+
img.thumbnail((expected_size[0], expected_size[1]))
|
16 |
+
delta_width = expected_size[0] - img.size[0]
|
17 |
+
delta_height = expected_size[1] - img.size[1]
|
18 |
+
pad_width = delta_width // 2
|
19 |
+
pad_height = delta_height // 2
|
20 |
+
padding = (pad_width, pad_height, delta_width - pad_width, delta_height - pad_height)
|
21 |
+
return ImageOps.expand(img, padding)
|
22 |
+
|
23 |
+
def process_image(input_image, prompt):
|
24 |
+
# Resize and process the input image
|
25 |
+
img = resize_with_padding(input_image, (512, 512))
|
26 |
+
|
27 |
+
# Load background detection model
|
28 |
+
remover = Remover(mode='base')
|
29 |
+
|
30 |
+
# Get foreground mask
|
31 |
+
fg_mask = remover.process(img, type='map')
|
32 |
+
|
33 |
+
seed = 13
|
34 |
+
mask = ImageOps.invert(fg_mask)
|
35 |
+
img = resize_with_padding(img, (512, 512))
|
36 |
+
generator = torch.Generator(device='cuda').manual_seed(seed)
|
37 |
+
cond_scale = 1.0
|
38 |
+
|
39 |
+
with torch.autocast("cuda"):
|
40 |
+
controlnet_image = pipeline(
|
41 |
+
prompt=prompt,
|
42 |
+
image=img,
|
43 |
+
mask_image=mask,
|
44 |
+
control_image=mask,
|
45 |
+
num_images_per_prompt=1,
|
46 |
+
generator=generator,
|
47 |
+
num_inference_steps=20,
|
48 |
+
guess_mode=False,
|
49 |
+
controlnet_conditioning_scale=cond_scale
|
50 |
+
).images[0]
|
51 |
+
|
52 |
+
return controlnet_image
|
53 |
+
|
54 |
+
# Create Gradio interface
|
55 |
+
iface = gr.Interface(
|
56 |
+
fn=process_image,
|
57 |
+
inputs=[
|
58 |
+
gr.inputs.Image(type="pil", label="Upload Image"),
|
59 |
+
gr.inputs.Textbox(label="Enter Prompt")
|
60 |
+
],
|
61 |
+
outputs=gr.outputs.Image(label="Generated Image"),
|
62 |
+
title="Image Processing with Diffusion Pipeline",
|
63 |
+
description="Upload an image and enter a prompt to generate a new image using the diffusion model."
|
64 |
+
)
|
65 |
+
|
66 |
+
# Launch the interface
|
67 |
+
iface.launch()
|