ssboost commited on
Commit
1ac2024
ยท
verified ยท
1 Parent(s): d83ed0a

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +177 -0
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()