Randa commited on
Commit
06b173c
·
verified ·
1 Parent(s): 029ffaa

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +94 -88
app.py CHANGED
@@ -86,49 +86,47 @@ def completed_tests(test):
86
  # Tests distribution over users
87
  import random
88
 
89
- # Global set to store all available tests
90
- categories_and_tests = choices
91
- remaining_tests = set(get_unique_tests(categories_and_tests))
92
-
93
- def assign_tests(categories_and_tests, num_tests):
94
  global remaining_tests
95
  assigned_tests = []
96
- tests = get_unique_tests(categories_and_tests)
97
 
98
  # Shuffle tests
99
  random.shuffle(tests)
100
 
 
 
 
 
 
 
101
  # Last remaining tests are assigned to the last user
102
  if num_tests > len(remaining_tests):
103
- selected_tests = remaining_tests
104
 
105
  # Ensure we still have enough tests
106
  if len(remaining_tests) == 0:
107
  print("***** Tests Completed *****")
108
- #return ["Tests Completed"]
109
 
110
  # Select tests that are not already assigned
111
  assigned_tests = [test for test in tests if test in remaining_tests][:num_tests]
112
 
113
  # Remove assigned tests from the set of all available tests
114
- remaining_tests -= set(assigned_tests)
115
-
116
- # # Update assigned tests set
117
- # if len(assigned_tests) != 0:
118
- # assigned_tests.append(selected_tests)
119
-
120
  return assigned_tests
121
-
122
  # Tracking user progress
 
123
  def test_progress(test):
124
  global completed_tests_list
 
125
 
126
  # Get total completed tests
127
  total_completed_tests, completed_tests_list = completed_tests(test)
128
- completed_tests_text = "\n".join(completed_tests_list)
129
-
130
  # Test completed to be displayed to the user
131
- len_assigned_test = len(assign_tests(categories_and_tests, num_tests))
132
 
133
  # Calculate how many tests are remaining
134
  total_remaining_tests = len_assigned_test - total_completed_tests
@@ -137,20 +135,28 @@ def test_progress(test):
137
  completed_tests_list.clear()
138
  progress_text = f"Congratulations! You have completed all tests"
139
  else:
140
- progress_text = f"You have completed {total_completed_tests} tests. {total_remaining_tests} tests are remaining"
141
  return progress_text, completed_tests_text
142
 
143
-
144
  # Authentication function
145
  def update_message(request: gr.Request):
146
  return f"Welcome, {request.username}"
147
 
 
148
  # Gradio apps
149
 
150
  # Get test categories
151
- data_list = choices
152
- image_path="all_imgs/"
153
  completed_tests_list = []
 
 
 
 
 
 
 
 
154
 
155
  # Flagging
156
  callback = gr.CSVLogger()
