Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,8 +8,58 @@ headers = {
|
|
| 8 |
"x-api-key": os.getenv("JIGSAWSTACK_API_KEY")
|
| 9 |
}
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
def transcribe_audio(input_type, audio_url, file_store_key, language):
|
| 12 |
"""Transcribe audio using JigsawStack Speech-to-Text API"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
if input_type == "Audio URL" and not audio_url:
|
| 14 |
return "Error: Please provide an audio URL.", ""
|
| 15 |
if input_type == "File Store Key" and not file_store_key:
|
|
|
|
| 8 |
"x-api-key": os.getenv("JIGSAWSTACK_API_KEY")
|
| 9 |
}
|
| 10 |
|
| 11 |
+
|
| 12 |
+
# Rate limiting configuration
|
| 13 |
+
request_times = defaultdict(list)
|
| 14 |
+
MAX_REQUESTS = 1 # Maximum requests per time window
|
| 15 |
+
TIME_WINDOW = 60 # Time window in seconds
|
| 16 |
+
|
| 17 |
+
def get_real_ip(request: gr.Request):
|
| 18 |
+
"""Extract real IP address using x-forwarded-for header or fallback"""
|
| 19 |
+
if not request:
|
| 20 |
+
return "unknown"
|
| 21 |
+
|
| 22 |
+
forwarded = request.headers.get("x-forwarded-for")
|
| 23 |
+
if forwarded:
|
| 24 |
+
ip = forwarded.split(",")[0].strip() # First IP in the list is the client's
|
| 25 |
+
else:
|
| 26 |
+
ip = request.client.host # fallback
|
| 27 |
+
return ip
|
| 28 |
+
|
| 29 |
+
def check_rate_limit(request: gr.Request):
|
| 30 |
+
"""Check if the current request exceeds rate limits"""
|
| 31 |
+
if not request:
|
| 32 |
+
return True, "Rate limit check failed - no request info"
|
| 33 |
+
|
| 34 |
+
ip = get_real_ip(request)
|
| 35 |
+
now = time.time()
|
| 36 |
+
|
| 37 |
+
print(f"Ip: {ip}")
|
| 38 |
+
print(f"Now: {now}")
|
| 39 |
+
print(f"Request times: {request_times[ip]}")
|
| 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 |
+
print(f"Request times: {request_times[ip]}")
|
| 44 |
+
|
| 45 |
+
# Check if rate limit exceeded
|
| 46 |
+
if len(request_times[ip]) >= MAX_REQUESTS:
|
| 47 |
+
time_remaining = int(TIME_WINDOW - (now - request_times[ip][0]))
|
| 48 |
+
return False, f"Rate limit exceeded. You can make {MAX_REQUESTS} requests per {TIME_WINDOW} seconds. Try again in {time_remaining} seconds."
|
| 49 |
+
|
| 50 |
+
# Add current request timestamp
|
| 51 |
+
request_times[ip].append(now)
|
| 52 |
+
return True, ""
|
| 53 |
+
|
| 54 |
+
|
| 55 |
def transcribe_audio(input_type, audio_url, file_store_key, language):
|
| 56 |
"""Transcribe audio using JigsawStack Speech-to-Text API"""
|
| 57 |
+
|
| 58 |
+
# Check rate limit first
|
| 59 |
+
rate_limit_ok, rate_limit_msg = check_rate_limit(request)
|
| 60 |
+
if not rate_limit_ok:
|
| 61 |
+
return rate_limit_msg, ""
|
| 62 |
+
|
| 63 |
if input_type == "Audio URL" and not audio_url:
|
| 64 |
return "Error: Please provide an audio URL.", ""
|
| 65 |
if input_type == "File Store Key" and not file_store_key:
|