Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,6 +4,7 @@ import random
|
|
| 4 |
from functools import partial
|
| 5 |
import gradio as gr
|
| 6 |
from huggingface_hub import InferenceClient
|
|
|
|
| 7 |
|
| 8 |
css = """
|
| 9 |
gradio-app {
|
|
@@ -79,16 +80,19 @@ for c in client.chat_completion(messages, max_tokens=200, stream=True):
|
|
| 79 |
"""
|
| 80 |
|
| 81 |
ip_requests = {}
|
|
|
|
| 82 |
|
| 83 |
def allow_ip(request: gr.Request, show_error=True):
|
| 84 |
ip = request.client.host
|
| 85 |
now = datetime.now()
|
| 86 |
window = timedelta(hours=24)
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
|
|
|
|
|
|
| 92 |
return True
|
| 93 |
|
| 94 |
def inference(prompt, hf_token, model, model_name):
|
|
@@ -213,4 +217,6 @@ with gr.Blocks(css=css, theme="NoCrypt/miku", js=js) as demo:
|
|
| 213 |
)
|
| 214 |
|
| 215 |
|
| 216 |
-
demo.launch(show_api=False)
|
|
|
|
|
|
|
|
|
| 4 |
from functools import partial
|
| 5 |
import gradio as gr
|
| 6 |
from huggingface_hub import InferenceClient
|
| 7 |
+
import threading
|
| 8 |
|
| 9 |
css = """
|
| 10 |
gradio-app {
|
|
|
|
| 80 |
"""
|
| 81 |
|
| 82 |
ip_requests = {}
|
| 83 |
+
ip_requests_lock = threading.Lock()
|
| 84 |
|
| 85 |
def allow_ip(request: gr.Request, show_error=True):
|
| 86 |
ip = request.client.host
|
| 87 |
now = datetime.now()
|
| 88 |
window = timedelta(hours=24)
|
| 89 |
+
|
| 90 |
+
with ip_requests_lock:
|
| 91 |
+
if ip in ip_requests:
|
| 92 |
+
ip_requests[ip] = [timestamp for timestamp in ip_requests[ip] if now - timestamp < window]
|
| 93 |
+
if len(ip_requests.get(ip, [])) >= 15:
|
| 94 |
+
raise gr.Error("Rate limit exceeded. Please try again tomorrow or use your Hugging Face Pro token.", visible=show_error)
|
| 95 |
+
ip_requests.setdefault(ip, []).append(now)
|
| 96 |
return True
|
| 97 |
|
| 98 |
def inference(prompt, hf_token, model, model_name):
|
|
|
|
| 217 |
)
|
| 218 |
|
| 219 |
|
| 220 |
+
demo.launch(show_api=False)
|
| 221 |
+
|
| 222 |
+
|