Spaces:
Running
Running
Upload 3 files
Browse files- README.md +5 -5
- app.py +143 -0
- requirements.txt +4 -0
README.md
CHANGED
@@ -1,14 +1,14 @@
|
|
1 |
---
|
2 |
-
title:
|
3 |
-
emoji:
|
4 |
-
colorFrom:
|
5 |
colorTo: purple
|
6 |
sdk: gradio
|
7 |
-
sdk_version: 5.
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
license: mit
|
11 |
-
short_description:
|
12 |
---
|
13 |
|
14 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
1 |
---
|
2 |
+
title: Convert Text to Speech with Ease
|
3 |
+
emoji: π
|
4 |
+
colorFrom: purple
|
5 |
colorTo: purple
|
6 |
sdk: gradio
|
7 |
+
sdk_version: 5.34.2
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
license: mit
|
11 |
+
short_description: 'Convert text to speech easily with jigsawstack '
|
12 |
---
|
13 |
|
14 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
ADDED
@@ -0,0 +1,143 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import requests
|
3 |
+
import json
|
4 |
+
import os
|
5 |
+
import time
|
6 |
+
from collections import defaultdict
|
7 |
+
from PIL import Image
|
8 |
+
import io
|
9 |
+
|
10 |
+
BASE_URL = "https://api.jigsawstack.com/v1"
|
11 |
+
headers = {
|
12 |
+
"x-api-key": os.getenv("JIGSAWSTACK_API_KEY")
|
13 |
+
}
|
14 |
+
|
15 |
+
# Rate limiting configuration
|
16 |
+
request_times = defaultdict(list)
|
17 |
+
MAX_REQUESTS = 20 # Maximum requests per time window
|
18 |
+
TIME_WINDOW = 3600 # Time window in seconds (1 hour)
|
19 |
+
|
20 |
+
def get_real_ip(request: gr.Request):
|
21 |
+
"""Extract real IP address using x-forwarded-for header or fallback"""
|
22 |
+
if not request:
|
23 |
+
return "unknown"
|
24 |
+
|
25 |
+
forwarded = request.headers.get("x-forwarded-for")
|
26 |
+
if forwarded:
|
27 |
+
ip = forwarded.split(",")[0].strip() # First IP in the list is the client's
|
28 |
+
else:
|
29 |
+
ip = request.client.host # fallback
|
30 |
+
return ip
|
31 |
+
|
32 |
+
def check_rate_limit(request: gr.Request):
|
33 |
+
"""Check if the current request exceeds rate limits"""
|
34 |
+
if not request:
|
35 |
+
return True, "Rate limit check failed - no request info"
|
36 |
+
|
37 |
+
ip = get_real_ip(request)
|
38 |
+
now = time.time()
|
39 |
+
|
40 |
+
# Clean up old timestamps outside the time window
|
41 |
+
request_times[ip] = [t for t in request_times[ip] if now - t < TIME_WINDOW]
|
42 |
+
|
43 |
+
|
44 |
+
# Check if rate limit exceeded
|
45 |
+
if len(request_times[ip]) >= MAX_REQUESTS:
|
46 |
+
time_remaining = int(TIME_WINDOW - (now - request_times[ip][0]))
|
47 |
+
time_remaining_minutes = round(time_remaining / 60, 1)
|
48 |
+
time_window_minutes = round(TIME_WINDOW / 60, 1)
|
49 |
+
|
50 |
+
return False, f"Rate limit exceeded. You can make {MAX_REQUESTS} requests per {time_window_minutes} minutes. Try again in {time_remaining_minutes} minutes."
|
51 |
+
|
52 |
+
# Add current request timestamp
|
53 |
+
request_times[ip].append(now)
|
54 |
+
return True, ""
|
55 |
+
|
56 |
+
# ----------------- JigsawStack API Wrappers ------------------
|
57 |
+
|
58 |
+
def web_ai_search(query, ai_overview, safe_search, spell_check, deep_research, max_depth, max_breadth, max_output_tokens, target_output_tokens, request: gr.Request):
|
59 |
+
rate_limit_ok, rate_limit_msg = check_rate_limit(request)
|
60 |
+
if not rate_limit_ok:
|
61 |
+
return f"β {rate_limit_msg}", "", [], ""
|
62 |
+
|
63 |
+
if not query or not query.strip():
|
64 |
+
return "β Please enter a search query.", "", [], ""
|
65 |
+
payload = {
|
66 |
+
"query": query.strip(),
|
67 |
+
"ai_overview": ai_overview,
|
68 |
+
"safe_search": safe_search,
|
69 |
+
"spell_check": spell_check
|
70 |
+
}
|
71 |
+
if deep_research:
|
72 |
+
payload["deep_research"] = True
|
73 |
+
config = {
|
74 |
+
"max_depth": max_depth if max_depth is not None else 3,
|
75 |
+
"max_breadth": max_breadth if max_breadth is not None else 3,
|
76 |
+
"max_output_tokens": max_output_tokens if max_output_tokens is not None else 32000
|
77 |
+
}
|
78 |
+
if target_output_tokens is not None and target_output_tokens != "":
|
79 |
+
config["target_output_tokens"] = target_output_tokens
|
80 |
+
payload["deep_research_config"] = config
|
81 |
+
try:
|
82 |
+
response = requests.post(f"{BASE_URL}/web/search", headers=headers, json=payload)
|
83 |
+
if response.status_code != 200:
|
84 |
+
return f"β Error: {response.status_code} - {response.text}", "", [], ""
|
85 |
+
result = response.json()
|
86 |
+
if not result.get("success"):
|
87 |
+
return "β Search failed.", "", [], ""
|
88 |
+
status = "β
Search successful!"
|
89 |
+
overview = result.get("ai_overview", "")
|
90 |
+
results = result.get("results", [])
|
91 |
+
# Format results for display
|
92 |
+
formatted_results = []
|
93 |
+
for r in results:
|
94 |
+
title = r.get("title", "")
|
95 |
+
url = r.get("url", "")
|
96 |
+
snippet = r.get("snippet", "")
|
97 |
+
formatted_results.append(f"{title}\n{url}\n{snippet}")
|
98 |
+
raw_json = json.dumps(result, indent=2)
|
99 |
+
return status, overview, formatted_results, raw_json
|
100 |
+
except Exception as e:
|
101 |
+
return f"β Error: {str(e)}", "", [], ""
|
102 |
+
|
103 |
+
with gr.Blocks() as demo:
|
104 |
+
gr.Markdown("""
|
105 |
+
<div style='text-align: center; margin-bottom: 24px;'>
|
106 |
+
<h1 style='font-size:2.2em; margin-bottom: 0.2em;'>π§© AI Search</h1>
|
107 |
+
<p style='font-size:1.2em; margin-top: 0;'>Effortlessly search the web and get high-quality results powered by AI.</p>
|
108 |
+
<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>
|
109 |
+
</div>
|
110 |
+
""")
|
111 |
+
with gr.Row():
|
112 |
+
with gr.Column():
|
113 |
+
search_query = gr.Textbox(label="Search Query", placeholder="Type your search here...")
|
114 |
+
ai_overview = gr.Checkbox(label="AI Overview", value=True)
|
115 |
+
safe_search = gr.Dropdown(choices=["moderate", "strict", "off"], value="moderate", label="Safe Search")
|
116 |
+
spell_check = gr.Checkbox(label="Spell Check", value=True)
|
117 |
+
deep_research = gr.Checkbox(label="Deep Research", value=True)
|
118 |
+
with gr.Group(visible=True) as deep_research_group:
|
119 |
+
max_depth = gr.Number(label="Max Depth", value=3, precision=0)
|
120 |
+
max_breadth = gr.Number(label="Max Breadth", value=3, precision=0)
|
121 |
+
max_output_tokens = gr.Number(label="Max Output Tokens", value=32000, precision=0)
|
122 |
+
target_output_tokens = gr.Number(label="Target Output Tokens (optional)", value=None, precision=0)
|
123 |
+
search_btn = gr.Button("π Search")
|
124 |
+
search_clear_btn = gr.Button("Clear")
|
125 |
+
with gr.Column():
|
126 |
+
search_status = gr.Textbox(label="Status", interactive=False)
|
127 |
+
overview_box = gr.Textbox(label="AI Overview", lines=4, interactive=False)
|
128 |
+
results_box = gr.Dataframe(headers=["Result"], label="Results", interactive=False)
|
129 |
+
search_json_box = gr.Accordion("Raw JSON Response", open=False)
|
130 |
+
with search_json_box:
|
131 |
+
search_json_output = gr.Textbox(show_label=False, lines=20, interactive=False)
|
132 |
+
def toggle_deep_research(checked):
|
133 |
+
return {deep_research_group: gr.update(visible=checked)}
|
134 |
+
deep_research.change(fn=toggle_deep_research, inputs=deep_research, outputs=deep_research_group)
|
135 |
+
def on_search(query, overview, safe, spell, deep, d_depth, d_breadth, d_tokens, d_target, request: gr.Request):
|
136 |
+
return web_ai_search(query, overview, safe, spell, deep, d_depth, d_breadth, d_tokens, d_target, request)
|
137 |
+
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],
|
138 |
+
outputs=[search_status, overview_box, results_box, search_json_output])
|
139 |
+
def clear_search():
|
140 |
+
return "", "", [], ""
|
141 |
+
search_clear_btn.click(fn=clear_search, inputs=[], outputs=[search_query, overview_box, results_box, search_json_output])
|
142 |
+
demo.launch()
|
143 |
+
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
gradio
|
2 |
+
requests
|
3 |
+
Pillow
|
4 |
+
python-dotenv
|