Spaces:
Running
Running
add limit
Browse files
app.py
CHANGED
@@ -3,6 +3,10 @@ from typing import List
|
|
3 |
from utils import get_base_answer, get_nudging_answer
|
4 |
from constant import js_code_label, custom_css, HEADER_MD, BASE_MODELS, NUDGING_MODELS
|
5 |
import datetime
|
|
|
|
|
|
|
|
|
6 |
|
7 |
addr_limit_counter = {}
|
8 |
LAST_UPDATE_TIME = datetime.datetime.now()
|
@@ -15,8 +19,24 @@ def respond_base(
|
|
15 |
message: str,
|
16 |
max_tokens: int,
|
17 |
base_model: str,
|
|
|
18 |
):
|
19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
def respond_nudging(
|
22 |
system_prompt: str,
|
@@ -28,10 +48,23 @@ def respond_nudging(
|
|
28 |
nudging_model: str,
|
29 |
request:gr.Request
|
30 |
):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
all_info = get_nudging_answer(base_model=base_model, nudging_model=nudging_model, system_prompt=system_prompt, question=message, max_token_total=max_tokens, top_prob_thres=nudging_thres)
|
32 |
all_completions = all_info["all_completions"]
|
33 |
nudging_words = all_info["all_nudging_words"]
|
34 |
formatted_response = format_response(all_completions, nudging_words)
|
|
|
|
|
|
|
35 |
return [(message, formatted_response)]
|
36 |
|
37 |
def clear_fn():
|
|
|
3 |
from utils import get_base_answer, get_nudging_answer
|
4 |
from constant import js_code_label, custom_css, HEADER_MD, BASE_MODELS, NUDGING_MODELS
|
5 |
import datetime
|
6 |
+
import logging
|
7 |
+
import urllib.request
|
8 |
+
# add logging info to console
|
9 |
+
logging.basicConfig(level=logging.INFO)
|
10 |
|
11 |
addr_limit_counter = {}
|
12 |
LAST_UPDATE_TIME = datetime.datetime.now()
|
|
|
19 |
message: str,
|
20 |
max_tokens: int,
|
21 |
base_model: str,
|
22 |
+
request:gr.Request
|
23 |
):
|
24 |
+
global LAST_UPDATE_TIME, addr_limit_counter
|
25 |
+
# if already 24 hours passed, reset the counter
|
26 |
+
if datetime.datetime.now() - LAST_UPDATE_TIME > datetime.timedelta(days=1):
|
27 |
+
addr_limit_counter = {}
|
28 |
+
LAST_UPDATE_TIME = datetime.datetime.now()
|
29 |
+
host_addr = request.client.host
|
30 |
+
if host_addr not in addr_limit_counter:
|
31 |
+
addr_limit_counter[host_addr] = 0
|
32 |
+
if addr_limit_counter[host_addr] > 1:
|
33 |
+
return gr.Error("You have reached the limit of 50 requests for today. Please use your own API key.")
|
34 |
+
|
35 |
+
base_answer = get_base_answer(base_model=base_model, system_prompt=system_prompt, question=message, max_tokens=max_tokens)
|
36 |
+
addr_limit_counter[host_addr] += 1
|
37 |
+
logging.info(f"Requesting chat completion from OpenAI API with model {base_model}")
|
38 |
+
logging.info(f"addr_limit_counter: {addr_limit_counter}; Last update time: {LAST_UPDATE_TIME};")
|
39 |
+
return [(message, base_answer)]
|
40 |
|
41 |
def respond_nudging(
|
42 |
system_prompt: str,
|
|
|
48 |
nudging_model: str,
|
49 |
request:gr.Request
|
50 |
):
|
51 |
+
global LAST_UPDATE_TIME, addr_limit_counter
|
52 |
+
# if already 24 hours passed, reset the counter
|
53 |
+
if datetime.datetime.now() - LAST_UPDATE_TIME > datetime.timedelta(days=1):
|
54 |
+
addr_limit_counter = {}
|
55 |
+
LAST_UPDATE_TIME = datetime.datetime.now()
|
56 |
+
host_addr = request.client.host
|
57 |
+
if host_addr not in addr_limit_counter:
|
58 |
+
addr_limit_counter[host_addr] = 0
|
59 |
+
if addr_limit_counter[host_addr] > 1:
|
60 |
+
return gr.Error("You have reached the limit of 50 requests for today. Please use your own API key.")
|
61 |
all_info = get_nudging_answer(base_model=base_model, nudging_model=nudging_model, system_prompt=system_prompt, question=message, max_token_total=max_tokens, top_prob_thres=nudging_thres)
|
62 |
all_completions = all_info["all_completions"]
|
63 |
nudging_words = all_info["all_nudging_words"]
|
64 |
formatted_response = format_response(all_completions, nudging_words)
|
65 |
+
addr_limit_counter[host_addr] += 1
|
66 |
+
logging.info(f"Requesting chat completion from OpenAI API with model {base_model} and {nudging_model}")
|
67 |
+
logging.info(f"addr_limit_counter: {addr_limit_counter}; Last update time: {LAST_UPDATE_TIME};")
|
68 |
return [(message, formatted_response)]
|
69 |
|
70 |
def clear_fn():
|