vineet124jig commited on
Commit
ec7c8ca
Β·
verified Β·
1 Parent(s): 70f926c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -3
app.py CHANGED
@@ -2,13 +2,64 @@ 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 web_ai_search(query, ai_overview, safe_search, spell_check, deep_research, max_depth, max_breadth, max_output_tokens, target_output_tokens):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  if not query or not query.strip():
13
  return "❌ Please enter a search query.", "", [], ""
14
  payload = {
@@ -81,8 +132,8 @@ with gr.Blocks() as demo:
81
  def toggle_deep_research(checked):
82
  return {deep_research_group: gr.update(visible=checked)}
83
  deep_research.change(fn=toggle_deep_research, inputs=deep_research, outputs=deep_research_group)
84
- def on_search(query, overview, safe, spell, deep, d_depth, d_breadth, d_tokens, d_target):
85
- return web_ai_search(query, overview, safe, spell, deep, d_depth, d_breadth, d_tokens, d_target)
86
  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],
87
  outputs=[search_status, overview_box, results_box, search_json_output])
88
  def clear_search():
 
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 = 1 # 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 = {
 
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():