File size: 9,106 Bytes
80316eb
 
 
 
 
 
 
 
 
9fe69b6
80316eb
 
 
 
9fe69b6
 
 
 
 
 
 
 
 
2e87213
9fe69b6
2e87213
9fe69b6
2e87213
 
 
 
9fe69b6
2e87213
 
 
 
9fe69b6
2e87213
 
 
 
9fe69b6
80316eb
9fe69b6
 
a0c634e
9fe69b6
a0c634e
 
 
9fe69b6
 
a0c634e
 
9fe69b6
 
a0c634e
9fe69b6
a0c634e
9fe69b6
a0c634e
 
9fe69b6
80316eb
cbcfce1
71c40f6
cbcfce1
 
 
70373d7
6cf20c5
 
70373d7
6cf20c5
 
 
 
 
 
 
 
 
 
 
70373d7
cbcfce1
 
f6b58d2
cbcfce1
71c40f6
cbcfce1
70373d7
71c40f6
70373d7
cbcfce1
 
80316eb
9fe69b6
80316eb
 
6cf20c5
 
80316eb
6cf20c5
 
 
 
 
 
 
 
 
 
 
80316eb
 
 
f6b58d2
80316eb
cbcfce1
80316eb
 
cbcfce1
80316eb
9fe69b6
 
80316eb
 
 
 
 
 
 
cbcfce1
 
 
 
 
 
 
 
 
 
 
 
9fe69b6
2e87213
 
 
 
 
9fe69b6
2e87213
9fe69b6
 
2e87213
cbcfce1
42a8c3c
 
cbcfce1
 
 
 
42a8c3c
cbcfce1
42a8c3c
 
cbcfce1
9fe69b6
a0c634e
 
9fe69b6
 
 
 
a0c634e
9fe69b6
 
 
cbcfce1
42a8c3c
 
80316eb
 
 
 
9fe69b6
80316eb
9fe69b6
80316eb
9fe69b6
80316eb
 
 
9fe69b6
80316eb
9fe69b6
80316eb
9fe69b6
80316eb
 
9fe69b6
2e87213
9fe69b6
 
2e87213
9fe69b6
80316eb
9fe69b6
 
80316eb
9fe69b6
80316eb
 
9fe69b6
 
80316eb
 
9fe69b6
80316eb
9fe69b6
80316eb
 
 
 
9fe69b6
 
80316eb
 
9fe69b6
80316eb
9fe69b6
 
80316eb
 
 
 
 
 
 
 
 
 
 
 
 
 
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
import gradio as gr
import replicate
import os
import requests
from PIL import Image, ImageDraw, ImageFilter, ImageEnhance
import io
import tempfile
import numpy as np

# Replicate API ํ† ํฐ ์„ค์ •
REPLICATE_API_TOKEN = os.getenv("REPLICATE_API_TOKEN")
if REPLICATE_API_TOKEN:
    os.environ["REPLICATE_API_TOKEN"] = REPLICATE_API_TOKEN

def upload_image_to_temp_url(image):
    """์ด๋ฏธ์ง€๋ฅผ ์ž„์‹œ ํŒŒ์ผ๋กœ ์ €์žฅ"""
    if image is None:
        return None
    
    with tempfile.NamedTemporaryFile(delete=False, suffix='.png') as tmp_file:
        image.save(tmp_file.name, format='PNG')
        return tmp_file.name

def remove_background_with_ai(image_path):
    """AI ๋ฐฐ๊ฒฝ ์ œ๊ฑฐ"""
    if not REPLICATE_API_TOKEN:
        return None, "Replicate API ํ† ํฐ์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค."
    
    try:
        output = replicate.run(
            "851-labs/background-remover:a029dff38972b5fda4ec5d75d7d1cd25aeff621d2cf4946a41055d7db66b80bc",
            input={"image": open(image_path, "rb")}
        )
        
        response = requests.get(output)
        if response.status_code == 200:
            return Image.open(io.BytesIO(response.content)), "๋ฐฐ๊ฒฝ ์ œ๊ฑฐ ์™„๋ฃŒ"
        else:
            return None, "๋ฐฐ๊ฒฝ ์ œ๊ฑฐ ์‹คํŒจ"
            
    except Exception as e:
        return None, f"๋ฐฐ๊ฒฝ ์ œ๊ฑฐ ์˜ค๋ฅ˜: {str(e)}"

