liujch1998 commited on
Commit
62bce9d
β€’
1 Parent(s): 15a601f

Async git push

Browse files
Files changed (1) hide show
  1. app.py +23 -19
app.py CHANGED
@@ -6,6 +6,7 @@ import huggingface_hub
6
  import datetime
7
  import json
8
  import shutil
 
9
 
10
  device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')
11
 
@@ -34,6 +35,8 @@ repo = huggingface_hub.Repository(
34
  )
35
  repo.git_pull()
36
 
 
 
37
  class Interactive:
38
  def __init__(self):
39
  self.tokenizer = transformers.AutoTokenizer.from_pretrained(MODEL_NAME, use_auth_token=HF_TOKEN_DOWNLOAD)
@@ -150,20 +153,15 @@ def predict(statements, do_saves):
150
  'False': 1 - output_raw['score_calibrated'],
151
  } for output_raw in output_raws]
152
  print('Logging statements to dataset:')
 
153
  for output_raw, do_save in zip(output_raws, do_saves):
154
  if do_save:
155
  print(output_raw)
156
  with open(DATA_PATH, 'a') as f:
157
  json.dump(output_raw, f, ensure_ascii=False)
158
  f.write('\n')
159
- if any(do_saves):
160
- try:
161
- commit_url = repo.push_to_hub()
162
- except Exception as e:
163
- print('Failed to push to GitHub:', e)
164
- repo.git_pull(rebase=True)
165
- # print('Commit URL:', commit_url)
166
- # print()
167
  return outputs, output_raws, \
168
  [gr.update(visible=False) for _ in statements], \
169
  [gr.update(visible=True) for _ in statements], \
@@ -172,6 +170,7 @@ def predict(statements, do_saves):
172
 
173
  def record_feedback(output_raws, feedback, do_saves):
174
  print('Logging feedbacks to dataset:')
 
175
  for output_raw, do_save in zip(output_raws, do_saves):
176
  if do_save:
177
  output_raw.update({ 'feedback': feedback })
@@ -179,14 +178,8 @@ def record_feedback(output_raws, feedback, do_saves):
179
  with open(DATA_PATH, 'a') as f:
180
  json.dump(output_raw, f, ensure_ascii=False)
181
  f.write('\n')
182
- if any(do_saves):
183
- try:
184
- commit_url = repo.push_to_hub()
185
- except Exception as e:
186
- print('Failed to push to GitHub:', e)
187
- repo.git_pull(rebase=True)
188
- # print('Commit URL:', commit_url)
189
- # print()
190
  return [gr.update(visible=True) for _ in output_raws], \
191
  [gr.update(visible=False) for _ in output_raws], \
192
  [gr.update(visible=False) for _ in output_raws], \
@@ -196,6 +189,18 @@ def record_feedback_agree(output_raws, do_saves):
196
  def record_feedback_disagree(output_raws, do_saves):
197
  return record_feedback(output_raws, 'disagree', do_saves)
198
 
 
 
 
 
 
 
 
 
 
 
 
 
199
  examples = [
200
  # # openbookqa
201
  # 'If a person walks in the opposite direction of a compass arrow they are walking south.',
@@ -320,7 +325,6 @@ with gr.Blocks() as demo:
320
  feedback_agree.click(record_feedback_agree, inputs=[output_raw, do_save], outputs=[submit, feedback_agree, feedback_disagree, feedback_ack], batch=True, max_batch_size=16)
321
  feedback_disagree.click(record_feedback_disagree, inputs=[output_raw, do_save], outputs=[submit, feedback_agree, feedback_disagree, feedback_ack], batch=True, max_batch_size=16)
322
 
323
- demo.queue(concurrency_count=1).launch(debug=True)
324
 
325
- # Concurrency, Batching
326
- # Theme, CSS
 
6
  import datetime
7
  import json
8
  import shutil
9
+ import threading
10
 
11
  device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')
12
 
 
35
  )
36
  repo.git_pull()
37
 
38
+ lock = threading.Lock()
39
+
40
  class Interactive:
41
  def __init__(self):
42
  self.tokenizer = transformers.AutoTokenizer.from_pretrained(MODEL_NAME, use_auth_token=HF_TOKEN_DOWNLOAD)
 
153
  'False': 1 - output_raw['score_calibrated'],
154
  } for output_raw in output_raws]
155
  print('Logging statements to dataset:')
156
+ lock.acquire()
157
  for output_raw, do_save in zip(output_raws, do_saves):
158
  if do_save:
159
  print(output_raw)
160
  with open(DATA_PATH, 'a') as f:
161
  json.dump(output_raw, f, ensure_ascii=False)
162
  f.write('\n')
163
+ print()
164
+ lock.release()
 
 
 
 
 
 
165
  return outputs, output_raws, \
166
  [gr.update(visible=False) for _ in statements], \
167
  [gr.update(visible=True) for _ in statements], \
 
170
 
171
  def record_feedback(output_raws, feedback, do_saves):
172
  print('Logging feedbacks to dataset:')
173
+ lock.acquire()
174
  for output_raw, do_save in zip(output_raws, do_saves):
175
  if do_save:
176
  output_raw.update({ 'feedback': feedback })
 
178
  with open(DATA_PATH, 'a') as f:
179
  json.dump(output_raw, f, ensure_ascii=False)
180
  f.write('\n')
181
+ print()
182
+ lock.release()
 
 
 
 
 
 
183
  return [gr.update(visible=True) for _ in output_raws], \
184
  [gr.update(visible=False) for _ in output_raws], \
185
  [gr.update(visible=False) for _ in output_raws], \
 
189
  def record_feedback_disagree(output_raws, do_saves):
190
  return record_feedback(output_raws, 'disagree', do_saves)
191
 
192
+ def push():
193
+ lock.acquire()
194
+ if repo.is_repo_clean():
195
+ print('No new data recorded, skipping git push ...')
196
+ else:
197
+ try:
198
+ commit_url = repo.push_to_hub()
199
+ except Exception as e:
200
+ print('Failed to push to git:', e)
201
+ repo.git_pull(rebase=True)
202
+ lock.release()
203
+
204
  examples = [
205
  # # openbookqa
206
  # 'If a person walks in the opposite direction of a compass arrow they are walking south.',
 
325
  feedback_agree.click(record_feedback_agree, inputs=[output_raw, do_save], outputs=[submit, feedback_agree, feedback_disagree, feedback_ack], batch=True, max_batch_size=16)
326
  feedback_disagree.click(record_feedback_disagree, inputs=[output_raw, do_save], outputs=[submit, feedback_agree, feedback_disagree, feedback_ack], batch=True, max_batch_size=16)
327
 
328
+ demo.load(push, inputs=None, outputs=None, every=60) # Push to git every 60 seconds
329
 
330
+ demo.queue(concurrency_count=1).launch(debug=True)