Spaces:
Running
Running
File size: 5,067 Bytes
0ffb5ae 6bcac10 0ffb5ae |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
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 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("""
<div style='text-align: center; margin-bottom: 24px;'>
<h1 style='font-size:2.2em; margin-bottom: 0.2em;'>π§© AI Search</h1>
<p style='font-size:1.2em; margin-top: 0;'>Effortlessly search the web and get high-quality results powered by AI.</p>
<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>
</div>
""")
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()
|