ml-visoft commited on
Commit
3555ea7
1 Parent(s): 17cd01c

GUI for evaluatio works and backend updates the frontend states.

Browse files
Files changed (1) hide show
  1. main.py +59 -17
main.py CHANGED
@@ -9,8 +9,6 @@ from sqlite_minutils.db import Database
9
 
10
  import eval_code
11
 
12
-
13
-
14
  # the secrets. Will be loaded from HF, or for docker --env-file or from IDE
15
 
16
  OAUTH_CLIENT_ID = os.environ.get('OAUTH_CLIENT_ID')
@@ -20,10 +18,14 @@ OPENID_PROVIDER_URL = os.environ.get('OPENID_PROVIDER_URL')
20
  SPACE_HOST = os.environ.get('SPACE_HOST')
21
 
22
  # The database. In memory bc it is efemerial on HF anyway.
23
- DATABASE_NAME = "data/sessions_meta.db"
24
- pathlib.Path(DATABASE_NAME).unlink(missing_ok=True)
25
 
26
 
 
 
 
 
 
 
27
  global_database = Database(DATABASE_NAME)
28
  global_database_tables = global_database.t
29
 
@@ -58,6 +60,7 @@ EVAL_STATE_ERROR=4
58
  HTML_SUBMIT_CODE_AREA = "submit_code_area"
59
  HTML_RESULTS_AREA = "prompt_response"
60
  HTML_CLEAR_FORM = "clear_the_form"
 
61
 
62
  hdrs = (HighlightJS(langs=['python', 'javascript', 'html', 'css']),)
63
 
@@ -70,7 +73,27 @@ else:
70
  app, rt = fast_app(debug=False, live=False, hdrs=hdrs)
71
  REFRESH_TIME = 1
72
 
73
- def html_create_feedback_updown_button(qe_id, ans_id, selected=0):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74
  html_target_id = f"buttons_{ans_id}"
75
  colors = ["grey", "blue"]
76
  up_col = colors[0]
@@ -78,9 +101,9 @@ def html_create_feedback_updown_button(qe_id, ans_id, selected=0):
78
  if selected == 1: up_col = colors[1]
79
  if selected == -1: down_col = colors[1]
80
  toggle_url = f"toggle_up_down/{qe_id}/{ans_id}/"
81
- up = Button("+", hx_put=f"{toggle_url}?which=1", hx_swap="outerHTML",
82
  hx_target="#" + html_target_id, style=f"background-color:{up_col}")
83
- down = Button("-", hx_put=f"{toggle_url}?which=-1", hx_swap="outerHTML",
84
  hx_target="#" + html_target_id, style=f"background-color:{down_col}")
85
  button_row = Div(up, down, _id=html_target_id)
86
  return button_row
@@ -124,30 +147,49 @@ def put(session, qe_id:int, ans_id:int, which:int):
124
  qa_obj_row = question_evaluation_table(limit=1, where=f"id == {answer_id}")
125
  if len(qa_obj_row) <= 0:
126
  return None
127
- qa_obj = qa_obj_row[0]
128
- if qe_id != qa_obj.id:
129
- print(f"QE {qe_id} does not belong to {qa_obj.id}")
130
  return None
131
- answer_eval_js = json.loads(qa_obj.answer_eval_text)
132
  crt_selection = answer_eval_js[ans_id]["EVAL"]
133
  input_button = which
134
  out_selection = (input_button if crt_selection == 0 else (0 if crt_selection == input_button else input_button))
135
  print(f"out selection: {out_selection}")
136
  # store it back in DB
137
  answer_eval_js[ans_id]["EVAL"] = out_selection
138
- qa_obj.answer_eval_text = answer_eval_js
139
- qa_obj.submitted = False # mark object as dirty
140
- question_evaluation_table.upsert(qa_obj)
141
 
142
  buttons= html_create_feedback_updown_button(qe_id, ans_id, selected=out_selection)
143
  return buttons
144
 
145
 
146
- def html_get_textual_feedback_form(qe_obj):
147
- form = Form(Input(), Button("Submit"))
148
- div = Div(P("Give us a general feedback for the evaluation (optional)"), form)
 
 
 
 
 
 
149
  return div
150
 
 
 
 
 
 
 
 
 
 
 
 
 
 
151
 
152
  def html_format_code_review_form(qe_obj, html_id=""):
