vineet124jig commited on
Commit
0ffb5ae
Β·
verified Β·
1 Parent(s): 56dfec2

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +134 -0
  2. requirements.txt +3 -0
app.py ADDED
@@ -0,0 +1,134 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
12
+ def detect_objects(image_url=None, file_store_key=None):
13
+ if not image_url and not file_store_key:
14
+ return "❌ Please provide either an image URL or file store key.", [], "", ""
15
+
16
+ if image_url and file_store_key:
17
+ return "❌ Provide only one: image URL or file store key.", [], "", ""
18
+
19
+ try:
20
+ payload = {}
21
+ if image_url:
22
+ payload["url"] = image_url
23
+ if file_store_key:
24
+ payload["file_store_key"] = file_store_key
25
+
26
+ response = requests.post(f"{BASE_URL}/ai/object_detection", headers=headers, json=payload)
27
+ if response.status_code != 200:
28
+ return f"❌ Error: {response.status_code} - {response.text}", [], "", ""
29
+
30
+ result = response.json()
31
+ if not result.get("success"):
32
+ return "❌ Detection failed.", [], "", ""
33
+
34
+ status = "βœ… Detection successful!"
35
+ tags = result.get("tags", [])
36
+ objects = result.get("objects", [])
37
+ description = f"Image Size: {result.get('width')} x {result.get('height')}\n\n"
38
+
39
+ for obj in objects:
40
+ bounds = obj.get("bounds", {})
41
+ bound_text = ""
42
+ if bounds.get("top_left") and bounds.get("top_right"):
43
+ tl = bounds["top_left"]
44
+ tr = bounds["top_right"]
45
+ bound_text = f"Bounds: ({tl['x']}, {tl['y']}) to ({tr['x']}, {tr['y']})"
46
+ description += f"β€’ {obj['name']} (Confidence: {obj['confidence']:.2f})\n {bound_text}\n"
47
+
48
+ raw_json = json.dumps(result, indent=2)
49
+ return status, tags, description.strip(), raw_json
50
+
51
+ except Exception as e:
52
+ return f"❌ Error: {str(e)}", [], "", ""
53
+
54
+ def web_ai_search(query, ai_overview, safe_search, spell_check, deep_research, max_depth, max_breadth, max_output_tokens, target_output_tokens):
55
+ if not query or not query.strip():
56
+ return "❌ Please enter a search query.", "", [], ""
57
+ payload = {
58
+ "query": query.strip(),
59
+ "ai_overview": ai_overview,
60
+ "safe_search": safe_search,
61
+ "spell_check": spell_check
62
+ }
63
+ if deep_research:
64
+ payload["deep_research"] = True
65
+ config = {
66
+ "max_depth": max_depth if max_depth is not None else 3,
67
+ "max_breadth": max_breadth if max_breadth is not None else 3,
68
+ "max_output_tokens": max_output_tokens if max_output_tokens is not None else 32000
69
+ }
70
+ if target_output_tokens is not None and target_output_tokens != "":
71
+ config["target_output_tokens"] = target_output_tokens
72
+ payload["deep_research_config"] = config
73
+ try:
74
+ response = requests.post(f"{BASE_URL}/web/search", headers=headers, json=payload)
75
+ if response.status_code != 200:
76
+ return f"❌ Error: {response.status_code} - {response.text}", "", [], ""
77
+ result = response.json()
78
+ if not result.get("success"):
79
+ return "❌ Search failed.", "", [], ""
80
+ status = "βœ… Search successful!"
81
+ overview = result.get("ai_overview", "")
82
+ results = result.get("results", [])
83
+ # Format results for display
84
+ formatted_results = []
85
+ for r in results:
86
+ title = r.get("title", "")
87
+ url = r.get("url", "")
88
+ snippet = r.get("snippet", "")
89
+ formatted_results.append(f"{title}\n{url}\n{snippet}")
90
+ raw_json = json.dumps(result, indent=2)
91
+ return status, overview, formatted_results, raw_json
92
+ except Exception as e:
93
+ return f"❌ Error: {str(e)}", "", [], ""
94
+
95
+ with gr.Blocks() as demo:
96
+ gr.Markdown("""
97
+ <div style='text-align: center; margin-bottom: 24px;'>
98
+ <h1 style='font-size:2.2em; margin-bottom: 0.2em;'>🌐 Web AI Search</h1>
99
+ <p style='font-size:1.2em; margin-top: 0;'>Effortlessly search the web and get high-quality results powered by AI.</p>
100
+ <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/web/ai-search' target='_blank'>documentation</a>.</p>
101
+ </div>
102
+ """)
103
+ with gr.Row():
104
+ with gr.Column():
105
+ search_query = gr.Textbox(label="Search Query", placeholder="Type your search here...")
106
+ ai_overview = gr.Checkbox(label="AI Overview", value=True)
107
+ safe_search = gr.Dropdown(choices=["moderate", "strict", "off"], value="moderate", label="Safe Search")
108
+ spell_check = gr.Checkbox(label="Spell Check", value=True)
109
+ deep_research = gr.Checkbox(label="Deep Research", value=False)
110
+ with gr.Group(visible=False) as deep_research_group:
111
+ max_depth = gr.Number(label="Max Depth", value=3, precision=0)
112
+ max_breadth = gr.Number(label="Max Breadth", value=3, precision=0)
113
+ max_output_tokens = gr.Number(label="Max Output Tokens", value=32000, precision=0)
114
+ target_output_tokens = gr.Number(label="Target Output Tokens (optional)", value=None, precision=0)
115
+ search_btn = gr.Button("πŸ” Search")
116
+ search_clear_btn = gr.Button("Clear")
117
+ with gr.Column():
118
+ search_status = gr.Textbox(label="Status", interactive=False)
119
+ overview_box = gr.Textbox(label="AI Overview", lines=4, interactive=False)
120
+ results_box = gr.Dataframe(headers=["Result"], label="Results", interactive=False)
121
+ search_json_box = gr.Accordion("Raw JSON Response", open=False)
122
+ with search_json_box:
123
+ search_json_output = gr.Textbox(show_label=False, lines=20, interactive=False)
124
+ def toggle_deep_research(checked):
125
+ return {deep_research_group: gr.update(visible=checked)}
126
+ deep_research.change(fn=toggle_deep_research, inputs=deep_research, outputs=deep_research_group)
127
+ def on_search(query, overview, safe, spell, deep, d_depth, d_breadth, d_tokens, d_target):
128
+ return web_ai_search(query, overview, safe, spell, deep, d_depth, d_breadth, d_tokens, d_target)
129
+ 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],
130
+ outputs=[search_status, overview_box, results_box, search_json_output])
131
+ def clear_search():
132
+ return "", "", [], ""
133
+ search_clear_btn.click(fn=clear_search, inputs=[], outputs=[search_query, overview_box, results_box, search_json_output])
134
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ gradio
2
+ requests
3
+ Pillow