Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,114 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
from diffusers import StableDiffusionInstructPix2PixPipeline
|
3 |
+
import gradio as gr
|
4 |
+
from PIL import Image
|
5 |
+
import random
|
6 |
+
|
7 |
+
# Load the InstructPix2Pix model
|
8 |
+
model_id = "timbrooks/instruct-pix2pix"
|
9 |
+
pipe = StableDiffusionInstructPix2PixPipeline.from_pretrained(model_id, torch_dtype=torch.float16)
|
10 |
+
pipe = pipe.to("cpu")
|
11 |
+
|
12 |
+
# Initialize a random seed
|
13 |
+
seed = random.randint(0, 10000)
|
14 |
+
|
15 |
+
# Function to reset the seed
|
16 |
+
def change_style():
|
17 |
+
global seed
|
18 |
+
seed = random.randint(0, 10000)
|
19 |
+
return f"Seed changed to: {seed}"
|
20 |
+
|
21 |
+
# Furniture adding function
|
22 |
+
def add_furniture(image, style, color, room_type):
|
23 |
+
# Construct the furniture prompt
|
24 |
+
prompt = f"Add {style} style furniture with a {color} tone to this {room_type}."
|
25 |
+
|
26 |
+
# Apply the edit using InstructPix2Pix
|
27 |
+
edited_image = pipe(prompt=prompt, image=image, num_inference_steps=50, guidance_scale=7.5, generator=torch.manual_seed(seed)).images[0]
|
28 |
+
return edited_image
|
29 |
+
|
30 |
+
# General image editing function
|
31 |
+
def edit_image(image, instruction):
|
32 |
+
# Apply the edit using InstructPix2Pix
|
33 |
+
edited_image = pipe(prompt=instruction, image=image, num_inference_steps=50, guidance_scale=7.5, generator=torch.manual_seed(seed)).images[0]
|
34 |
+
return edited_image
|
35 |
+
|
36 |
+
# Gradio interface for furniture adding
|
37 |
+
def furniture_interface():
|
38 |
+
with gr.Blocks() as demo_furniture:
|
39 |
+
gr.Markdown("## Furniture Adding App")
|
40 |
+
|
41 |
+
# Image upload
|
42 |
+
image_input = gr.Image(type="pil", label="Upload an Image")
|
43 |
+
|
44 |
+
# Dropdown for furniture style
|
45 |
+
style_input = gr.Dropdown(["Modern", "Classic", "Minimalist", "Vintage"], label="Choose Furniture Style")
|
46 |
+
|
47 |
+
# Dropdown for color
|
48 |
+
color_input = gr.Dropdown(["Blue", "Green", "Red", "White", "Black"], label="Choose Furniture Color")
|
49 |
+
|
50 |
+
# Dropdown for room type
|
51 |
+
room_type_input = gr.Dropdown(["Living Room", "Bedroom", "Office", "Dining Room"], label="Room Type")
|
52 |
+
|
53 |
+
# Display output image
|
54 |
+
result_image = gr.Image(label="Edited Image")
|
55 |
+
|
56 |
+
# Button to apply the furniture transformation
|
57 |
+
submit_button = gr.Button("Add Furniture")
|
58 |
+
|
59 |
+
# Button to change the seed (style)
|
60 |
+
change_style_button = gr.Button("Change the Style")
|
61 |
+
|
62 |
+
# Output for seed change message
|
63 |
+
seed_output = gr.Textbox(label="Seed Info", interactive=False)
|
64 |
+
|
65 |
+
# Define action on button click
|
66 |
+
submit_button.click(fn=add_furniture, inputs=[image_input, style_input, color_input, room_type_input], outputs=result_image)
|
67 |
+
change_style_button.click(fn=change_style, outputs=seed_output)
|
68 |
+
|
69 |
+
return demo_furniture
|
70 |
+
|
71 |
+
# Gradio interface for general image editing
|
72 |
+
def general_editing_interface():
|
73 |
+
with gr.Blocks() as demo_general:
|
74 |
+
gr.Markdown("## General Image Editing App")
|
75 |
+
|
76 |
+
# Image upload
|
77 |
+
image_input = gr.Image(type="pil", label="Upload an Image")
|
78 |
+
|
79 |
+
# Textbox for instruction
|
80 |
+
instruction_input = gr.Textbox(label="Enter the Instruction", placeholder="Describe the changes (e.g., 'Add sunglasses to the person')")
|
81 |
+
|
82 |
+
# Display output image
|
83 |
+
result_image = gr.Image(label="Edited Image")
|
84 |
+
|
85 |
+
# Button to apply the transformation
|
86 |
+
submit_button = gr.Button("Apply Edit")
|
87 |
+
|
88 |
+
# Button to change the seed (style)
|
89 |
+
change_style_button = gr.Button("Change the Style")
|
90 |
+
|
91 |
+
# Output for seed change message
|
92 |
+
seed_output = gr.Textbox(label="Seed Info", interactive=False)
|
93 |
+
|
94 |
+
# Define action on button click
|
95 |
+
submit_button.click(fn=edit_image, inputs=[image_input, instruction_input], outputs=result_image)
|
96 |
+
change_style_button.click(fn=change_style, outputs=seed_output)
|
97 |
+
|
98 |
+
return demo_general
|
99 |
+
|
100 |
+
# Launch both Gradio apps
|
101 |
+
furniture_app = furniture_interface()
|
102 |
+
general_editing_app = general_editing_interface()
|
103 |
+
|
104 |
+
with gr.Blocks() as combined_demo:
|
105 |
+
gr.Markdown("## Select the Application")
|
106 |
+
|
107 |
+
with gr.Tab("Furniture Adding App"):
|
108 |
+
furniture_app.render()
|
109 |
+
|
110 |
+
with gr.Tab("General Image Editing App"):
|
111 |
+
general_editing_app.render()
|
112 |
+
|
113 |
+
# Launch the combined Gradio app
|
114 |
+
combined_demo.launch()
|