Update app.py
Browse files
app.py
CHANGED
@@ -3,7 +3,6 @@ import replicate
|
|
3 |
import os
|
4 |
import requests
|
5 |
import tempfile
|
6 |
-
from pathlib import Path
|
7 |
import logging
|
8 |
import base64
|
9 |
|
@@ -16,77 +15,54 @@ logger = logging.getLogger(__name__)
|
|
16 |
def process_image(password, input_image):
|
17 |
# ๋น๋ฐ๋ฒํธ ๊ฒ์ฆ
|
18 |
if password != "1089":
|
19 |
-
raise ValueError("์๋ชป๋
|
20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
try:
|
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 |
-
logger.info("๋ชจ๋ธ ์คํ ์์")
|
47 |
-
|
48 |
-
output = replicate.run(
|
49 |
-
"851-labs/background-remover:a029dff38972b5fda4ec5d75d7d1cd25aeff621d2cf4946a41055d7db66b80bc",
|
50 |
-
input={"image": image_uri}
|
51 |
-
)
|
52 |
-
|
53 |
-
logger.info("๋ชจ๋ธ ์คํ ์๋ฃ")
|
54 |
-
logger.info(f"๊ฒฐ๊ณผ ํ์
: {type(output)}")
|
55 |
-
|
56 |
-
with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as tmp_file:
|
57 |
-
# replicate.run() ๋ฐํ ํ์
์ด fileโlike ๊ฐ์ฒด์ผ ๊ฒฝ์ฐ
|
58 |
-
if hasattr(output, "read"):
|
59 |
-
tmp_file.write(output.read())
|
60 |
-
# ๋๋ URL ๋ฆฌ์คํธ์ธ ๊ฒฝ์ฐ ๋ค์ด๋ก๋
|
61 |
-
elif isinstance(output, (list, tuple)) and output and isinstance(output[0], str):
|
62 |
-
resp = requests.get(output[0])
|
63 |
-
resp.raise_for_status()
|
64 |
-
tmp_file.write(resp.content)
|
65 |
-
tmp_file_path = tmp_file.name
|
66 |
-
|
67 |
-
logger.info(f"๊ฒฐ๊ณผ ์ด๋ฏธ์ง ์ ์ฅ ์๋ฃ: {tmp_file_path}")
|
68 |
-
return tmp_file_path
|
69 |
-
|
70 |
-
except Exception as file_error:
|
71 |
-
logger.error(f"ํ์ผ ์ฒ๋ฆฌ ์ค๋ฅ: {str(file_error)}")
|
72 |
-
raise file_error
|
73 |
-
|
74 |
except replicate.exceptions.ReplicateError as re:
|
75 |
-
logger.error(f"API ์ค๋ฅ: {
|
76 |
-
return None
|
77 |
-
|
78 |
except Exception as e:
|
79 |
-
logger.error(f"์์์น ๋ชปํ ์ค๋ฅ: {
|
80 |
-
return None
|
81 |
|
82 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
|
|
|
83 |
with gr.Row():
|
84 |
with gr.Column():
|
85 |
-
# ๋น๋ฐ๋ฒํธ ์
๋ ฅ ํ๋
|
86 |
password_input = gr.Textbox(
|
87 |
-
label="
|
88 |
-
type="password",
|
89 |
-
placeholder="
|
90 |
)
|
91 |
input_image = gr.Image(
|
92 |
type="filepath",
|
@@ -94,21 +70,24 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
94 |
interactive=True
|
95 |
)
|
96 |
process_btn = gr.Button("์คํ", variant="primary")
|
97 |
-
|
98 |
with gr.Column():
|
99 |
output_image = gr.Image(
|
100 |
type="filepath",
|
101 |
-
label="
|
102 |
interactive=False
|
103 |
)
|
104 |
-
|
105 |
-
|
|
|
|
|
|
|
106 |
process_btn.click(
|
107 |
fn=process_image,
|
108 |
inputs=[password_input, input_image],
|
109 |
-
outputs=[output_image]
|
110 |
)
|
111 |
-
|
112 |
gr.Markdown("---")
|
113 |
|
114 |
if __name__ == "__main__":
|
|
|
3 |
import os
|
4 |
import requests
|
5 |
import tempfile
|
|
|
6 |
import logging
|
7 |
import base64
|
8 |
|
|
|
15 |
def process_image(password, input_image):
|
16 |
# ๋น๋ฐ๋ฒํธ ๊ฒ์ฆ
|
17 |
if password != "1089":
|
18 |
+
raise ValueError("์๋ชป๋ ๋น๋ฐ๋ฒํธ์
๋๋ค.")
|
19 |
+
if not os.getenv("REPLICATE_API_TOKEN"):
|
20 |
+
logger.error("REPLICATE_API_TOKEN์ด ์ค์ ๋์ง ์์์ต๋๋ค.")
|
21 |
+
return None, None
|
22 |
+
if input_image is None:
|
23 |
+
logger.error("์
๋ ฅ ์ด๋ฏธ์ง๊ฐ ์์ต๋๋ค.")
|
24 |
+
return None, None
|
25 |
+
|
26 |
try:
|
27 |
+
# base64๋ก ๋ณํ
|
28 |
+
with open(input_image, "rb") as f:
|
29 |
+
data = base64.b64encode(f.read()).decode()
|
30 |
+
image_uri = f"data:image/png;base64,{data}"
|
31 |
+
|
32 |
+
# ๋ชจ๋ธ ์คํ
|
33 |
+
output = replicate.run(
|
34 |
+
"851-labs/background-remover:a029dff38972b5fda4ec5d75d7d1cd25aeff621d2cf4946a41055d7db66b80bc",
|
35 |
+
input={"image": image_uri}
|
36 |
+
)
|
37 |
+
|
38 |
+
# ๊ฒฐ๊ณผ ์ ์ฅ
|
39 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as tmp:
|
40 |
+
if hasattr(output, "read"):
|
41 |
+
tmp.write(output.read())
|
42 |
+
elif isinstance(output, (list, tuple)) and output:
|
43 |
+
resp = requests.get(output[0])
|
44 |
+
resp.raise_for_status()
|
45 |
+
tmp.write(resp.content)
|
46 |
+
out_path = tmp.name
|
47 |
+
|
48 |
+
return out_path, out_path
|
49 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
50 |
except replicate.exceptions.ReplicateError as re:
|
51 |
+
logger.error(f"API ์ค๋ฅ: {re}")
|
52 |
+
return None, None
|
|
|
53 |
except Exception as e:
|
54 |
+
logger.error(f"์์์น ๋ชปํ ์ค๋ฅ: {e}")
|
55 |
+
return None, None
|
56 |
|
57 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
58 |
+
gr.Markdown("# Background Remover (Password Protected)")
|
59 |
+
|
60 |
with gr.Row():
|
61 |
with gr.Column():
|
|
|
62 |
password_input = gr.Textbox(
|
63 |
+
label="๋น๋ฐ๋ฒํธ",
|
64 |
+
type="password",
|
65 |
+
placeholder="๋น๋ฐ๋ฒํธ๋ฅผ ์
๋ ฅํ์ธ์"
|
66 |
)
|
67 |
input_image = gr.Image(
|
68 |
type="filepath",
|
|
|
70 |
interactive=True
|
71 |
)
|
72 |
process_btn = gr.Button("์คํ", variant="primary")
|
73 |
+
|
74 |
with gr.Column():
|
75 |
output_image = gr.Image(
|
76 |
type="filepath",
|
77 |
+
label="๊ฒฐ๊ณผ ์ด๋ฏธ์ง",
|
78 |
interactive=False
|
79 |
)
|
80 |
+
download_btn = gr.DownloadButton(
|
81 |
+
label="์ด๋ฏธ์ง ๋ค์ด๋ก๋"
|
82 |
+
)
|
83 |
+
|
84 |
+
# click ์ (ํ์์ฉ, ๋ค์ด๋ก๋์ฉ) ๋ ๊ฐ ๋ฆฌํด
|
85 |
process_btn.click(
|
86 |
fn=process_image,
|
87 |
inputs=[password_input, input_image],
|
88 |
+
outputs=[output_image, download_btn]
|
89 |
)
|
90 |
+
|
91 |
gr.Markdown("---")
|
92 |
|
93 |
if __name__ == "__main__":
|