File size: 4,288 Bytes
2066d76
 
 
4deeba7
2066d76
4d5a47b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2066d76
 
 
4d5a47b
 
 
 
2066d76
 
 
 
 
 
 
 
 
4d5a47b
 
 
 
 
 
2066d76
 
 
4d5a47b
 
 
 
 
 
 
2066d76
 
 
 
 
 
 
 
 
4d5a47b
 
2066d76
 
 
 
4d5a47b
 
 
 
 
2066d76
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4c2c03f
4d5a47b
 
 
 
2066d76
 
 
 
4d5a47b
2066d76
 
 
 
 
 
4d5a47b
 
2066d76
 
 
 
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
import gradio as gr
import requests

prefix = "https://smartfeed-custom-tools.hf.space/gradio_api/file="

extract_models = [
    "o3",
    "gpt-5",
    "o4-mini",
    "o4-mini-high",
]


def change_image_style(
    image_url,
    style_image_url,
    extract_model,
    extract_prompt,
    prompt_prefix
):
    image_url = "https://replicate.delivery/pbxt/Mjo5VWBA0fm8oq3BefpbdtXeJitBJc6AobPnnMdFxxCKzT3P/linkedin.jpeg"
    style_image_url = "https://replicate.delivery/pbxt/N55l5TWGh8mSlNzW8usReoaNhGbFwvLeZR3TX1NL4pd2Wtfv/replicate-prediction-f2d25rg6gnrma0cq257vdw2n4c.png"

    data = {
        "image_url": image_url,
        "style_image_url": style_image_url,
        "extract_model": extract_model,
        "extract_prompt": extract_prompt,
        "prompt_prefix": prompt_prefix
    }

    response = requests.post(
        "https://api.hkhappymobile.com/tools/change-image-style",
        json=data,
        headers={"content-type": "application/json"}
    )

    if response.status_code == 200:
        data = response.json().get("data")
        resultImageUrl = data.get("image_url")
        edit_prompt = data.get("edit_prompt")
        # replace \n to <br>
        edit_prompt = edit_prompt.replace("\n", "<br>")
        return resultImageUrl, edit_prompt
    else:
        raise Exception(f"Error: {response.status_code} - {response.text}")

def generate_image(
    input_image,
    style_images,
    extract_model,
    extract_prompt,
    prompt_prefix
):

    if not input_image:
        raise gr.Error(f"Please upload an input image! Refer to step 1️⃣")

    if style_images is None:
        raise gr.Error(f"Cannot find any style image! Please refer to step 1️⃣")

    inputImageUrl = prefix + input_image

    result_images = []
    markdownStr = ""
    for style_image in style_images:
        if not style_image:
            raise gr.Error(f"Cannot find any style image! Please refer to step 1️⃣")
        styleImageUrl = prefix + style_image[0]
        result_image, edit_prompt = change_image_style(inputImageUrl, styleImageUrl, extract_model, extract_prompt, prompt_prefix)
        result_images.append(result_image)
        markdownStr += f"{edit_prompt}: <img src='{result_image}' style='zoom: 33%;' />\n"

    return result_images, markdownStr


def swap_to_gallery(images):
    return gr.update(value=images, visible=True), gr.update(visible=True), gr.update(visible=False)

def remove_back_to_files():
    return gr.update(visible=False), gr.update(visible=False), gr.update(visible=True)

with gr.Blocks() as demo:
    with gr.Row():
        with gr.Column():

            files = gr.File(
                        label="Drag (Select) 1 or more style images",
                        file_types=["image"],
                        file_count="multiple"
                    )
            uploaded_files = gr.Gallery(label="Your images", visible=False, columns=5, rows=1, height=200)
            with gr.Column(visible=False) as clear_button:
                remove_and_reupload = gr.ClearButton(value="Remove and upload new ones", components=files, size="sm")
            input_image = gr.Image(label="Input Image", type="filepath", interactive=True)
            extract_model = gr.Dropdown(choices=extract_models, label="Extract Model", value="o3")
            extract_prompt = gr.Textbox(lines=2, label="Extracted Prompt", value="Accurately extract everything from this photo into a prompt especially the hairstyle excluding the backgrounds. I want to replicate it perfectly. need to be a complete paragraph")
            prompt_prefix = gr.Textbox(lines=1, label="Prompt Prefix", value="change the backgrounds to match this:")

            submit = gr.Button("Submit")

        with gr.Column():
            gallery = gr.Gallery(label="Generated Images")
            markdown = gr.Markdown(label="Generated Images")

        files.upload(fn=swap_to_gallery, inputs=files, outputs=[uploaded_files, clear_button, files])
        remove_and_reupload.click(fn=remove_back_to_files, outputs=[uploaded_files, clear_button, files])

        submit.click(
            fn=generate_image,
            inputs=[input_image, uploaded_files, extract_model, extract_prompt, prompt_prefix],
            outputs=[gallery, markdown]
        )

    
demo.launch()