File size: 5,452 Bytes
62a39be
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import os
import gradio as gr
import requests
from PIL import Image
from io import BytesIO
from base64 import b64encode

SEGMIND_MODEL_URL = "https://api.segmind.com/v1/inpaint-auto"

def urlToB64(imgUrl):
    return str(b64encode(requests.get(imgUrl).content))[2:-1]

def imageToB64(img):
    buffered = BytesIO()
    img.save(buffered, format="JPEG")
    return str(b64encode(buffered.getvalue()))[2:-1]

def generate_image(
    upload_method,
    img_url,
    uploaded_img,
    prompt,
    negative_prompt,
    cn_model,
    cn_processor,
    base_model
):
    if upload_method == "URL":
        if not img_url:
            raise ValueError("Image URL is required.")
        img_b64 = urlToB64(img_url)
    else:
        if not uploaded_img:
            raise ValueError("Image upload is required.")
        img_b64 = imageToB64(uploaded_img)

    data = {
        "image": img_b64,
        "prompt": prompt,
        "negative_prompt": negative_prompt,
        "samples": 1,
        "base_model": base_model,
        "cn_model": cn_model,
        "cn_processor": cn_processor,
        "scheduler": "DPM++ 2M SDE Karras",
        "num_inference_steps": 25,
        "guidance_scale": 7.5,
        "seed": -1,
        "strength": 0.9,
        "base64": False,
    }
    response = requests.post(
      SEGMIND_MODEL_URL,
      json=data,
      headers={"x-api-key": os.environ['SEGMIND_API_KEY']}
    )
    output_img = Image.open(BytesIO(response.content))

    return output_img


def invertBox(upload_method):
    # Return gr.update objects with visibility settings
    if upload_method == "URL":
        return gr.update(visible=True), gr.update(visible=False)
    else:
        return gr.update(visible=False), gr.update(visible=True)

with gr.Blocks() as demo:
    gr.Markdown("### Photo Background Changer")
    gr.Markdown(
        "Change the bavkground of the image in one click to anything that you can imagine"
    )
    with gr.Row():
        upload_method = gr.Radio(
            choices=["URL", "Upload"], label="Choose Image Upload Method", value="URL"
        )
        img_url = gr.Textbox(label="Image URL")
        uploaded_img = gr.Image(type="pil", label="Upload Image", visible=False)
        upload_method.change(
            invertBox, inputs=upload_method, outputs=[img_url, uploaded_img]
        )
    with gr.Row():
        prompt = gr.Textbox(label="Prompt")
        negative_prompt = gr.Textbox(
            label="Negative Prompt",
            value="disfigured, deformed, ugly, floating in air, blur, haze, uneven edges, improper blending, animated, cartoon",
        )
    with gr.Row():
        cn_model = gr.Dropdown(
            label="Select Controlnet Model",
            choices=["Canny", "Depth", "SoftEdge", "OpenPose"],
            value="Depth",
        )
        cn_processor = gr.Dropdown(
            label="Select Controlnet Processor",
            choices=[
                "canny",
                "depth",
                "depth_leres",
                "depth_leres++",
                "hed",
                "hed_safe",
                "mediapipe_face",
                "mlsd",
                "normal_map",
                "openpose",
                "openpose_hand",
                "openpose_face",
                "openpose_faceonly",
                "openpose_full",
                "dw_openpose_full",
                "animal_openpose",
                "clip_vision",
                "revision_clipvision",
                "revision_ignore_prompt",
                "ip-adapter_clip_sd15",
                "ip-adapter_clip_sdxl_plus_vith",
                "ip-adapter_clip_sdxl",
                "color",
                "pidinet",
                "pidinet_safe",
                "pidinet_sketch",
                "pidinet_scribble",
                "scribble_xdog",
                "scribble_hed",
                "segmentation",
                "threshold",
                "depth_zoe",
                "normal_bae",
                "oneformer_coco",
                "oneformer_ade20k",
                "lineart",
                "lineart_coarse",
                "lineart_anime",
                "lineart_standard",
                "shuffle",
                "tile_resample",
                "invert",
                "lineart_anime_denoise",
                "reference_only",
                "reference_adain",
                "reference_adain+attn",
                "inpaint",
                "inpaint_only",
                "inpaint_only+lama",
                "tile_colorfix",
                "tile_colorfix+sharp",
                "recolor_luminance",
                "recolor_intensity",
                "blur_gaussian",
                "anime_face_segment",
            ],
            value="canny",
        )
    with gr.Row():
        base_model = gr.Dropdown(
            label="Select Base SD Model to use",
            choices=["Real Vision XL", "SDXL", "Juggernaut XL", "DreamShaper XL"],
            value="Juggernaut XL",
        )
    with gr.Row():
        generate_btn = gr.Button("Generate Image")
    output_image = gr.Image(type="pil")

    generate_btn.click(
        fn=generate_image,
        inputs=[
            upload_method,
            img_url,
            uploaded_img,
            prompt,
            negative_prompt,
            cn_model,
            cn_processor,
            base_model
        ],
        outputs=[output_image],
    )

demo.launch(debug=True)