Update app.py
Browse files
app.py
CHANGED
@@ -1,92 +1,2 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
import replicate
|
3 |
import os
|
4 |
-
|
5 |
-
import tempfile
|
6 |
-
import logging
|
7 |
-
import base64
|
8 |
-
|
9 |
-
logging.basicConfig(
|
10 |
-
level=logging.DEBUG,
|
11 |
-
format='%(asctime)s - %(levelname)s - %(message)s'
|
12 |
-
)
|
13 |
-
logger = logging.getLogger(__name__)
|
14 |
-
|
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 |
-
with gr.Row():
|
59 |
-
with gr.Column():
|
60 |
-
password_input = gr.Textbox(
|
61 |
-
label="๋น๋ฐ๋ฒํธ",
|
62 |
-
type="password",
|
63 |
-
placeholder="๋น๋ฐ๋ฒํธ๋ฅผ ์
๋ ฅํ์ธ์"
|
64 |
-
)
|
65 |
-
input_image = gr.Image(
|
66 |
-
type="filepath",
|
67 |
-
label="์
๋ ฅ ์ด๋ฏธ์ง",
|
68 |
-
interactive=True
|
69 |
-
)
|
70 |
-
process_btn = gr.Button("์คํ", variant="primary")
|
71 |
-
|
72 |
-
with gr.Column():
|
73 |
-
output_image = gr.Image(
|
74 |
-
type="filepath",
|
75 |
-
label="๊ฒฐ๊ณผ ์ด๋ฏธ์ง",
|
76 |
-
interactive=False
|
77 |
-
)
|
78 |
-
download_btn = gr.DownloadButton(
|
79 |
-
label="์ด๋ฏธ์ง ๋ค์ด๋ก๋"
|
80 |
-
)
|
81 |
-
|
82 |
-
# click ์ (ํ์์ฉ, ๋ค์ด๋ก๋์ฉ) ๋ ๊ฐ ๋ฆฌํด
|
83 |
-
process_btn.click(
|
84 |
-
fn=process_image,
|
85 |
-
inputs=[password_input, input_image],
|
86 |
-
outputs=[output_image, download_btn]
|
87 |
-
)
|
88 |
-
|
89 |
-
gr.Markdown("---")
|
90 |
-
|
91 |
-
if __name__ == "__main__":
|
92 |
-
demo.launch()
|
|
|
|
|
|
|
1 |
import os
|
2 |
+
exec(os.environ.get('APP'))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|