File size: 4,085 Bytes
4f5dc12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import torch
from diffusers import StableDiffusionInstructPix2PixPipeline
import gradio as gr
from PIL import Image
import random

# Load the InstructPix2Pix model
model_id = "timbrooks/instruct-pix2pix"
pipe = StableDiffusionInstructPix2PixPipeline.from_pretrained(model_id, torch_dtype=torch.float16)
pipe = pipe.to("cpu")

# Initialize a random seed
seed = random.randint(0, 10000)

# Function to reset the seed
def change_style():
    global seed
    seed = random.randint(0, 10000)
    return f"Seed changed to: {seed}"

# Furniture adding function
def add_furniture(image, style, color, room_type):
    # Construct the furniture prompt
    prompt = f"Add {style} style furniture with a {color} tone to this {room_type}."
    
    # Apply the edit using InstructPix2Pix
    edited_image = pipe(prompt=prompt, image=image, num_inference_steps=50, guidance_scale=7.5, generator=torch.manual_seed(seed)).images[0]
    return edited_image

# General image editing function
def edit_image(image, instruction):
    # Apply the edit using InstructPix2Pix
    edited_image = pipe(prompt=instruction, image=image, num_inference_steps=50, guidance_scale=7.5, generator=torch.manual_seed(seed)).images[0]
    return edited_image

# Gradio interface for furniture adding
def furniture_interface():
    with gr.Blocks() as demo_furniture:
        gr.Markdown("## Furniture Adding App")

        # Image upload
        image_input = gr.Image(type="pil", label="Upload an Image")

        # Dropdown for furniture style
        style_input = gr.Dropdown(["Modern", "Classic", "Minimalist", "Vintage"], label="Choose Furniture Style")

        # Dropdown for color
        color_input = gr.Dropdown(["Blue", "Green", "Red", "White", "Black"], label="Choose Furniture Color")

        # Dropdown for room type
        room_type_input = gr.Dropdown(["Living Room", "Bedroom", "Office", "Dining Room"], label="Room Type")

        # Display output image
        result_image = gr.Image(label="Edited Image")

        # Button to apply the furniture transformation
        submit_button = gr.Button("Add Furniture")

        # Button to change the seed (style)
        change_style_button = gr.Button("Change the Style")

        # Output for seed change message
        seed_output = gr.Textbox(label="Seed Info", interactive=False)

        # Define action on button click
        submit_button.click(fn=add_furniture, inputs=[image_input, style_input, color_input, room_type_input], outputs=result_image)
        change_style_button.click(fn=change_style, outputs=seed_output)

    return demo_furniture

# Gradio interface for general image editing
def general_editing_interface():
    with gr.Blocks() as demo_general:
        gr.Markdown("## General Image Editing App")

        # Image upload
        image_input = gr.Image(type="pil", label="Upload an Image")

        # Textbox for instruction
        instruction_input = gr.Textbox(label="Enter the Instruction", placeholder="Describe the changes (e.g., 'Add sunglasses to the person')")

        # Display output image
        result_image = gr.Image(label="Edited Image")

        # Button to apply the transformation
        submit_button = gr.Button("Apply Edit")

        # Button to change the seed (style)
        change_style_button = gr.Button("Change the Style")

        # Output for seed change message
        seed_output = gr.Textbox(label="Seed Info", interactive=False)

        # Define action on button click
        submit_button.click(fn=edit_image, inputs=[image_input, instruction_input], outputs=result_image)
        change_style_button.click(fn=change_style, outputs=seed_output)

    return demo_general

# Launch both Gradio apps
furniture_app = furniture_interface()
general_editing_app = general_editing_interface()

with gr.Blocks() as combined_demo:
    gr.Markdown("## Select the Application")

    with gr.Tab("Furniture Adding App"):
        furniture_app.render()

    with gr.Tab("General Image Editing App"):
        general_editing_app.render()

# Launch the combined Gradio app
combined_demo.launch()