jbilcke-hf HF Staff commited on
Commit
56b3741
·
verified ·
1 Parent(s): a0cbc48

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -6
app.py CHANGED
@@ -14,6 +14,8 @@ from io import BytesIO
14
  import base64
15
  import re
16
 
 
 
17
  # Regex pattern to match data URI scheme
18
  data_uri_pattern = re.compile(r'data:image/(png|jpeg|jpg|webp);base64,')
19
 
@@ -49,8 +51,12 @@ def resize_image(image):
49
  return image
50
 
51
 
52
- def process(image_base64):
53
- orig_image = readb64(image_base64)
 
 
 
 
54
 
55
  # prepare input
56
  w,h = orig_im_size = orig_image.size
@@ -77,9 +83,30 @@ def process(image_base64):
77
  new_im = Image.new("RGBA", pil_im.size, (0,0,0,0))
78
  new_im.paste(orig_image, mask=pil_im)
79
 
80
- return writeb64(new_im)
 
 
 
81
 
82
- demo = gr.Interface(fn=process, inputs="text", outputs="text")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
83
 
84
- if __name__ == "__main__":
85
- demo.launch(share=False)
 
14
  import base64
15
  import re
16
 
17
+ SECRET_TOKEN = os.getenv('SECRET_TOKEN', 'default_secret')
18
+
19
  # Regex pattern to match data URI scheme
20
  data_uri_pattern = re.compile(r'data:image/(png|jpeg|jpg|webp);base64,')
21
 
 
51
  return image
52
 
53
 
54
+ def process(secret_token, base64_in):
55
+ if secret_token != SECRET_TOKEN:
56
+ raise gr.Error(
57
+ f'Invalid secret token. Please fork the original space if you want to use it for yourself.')
58
+
59
+ orig_image = readb64(base64_in)
60
 
61
  # prepare input
62
  w,h = orig_im_size = orig_image.size
 
83
  new_im = Image.new("RGBA", pil_im.size, (0,0,0,0))
84
  new_im.paste(orig_image, mask=pil_im)
85
 
86
+ base64_out = writeb64(new_im)
87
+
88
+ return base64_out
89
+
90
 
91
+ with gr.Blocks() as demo:
92
+ secret_token = gr.Text(
93
+ label='Secret Token',
94
+ max_lines=1,
95
+ placeholder='Enter your secret token')
96
+ gr.HTML("""
97
+ <div style="z-index: 100; position: fixed; top: 0px; right: 0px; left: 0px; bottom: 0px; width: 100%; height: 100%; background: white; display: flex; align-items: center; justify-content: center; color: black;">
98
+ <div style="text-align: center; color: black;">
99
+ <p style="color: black;">This space is a REST API to programmatically remove the background of an image.</p>
100
+ <p style="color: black;">Interested in using it? Please use the <a href="https://huggingface.co/spaces/briaai/BRIA-RMBG-1.4" target="_blank">original space</a>, thank you!</p>
101
+ </div>
102
+ </div>""")
103
+ base64_in = gr.Textbox(label="Base64 Input")
104
+ base64_out = gr.Textbox(label="Base64 Output")
105
+
106
+ generate_btn.click(
107
+ fn=process,
108
+ inputs=[secret_token, base64_in],
109
+ outputs=base64_out,
110
+ api_name="run")
111
 
112
+ demo.queue(max_size=20).launch()