ssboost commited on
Commit
50d479d
ยท
verified ยท
1 Parent(s): 7c6a5f6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -70
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
- logger.info("=== ์ฒ˜๋ฆฌ ์‹œ์ž‘ ===")
23
-
24
- api_token = os.getenv("REPLICATE_API_TOKEN")
25
- if not api_token:
26
- logger.error("REPLICATE_API_TOKEN์ด ์„ค์ •๋˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค.")
27
- return None
28
-
29
- logger.info(f"API ํ† ํฐ ํ™•์ธ ์™„๋ฃŒ: {api_token[:5]}...")
30
-
31
- if input_image is None:
32
- logger.error("์ž…๋ ฅ ์ด๋ฏธ์ง€๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.")
33
- return None
34
-
35
- logger.info(f"์ž…๋ ฅ ์ด๋ฏธ์ง€ ๊ฒฝ๋กœ: {input_image}")
36
- logger.info(f"์ž…๋ ฅ ์ด๋ฏธ์ง€ ํƒ€์ž…: {type(input_image)}")
37
-
38
- try:
39
- with open(input_image, "rb") as image_file:
40
- image_data = base64.b64encode(image_file.read()).decode('utf-8')
41
- image_uri = f"data:image/png;base64,{image_data}"
42
-
43
- logger.info("์ด๋ฏธ์ง€ ๋ณ€ํ™˜ ์™„๋ฃŒ")
44
- logger.info(f"Data URI ๊ธธ์ด: {len(image_uri)}")
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 ์˜ค๋ฅ˜: {str(re)}")
76
- return None
77
-
78
  except Exception as e:
79
- logger.error(f"์˜ˆ์ƒ์น˜ ๋ชปํ•œ ์˜ค๋ฅ˜: {str(e)}")
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__":