LogicGoInfotechSpaces commited on
Commit
e670614
·
1 Parent(s): d0c167f

Switch Space to Gradio UI: add Gradio app, update README and requirements

Browse files
Files changed (3) hide show
  1. README.md +7 -8
  2. app.py +61 -12
  3. requirements.txt +1 -0
README.md CHANGED
@@ -1,14 +1,13 @@
1
  ---
2
- title: Remove Photo Object
3
- emoji:
4
- colorFrom: pink
5
- colorTo: purple
6
- sdk: streamlit
7
- sdk_version: 1.10.0
8
- python_version: 3.9.5
9
  app_file: app.py
10
  pinned: false
11
  license: mit
12
  ---
13
 
14
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces#reference
 
1
  ---
2
+ title: Object Remover
3
+ emoji: 🌍
4
+ colorFrom: green
5
+ colorTo: green
6
+ sdk: gradio
7
+ sdk_version: "5.47.0"
 
8
  app_file: app.py
9
  pinned: false
10
  license: mit
11
  ---
12
 
13
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py CHANGED
@@ -4,6 +4,7 @@
4
  import numpy as np
5
  import pandas as pd
6
  import streamlit as st
 
7
  import os
8
  from datetime import datetime
9
  from PIL import Image
@@ -44,19 +45,21 @@ if 'reuse_image' not in st.session_state:
44
  def set_image(img):
45
  st.session_state.reuse_image = img
46
 
47
- st.title("AI Photo Object Removal")
48
 
49
- st.image(open("assets/demo.png", "rb").read())
 
50
 
51
- st.markdown(
52
- """
53
- So you want to remove an object in your photo? You don't need to learn photo editing skills.
54
- **Just draw over the parts of the image you want to remove, then our AI will remove them.**
55
- """
56
- )
57
- uploaded_file = st.file_uploader("Choose image", accept_multiple_files=False, type=["png", "jpg", "jpeg"])
58
 
59
- if uploaded_file is not None:
 
 
 
 
 
 
 
 
60
 
61
  if st.session_state.reuse_image is not None:
62
  img_input = Image.fromarray(st.session_state.reuse_image)
@@ -114,8 +117,6 @@ if uploaded_file is not None:
114
  im[background]=[0,0,0,255]
115
  im[drawing]=[0,0,0,0] # RGBA
116
 
117
- reuse = False
118
-
119
  if st.button('Submit'):
120
 
121
  with st.spinner("AI is doing the magic!"):
@@ -136,3 +137,51 @@ if uploaded_file is not None:
136
 
137
  st.info("**TIP**: If the result is not perfect, you can download it then "
138
  "upload then remove the artifacts.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
  import numpy as np
5
  import pandas as pd
6
  import streamlit as st
7
+ import gradio as gr
8
  import os
9
  from datetime import datetime
10
  from PIL import Image
 
45
  def set_image(img):
46
  st.session_state.reuse_image = img
47
 
 
48
 
49
+ def run_streamlit_ui():
50
+ st.title("AI Photo Object Removal")
51
 
52
+ st.image(open("assets/demo.png", "rb").read())
 
 
 
 
 
 
53
 
54
+ st.markdown(
55
+ """
56
+ So you want to remove an object in your photo? You don't need to learn photo editing skills.
57
+ **Just draw over the parts of the image you want to remove, then our AI will remove them.**
58
+ """
59
+ )
60
+ uploaded_file = st.file_uploader("Choose image", accept_multiple_files=False, type=["png", "jpg", "jpeg"])
61
+
62
+ if uploaded_file is not None:
63
 
64
  if st.session_state.reuse_image is not None:
65
  img_input = Image.fromarray(st.session_state.reuse_image)
 
117
  im[background]=[0,0,0,255]
118
  im[drawing]=[0,0,0,0] # RGBA
119
 
 
 
120
  if st.button('Submit'):
121
 
122
  with st.spinner("AI is doing the magic!"):
 
137
 
138
  st.info("**TIP**: If the result is not perfect, you can download it then "
139
  "upload then remove the artifacts.")
140
+
141
+
142
+ def _prepare_mask_rgba(image: Image.Image, mask: np.ndarray) -> np.ndarray:
143
+ if mask is None:
144
+ rgba = np.zeros((image.height, image.width, 4), dtype=np.uint8)
145
+ rgba[:, :, 3] = 255
146
+ return rgba
147
+ mask_img = Image.fromarray(mask).convert("L").resize(image.size)
148
+ mask_np = np.array(mask_img)
149
+ rgba = np.zeros((image.height, image.width, 4), dtype=np.uint8)
150
+ rgba[:, :, :3] = 0
151
+ rgba[:, :, 3] = 255
152
+ rgba[mask_np > 0, 3] = 0
153
+ return rgba
154
+
155
+
156
+ def gradio_inpaint(data):
157
+ if data is None:
158
+ return None
159
+ img = data.get("image") if isinstance(data, dict) else data
160
+ mask = data.get("mask") if isinstance(data, dict) else None
161
+ if img is None:
162
+ return None
163
+ if not isinstance(img, Image.Image):
164
+ img = Image.fromarray(img)
165
+ img_rgba = img.convert("RGBA")
166
+ rgba_mask = _prepare_mask_rgba(img_rgba, mask)
167
+ output = process_inpaint(np.array(img_rgba), rgba_mask)
168
+ return Image.fromarray(output).convert("RGB")
169
+
170
+
171
+ with gr.Blocks() as demo:
172
+ gr.Markdown("# AI Photo Object Removal")
173
+ gr.Markdown("Draw on the image to remove objects. Use the Sketch tool to paint the mask.")
174
+ with gr.Row():
175
+ input_image = gr.Image(label="Image", type="pil", tool="sketch", image_mode="RGBA", sources=["upload"])
176
+ run_btn = gr.Button("Remove Object")
177
+ output_image = gr.Image(label="Result", type="pil")
178
+ run_btn.click(gradio_inpaint, inputs=input_image, outputs=output_image)
179
+
180
+
181
+ if __name__ == "__main__":
182
+ # Default to Gradio for HF Spaces
183
+ use_gradio = True
184
+ if use_gradio:
185
+ demo.queue().launch()
186
+ else:
187
+ run_streamlit_ui()
requirements.txt CHANGED
@@ -4,6 +4,7 @@ numpy
4
  opencv-python-headless
5
  matplotlib
6
  streamlit
 
7
  streamlit-drawable-canvas==0.9.0
8
  pyyaml
9
  tqdm
 
4
  opencv-python-headless
5
  matplotlib
6
  streamlit
7
+ gradio>=4.0.0
8
  streamlit-drawable-canvas==0.9.0
9
  pyyaml
10
  tqdm