@@ -216,10 +222,6 @@ with demo :
216
  """
217
  )
218
 
219
- # Function to get the corresponding values of correct, close and wrong text and image for each test
220
- num_tests = 10 # number of tests assigned per user
221
- assigned_tests = assign_tests(data_list, num_tests)
222
-
223
  # Assign value to each step
224
  def get_test_text(test):
225
  correct, close, wrong, image = get_values_image_for_test(data_list, test)
@@ -233,81 +235,85 @@ with demo :
233
  _, completed_tests = gr.TextArea(completed_tests(test))
234
  return completed_tests
235
 
236
- with gr.Row(elem_classes="row"):
237
- with gr.Column():
238
- image = gr.Image(label="Written Form", elem_id="img", container=False, show_label=True, show_download_button=False)
239
-
240
  with gr.Row():
241
- # First column
242
- with gr.Column():
243
 
244
  # User progress
245
- progress_text = gr.Textbox(info=" Your progress will appear here ", interactive=False, container=False, elem_id="progress_text", elem_classes="drop_color")
246
-
 
247
  # Test selection
248
  tests_list = assigned_tests
249
  test_dropdown = gr.Dropdown(tests_list, label="Tests", info="Select a test", scale=1, elem_id="test_color", elem_classes="drop_color")
250
- textarea_completed = gr.TextArea(info=" Your completed tests will appear here ", interactive=False, rtl=True, container=True, show_label=False, elem_classes="drop_color")
251
  test_dropdown.select(test_progress, test_dropdown, outputs=[progress_text, textarea_completed])
252
 
253
- with gr.Column():
254
- correct_text = gr.Textbox(label= "Step 1", info="Correct Text", interactive=False, rtl=True)
255
- correct_audio_record = gr.Audio(sources="microphone" ,type="filepath", label="Record Audio", show_label=False)
256
- correct_trans_text = gr.Textbox(info="Transcription", interactive=False, rtl=True, show_label=False)
257
- correct_score = gr.Textbox(label="Score", visible= False)
258
- correct_audio_record.stop_recording(fn=transcribe, inputs=[correct_audio_record, correct_text], outputs=[correct_trans_text, correct_score])
259
-
260
- # Third column
261
- with gr.Column():
262
- close_text = gr.Textbox(label="Step 2", info="Close Text", interactive=False, rtl=True)
263
- close_audio_record = gr.Audio(sources="microphone", type="filepath", label="Record Audio", show_label=False)
264
- close_trans_text = gr.Textbox(info="Transcription", interactive=False, rtl=True, show_label=False)
265
- close_score = gr.Textbox(label="Score", visible= False)
266
- close_audio_record.stop_recording(fn=transcribe, inputs=[close_audio_record, close_text], outputs=[close_trans_text, close_score])
267
-
268
- # Fourth column
269
- with gr.Column():
270
- wrong_text = gr.Textbox(label="Step 3", info="Wrong Text", interactive=False, rtl=True)
271
- wrong_audio_record = gr.Audio(sources="microphone" ,type="filepath", label="Record Audio", show_label=False)
272
- wrong_trans_text = gr.Textbox(info="Transcription", interactive=False, rtl=True, show_label=False)
273
- wrong_score = gr.Textbox(label="Score", visible= False)
274
- wrong_audio_record.stop_recording(fn=transcribe, inputs=[wrong_audio_record, wrong_text], outputs=[wrong_trans_text, wrong_score])
275
 
 
276
  with gr.Row():
277
- #user_name = gr.Request.username
278
- def save_outputs(correct_text, correct_trans_text, correct_score, correct_audio_record,
279
- close_text, close_trans_text, close_score, close_audio_record,
280
- wrong_text, wrong_trans_text, wrong_score, wrong_audio_record):
281
- if any(len(text)==0 for text in [correct_trans_text, close_trans_text, wrong_trans_text]):
282
- return gr.Warning("Please complete the test before saving")
283
- return (lambda *args: callback.flag(args, username="randa"))
284
-
285
- flag_btn = gr.Button(value="Save this test", variant="primary", scale=2)
286
- # Setup the components to flag
287
- callback.setup([correct_text, correct_trans_text, correct_score, correct_audio_record,
288
- close_text, close_trans_text, close_score, close_audio_record,
289
- wrong_text, wrong_trans_text, wrong_score, wrong_audio_record], "flagged_data")
290
- # We can choose which components to flag
291
- flag_btn.click(save_outputs,
292
- [correct_text, correct_trans_text, correct_score, correct_audio_record,
293
- close_text, close_trans_text, close_score, close_audio_record,
294
- wrong_text, wrong_trans_text, wrong_score, wrong_audio_record],
295
- None, preprocess=False)
296
-
297
- # Update test values according to the selected test
298
- test_dropdown.input(get_test_text, test_dropdown, [correct_text, close_text, wrong_text, image])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
299
 
300
 
301
  # Clear the transcription after selecting a new test
302
  test_dropdown.select(lambda: [None]*10, None,
303
  outputs=[correct_text, close_text, wrong_text, correct_audio_record, close_audio_record, wrong_audio_record, correct_trans_text, close_trans_text, wrong_trans_text, image],
304
  queue=False)
305
-
306
-
307
- #demo.launch(inbrowser=True)
308
- demo.launch(auth=[("randa", "randa"), ("randa1", "randa1"),("randa2", "randa2"), ("randa3", "randa3"),
309
- ("karim1", "karim1"), ("karim2", "karim2"), ("karim3", "karim3"),
310
- ("yassir1", "yassir1"), ("yassir2", "yassir2"), ("yassir3", "yassir3"),
311
- ("mehdi", "mehdi")],
312
- share=True, inbrowser=True)
313
-
 
86
  # Tests distribution over users
87
  import random
88
 
89
+ def assign_tests(num_tests):
 
 
 
 
90
  global remaining_tests
91
  assigned_tests = []
92
+ tests = get_unique_tests(full_test_list)
93
 
94
  # Shuffle tests
95
  random.shuffle(tests)
96
 
97
+ # Updating remaining_tests
98
+ if (len(remaining_tests) == 0) and (len(assigned_tests) == 0):
99
+ remaining_tests = set(tests)
100
+ # else:
101
+ # remaining_tests -= set(assigned_tests)
102
+
103
  # Last remaining tests are assigned to the last user
104
  if num_tests > len(remaining_tests):
105
+ assigned_tests = remaining_tests
106
 
107
  # Ensure we still have enough tests
108
  if len(remaining_tests) == 0:
109
  print("***** Tests Completed *****")
110
+ pass#return ["Tests Completed"]
111
 
112
  # Select tests that are not already assigned
113
  assigned_tests = [test for test in tests if test in remaining_tests][:num_tests]
114
 
115
  # Remove assigned tests from the set of all available tests
116
+ remaining_tests -= set(assigned_tests)
 
 
 
 
 
117
  return assigned_tests
118
+
119
  # Tracking user progress
120
+
121
  def test_progress(test):
122
  global completed_tests_list
123
+ global len_assigned_test
124
 
125
  # Get total completed tests
126
  total_completed_tests, completed_tests_list = completed_tests(test)
127
+
 
128
  # Test completed to be displayed to the user
129
+ completed_tests_text = "\n".join(completed_tests_list)
130
 
131
  # Calculate how many tests are remaining
132
  total_remaining_tests = len_assigned_test - total_completed_tests
 
135
  completed_tests_list.clear()
136
  progress_text = f"Congratulations! You have completed all tests"
137
  else:
138
+ progress_text = f"Completed tests {total_completed_tests} . Remaining {total_remaining_tests}"
139
  return progress_text, completed_tests_text
140
 
 
141
  # Authentication function
142
  def update_message(request: gr.Request):
143
  return f"Welcome, {request.username}"
144
 
145
+
146
  # Gradio apps
147
 
148
  # Get test categories
149
+ data_list = full_test_list
150
+ image_path="/all_imgs/"
151
  completed_tests_list = []
152
+ remaining_tests = []
153
+
154
+ # Get a set of tests for each user
155
+ num_tests = 10
156
+ assigned_tests = assign_tests(num_tests)
157
+ len_assigned_test = len(assigned_tests)
158
+
159
+ print(assigned_tests)
160
 
161
  # Flagging
162
  callback = gr.CSVLogger()
 
222
  """
