Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,16 +1,66 @@
|
|
1 |
import gradio as gr
|
2 |
import requests
|
|
|
|
|
|
|
|
|
3 |
from PIL import Image
|
4 |
import io
|
5 |
-
|
6 |
|
7 |
BASE_URL = "https://api.jigsawstack.com/v1"
|
8 |
-
headers = {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
# ----------------- JigsawStack API Wrappers ------------------
|
11 |
|
12 |
|
13 |
-
def translate_text(text_input, target_lang):
|
|
|
|
|
|
|
|
|
|
|
14 |
# Validate required inputs
|
15 |
if not text_input or not text_input.strip():
|
16 |
return {"error": "Text input is required"}
|
@@ -149,7 +199,7 @@ with gr.Blocks() as demo:
|
|
149 |
translate_btn.click(
|
150 |
translate_text,
|
151 |
inputs=[text_input, target_lang],
|
152 |
-
outputs=translate_result
|
153 |
)
|
154 |
|
155 |
|
|
|
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 |
|
11 |
BASE_URL = "https://api.jigsawstack.com/v1"
|
12 |
+
headers = {
|
13 |
+
"x-api-key": os.getenv("JIGSAWSTACK_API_KEY")
|
14 |
+
}
|
15 |
+
|
16 |
+
# Rate limiting configuration
|
17 |
+
request_times = defaultdict(list)
|
18 |
+
MAX_REQUESTS = 1 # Maximum requests per time window
|
19 |
+
TIME_WINDOW = 3600 # Time window in seconds (1 hour)
|
20 |
+
|
21 |
+
def get_real_ip(request: gr.Request):
|
22 |
+
"""Extract real IP address using x-forwarded-for header or fallback"""
|
23 |
+
if not request:
|
24 |
+
return "unknown"
|
25 |
+
|
26 |
+
forwarded = request.headers.get("x-forwarded-for")
|
27 |
+
if forwarded:
|
28 |
+
ip = forwarded.split(",")[0].strip() # First IP in the list is the client's
|
29 |
+
else:
|
30 |
+
ip = request.client.host # fallback
|
31 |
+
return ip
|
32 |
|
33 |
+
def check_rate_limit(request: gr.Request):
|
34 |
+
"""Check if the current request exceeds rate limits"""
|
35 |
+
if not request:
|
36 |
+
return True, "Rate limit check failed - no request info"
|
37 |
+
|
38 |
+
ip = get_real_ip(request)
|
39 |
+
now = time.time()
|
40 |
+
|
41 |
+
# Clean up old timestamps outside the time window
|
42 |
+
request_times[ip] = [t for t in request_times[ip] if now - t < TIME_WINDOW]
|
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 |
# ----------------- JigsawStack API Wrappers ------------------
|
56 |
|
57 |
|
58 |
+
def translate_text(text_input, target_lang, request: gr.Request):
|
59 |
+
# Check rate limit first
|
60 |
+
rate_limit_ok, rate_limit_msg = check_rate_limit(request)
|
61 |
+
if not rate_limit_ok:
|
62 |
+
return {"error": "Rate limit exceeded", "message": rate_limit_msg}
|
63 |
+
|
64 |
# Validate required inputs
|
65 |
if not text_input or not text_input.strip():
|
66 |
return {"error": "Text input is required"}
|
|
|
199 |
translate_btn.click(
|
200 |
translate_text,
|
201 |
inputs=[text_input, target_lang],
|
202 |
+
outputs=translate_result,
|
203 |
)
|
204 |
|
205 |
|