周倬孚 commited on
Commit
507564e
·
1 Parent(s): d248439

add auto save and input limit

Browse files
Files changed (2) hide show
  1. app.py +58 -5
  2. save_data.py +9 -0
app.py CHANGED
@@ -114,6 +114,25 @@ def login(identification_code):
114
  return update_content(user_info["group"])
115
  else:
116
  return update_content(None)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
117
 
118
 
119
  if __name__ == "__main__":
@@ -157,7 +176,7 @@ if __name__ == "__main__":
157
 
158
  with group_a_content:
159
  with gr.Row():
160
- human_input = gr.Textbox(label="Human Input")
161
  with gr.Row():
162
  submit_btn = gr.Button("Create")
163
  with gr.Row():
@@ -165,7 +184,7 @@ if __name__ == "__main__":
165
  session_index = gr.Number(label="Session Index", visible=False)
166
 
167
  submit_btn.click(
168
- fn=lambda task, human_input, id: handle_create_sequential(task, human_input, session_manager, api_key, id),
169
  inputs=[task, human_input, identification_code],
170
  outputs=[ai_output, session_index]
171
  )
@@ -191,7 +210,7 @@ if __name__ == "__main__":
191
 
192
  with group_b_content:
193
  with gr.Row():
194
- human_input = gr.Textbox(label="Human Input")
195
  with gr.Row():
196
  create_btn = gr.Button("Create")
197
  with gr.Row():
@@ -200,7 +219,7 @@ if __name__ == "__main__":
200
  session_index = gr.Number(label="Session Index", visible=False)
201
 
202
  create_btn.click(
203
- fn=lambda task, human_input, id: handle_create_parallel(task, human_input, session_manager, api_key, id),
204
  inputs=[task, human_input, identification_code],
205
  outputs=[ai_initial_output, final_output, session_index]
206
  )
@@ -270,4 +289,38 @@ if __name__ == "__main__":
270
  )
271
 
272
 
273
- app.launch(share=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
114
  return update_content(user_info["group"])
115
  else:
116
  return update_content(None)
117
+
118
+
119
+ def word_limit_validation(human_input):
120
+ words = human_input.split()
121
+ if len(words) < 50:
122
+ return "Error: Your input is less than 50 words."
123
+ return None # No error
124
+
125
+ def handle_create_with_limit(task, human_input, session_manager, api_key, id):
126
+ validation_error = word_limit_validation(human_input)
127
+ if validation_error:
128
+ return validation_error, None # Return the error to ai_output and keep session_index None
129
+ # If valid, proceed with task handling
130
+ if group_a_content:
131
+ return handle_create_sequential(task, human_input, session_manager, api_key, id)
132
+ elif group_b_content:
133
+ return handle_create_parallel(task, human_input, session_manager, api_key, id)
134
+ else:
135
+ return "Error: Invalid group", None
136
 
137
 
138
  if __name__ == "__main__":
 
176
 
177
  with group_a_content:
178
  with gr.Row():
179
+ human_input = gr.Textbox(label="Human Input (preperably between 100 and 150 words)")
180
  with gr.Row():
181
  submit_btn = gr.Button("Create")
182
  with gr.Row():
 
184
  session_index = gr.Number(label="Session Index", visible=False)
185
 
186
  submit_btn.click(
187
+ fn=lambda task, human_input, id: handle_create_with_limit(task, human_input, session_manager, api_key, id),
188
  inputs=[task, human_input, identification_code],
189
  outputs=[ai_output, session_index]
190
  )
 
210
 
211
  with group_b_content:
212
  with gr.Row():
213
+ human_input = gr.Textbox(label="Human Input (preperably between 100 and 150 words)")
214
  with gr.Row():
215
  create_btn = gr.Button("Create")
216
  with gr.Row():
 
219
  session_index = gr.Number(label="Session Index", visible=False)
220
 
221
  create_btn.click(
222
+ fn=lambda task, human_input, id: handle_create_with_limit(task, human_input, session_manager, api_key, id),
223
  inputs=[task, human_input, identification_code],
224
  outputs=[ai_initial_output, final_output, session_index]
225
  )
 
289
  )
290
 
291
 
292
+ # Add auto-save button
293
+
294
+ with gr.Row():
295
+ auto_save_btn = gr.Button("Auto Save", visible=False)
296
+ auto_save_result = gr.Label(visible=False)
297
+
298
+ def auto_save(session_index):
299
+ return save_data(session_index, session_manager, service, SHEET_ID1)
300
+
301
+ auto_save_btn.click(
302
+ fn=lambda session_index: auto_save(session_index),
303
+ inputs=[session_index],
304
+ outputs=[auto_save_result]
305
+ )
306
+
307
+ # Inject JavaScript to trigger the hidden save button every 30 seconds
308
+ auto_save_js = """
309
+ <script>
310
+ // Function to trigger the hidden save button
311
+ function triggerAutoSave() {
312
+ const saveButton = document.querySelector('button[title="Auto Save"]');
313
+ if (saveButton) {
314
+ saveButton.click();
315
+ }
316
+ }
317
+
318
+ // Trigger auto-save every 30 seconds
319
+ setInterval(triggerAutoSave, 30000); // 30000 milliseconds = 30 seconds
320
+ </script>
321
+ """
322
+
323
+ app.add_component(gr.HTML(auto_save_js))
324
+
325
+
326
+ app.launch(share=True)
save_data.py CHANGED
@@ -40,6 +40,9 @@ def col_letter(col_num):
40
  letter = chr(65 + remainder) + letter
41
  return letter
42
 
 
 
 
43
  def add_new_data(new_row, service, SPREADSHEET_ID, num_of_columns = 5):
44
  """Add new data to the spreadsheet.
45
  new_row: list of data to be added. """
@@ -51,6 +54,12 @@ def add_new_data(new_row, service, SPREADSHEET_ID, num_of_columns = 5):
51
  range=range_to_read
52
  ).execute()
53
  values = result.get('values', [])
 
 
 
 
 
 
54
  number_of_rows = len(values)
55
  new_row = [new_row]
56
  range_to_write = f'Sheet1!A{number_of_rows + 1}'
 
40
  letter = chr(65 + remainder) + letter
41
  return letter
42
 
43
+ def is_redudant_data(new_row, last_row):
44
+ return new_row == last_row
45
+
46
  def add_new_data(new_row, service, SPREADSHEET_ID, num_of_columns = 5):
47
  """Add new data to the spreadsheet.
48
  new_row: list of data to be added. """
 
54
  range=range_to_read
55
  ).execute()
56
  values = result.get('values', [])
57
+
58
+ if values:
59
+ last_row = values[-1]
60
+ if is_redudant_data(new_row, last_row):
61
+ return
62
+
63
  number_of_rows = len(values)
64
  new_row = [new_row]
65
  range_to_write = f'Sheet1!A{number_of_rows + 1}'