mkw18 commited on
Commit
9eec015
·
1 Parent(s): 4825645

rate limit

Browse files
Files changed (1) hide show
  1. app.py +6 -11
app.py CHANGED
@@ -102,21 +102,17 @@ def reset_user_input():
102
  return gr.update(value='')
103
 
104
 
105
- def rate_cnt():
106
- global host_cnt
107
- print('Resetting count')
108
- host_cnt = {}
109
-
110
  def reset_state(chatbot, messages, answer_output, answerBtn, answer, idx, question, show_ans, request: gr.Request):
111
  global host_cnt
112
  host = request.client.host
 
113
  if not host in host_cnt:
114
- host_cnt[host] = 1
115
  else:
116
- host_cnt[host] += 1
117
- print(host_cnt[host])
118
- if host_cnt[host] >= 10:
119
- return chatbot, messages, answer_output, answerBtn, answer, idx, question, show_ans
120
  data = {'refresh': ''}
121
  data=requests.post(os.environ.get("URL"), data=json.dumps(data, ensure_ascii=False).encode('utf-8')).content
122
  data = json.loads(str(data, encoding="utf-8"))
@@ -137,7 +133,6 @@ def show_hide_answer(answer, show_ans):
137
 
138
 
139
  host_cnt = {}
140
- schedule.every(1).minutes.do(rate_cnt)
141
 
142
  with gr.Blocks() as demo:
143
  gr.HTML("""<h1 align="center">海龟汤</h1>""")
 
102
  return gr.update(value='')
103
 
104
 
 
 
 
 
 
105
  def reset_state(chatbot, messages, answer_output, answerBtn, answer, idx, question, show_ans, request: gr.Request):
106
  global host_cnt
107
  host = request.client.host
108
+
109
  if not host in host_cnt:
110
+ host_cnt[host] = time.time()
111
  else:
112
+ elapse = time.time()-host_cnt[host]
113
+ if elapse < 10:
114
+ time.sleep(10-elapse)
115
+ host_cnt[host] = time.time()
116
  data = {'refresh': ''}
117
  data=requests.post(os.environ.get("URL"), data=json.dumps(data, ensure_ascii=False).encode('utf-8')).content
118
  data = json.loads(str(data, encoding="utf-8"))
 
133
 
134
 
135
  host_cnt = {}
 
136
 
137
  with gr.Blocks() as demo:
138
  gr.HTML("""<h1 align="center">海龟汤</h1>""")