153
  """
 
9
 
10
  import eval_code
11
 
 
 
12
  # the secrets. Will be loaded from HF, or for docker --env-file or from IDE
13
 
14
  OAUTH_CLIENT_ID = os.environ.get('OAUTH_CLIENT_ID')
 
18
  SPACE_HOST = os.environ.get('SPACE_HOST')
19
 
20
  # The database. In memory bc it is efemerial on HF anyway.
 
 
21
 
22
 
23
+ if "localhost" in SPACE_HOST:
24
+ DATABASE_NAME = "data/sessions_meta.db"
25
+ pathlib.Path(DATABASE_NAME).unlink(missing_ok=True)
26
+ else:
27
+ DATABASE_NAME = "/data/sessions_meta.db"
28
+
29
  global_database = Database(DATABASE_NAME)
30
  global_database_tables = global_database.t
31
 
 
60
  HTML_SUBMIT_CODE_AREA = "submit_code_area"
61
  HTML_RESULTS_AREA = "prompt_response"
62
  HTML_CLEAR_FORM = "clear_the_form"
63
+ HTML_SUBMIT_FEEDBACK = "submit_feedback"
64
 
65
  hdrs = (HighlightJS(langs=['python', 'javascript', 'html', 'css']),)
66
 
 
73
  app, rt = fast_app(debug=False, live=False, hdrs=hdrs)
74
  REFRESH_TIME = 1
75
 
76
+
77
+ def vallidate_and_get_question_evaluation_objectid(session, qe_id:int):
78
+ if 'session_id' not in session: return None
79
+ session_id = session["session_id"]
80
+ state_rows = session_state_table(limit=1, where=f"session_id == '{session_id}'", order_by="id DESC")
81
+ if len(state_rows) <= 0:
82
+ print("There is no state")
83
+ return False, None
84
+ answer_id = state_rows[0].current_qeval
85
+ qa_obj_row = question_evaluation_table(limit=1, where=f"id == {answer_id}")
86
+ if len(qa_obj_row) <= 0:
87
+ print("There is no answer recorded")
88
+ return False, None
89
+ qe_obj = qa_obj_row[0]
90
+ if qe_id != qe_obj.id:
91
+ print(f"QE {qe_id} does not belong to {qe_obj.id}")
92
+ return False, None
93
+ return True, qe_obj
94
+
95
+
96
+ def html_create_feedback_updown_button(qe_id, ans_id, selected=0, disabled=0):
97
  html_target_id = f"buttons_{ans_id}"
98
  colors = ["grey", "blue"]
99
  up_col = colors[0]
 
101
  if selected == 1: up_col = colors[1]
102
  if selected == -1: down_col = colors[1]
103
  toggle_url = f"toggle_up_down/{qe_id}/{ans_id}/"
104
+ up = Button("+", hx_put=f"{toggle_url}?which=1", disabled=disabled, hx_swap="outerHTML",
105
  hx_target="#" + html_target_id, style=f"background-color:{up_col}")
106
+ down = Button("-", hx_put=f"{toggle_url}?which=-1", disabled=disabled, hx_swap="outerHTML",
107
  hx_target="#" + html_target_id, style=f"background-color:{down_col}")
108
  button_row = Div(up, down, _id=html_target_id)
109
  return button_row
 
147
  qa_obj_row = question_evaluation_table(limit=1, where=f"id == {answer_id}")
148
  if len(qa_obj_row) <= 0:
149
  return None
150
+ qe_obj = qa_obj_row[0]
151
+ if qe_id != qe_obj.id:
152
+ print(f"QE {qe_id} does not belong to {qe_obj.id}")
153
  return None
154
+ answer_eval_js = json.loads(qe_obj.answer_eval_text)
155
  crt_selection = answer_eval_js[ans_id]["EVAL"]
156
  input_button = which
157
  out_selection = (input_button if crt_selection == 0 else (0 if crt_selection == input_button else input_button))
158
  print(f"out selection: {out_selection}")
159
  # store it back in DB
160
  answer_eval_js[ans_id]["EVAL"] = out_selection
161
+ qe_obj.answer_eval_text = answer_eval_js
162
+ qe_obj.submitted = False # mark object as dirty
163
+ question_evaluation_table.upsert(qe_obj)
164
 
165
  buttons= html_create_feedback_updown_button(qe_id, ans_id, selected=out_selection)
166
  return buttons
167
 
168
 
169
+ def html_get_textual_feedback_form(qe_obj, thank=False):
170
+ if thank:
171
+ ph = "Thank you!"
172
+ else:
173
+ ph = "Write your general feedback here"
174
+ form = Form(Input(name="freeform_feedback", placeholder=ph),
175
+ Button("Submit", disabled=(qe_obj.submitted == 1)), hx_post=f"/submit_feedback/{qe_obj.id}",
176
+ hx_target="#" + HTML_SUBMIT_FEEDBACK, hx_swap="outerHTML",)
177
+ div = Div(P("Give us a general feedback for the evaluation (optional)"), form, id=HTML_SUBMIT_FEEDBACK)
178
  return div
179
 
180
+ @rt("/submit_feedback/{qe_id}")
181
+ def post(session, qe_id:int, freeform_feedback:str):
182
+ print(qe_id, freeform_feedback)
183
+ # Update the object
184
+ is_ok, qe_obj = vallidate_and_get_question_evaluation_objectid(session, qe_id)
185
+ if not is_ok:
186
+ return "Error"
187
+
188
+ answer_eval_js = json.loads(qe_obj.answer_eval_text)
189
+ answer_eval_js[0]["explanation"] = freeform_feedback
190
+ qe_obj.submitted = True
191
+ question_evaluation_table.upsert(qe_obj)
192
+ return html_get_textual_feedback_form(qe_obj, thank=True)
193
 
194
  def html_format_code_review_form(qe_obj, html_id=""):
195
  """