import gradio as gr import requests import json import os BASE_URL = "https://api.jigsawstack.com/v1" headers = { "x-api-key": os.getenv("JIGSAWSTACK_API_KEY") } def detect_objects(image_url=None, file_store_key=None): if not image_url and not file_store_key: return "❌ Please provide either an image URL or file store key.", [], "", "" if image_url and file_store_key: return "❌ Provide only one: image URL or file store key.", [], "", "" try: payload = {} if image_url: payload["url"] = image_url if file_store_key: payload["file_store_key"] = file_store_key response = requests.post(f"{BASE_URL}/ai/object_detection", headers=headers, json=payload) if response.status_code != 200: return f"❌ Error: {response.status_code} - {response.text}", [], "", "" result = response.json() if not result.get("success"): return "❌ Detection failed.", [], "", "" status = "✅ Detection successful!" tags = result.get("tags", []) objects = result.get("objects", []) description = f"Image Size: {result.get('width')} x {result.get('height')}\n\n" for obj in objects: bounds = obj.get("bounds", {}) bound_text = "" if bounds.get("top_left") and bounds.get("top_right"): tl = bounds["top_left"] tr = bounds["top_right"] bound_text = f"Bounds: ({tl['x']}, {tl['y']}) to ({tr['x']}, {tr['y']})" description += f"• {obj['name']} (Confidence: {obj['confidence']:.2f})\n {bound_text}\n" raw_json = json.dumps(result, indent=2) return status, tags, description.strip(), raw_json except Exception as e: return f"❌ Error: {str(e)}", [], "", "" def web_ai_search(query, ai_overview, safe_search, spell_check, deep_research, max_depth, max_breadth, max_output_tokens, target_output_tokens): if not query or not query.strip(): return "❌ Please enter a search query.", "", [], "" payload = { "query": query.strip(), "ai_overview": ai_overview, "safe_search": safe_search, "spell_check": spell_check } if deep_research: payload["deep_research"] = True config = { "max_depth": max_depth if max_depth is not None else 3, "max_breadth": max_breadth if max_breadth is not None else 3, "max_output_tokens": max_output_tokens if max_output_tokens is not None else 32000 } if target_output_tokens is not None and target_output_tokens != "": config["target_output_tokens"] = target_output_tokens payload["deep_research_config"] = config try: response = requests.post(f"{BASE_URL}/web/search", headers=headers, json=payload) if response.status_code != 200: return f"❌ Error: {response.status_code} - {response.text}", "", [], "" result = response.json() if not result.get("success"): return "❌ Search failed.", "", [], "" status = "✅ Search successful!" overview = result.get("ai_overview", "") results = result.get("results", []) # Format results for display formatted_results = [] for r in results: title = r.get("title", "") url = r.get("url", "") snippet = r.get("snippet", "") formatted_results.append(f"{title}\n{url}\n{snippet}") raw_json = json.dumps(result, indent=2) return status, overview, formatted_results, raw_json except Exception as e: return f"❌ Error: {str(e)}", "", [], "" with gr.Blocks() as demo: gr.Markdown("""

🌐 Web AI Search

Effortlessly search the web and get high-quality results powered by AI.

For more details and API usage, see the documentation.

""") with gr.Row(): with gr.Column(): search_query = gr.Textbox(label="Search Query", placeholder="Type your search here...") ai_overview = gr.Checkbox(label="AI Overview", value=True) safe_search = gr.Dropdown(choices=["moderate", "strict", "off"], value="moderate", label="Safe Search") spell_check = gr.Checkbox(label="Spell Check", value=True) deep_research = gr.Checkbox(label="Deep Research", value=False) with gr.Group(visible=False) as deep_research_group: max_depth = gr.Number(label="Max Depth", value=3, precision=0) max_breadth = gr.Number(label="Max Breadth", value=3, precision=0) max_output_tokens = gr.Number(label="Max Output Tokens", value=32000, precision=0) target_output_tokens = gr.Number(label="Target Output Tokens (optional)", value=None, precision=0) search_btn = gr.Button("🔍 Search") search_clear_btn = gr.Button("Clear") with gr.Column(): search_status = gr.Textbox(label="Status", interactive=False) overview_box = gr.Textbox(label="AI Overview", lines=4, interactive=False) results_box = gr.Dataframe(headers=["Result"], label="Results", interactive=False) search_json_box = gr.Accordion("Raw JSON Response", open=False) with search_json_box: search_json_output = gr.Textbox(show_label=False, lines=20, interactive=False) def toggle_deep_research(checked): return {deep_research_group: gr.update(visible=checked)} deep_research.change(fn=toggle_deep_research, inputs=deep_research, outputs=deep_research_group) def on_search(query, overview, safe, spell, deep, d_depth, d_breadth, d_tokens, d_target): return web_ai_search(query, overview, safe, spell, deep, d_depth, d_breadth, d_tokens, d_target) search_btn.click(fn=on_search, inputs=[search_query, ai_overview, safe_search, spell_check, deep_research, max_depth, max_breadth, max_output_tokens, target_output_tokens], outputs=[search_status, overview_box, results_box, search_json_output]) def clear_search(): return "", "", [], "" search_clear_btn.click(fn=clear_search, inputs=[], outputs=[search_query, overview_box, results_box, search_json_output]) demo.launch()