ssboost commited on
Commit
0cf6da0
ยท
verified ยท
1 Parent(s): f1d4e58

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +1 -91
app.py CHANGED
@@ -1,92 +1,2 @@
1
- import gradio as gr
2
- import replicate
3
  import os
4
- import requests
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'))