223
  )
224
 
 
 
 
 
225
  # Assign value to each step
226
  def get_test_text(test):
227
  correct, close, wrong, image = get_values_image_for_test(data_list, test)
 
235
  _, completed_tests = gr.TextArea(completed_tests(test))
236
  return completed_tests
237
 
 
 
 
 
238
  with gr.Row():
239
+ # First big column
240
+ with gr.Column(scale=1):
241
 
242
  # User progress
243
+ progress_text = gr.Textbox(interactive=False, show_label=False, container=True, elem_id="progress_text", elem_classes="drop_color")
244
+ textarea_completed = gr.TextArea(info=" Your completed tests will appear here ", interactive=False, rtl=True, container=True, show_label=False, elem_classes="drop_color")
245
+
246
  # Test selection
247
  tests_list = assigned_tests
248
  test_dropdown = gr.Dropdown(tests_list, label="Tests", info="Select a test", scale=1, elem_id="test_color", elem_classes="drop_color")
 
249
  test_dropdown.select(test_progress, test_dropdown, outputs=[progress_text, textarea_completed])
250
 
251
+ # Second big column
252
+ with gr.Column(scale=3):
253
+ # Fisrt Row for image
254
+ with gr.Row():
255
+ image = gr.Image(elem_id="img", elem_classes="row", container=False, show_label=True, show_download_button=False)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
256
 
257
+ # Second row for steps
258
  with gr.Row():
