Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,12 +8,12 @@ headers = {
|
|
| 8 |
"x-api-key": os.getenv("JIGSAWSTACK_API_KEY")
|
| 9 |
}
|
| 10 |
|
| 11 |
-
def detect_objects(image_url=None, file_store_key=None):
|
| 12 |
if not image_url and not file_store_key:
|
| 13 |
-
return "β Please provide either an image URL or file store key.",
|
| 14 |
|
| 15 |
if image_url and file_store_key:
|
| 16 |
-
return "β Provide only one: image URL or file store key.",
|
| 17 |
|
| 18 |
try:
|
| 19 |
payload = {}
|
|
@@ -21,34 +21,55 @@ def detect_objects(image_url=None, file_store_key=None):
|
|
| 21 |
payload["url"] = image_url
|
| 22 |
if file_store_key:
|
| 23 |
payload["file_store_key"] = file_store_key
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
response = requests.post(f"{BASE_URL}/ai/object_detection", headers=headers, json=payload)
|
| 26 |
if response.status_code != 200:
|
| 27 |
-
return f"β Error: {response.status_code} - {response.text}",
|
| 28 |
|
| 29 |
result = response.json()
|
| 30 |
if not result.get("success"):
|
| 31 |
-
return "β Detection failed.",
|
| 32 |
|
| 33 |
status = "β
Detection successful!"
|
| 34 |
-
tags = result.get("tags", [])
|
| 35 |
objects = result.get("objects", [])
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
-
for obj in objects:
|
| 39 |
bounds = obj.get("bounds", {})
|
|
|
|
| 40 |
bound_text = ""
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
raw_json = json.dumps(result, indent=2)
|
| 48 |
-
return status,
|
| 49 |
|
| 50 |
except Exception as e:
|
| 51 |
-
return f"β Error: {str(e)}",
|
| 52 |
|
| 53 |
with gr.Blocks() as demo:
|
| 54 |
gr.Markdown("""
|
|
@@ -64,13 +85,22 @@ with gr.Blocks() as demo:
|
|
| 64 |
input_type = gr.Radio(choices=["Image URL", "File Store Key"], value="Image URL", label="Input Type")
|
| 65 |
image_url = gr.Textbox(label="Image URL", placeholder="https://example.com/image.jpg", visible=True)
|
| 66 |
file_store_key = gr.Textbox(label="File Store Key", placeholder="my-image.jpg", visible=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
detect_btn = gr.Button("π Detect Objects")
|
| 68 |
clear_btn = gr.Button("Clear")
|
| 69 |
|
| 70 |
with gr.Column():
|
| 71 |
status_box = gr.Textbox(label="Status", interactive=False)
|
| 72 |
-
tag_display = gr.Label(label="Detected Tags")
|
| 73 |
desc_display = gr.Textbox(label="Object Details", lines=10, interactive=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
json_box = gr.Accordion("Raw JSON Response", open=False)
|
| 75 |
with json_box:
|
| 76 |
json_output = gr.Textbox(show_label=False, lines=20, interactive=False)
|
|
@@ -83,20 +113,42 @@ with gr.Blocks() as demo:
|
|
| 83 |
|
| 84 |
input_type.change(fn=toggle_inputs, inputs=input_type, outputs=[image_url, file_store_key])
|
| 85 |
|
| 86 |
-
def on_detect(input_mode, url, key):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
if input_mode == "Image URL":
|
| 88 |
-
return detect_objects(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
else:
|
| 90 |
-
return detect_objects(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
|
| 92 |
-
|
| 93 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
|
| 95 |
def clear_all():
|
| 96 |
-
return "Image URL", "", "", "", "", ""
|
| 97 |
|
| 98 |
clear_btn.click(fn=clear_all, inputs=[], outputs=[
|
| 99 |
-
input_type, image_url, file_store_key,
|
|
|
|
| 100 |
])
|
| 101 |
|
| 102 |
demo.launch()
|
|
|
|
| 8 |
"x-api-key": os.getenv("JIGSAWSTACK_API_KEY")
|
| 9 |
}
|
| 10 |
|
| 11 |
+
def detect_objects(image_url=None, file_store_key=None, prompts=None, features=None, annotated_image=False):
|
| 12 |
if not image_url and not file_store_key:
|
| 13 |
+
return "β Please provide either an image URL or file store key.", "", "", None
|
| 14 |
|
| 15 |
if image_url and file_store_key:
|
| 16 |
+
return "β Provide only one: image URL or file store key.", "", "", None
|
| 17 |
|
| 18 |
try:
|
| 19 |
payload = {}
|
|
|
|
| 21 |
payload["url"] = image_url
|
| 22 |
if file_store_key:
|
| 23 |
payload["file_store_key"] = file_store_key
|
| 24 |
+
|
| 25 |
+
# Add optional parameters
|
| 26 |
+
if prompts:
|
| 27 |
+
payload["prompts"] = prompts
|
| 28 |
+
if features:
|
| 29 |
+
payload["features"] = features
|
| 30 |
+
|
| 31 |
+
# Always return annotated image
|
| 32 |
+
payload["annotated_image"] = True
|
| 33 |
+
|
| 34 |
+
# Always use url as return_type
|
| 35 |
+
payload["return_type"] = "url"
|
| 36 |
|
| 37 |
response = requests.post(f"{BASE_URL}/ai/object_detection", headers=headers, json=payload)
|
| 38 |
if response.status_code != 200:
|
| 39 |
+
return f"β Error: {response.status_code} - {response.text}", "", "", None
|
| 40 |
|
| 41 |
result = response.json()
|
| 42 |
if not result.get("success"):
|
| 43 |
+
return "β Detection failed.", "", "", None
|
| 44 |
|
| 45 |
status = "β
Detection successful!"
|
|
|
|
| 46 |
objects = result.get("objects", [])
|
| 47 |
+
annotated_image_url = result.get("annotated_image")
|
| 48 |
+
|
| 49 |
+
# Create description with object details
|
| 50 |
+
description = f"Image Size: {result.get('width', 'Unknown')} x {result.get('height', 'Unknown')}\n\n"
|
| 51 |
+
description += f"Total Objects Detected: {len(objects)}\n\n"
|
| 52 |
|
| 53 |
+
for i, obj in enumerate(objects):
|
| 54 |
bounds = obj.get("bounds", {})
|
| 55 |
+
label = obj.get("label", "Unknown")
|
| 56 |
bound_text = ""
|
| 57 |
+
|
| 58 |
+
if bounds:
|
| 59 |
+
width = bounds.get("width", "Unknown")
|
| 60 |
+
height = bounds.get("height", "Unknown")
|
| 61 |
+
top_left = bounds.get("top_left", {})
|
| 62 |
+
if top_left:
|
| 63 |
+
x, y = top_left.get("x", "?"), top_left.get("y", "?")
|
| 64 |
+
bound_text = f"Position: ({x}, {y}), Size: {width}x{height}"
|
| 65 |
+
|
| 66 |
+
description += f"β’ {label}\n {bound_text}\n"
|
| 67 |
|
| 68 |
raw_json = json.dumps(result, indent=2)
|
| 69 |
+
return status, description.strip(), raw_json, annotated_image_url
|
| 70 |
|
| 71 |
except Exception as e:
|
| 72 |
+
return f"β Error: {str(e)}", "", "", None
|
| 73 |
|
| 74 |
with gr.Blocks() as demo:
|
| 75 |
gr.Markdown("""
|
|
|
|
| 85 |
input_type = gr.Radio(choices=["Image URL", "File Store Key"], value="Image URL", label="Input Type")
|
| 86 |
image_url = gr.Textbox(label="Image URL", placeholder="https://example.com/image.jpg", visible=True)
|
| 87 |
file_store_key = gr.Textbox(label="File Store Key", placeholder="my-image.jpg", visible=False)
|
| 88 |
+
|
| 89 |
+
# Advanced options
|
| 90 |
+
prompts = gr.Textbox(label="Prompts (comma-separated)", placeholder="wine glass, bottle, cup", info="Targeted object detection prompts")
|
| 91 |
+
features = gr.CheckboxGroup(choices=["object_detection", "gui"], value=["object_detection"], label="Features")
|
| 92 |
+
annotated_image = gr.Checkbox(label="Return Annotated Image", value=True)
|
| 93 |
+
|
| 94 |
detect_btn = gr.Button("π Detect Objects")
|
| 95 |
clear_btn = gr.Button("Clear")
|
| 96 |
|
| 97 |
with gr.Column():
|
| 98 |
status_box = gr.Textbox(label="Status", interactive=False)
|
|
|
|
| 99 |
desc_display = gr.Textbox(label="Object Details", lines=10, interactive=False)
|
| 100 |
+
|
| 101 |
+
# Annotated image display
|
| 102 |
+
annotated_image_display = gr.Image(label="Annotated Image", visible=False)
|
| 103 |
+
|
| 104 |
json_box = gr.Accordion("Raw JSON Response", open=False)
|
| 105 |
with json_box:
|
| 106 |
json_output = gr.Textbox(show_label=False, lines=20, interactive=False)
|
|
|
|
| 113 |
|
| 114 |
input_type.change(fn=toggle_inputs, inputs=input_type, outputs=[image_url, file_store_key])
|
| 115 |
|
| 116 |
+
def on_detect(input_mode, url, key, prompts_text, features_list, annotated):
|
| 117 |
+
# Parse prompts
|
| 118 |
+
prompts_list = None
|
| 119 |
+
if prompts_text.strip():
|
| 120 |
+
prompts_list = [p.strip() for p in prompts_text.split(",") if p.strip()]
|
| 121 |
+
|
| 122 |
if input_mode == "Image URL":
|
| 123 |
+
return detect_objects(
|
| 124 |
+
image_url=url.strip(),
|
| 125 |
+
prompts=prompts_list,
|
| 126 |
+
features=features_list,
|
| 127 |
+
annotated_image=annotated
|
| 128 |
+
)
|
| 129 |
else:
|
| 130 |
+
return detect_objects(
|
| 131 |
+
file_store_key=key.strip(),
|
| 132 |
+
prompts=prompts_list,
|
| 133 |
+
features=features_list,
|
| 134 |
+
annotated_image=annotated
|
| 135 |
+
)
|
| 136 |
|
| 137 |
+
def update_annotated_image_visibility(annotated):
|
| 138 |
+
return gr.update(visible=annotated)
|
| 139 |
+
|
| 140 |
+
detect_btn.click(fn=on_detect, inputs=[
|
| 141 |
+
input_type, image_url, file_store_key, prompts, features, annotated_image
|
| 142 |
+
], outputs=[status_box, desc_display, json_output, annotated_image_display])
|
| 143 |
+
|
| 144 |
+
annotated_image.change(fn=update_annotated_image_visibility, inputs=annotated_image, outputs=annotated_image_display)
|
| 145 |
|
| 146 |
def clear_all():
|
| 147 |
+
return "Image URL", "", "", "", "", ["object_detection"], False, "", "", "", None
|
| 148 |
|
| 149 |
clear_btn.click(fn=clear_all, inputs=[], outputs=[
|
| 150 |
+
input_type, image_url, file_store_key, prompts, features, annotated_image,
|
| 151 |
+
status_box, desc_display, json_output, annotated_image_display
|
| 152 |
])
|
| 153 |
|
| 154 |
demo.launch()
|