ThomasSimonini HF staff commited on
Commit
1c66a83
·
verified ·
1 Parent(s): a0e5687

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -0
app.py CHANGED
@@ -12,6 +12,12 @@ EXAM_PASSING_SCORE = os.getenv("EXAM_PASSING_SCORE") or 0.7
12
 
13
  ds = load_dataset(EXAM_DATASET_ID, split="train")
14
 
 
 
 
 
 
 
15
  # Convert dataset to a list of dicts and randomly sort
16
  quiz_data = ds.to_pandas().to_dict("records")
17
  random.shuffle(quiz_data)
@@ -55,6 +61,24 @@ def on_user_logged_in(token: gr.OAuthToken | None):
55
  None, # no token
56
  ]
57
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58
 
59
  def push_results_to_hub(user_answers, token: gr.OAuthToken | None):
60
  """
@@ -91,9 +115,18 @@ def push_results_to_hub(user_answers, token: gr.OAuthToken | None):
91
  }
92
  )
93
  new_ds.push_to_hub(repo_id=repo_id, split=user_info["name"])
 
 
 
 
 
 
 
94
  return f"Your responses have been submitted to the Hub! Final grade: {grade:.1%}"
95
 
96
 
 
 
97
  def handle_quiz(question_idx, user_answers, selected_answer, is_start):
98
  """
99
  Handle quiz state transitions and store answers
 
12
 
13
  ds = load_dataset(EXAM_DATASET_ID, split="train")
14
 
15
+ CERTIFIED_USERS_FILENAME = "certified_users.csv"
16
+ CERTIFIED_USERS_DIR = "certificates"
17
+ repo = Repository(
18
+ local_dir=CERTIFIED_USERS_DIR, clone_from=DATASET_REPO_URL, use_auth_token=HF_TOKEN
19
+ )
20
+
21
  # Convert dataset to a list of dicts and randomly sort
22
  quiz_data = ds.to_pandas().to_dict("records")
23
  random.shuffle(quiz_data)
 
61
  None, # no token
62
  ]
63
 
64
+ def add_certified_user(hf_username, pass_percentage, submission_time):
65
+ """
66
+ Add the certified user to the database
67
+ """
68
+ print("ADD CERTIFIED USER")
69
+ repo.git_pull()
70
+ history = pd.read_csv(os.path.join(CERTIFIED_USERS_DIR, CERTIFIED_USERS_FILENAME))
71
+
72
+ # Check if this hf_username is already in our dataset:
73
+ check = history.loc[history['hf_username'] == hf_username]
74
+ if not check.empty:
75
+ history = history.drop(labels=check.index[0], axis=0)
76
+
77
+ new_row = pd.DataFrame({'hf_username': hf_username, 'pass_percentage': pass_percentage, 'datetime': submission_time}, index=[0])
78
+ history = pd.concat([new_row, history[:]]).reset_index(drop=True)
79
+
80
+ history.to_csv(os.path.join(CERTIFIED_USERS_DIR, CERTIFIED_USERS_FILENAME), index=False)
81
+ repo.push_to_hub(commit_message="Update certified users list")
82
 
83
  def push_results_to_hub(user_answers, token: gr.OAuthToken | None):
84
  """
 
115
  }
116
  )
117
  new_ds.push_to_hub(repo_id=repo_id, split=user_info["name"])
118
+
119
+ # I'm adding a csv version
120
+ # The idea, if the user passed, we create a simple row in a csv
121
+ print("ADD CERTIFIED USER")
122
+ # Add this user to our database
123
+ add_certified_user(user_info["name"], grade, submission_time)
124
+
125
  return f"Your responses have been submitted to the Hub! Final grade: {grade:.1%}"
126
 
127
 
128
+
129
+
130
  def handle_quiz(question_idx, user_answers, selected_answer, is_start):
131
  """
132
  Handle quiz state transitions and store answers