259
+ # First sub column
260
+ with gr.Column():
261
+ correct_text = gr.Textbox(label= "Step 1", info="Correct Text", interactive=False, rtl=True)
262
+ correct_audio_record = gr.Audio(sources="microphone" ,type="filepath", label="Record Audio", show_label=False)
263
+ correct_trans_text = gr.Textbox(info="Transcription", interactive=False, rtl=True, show_label=False)
264
+ correct_score = gr.Textbox(label="Score", visible= False)
265
+ correct_audio_record.stop_recording(fn=transcribe, inputs=[correct_audio_record, correct_text], outputs=[correct_trans_text, correct_score])
266
+
267
+ # Second column
268
+ with gr.Column():
269
+ close_text = gr.Textbox(label="Step 2", info="Close Text", interactive=False, rtl=True)
270
+ close_audio_record = gr.Audio(sources="microphone", type="filepath", label="Record Audio", show_label=False)
271
+ close_trans_text = gr.Textbox(info="Transcription", interactive=False, rtl=True, show_label=False)
272
+ close_score = gr.Textbox(label="Score", visible= False)
273
+ close_audio_record.stop_recording(fn=transcribe, inputs=[close_audio_record, close_text], outputs=[close_trans_text, close_score])
274
+
275
+ # Fourth column
276
+ with gr.Column():
277
+ wrong_text = gr.Textbox(label="Step 3", info="Wrong Text", interactive=False, rtl=True)
278
+ wrong_audio_record = gr.Audio(sources="microphone" ,type="filepath", label="Record Audio", show_label=False)
279
+ wrong_trans_text = gr.Textbox(info="Transcription", interactive=False, rtl=True, show_label=False)
280
+ wrong_score = gr.Textbox(label="Score", visible= False)
281
+ wrong_audio_record.stop_recording(fn=transcribe, inputs=[wrong_audio_record, wrong_text], outputs=[wrong_trans_text, wrong_score])
282
+
283
+ # Third row for flag
284
+ with gr.Row():
285
+ #user_name = gr.Request.username
286
+ def save_outputs(correct_text, correct_trans_text, correct_score, correct_audio_record,
287
+ close_text, close_trans_text, close_score, close_audio_record,
288
+ wrong_text, wrong_trans_text, wrong_score, wrong_audio_record):
289
+ if any(len(text)==0 for text in [correct_trans_text, close_trans_text, wrong_trans_text]):
290
+ return gr.Warning("Please complete the test before saving")
291
+ return (lambda *args: callback.flag(args, username="randa"))
292
+
293
+ flag_btn = gr.Button(value="Save this test", variant="primary", scale=2)
294
+ # Setup the components to flag
295
+ callback.setup([correct_text, correct_trans_text, correct_score, correct_audio_record,
296
+ close_text, close_trans_text, close_score, close_audio_record,
297
+ wrong_text, wrong_trans_text, wrong_score, wrong_audio_record], "flagged_data")
298
+ # We can choose which components to flag
299
+ flag_btn.click(save_outputs,
300
+ [correct_text, correct_trans_text, correct_score, correct_audio_record,
301
+ close_text, close_trans_text, close_score, close_audio_record,
302
+ wrong_text, wrong_trans_text, wrong_score, wrong_audio_record],
303
+ None, preprocess=False)
304
+
305
+ # Update test values according to the selected test
306
+ test_dropdown.input(get_test_text, test_dropdown, [correct_text, close_text, wrong_text, image])
307
 
308
 
309
  # Clear the transcription after selecting a new test
310
  test_dropdown.select(lambda: [None]*10, None,
311
  outputs=[correct_text, close_text, wrong_text, correct_audio_record, close_audio_record, wrong_audio_record, correct_trans_text, close_trans_text, wrong_trans_text, image],
312
  queue=False)
313
+ #demo.launch(inbrowser=True)
314
+ if __name__ == "__main__":
315
+ demo.launch(auth=[("randa", "randa"), ("randa1", "randa1"),("randa2", "randa2"), ("randa3", "randa3"),
316
+ ("karim1", "karim1"), ("karim2", "karim2"), ("karim3", "karim3"),
317
+ ("yassir1", "yassir1"), ("yassir2", "yassir2"), ("yassir3", "yassir3"),
318
+ ("mehdi", "mehdi")],
319
+ share=True, inbrowser=True)