zhangjf commited on
Commit
feaf069
1 Parent(s): 8e72a75

support downloading updated data

Browse files
Files changed (2) hide show
  1. app.py +24 -18
  2. dialogues_set/qas.json +2 -2
app.py CHANGED
@@ -85,12 +85,11 @@ qs, qas = read_qs()
85
  start_time = time.time()
86
  num_read_qas = len(qas)
87
 
88
- def ask(query, max_attempt_times=3):
89
  answer = None
 
90
  attempt_times = 0
91
- while answer is None and attempt_times<max_attempt_times:
92
- wait_time = int(attempt_times*10)
93
- time.sleep(wait_time)
94
  try:
95
  answer = openai.ChatCompletion.create(
96
  model="gpt-3.5-turbo",
@@ -99,18 +98,19 @@ def ask(query, max_attempt_times=3):
99
  ]
100
  )["choices"][0]["message"]["content"]
101
  except Exception as e:
102
- print(e)
103
- if "You exceeded your current quota, please check your plan and billing details." in str(e):
104
- idx = openai.api_keys.index(openai.api_key)
105
- idx = (idx + 1) % len(openai.api_keys)
106
- openai.api_key = openai.api_keys[idx]
107
- attempt_times += 0
108
- print(f"switch api_key")
109
- elif "Request timed out: HTTPSConnectionPool(host='api.openai.com', port=443): Read timed out. (read timeout=600)" in str(e):
110
- attempt_times += 1000
111
- else:
112
- attempt_times += 1
113
- print(f"retry in {attempt_times*10} seconds...")
 
114
  return answer
115
 
116
 
@@ -122,7 +122,7 @@ def askingChatGPT(qs, qas, min_interval_seconds=3, max_interval_seconds=15, max_
122
  #a = ask(q)
123
  def ask_(q, timeout):
124
  executor = concurrent.futures.ThreadPoolExecutor()
125
- future = executor.submit(ask, q) # 提交函数调用任务
126
  try:
127
  a = future.result(timeout=timeout) # 等待函数调用任务完成,超时时间为30秒
128
  return a
@@ -170,9 +170,15 @@ def showcase(access_key):
170
 
171
 
172
  def download(access_key):
173
- if not access_key==os.getenv('access_key'):
174
  chatbot_ret = [(f"Your entered Access Key:<br>{access_key}<br>is incorrect.", f"So i cannot provide you any information in this private space.")]
175
  file_ret = gr.File.update(value=None, visible=False)
 
 
 
 
 
 
176
  else:
177
  chatbot_ret = [(f"Your entered Access Key is correct.", f"The file containing all processed query-responses ({len(qas)} in total) can be downloaded below.")]
178
  filename = f"qas-{len(qas)}.json"
 
85
  start_time = time.time()
86
  num_read_qas = len(qas)
87
 
88
+ def ask(query, timeout=600):
89
  answer = None
90
+ dead_time = time.time() + timeout
91
  attempt_times = 0
92
+ while answer is None and time.time()<dead_time:
 
 
93
  try:
94
  answer = openai.ChatCompletion.create(
95
  model="gpt-3.5-turbo",
 
98
  ]
99
  )["choices"][0]["message"]["content"]
100
  except Exception as e:
101
+ if time.time()<dead_time:
102
+ print(e)
103
+ if "You exceeded your current quota, please check your plan and billing details." in str(e):
104
+ idx = openai.api_keys.index(openai.api_key)
105
+ idx = (idx + 1) % len(openai.api_keys)
106
+ openai.api_key = openai.api_keys[idx]
107
+ attempt_times += 0
108
+ print(f"switch api_key")
109
+ else:
110
+ attempt_times += 1
111
+ wait_time = int(attempt_times*10)
112
+ time.sleep(wait_time)
113
+ print(f"retry in {attempt_times*10} seconds...")
114
  return answer
115
 
116
 
 
122
  #a = ask(q)
123
  def ask_(q, timeout):
124
  executor = concurrent.futures.ThreadPoolExecutor()
125
+ future = executor.submit(ask, q, timeout) # 提交函数调用任务
126
  try:
127
  a = future.result(timeout=timeout) # 等待函数调用任务完成,超时时间为30秒
128
  return a
 
170
 
171
 
172
  def download(access_key):
173
+ if not access_key.startswith(os.getenv('access_key')):
174
  chatbot_ret = [(f"Your entered Access Key:<br>{access_key}<br>is incorrect.", f"So i cannot provide you any information in this private space.")]
175
  file_ret = gr.File.update(value=None, visible=False)
176
+ elif: access_key == f"{os.getenv('access_key')}: update":
177
+ chatbot_ret = [(f"Your entered Access Key is correct.", f"The file containing new processed query-responses ({len(qas)-num_read_qas} in total) can be downloaded below.")]
178
+ filename = f"qas-{num_read_qas}-{len(qas)}.json"
179
+ with open(filename, "w", encoding="utf-8") as f:
180
+ f.write(json.dumps(qas[num_read_qas:], ensure_ascii=False, indent=2))
181
+ file_ret = gr.File.update(value=filename, visible=True)
182
  else:
183
  chatbot_ret = [(f"Your entered Access Key is correct.", f"The file containing all processed query-responses ({len(qas)} in total) can be downloaded below.")]
184
  filename = f"qas-{len(qas)}.json"
dialogues_set/qas.json CHANGED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:5d9eb4ab3d7f781cc43111beaebc295c888b625f02f61d6407a3f6e610c8f2a4
3
- size 24089657
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:2edd0de80b0cad6a4af7ce82d7042ceed8305f490ae56cc70c5615225abdcb7d
3
+ size 25058283