def upscale_with_clarity(image_path):
    """ํ™”์งˆ ๊ฐœ์„  ์—…์Šค์ผ€์ผ๋Ÿฌ"""
    if not REPLICATE_API_TOKEN:
        return None, "Replicate API ํ† ํฐ์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค."
    
    try:
        output = replicate.run(
            "philz1337x/clarity-upscaler:dfad41707589d68ecdccd1dfa600d55a208f9310748e44bfe35b4a6291453d5e",
            input={"image": open(image_path, "rb")}
        )
        
        # output์€ ๋ฆฌ์ŠคํŠธ์ด๋ฏ€๋กœ ์ฒซ ๋ฒˆ์งธ ํ•ญ๋ชฉ ์‚ฌ์šฉ
        response = requests.get(output[0])
        if response.status_code == 200:
            return Image.open(io.BytesIO(response.content)), "ํ™”์งˆ ๊ฐœ์„  ์™„๋ฃŒ"
        else:
            return None, "ํ™”์งˆ ๊ฐœ์„  ์‹คํŒจ"
            
    except Exception as e:
        return None, f"ํ™”์งˆ ๊ฐœ์„  ์˜ค๋ฅ˜: {str(e)}"

def change_product_angle(image_path):
    """์นด๋ฉ”๋ผ ์œ„์น˜๋ฅผ ๋ฐ”๊ฟ”์„œ ๋‹ค๋ฅธ ์•ต๊ธ€๋กœ ์ดฌ์˜"""
    if not REPLICATE_API_TOKEN:
        return None, "Replicate API ํ† ํฐ์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค."
    
    try:
        # Gen4 Image๋กœ ์นด๋ฉ”๋ผ ์œ„์น˜ ๋ณ€๊ฒฝ (ํŒŒ์ผ ์ง์ ‘ ์—…๋กœ๋“œ)
        angle_prompt = "Professional product photography of @product from 45-degree side angle, three-quarter view, diagonal perspective, different camera position, clean background"
        
        with open(image_path, "rb") as image_file:
            output = replicate.run(
                "runwayml/gen4-image",
                input={
                    "prompt": angle_prompt,
                    "aspect_ratio": "1:1",
                    "resolution": "1080p",
                    "reference_images": [image_file],
                    "reference_tags": ["product"]
                }
            )
        
        response = requests.get(output)
        if response.status_code == 200:
            return Image.open(io.BytesIO(response.content)), "์นด๋ฉ”๋ผ ์•ต๊ธ€ ๋ณ€๊ฒฝ ์™„๋ฃŒ (Gen4)"
        else:
            return None, "์นด๋ฉ”๋ผ ์•ต๊ธ€ ๋ณ€๊ฒฝ ์‹คํŒจ"
            
    except Exception as e:
        return None, f"์นด๋ฉ”๋ผ ์•ต๊ธ€ ๋ณ€๊ฒฝ ์˜ค๋ฅ˜: {str(e)}"

def add_shadow_only(image_path):
    """๋ฐฐ๊ฒฝ ์ œ๊ฑฐ๋œ ์ƒํ’ˆ์— ๊ทธ๋ฆผ์ž๋งŒ ์ถ”๊ฐ€ (์™„์ „ํ•œ ํฐ์ƒ‰ ๋ฐฐ๊ฒฝ)"""
    if not REPLICATE_API_TOKEN:
        return None, "Replicate API ํ† ํฐ์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค."
    
    try:
        # Gen4 Image๋กœ ๊ทธ๋ฆผ์ž ์ถ”๊ฐ€ (ํŒŒ์ผ ์ง์ ‘ ์—…๋กœ๋“œ)
        shadow_prompt = "Professional product photography of @product on pure white background with natural soft drop shadow, clean white backdrop, subtle shadow effect, e-commerce style"
        
        with open(image_path, "rb") as image_file:
            output = replicate.run(
                "runwayml/gen4-image",
                input={
                    "prompt": shadow_prompt,
                    "aspect_ratio": "1:1", 
                    "resolution": "1080p",
                    "reference_images": [image_file],
                    "reference_tags": ["product"]
                }
            )
        
        response = requests.get(output)
        if response.status_code == 200:
            return Image.open(io.BytesIO(response.content)), "๊ทธ๋ฆผ์ž ์ถ”๊ฐ€ ์™„๋ฃŒ (Gen4)"
        else:
            return None, "๊ทธ๋ฆผ์ž ์ถ”๊ฐ€ ์‹คํŒจ"
            
    except Exception as e:
        return None, f"๊ทธ๋ฆผ์ž ์ถ”๊ฐ€ ์˜ค๋ฅ˜: {str(e)}"

