Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,177 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import replicate
|
3 |
+
import os
|
4 |
+
import requests
|
5 |
+
import tempfile
|
6 |
+
from pathlib import Path
|
7 |
+
import logging
|
8 |
+
import base64
|
9 |
+
|
10 |
+
# ๋ก๊น
์ค์
|
11 |
+
logging.basicConfig(
|
12 |
+
level=logging.DEBUG,
|
13 |
+
format='%(asctime)s - %(levelname)s - %(message)s'
|
14 |
+
)
|
15 |
+
logger = logging.getLogger(__name__)
|
16 |
+
|
17 |
+
# API ํ ํฐ ์ค์ (Hugging Face Space์์๋ SECRET์ผ๋ก ์ค์ ํด์ผ ํจ)
|
18 |
+
# REPLICATE_API_TOKEN = os.getenv("REPLICATE_API_TOKEN")
|
19 |
+
|
20 |
+
def remove_background(input_image):
|
21 |
+
"""
|
22 |
+
์ด๋ฏธ์ง์ ๋ฐฐ๊ฒฝ์ ์ ๊ฑฐํ๋ ํจ์
|
23 |
+
"""
|
24 |
+
try:
|
25 |
+
logger.info("=== ๋ฐฐ๊ฒฝ ์ ๊ฑฐ ์์ ===")
|
26 |
+
|
27 |
+
# Replicate API ํ ํฐ ํ์ธ
|
28 |
+
api_token = os.getenv("REPLICATE_API_TOKEN")
|
29 |
+
if not api_token:
|
30 |
+
logger.error("REPLICATE_API_TOKEN์ด ์ค์ ๋์ง ์์์ต๋๋ค.")
|
31 |
+
return None, "Error: REPLICATE_API_TOKEN์ด ์ค์ ๋์ง ์์์ต๋๋ค."
|
32 |
+
|
33 |
+
logger.info(f"API ํ ํฐ ํ์ธ ์๋ฃ: {api_token[:5]}...")
|
34 |
+
|
35 |
+
# ์
๋ ฅ ์ด๋ฏธ์ง๊ฐ ์ ํจํ์ง ํ์ธ
|
36 |
+
if input_image is None:
|
37 |
+
logger.error("์
๋ ฅ ์ด๋ฏธ์ง๊ฐ ์์ต๋๋ค.")
|
38 |
+
return None, "์ด๋ฏธ์ง๋ฅผ ์
๋ก๋ํด์ฃผ์ธ์."
|
39 |
+
|
40 |
+
logger.info(f"์
๋ ฅ ์ด๋ฏธ์ง ๊ฒฝ๋ก: {input_image}")
|
41 |
+
logger.info(f"์
๋ ฅ ์ด๋ฏธ์ง ํ์
: {type(input_image)}")
|
42 |
+
|
43 |
+
# ํ์ผ์ ์ด์ด์ URI ํ์์ผ๋ก ๋ณํ
|
44 |
+
try:
|
45 |
+
with open(input_image, "rb") as image_file:
|
46 |
+
# Data URI ํ์์ผ๋ก ๋ณํ
|
47 |
+
image_data = base64.b64encode(image_file.read()).decode('utf-8')
|
48 |
+
image_uri = f"data:image/png;base64,{image_data}"
|
49 |
+
|
50 |
+
logger.info("์ด๋ฏธ์ง๋ฅผ Data URI๋ก ๋ณํ ์๋ฃ")
|
51 |
+
logger.info(f"Data URI ๊ธธ์ด: {len(image_uri)}")
|
52 |
+
|
53 |
+
# Replicate ๋ชจ๋ธ ์คํ
|
54 |
+
logger.info("Replicate ๋ชจ๋ธ ์คํ ์์")
|
55 |
+
|
56 |
+
output = replicate.run(
|
57 |
+
"851-labs/background-remover:a029dff38972b5fda4ec5d75d7d1cd25aeff621d2cf4946a41055d7db66b80bc",
|
58 |
+
input={"image": image_uri}
|
59 |
+
)
|
60 |
+
|
61 |
+
logger.info("Replicate ๋ชจ๋ธ ์คํ ์๋ฃ")
|
62 |
+
logger.info(f"๊ฒฐ๊ณผ ํ์
: {type(output)}")
|
63 |
+
|
64 |
+
# ์์ ํ์ผ๋ก ๊ฒฐ๊ณผ ์ ์ฅ
|
65 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as tmp_file:
|
66 |
+
tmp_file.write(output.read())
|
67 |
+
tmp_file_path = tmp_file.name
|
68 |
+
|
69 |
+
logger.info(f"๊ฒฐ๊ณผ ์ด๋ฏธ์ง ์ ์ฅ ์๋ฃ: {tmp_file_path}")
|
70 |
+
return tmp_file_path, "๋ฐฐ๊ฒฝ ์ ๊ฑฐ๊ฐ ์๋ฃ๋์์ต๋๋ค!"
|
71 |
+
|
72 |
+
except Exception as file_error:
|
73 |
+
logger.error(f"ํ์ผ ์ฒ๋ฆฌ ์ค๋ฅ: {str(file_error)}")
|
74 |
+
raise file_error
|
75 |
+
|
76 |
+
except replicate.exceptions.ReplicateError as re:
|
77 |
+
logger.error(f"Replicate API ์ค๋ฅ: {str(re)}")
|
78 |
+
logger.error(f"์ค๋ฅ ์์ธ: {re.__dict__}")
|
79 |
+
return None, f"Replicate API ์ค๋ฅ: {str(re)}"
|
80 |
+
|
81 |
+
except Exception as e:
|
82 |
+
logger.error(f"์์์น ๋ชปํ ์ค๋ฅ: {str(e)}")
|
83 |
+
logger.error(f"์ค๋ฅ ํ์
: {type(e)}")
|
84 |
+
logger.error(f"์ค๋ฅ ์์ธ: {e.__dict__ if hasattr(e, '__dict__') else 'no details'}")
|
85 |
+
return None, f"์ค๋ฅ ๋ฐ์: {str(e)}"
|
86 |
+
|
87 |
+
# Gradio ์ธํฐํ์ด์ค ์์ฑ
|
88 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
89 |
+
gr.Markdown(
|
90 |
+
"""
|
91 |
+
# ๐ผ๏ธ AI ๋ฐฐ๊ฒฝ ์ ๊ฑฐ๊ธฐ
|
92 |
+
|
93 |
+
์ด ์ฑ์ Replicate์ 851-labs/background-remover ๋ชจ๋ธ์ ์ฌ์ฉํ์ฌ ์ด๋ฏธ์ง์ ๋ฐฐ๊ฒฝ์ ์ ๊ฑฐํฉ๋๋ค.
|
94 |
+
|
95 |
+
### ์ฌ์ฉ ๋ฐฉ๋ฒ:
|
96 |
+
1. ์ด๋ฏธ์ง๋ฅผ ์
๋ก๋ํ์ธ์
|
97 |
+
2. '๋ฐฐ๊ฒฝ ์ ๊ฑฐ' ๋ฒํผ์ ํด๋ฆญํ์ธ์
|
98 |
+
3. ๊ฒฐ๊ณผ ์ด๋ฏธ์ง๋ฅผ ๋ค์ด๋ก๋ํ์ธ์
|
99 |
+
"""
|
100 |
+
)
|
101 |
+
|
102 |
+
with gr.Row():
|
103 |
+
with gr.Column():
|
104 |
+
input_image = gr.Image(
|
105 |
+
type="filepath",
|
106 |
+
label="์๋ณธ ์ด๋ฏธ์ง",
|
107 |
+
interactive=True
|
108 |
+
)
|
109 |
+
remove_btn = gr.Button("๋ฐฐ๊ฒฝ ์ ๊ฑฐ", variant="primary")
|
110 |
+
|
111 |
+
with gr.Column():
|
112 |
+
output_image = gr.Image(
|
113 |
+
type="filepath",
|
114 |
+
label="๊ฒฐ๊ณผ ์ด๋ฏธ์ง",
|
115 |
+
interactive=False
|
116 |
+
)
|
117 |
+
status_text = gr.Textbox(
|
118 |
+
label="์ํ",
|
119 |
+
interactive=False,
|
120 |
+
show_label=True,
|
121 |
+
lines=3
|
122 |
+
)
|
123 |
+
|
124 |
+
# ๋๋ฒ๊ทธ ์ ๋ณด๋ฅผ ์ํ ์ถ๊ฐ UI
|
125 |
+
with gr.Accordion("๋๋ฒ๊ทธ ์ ๋ณด", open=False):
|
126 |
+
debug_output = gr.Textbox(
|
127 |
+
label="๋๋ฒ๊ทธ ๋ก๊ทธ",
|
128 |
+
interactive=False,
|
129 |
+
lines=10
|
130 |
+
)
|
131 |
+
|
132 |
+
# ์ด๋ฒคํธ ํธ๋ค๋ฌ
|
133 |
+
def process_and_log(image):
|
134 |
+
# ๋ก๊ทธ ์บก์ฒ๋ฅผ ์ํ ํธ๋ค๋ฌ
|
135 |
+
log_capture = []
|
136 |
+
|
137 |
+
class ListHandler(logging.Handler):
|
138 |
+
def emit(self, record):
|
139 |
+
log_capture.append(self.format(record))
|
140 |
+
|
141 |
+
list_handler = ListHandler()
|
142 |
+
list_handler.setFormatter(logging.Formatter('%(asctime)s - %(levelname)s - %(message)s'))
|
143 |
+
logger.addHandler(list_handler)
|
144 |
+
|
145 |
+
try:
|
146 |
+
result_image, status = remove_background(image)
|
147 |
+
debug_log = "\n".join(log_capture)
|
148 |
+
return result_image, status, debug_log
|
149 |
+
finally:
|
150 |
+
logger.removeHandler(list_handler)
|
151 |
+
|
152 |
+
remove_btn.click(
|
153 |
+
fn=process_and_log,
|
154 |
+
inputs=[input_image],
|
155 |
+
outputs=[output_image, status_text, debug_output]
|
156 |
+
)
|
157 |
+
|
158 |
+
gr.Markdown(
|
159 |
+
"""
|
160 |
+
---
|
161 |
+
### โ๏ธ ๊ธฐ์ ์ ๋ณด
|
162 |
+
- ๋ชจ๋ธ: 851-labs/background-remover
|
163 |
+
- API: Replicate
|
164 |
+
- Framework: Gradio 5.0+
|
165 |
+
|
166 |
+
### ๐ ๋ณด์ ์ฐธ๊ณ ์ฌํญ
|
167 |
+
์ด ์ฑ์ ์ฌ์ฉํ๋ ค๋ฉด Replicate API ํ ํฐ์ด ํ์ํฉ๋๋ค. Hugging Face Space์ Settings์์
|
168 |
+
REPLICATE_API_TOKEN์ Secret์ผ๋ก ์ค์ ํด์ฃผ์ธ์.
|
169 |
+
|
170 |
+
### ๐ ๋๋ฒ๊น
|
171 |
+
๋ฌธ์ ๊ฐ ๋ฐ์ํ๋ฉด '๋๋ฒ๊ทธ ์ ๋ณด' ์น์
์ ํผ์ณ์ ์์ธ ๋ก๊ทธ๋ฅผ ํ์ธํ์ธ์.
|
172 |
+
"""
|
173 |
+
)
|
174 |
+
|
175 |
+
# Gradio ์ฑ ์คํ
|
176 |
+
if __name__ == "__main__":
|
177 |
+
demo.launch()
|