Update app.py
Browse files
app.py
CHANGED
@@ -62,7 +62,7 @@ def array_to_image_path(image_array, session_id):
|
|
62 |
if image_array is None:
|
63 |
raise ValueError("No image provided. Please upload an image before submitting.")
|
64 |
img = Image.fromarray(np.uint8(image_array))
|
65 |
-
filename = f"
|
66 |
img.save(filename)
|
67 |
return os.path.abspath(filename)
|
68 |
|
@@ -126,8 +126,12 @@ def run_showui(image, query, session_id):
|
|
126 |
result_image = draw_point(image_path, click_xy, radius=10)
|
127 |
return result_image, str(click_xy), image_path
|
128 |
|
129 |
-
def save_and_upload_data(image_path, query, session_id, votes=None):
|
130 |
"""Save the data to a JSON file and upload to S3."""
|
|
|
|
|
|
|
|
|
131 |
votes = votes or {"upvotes": 0, "downvotes": 0}
|
132 |
data = {
|
133 |
"image_path": image_path,
|
@@ -136,18 +140,23 @@ def save_and_upload_data(image_path, query, session_id, votes=None):
|
|
136 |
"timestamp": datetime.now().isoformat()
|
137 |
}
|
138 |
|
139 |
-
local_file_name = f"
|
140 |
|
141 |
with open(local_file_name, "w") as f:
|
142 |
json.dump(data, f)
|
143 |
|
144 |
upload_to_s3(local_file_name, 'altair.storage', object_name=f"ootb/{local_file_name}")
|
|
|
145 |
|
146 |
return data
|
147 |
|
148 |
-
def update_vote(vote_type, session_id):
|
149 |
"""Update the vote count and re-upload the JSON file."""
|
150 |
-
|
|
|
|
|
|
|
|
|
151 |
|
152 |
with open(local_file_name, "r") as f:
|
153 |
data = json.load(f)
|
@@ -171,6 +180,7 @@ def build_demo(embed_mode, concurrency_count=1):
|
|
171 |
with gr.Blocks(title="ShowUI Demo", theme=gr.themes.Default()) as demo:
|
172 |
state_image_path = gr.State(value=None)
|
173 |
state_session_id = gr.State(value=None)
|
|
|
174 |
|
175 |
if not embed_mode:
|
176 |
gr.HTML(
|
@@ -240,35 +250,37 @@ def build_demo(embed_mode, concurrency_count=1):
|
|
240 |
raise ValueError("No image provided. Please upload an image before submitting.")
|
241 |
|
242 |
session_id = datetime.now().strftime("%Y%m%d_%H%M%S")
|
|
|
|
|
243 |
result_image, click_coords, image_path = run_showui(image, query, session_id)
|
244 |
|
245 |
-
save_and_upload_data(image_path, query, session_id)
|
246 |
|
247 |
-
return result_image, click_coords, image_path, session_id
|
248 |
|
249 |
submit_btn.click(
|
250 |
on_submit,
|
251 |
[imagebox, textbox],
|
252 |
-
[output_img, output_coords, state_image_path, state_session_id],
|
253 |
)
|
254 |
|
255 |
clear_btn.click(
|
256 |
lambda: (None, None, None, None, None),
|
257 |
inputs=None,
|
258 |
-
outputs=[imagebox, textbox, output_img, output_coords, state_image_path, state_session_id],
|
259 |
queue=False
|
260 |
)
|
261 |
|
262 |
upvote_btn.click(
|
263 |
-
lambda session_id: update_vote("upvote", session_id),
|
264 |
-
inputs=state_session_id,
|
265 |
outputs=[],
|
266 |
queue=False
|
267 |
)
|
268 |
|
269 |
downvote_btn.click(
|
270 |
-
lambda session_id: update_vote("downvote", session_id),
|
271 |
-
inputs=state_session_id,
|
272 |
outputs=[],
|
273 |
queue=False
|
274 |
)
|
|
|
62 |
if image_array is None:
|
63 |
raise ValueError("No image provided. Please upload an image before submitting.")
|
64 |
img = Image.fromarray(np.uint8(image_array))
|
65 |
+
filename = f"{session_id}.png"
|
66 |
img.save(filename)
|
67 |
return os.path.abspath(filename)
|
68 |
|
|
|
126 |
result_image = draw_point(image_path, click_xy, radius=10)
|
127 |
return result_image, str(click_xy), image_path
|
128 |
|
129 |
+
def save_and_upload_data(image_path, query, session_id, is_example_image, votes=None):
|
130 |
"""Save the data to a JSON file and upload to S3."""
|
131 |
+
if is_example_image:
|
132 |
+
print("Example image used. Skipping upload.")
|
133 |
+
return
|
134 |
+
|
135 |
votes = votes or {"upvotes": 0, "downvotes": 0}
|
136 |
data = {
|
137 |
"image_path": image_path,
|
|
|
140 |
"timestamp": datetime.now().isoformat()
|
141 |
}
|
142 |
|
143 |
+
local_file_name = f"{session_id}.json"
|
144 |
|
145 |
with open(local_file_name, "w") as f:
|
146 |
json.dump(data, f)
|
147 |
|
148 |
upload_to_s3(local_file_name, 'altair.storage', object_name=f"ootb/{local_file_name}")
|
149 |
+
upload_to_s3(image_path, 'altair.storage', object_name=f"ootb/{os.path.basename(image_path)}")
|
150 |
|
151 |
return data
|
152 |
|
153 |
+
def update_vote(vote_type, session_id, is_example_image):
|
154 |
"""Update the vote count and re-upload the JSON file."""
|
155 |
+
if is_example_image:
|
156 |
+
print("Example image used. Skipping vote update.")
|
157 |
+
return "Example image used. No vote recorded."
|
158 |
+
|
159 |
+
local_file_name = f"{session_id}.json"
|
160 |
|
161 |
with open(local_file_name, "r") as f:
|
162 |
data = json.load(f)
|
|
|
180 |
with gr.Blocks(title="ShowUI Demo", theme=gr.themes.Default()) as demo:
|
181 |
state_image_path = gr.State(value=None)
|
182 |
state_session_id = gr.State(value=None)
|
183 |
+
state_is_example_image = gr.State(value=False)
|
184 |
|
185 |
if not embed_mode:
|
186 |
gr.HTML(
|
|
|
250 |
raise ValueError("No image provided. Please upload an image before submitting.")
|
251 |
|
252 |
session_id = datetime.now().strftime("%Y%m%d_%H%M%S")
|
253 |
+
is_example_image = isinstance(image, str) and image.startswith("./examples/")
|
254 |
+
|
255 |
result_image, click_coords, image_path = run_showui(image, query, session_id)
|
256 |
|
257 |
+
save_and_upload_data(image_path, query, session_id, is_example_image)
|
258 |
|
259 |
+
return result_image, click_coords, image_path, session_id, is_example_image
|
260 |
|
261 |
submit_btn.click(
|
262 |
on_submit,
|
263 |
[imagebox, textbox],
|
264 |
+
[output_img, output_coords, state_image_path, state_session_id, state_is_example_image],
|
265 |
)
|
266 |
|
267 |
clear_btn.click(
|
268 |
lambda: (None, None, None, None, None),
|
269 |
inputs=None,
|
270 |
+
outputs=[imagebox, textbox, output_img, output_coords, state_image_path, state_session_id, state_is_example_image],
|
271 |
queue=False
|
272 |
)
|
273 |
|
274 |
upvote_btn.click(
|
275 |
+
lambda session_id, is_example_image: update_vote("upvote", session_id, is_example_image),
|
276 |
+
inputs=[state_session_id, state_is_example_image],
|
277 |
outputs=[],
|
278 |
queue=False
|
279 |
)
|
280 |
|
281 |
downvote_btn.click(
|
282 |
+
lambda session_id, is_example_image: update_vote("downvote", session_id, is_example_image),
|
283 |
+
inputs=[state_session_id, state_is_example_image],
|
284 |
outputs=[],
|
285 |
queue=False
|
286 |
)
|