gaur3009 commited on
Commit
66a6105
·
verified ·
1 Parent(s): 1e68838

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -58
app.py CHANGED
@@ -1,60 +1,43 @@
1
  import gradio as gr
2
- from gradio_imageslider import ImageSlider
3
- from loadimg import load_img
4
- import spaces
5
- from transformers import AutoModelForImageSegmentation
6
  import torch
7
- from torchvision import transforms
8
-
9
- torch.set_float32_matmul_precision(["high", "highest"][0])
10
-
11
- birefnet = AutoModelForImageSegmentation.from_pretrained(
12
- "ZhengPeng7/BiRefNet", trust_remote_code=True
13
- )
14
- birefnet.to("cuda")
15
- transform_image = transforms.Compose(
16
- [
17
- transforms.Resize((1024, 1024)),
18
- transforms.ToTensor(),
19
- transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),
20
- ]
21
- )
22
-
23
-
24
- @spaces.GPU
25
- def fn(image):
26
- im = load_img(image, output_type="pil")
27
- im = im.convert("RGB")
28
- image_size = im.size
29
- origin = im.copy()
30
- image = load_img(im)
31
- input_images = transform_image(image).unsqueeze(0).to("cuda")
32
- # Prediction
33
- with torch.no_grad():
34
- preds = birefnet(input_images)[-1].sigmoid().cpu()
35
- pred = preds[0].squeeze()
36
- pred_pil = transforms.ToPILImage()(pred)
37
- mask = pred_pil.resize(image_size)
38
- image.putalpha(mask)
39
- return (image, origin)
40
-
41
-
42
- slider1 = ImageSlider(label="birefnet", type="pil")
43
- slider2 = ImageSlider(label="birefnet", type="pil")
44
- image = gr.Image(label="Upload an image")
45
- text = gr.Textbox(label="Paste an image URL")
46
-
47
-
48
- chameleon = load_img("chameleon.jpg", output_type="pil")
49
-
50
- url = "https://hips.hearstapps.com/hmg-prod/images/gettyimages-1229892983-square.jpg"
51
- tab1 = gr.Interface(
52
- fn, inputs=image, outputs=slider1, examples=[chameleon], api_name="image"
53
- )
54
-
55
- tab2 = gr.Interface(fn, inputs=text, outputs=slider2, examples=[url], api_name="text")
56
-
57
-
58
- demo = gr.TabbedInterface(
59
- [tab1, tab2], ["image", "text"], title="birefnet for background removal"
60
- )
 
1
  import gradio as gr
 
 
 
 
2
  import torch
3
+ from diffusers import StableDiffusionInpaintPipeline
4
+
5
+ # Load the Stable Diffusion inpainting model
6
+ def load_inpainting_model():
7
+ device = "cuda" if torch.cuda.is_available() else "cpu"
8
+ model = StableDiffusionInpaintPipeline.from_pretrained(
9
+ "runwayml/stable-diffusion-inpainting",
10
+ torch_dtype=torch.float16 if device == "cuda" else torch.float32
11
+ )
12
+ model.to(device)
13
+ return model
14
+
15
+ # Function to edit the clothing image based on prompt
16
+ def edit_clothing_image(prompt, image):
17
+ # Load the model
18
+ model = load_inpainting_model()
19
+
20
+ # Inpainting to edit the image based on the text prompt
21
+ edited_image = model(prompt=prompt, image=image).images[0]
22
+ return edited_image
23
+
24
+ # Gradio interface
25
+ def interface():
26
+ with gr.Blocks() as ui:
27
+ gr.Markdown("# Clothing Image Editing with AI")
28
+
29
+ with gr.Row():
30
+ with gr.Column():
31
+ prompt = gr.Textbox(label="Editing Prompt", placeholder="e.g., change the color of the t-shirt to red")
32
+ input_image = gr.Image(label="Upload Image", tool="editor", type="pil")
33
+ output_image = gr.Image(label="Edited Image")
34
+
35
+ edit_button = gr.Button("Apply Edit")
36
+
37
+ edit_button.click(edit_clothing_image, inputs=[prompt, input_image], outputs=output_image)
38
+
39
+ return ui
40
+
41
+ # Launch the interface
42
+ ui = interface()
43
+ ui.launch()