Spaces:
Running
Running
Kang Suhyun
commited on
Commit
•
af3d965
1
Parent(s):
58ea8e3
[#122] Replace cookie with local storage for token (#123)
Browse files- app.py +3 -2
- rate_limit.py +22 -9
- response.py +1 -6
app.py
CHANGED
@@ -80,7 +80,8 @@ css = """
|
|
80 |
"""
|
81 |
|
82 |
with gr.Blocks(title="Yanolja Arena", css=css) as app:
|
83 |
-
|
|
|
84 |
|
85 |
with gr.Row():
|
86 |
gr.HTML("""
|
@@ -188,7 +189,7 @@ with gr.Blocks(title="Yanolja Arena", css=css) as app:
|
|
188 |
]).then(fn=get_responses,
|
189 |
inputs=[
|
190 |
prompt_textarea, category_radio, source_language,
|
191 |
-
target_language
|
192 |
],
|
193 |
outputs=response_boxes + model_names + [instruction_state])
|
194 |
submit_event.success(fn=lambda: gr.Row(visible=True), outputs=vote_row)
|
|
|
80 |
"""
|
81 |
|
82 |
with gr.Blocks(title="Yanolja Arena", css=css) as app:
|
83 |
+
token = gr.Textbox(visible=False)
|
84 |
+
set_token(app, token)
|
85 |
|
86 |
with gr.Row():
|
87 |
gr.HTML("""
|
|
|
189 |
]).then(fn=get_responses,
|
190 |
inputs=[
|
191 |
prompt_textarea, category_radio, source_language,
|
192 |
+
target_language, token
|
193 |
],
|
194 |
outputs=response_boxes + model_names + [instruction_state])
|
195 |
submit_event.success(fn=lambda: gr.Row(visible=True), outputs=vote_row)
|
rate_limit.py
CHANGED
@@ -55,7 +55,7 @@ class RateLimiter:
|
|
55 |
self.scheduler.start()
|
56 |
|
57 |
def check_rate_limit(self, token: str):
|
58 |
-
if not token or
|
59 |
raise InvalidTokenException()
|
60 |
|
61 |
if (datetime.now() - self.last_request_times[token]).seconds < 5:
|
@@ -70,6 +70,9 @@ class RateLimiter:
|
|
70 |
def initialize_request(self, token: str):
|
71 |
self.last_request_times[token] = datetime.min
|
72 |
|
|
|
|
|
|
|
73 |
def _remove_old_tokens(self):
|
74 |
for token, last_request_time in dict(self.last_request_times).items():
|
75 |
if (datetime.now() - last_request_time).days >= 1:
|
@@ -82,23 +85,33 @@ class RateLimiter:
|
|
82 |
rate_limiter = RateLimiter()
|
83 |
|
84 |
|
85 |
-
def set_token(app: gr.Blocks):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
86 |
|
87 |
-
def set_token_server():
|
88 |
new_token = uuid4().hex
|
89 |
rate_limiter.initialize_request(new_token)
|
90 |
return new_token
|
91 |
|
92 |
-
|
93 |
function(newToken) {
|
94 |
-
|
95 |
-
document.cookie = `arena_token=${newToken}; expires=${expiresDateString};`;
|
96 |
}
|
97 |
"""
|
98 |
|
99 |
-
|
100 |
-
|
101 |
-
|
|
|
|
|
102 |
|
103 |
|
104 |
def signal_handler(sig, frame):
|
|
|
55 |
self.scheduler.start()
|
56 |
|
57 |
def check_rate_limit(self, token: str):
|
58 |
+
if not token or not self.token_exists(token):
|
59 |
raise InvalidTokenException()
|
60 |
|
61 |
if (datetime.now() - self.last_request_times[token]).seconds < 5:
|
|
|
70 |
def initialize_request(self, token: str):
|
71 |
self.last_request_times[token] = datetime.min
|
72 |
|
73 |
+
def token_exists(self, token: str):
|
74 |
+
return token in self.last_request_times
|
75 |
+
|
76 |
def _remove_old_tokens(self):
|
77 |
for token, last_request_time in dict(self.last_request_times).items():
|
78 |
if (datetime.now() - last_request_time).days >= 1:
|
|
|
85 |
rate_limiter = RateLimiter()
|
86 |
|
87 |
|
88 |
+
def set_token(app: gr.Blocks, token: gr.Textbox):
|
89 |
+
|
90 |
+
get_client_token = """
|
91 |
+
function() {
|
92 |
+
return localStorage.getItem("arena_token");
|
93 |
+
}
|
94 |
+
"""
|
95 |
+
|
96 |
+
def set_server_token(existing_token):
|
97 |
+
if existing_token and rate_limiter.token_exists(existing_token):
|
98 |
+
return existing_token
|
99 |
|
|
|
100 |
new_token = uuid4().hex
|
101 |
rate_limiter.initialize_request(new_token)
|
102 |
return new_token
|
103 |
|
104 |
+
set_client_token = """
|
105 |
function(newToken) {
|
106 |
+
localStorage.setItem("arena_token", newToken);
|
|
|
107 |
}
|
108 |
"""
|
109 |
|
110 |
+
app.load(fn=set_server_token,
|
111 |
+
js=get_client_token,
|
112 |
+
inputs=[token],
|
113 |
+
outputs=[token])
|
114 |
+
token.change(fn=lambda _: None, js=set_client_token, inputs=[token])
|
115 |
|
116 |
|
117 |
def signal_handler(sig, frame):
|
response.py
CHANGED
@@ -3,7 +3,6 @@ This module contains functions for generating responses using LLMs.
|
|
3 |
"""
|
4 |
|
5 |
import enum
|
6 |
-
from http import cookies
|
7 |
import logging
|
8 |
from random import sample
|
9 |
from typing import List
|
@@ -66,7 +65,7 @@ def get_instruction(category: str, model: Model, source_lang: str,
|
|
66 |
|
67 |
|
68 |
def get_responses(prompt: str, category: str, source_lang: str,
|
69 |
-
target_lang: str,
|
70 |
if not category:
|
71 |
raise gr.Error("Please select a category.")
|
72 |
|
@@ -74,10 +73,6 @@ def get_responses(prompt: str, category: str, source_lang: str,
|
|
74 |
not target_lang):
|
75 |
raise gr.Error("Please select source and target languages.")
|
76 |
|
77 |
-
cookie = cookies.SimpleCookie()
|
78 |
-
cookie.load(request.headers["cookie"])
|
79 |
-
token = cookie["arena_token"].value if "arena_token" in cookie else None
|
80 |
-
|
81 |
try:
|
82 |
rate_limiter.check_rate_limit(token)
|
83 |
except rate_limit.InvalidTokenException as e:
|
|
|
3 |
"""
|
4 |
|
5 |
import enum
|
|
|
6 |
import logging
|
7 |
from random import sample
|
8 |
from typing import List
|
|
|
65 |
|
66 |
|
67 |
def get_responses(prompt: str, category: str, source_lang: str,
|
68 |
+
target_lang: str, token: str):
|
69 |
if not category:
|
70 |
raise gr.Error("Please select a category.")
|
71 |
|
|
|
73 |
not target_lang):
|
74 |
raise gr.Error("Please select source and target languages.")
|
75 |
|
|
|
|
|
|
|
|
|
76 |
try:
|
77 |
rate_limiter.check_rate_limit(token)
|
78 |
except rate_limit.InvalidTokenException as e:
|