vineet124jig commited on
Commit
891c8f0
Β·
verified Β·
1 Parent(s): 3d88879

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +102 -0
  2. requirements.txt +3 -0
app.py ADDED
@@ -0,0 +1,102 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import requests
3
+ import json
4
+ import os
5
+
6
+ BASE_URL = "https://api.jigsawstack.com/v1"
7
+ 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 = {}
20
+ if image_url:
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
+ description = f"Image Size: {result.get('width')} x {result.get('height')}\n\n"
37
+
38
+ for obj in objects:
39
+ bounds = obj.get("bounds", {})
40
+ bound_text = ""
41
+ if bounds.get("top_left") and bounds.get("top_right"):
42
+ tl = bounds["top_left"]
43
+ tr = bounds["top_right"]
44
+ bound_text = f"Bounds: ({tl['x']}, {tl['y']}) to ({tr['x']}, {tr['y']})"
45
+ description += f"β€’ {obj['name']} (Confidence: {obj['confidence']:.2f})\n {bound_text}\n"
46
+
47
+ raw_json = json.dumps(result, indent=2)
48
+ return status, tags, description.strip(), raw_json
49
+
50
+ except Exception as e:
51
+ return f"❌ Error: {str(e)}", [], "", ""
52
+
53
+ with gr.Blocks() as demo:
54
+ gr.Markdown("""
55
+ <div style='text-align: center; margin-bottom: 24px;'>
56
+ <h1 style='font-size:2.2em; margin-bottom: 0.2em;'>🧩 Object Detection</h1>
57
+ <p style='font-size:1.2em; margin-top: 0;'>Detect objects within images with great accuracy using AI models.</p>
58
+ <p style='font-size:1em; margin-top: 0.5em;'>For more details and API usage, see the <a href='https://jigsawstack.com/docs/api-reference/ai/object-detection' target='_blank'>documentation</a>.</p>
59
+ </div>
60
+ """)
61
+
62
+ with gr.Row():
63
+ with gr.Column():
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)
77
+
78
+ def toggle_inputs(choice):
79
+ return (
80
+ gr.update(visible=(choice == "Image URL")),
81
+ gr.update(visible=(choice == "File Store Key"))
82
+ )
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(image_url=url.strip())
89
+ else:
90
+ return detect_objects(file_store_key=key.strip())
91
+
92
+ detect_btn.click(fn=on_detect, inputs=[input_type, image_url, file_store_key],
93
+ outputs=[status_box, tag_display, desc_display, json_output])
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, status_box, desc_display, json_output
100
+ ])
101
+
102
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ gradio
2
+ requests
3
+ Pillow