def process_image(image, remove_bg, upscale_quality):
    """๋ฉ”์ธ ์ฒ˜๋ฆฌ ํ•จ์ˆ˜"""
    if image is None:
        return None, "์ด๋ฏธ์ง€๋ฅผ ์—…๋กœ๋“œํ•ด์ฃผ์„ธ์š”."
    
    try:
        result_image = image.copy()
        status_messages = []
        
        # 1๋‹จ๊ณ„: ์ƒํ’ˆ ๊ฐ๋„ ๋ณ€๊ฒฝ
        temp_path = upload_image_to_temp_url(result_image)
        if temp_path:
            angled_img, angle_msg = change_product_angle(temp_path)
            if angled_img:
                result_image = angled_img
                status_messages.append(f"โœ… {angle_msg}")
            else:
                status_messages.append(f"โŒ {angle_msg}")
            os.unlink(temp_path)
        
        # 2๋‹จ๊ณ„: ๋ฐฐ๊ฒฝ ์ œ๊ฑฐ (๋ˆ„๋ผ)
        if remove_bg:
            temp_path = upload_image_to_temp_url(result_image)
            if temp_path:
                bg_removed_img, bg_msg = remove_background_with_ai(temp_path)
                if bg_removed_img:
                    result_image = bg_removed_img
                    status_messages.append(f"โœ… {bg_msg}")
                else:
                    status_messages.append(f"โŒ {bg_msg}")
                os.unlink(temp_path)
        
        # 3๋‹จ๊ณ„: ๊ทธ๋ฆผ์ž๋งŒ ์ถ”๊ฐ€ (์™„์ „ํ•œ ํฐ์ƒ‰ ๋ฐฐ๊ฒฝ)
        temp_path = upload_image_to_temp_url(result_image)
        if temp_path:
            shadow_img, shadow_msg = add_shadow_only(temp_path)
            if shadow_img:
                result_image = shadow_img
                status_messages.append(f"โœ… {shadow_msg}")
            else:
                status_messages.append(f"โŒ {shadow_msg}")
            os.unlink(temp_path)
        
        # 4๋‹จ๊ณ„: ํ™”์งˆ ๊ฐœ์„  (์ด๋ฏธ์ง€ ์ƒ์„ฑ ํ›„)
        if upscale_quality:
            temp_path = upload_image_to_temp_url(result_image)
            if temp_path:
                upscaled_img, upscale_msg = upscale_with_clarity(temp_path)
                if upscaled_img:
                    result_image = upscaled_img
                    status_messages.append(f"โœ… {upscale_msg}")
                else:
                    status_messages.append(f"โŒ {upscale_msg}")
                os.unlink(temp_path)
        
        # 5๋‹จ๊ณ„: ์ตœ์ข… ํฌ๊ธฐ ์กฐ์ •
        result_image = result_image.resize((1000, 1000), Image.Resampling.LANCZOS)
        status_messages.append("โœ… 1000x1000 ํฌ๊ธฐ ์กฐ์ • ์™„๋ฃŒ")
        
        return result_image, "\n".join(status_messages)
        
    except Exception as e:
        return None, f"์ฒ˜๋ฆฌ ์ค‘ ์˜ค๋ฅ˜: {str(e)}"

# Gradio ์ธํ„ฐํŽ˜์ด์Šค
def create_interface():
    with gr.Blocks(title="์ฟ ํŒก ์ธ๋„ค์ผ ์ƒ์„ฑ๊ธฐ", theme=gr.themes.Soft()) as iface:
        
        with gr.Row():
            with gr.Column(scale=1):
                # ์ด๋ฏธ์ง€ ์—…๋กœ๋“œ
                input_image = gr.Image(
                    label="์ƒํ’ˆ ์ด๋ฏธ์ง€ ์—…๋กœ๋“œ", 
                    type="pil",
                    height=400
                )
                
                # ์„ ํƒ ๊ธฐ๋Šฅ 2๊ฐ€์ง€
                with gr.Group():
                    remove_bg = gr.Checkbox(
                        label="๋ฐฐ๊ฒฝ ์ œ๊ฑฐ", 
                        value=True,
                        info="AI๋กœ ๋ฐฐ๊ฒฝ์„ ์ž๋™ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค"
                    )
                    upscale_quality = gr.Checkbox(
                        label="ํ™”์งˆ ๊ฐœ์„ ๊ธฐ", 
                        value=True,
                        info="AI๋กœ ํ™”์งˆ์„ ํ–ฅ์ƒ์‹œํ‚ต๋‹ˆ๋‹ค"
                    )
                
                # ์‹คํ–‰ ๋ฒ„ํŠผ
                process_btn = gr.Button("๐Ÿš€ ์ธ๋„ค์ผ ์ƒ์„ฑ", variant="primary", size="lg")
            
            with gr.Column(scale=1):
                # ์ถœ๋ ฅ ์ด๋ฏธ์ง€
                output_image = gr.Image(
                    label="์ฟ ํŒก ์ธ๋„ค์ผ ๊ฒฐ๊ณผ", 
                    height=400
                )
                status_output = gr.Textbox(
                    label="์ฒ˜๋ฆฌ ์ƒํƒœ", 
                    lines=6,
                    max_lines=10
                )
        
        # ๋ฒ„ํŠผ ํด๋ฆญ ์ด๋ฒคํŠธ
        process_btn.click(
            process_image,
            inputs=[input_image, remove_bg, upscale_quality],
            outputs=[output_image, status_output]
        )
    
    return iface

# ์•ฑ ์‹คํ–‰
if __name__ == "__main__":
    app = create_interface()
    app.launch(
        server_name="0.0.0.0",
        server_port=7860,
        share=True,
        show_error=True
    )