Spaces:
Running
Running
add rate limit
Browse files
app.py
CHANGED
@@ -6,19 +6,18 @@ import urllib.request
|
|
6 |
from utils import model_name_mapping, urial_template, openai_base_request, DEFAULT_API_KEY
|
7 |
from constant import js_code_label, HEADER_MD
|
8 |
from openai import OpenAI
|
9 |
-
|
10 |
# add logging info to console
|
11 |
logging.basicConfig(level=logging.INFO)
|
12 |
|
13 |
-
|
14 |
-
|
15 |
URIAL_VERSION = "inst_1k_v4.help"
|
16 |
-
|
17 |
URIAL_URL = f"https://raw.githubusercontent.com/Re-Align/URIAL/main/urial_prompts/{URIAL_VERSION}.txt"
|
18 |
urial_prompt = urllib.request.urlopen(URIAL_URL).read().decode('utf-8')
|
19 |
urial_prompt = urial_prompt.replace("```", '"""') # new version of URIAL uses """ instead of ```
|
20 |
STOP_STRS = ['"""', '# Query:', '# Answer:']
|
21 |
|
|
|
|
|
22 |
|
23 |
def respond(
|
24 |
message,
|
@@ -28,9 +27,10 @@ def respond(
|
|
28 |
top_p,
|
29 |
rp,
|
30 |
model_name,
|
31 |
-
together_api_key
|
|
|
32 |
):
|
33 |
-
global STOP_STRS, urial_prompt
|
34 |
rp = 1.0
|
35 |
prompt = urial_template(urial_prompt, history, message)
|
36 |
|
@@ -42,14 +42,29 @@ def respond(
|
|
42 |
else:
|
43 |
api_key = DEFAULT_API_KEY
|
44 |
|
45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
temperature=temperature,
|
47 |
max_tokens=max_tokens,
|
48 |
top_p=top_p,
|
49 |
repetition_penalty=rp,
|
50 |
stop=STOP_STRS, api_key=api_key)
|
|
|
|
|
|
|
|
|
51 |
response = ""
|
52 |
-
for msg in
|
53 |
# print(msg.choices[0].delta.keys())
|
54 |
token = msg.choices[0].delta["content"]
|
55 |
should_stop = False
|
@@ -91,5 +106,5 @@ with gr.Blocks(gr.themes.Soft(), js=js_code_label) as demo:
|
|
91 |
chat.chatbot.height = 550
|
92 |
chat.chatbot.show_copy_button = True
|
93 |
|
94 |
-
if __name__ == "__main__":
|
95 |
demo.launch(show_api=False)
|
|
|
6 |
from utils import model_name_mapping, urial_template, openai_base_request, DEFAULT_API_KEY
|
7 |
from constant import js_code_label, HEADER_MD
|
8 |
from openai import OpenAI
|
9 |
+
import datetime
|
10 |
# add logging info to console
|
11 |
logging.basicConfig(level=logging.INFO)
|
12 |
|
|
|
|
|
13 |
URIAL_VERSION = "inst_1k_v4.help"
|
|
|
14 |
URIAL_URL = f"https://raw.githubusercontent.com/Re-Align/URIAL/main/urial_prompts/{URIAL_VERSION}.txt"
|
15 |
urial_prompt = urllib.request.urlopen(URIAL_URL).read().decode('utf-8')
|
16 |
urial_prompt = urial_prompt.replace("```", '"""') # new version of URIAL uses """ instead of ```
|
17 |
STOP_STRS = ['"""', '# Query:', '# Answer:']
|
18 |
|
19 |
+
addr_limit_counter = {}
|
20 |
+
LAST_UPDATE_TIME = datetime.datetime.now()
|
21 |
|
22 |
def respond(
|
23 |
message,
|
|
|
27 |
top_p,
|
28 |
rp,
|
29 |
model_name,
|
30 |
+
together_api_key,
|
31 |
+
request:gr.Request
|
32 |
):
|
33 |
+
global STOP_STRS, urial_prompt, LAST_UPDATE_TIME, addr_limit_counter
|
34 |
rp = 1.0
|
35 |
prompt = urial_template(urial_prompt, history, message)
|
36 |
|
|
|
42 |
else:
|
43 |
api_key = DEFAULT_API_KEY
|
44 |
|
45 |
+
# headers = request.headers
|
46 |
+
# if already 24 hours passed, reset the counter
|
47 |
+
if datetime.datetime.now() - LAST_UPDATE_TIME > datetime.timedelta(days=1):
|
48 |
+
addr_limit_counter = {}
|
49 |
+
LAST_UPDATE_TIME = datetime.datetime.now()
|
50 |
+
host_addr = request.client.host
|
51 |
+
if host_addr not in addr_limit_counter:
|
52 |
+
addr_limit_counter[host_addr] = 0
|
53 |
+
if addr_limit_counter[host_addr] > 100:
|
54 |
+
return "You have reached the limit of 100 requests for today. Please use your own API key."
|
55 |
+
|
56 |
+
infer_request = openai_base_request(prompt=prompt, model=_model_name,
|
57 |
temperature=temperature,
|
58 |
max_tokens=max_tokens,
|
59 |
top_p=top_p,
|
60 |
repetition_penalty=rp,
|
61 |
stop=STOP_STRS, api_key=api_key)
|
62 |
+
addr_limit_counter[host_addr] += 1
|
63 |
+
logging.info(f"Requesting chat completion from OpenAI API with model {_model_name}")
|
64 |
+
logging.info(f"addr_limit_counter: {addr_limit_counter}; Last update time: {LAST_UPDATE_TIME};")
|
65 |
+
|
66 |
response = ""
|
67 |
+
for msg in infer_request:
|
68 |
# print(msg.choices[0].delta.keys())
|
69 |
token = msg.choices[0].delta["content"]
|
70 |
should_stop = False
|
|
|
106 |
chat.chatbot.height = 550
|
107 |
chat.chatbot.show_copy_button = True
|
108 |
|
109 |
+
if __name__ == "__main__":
|
110 |
demo.launch(show_api=False)
|