ml-visoft commited on
Commit
715e9c8
1 Parent(s): 333a6de

Enter code, show results GUI works.

Browse files
Files changed (2) hide show
  1. eval_code.py +1 -1
  2. main.py +128 -35
eval_code.py CHANGED
@@ -41,7 +41,7 @@ def eval_code_by_chatgpt(openai_client, ccode):
41
  :param ccode:
42
  :return:
43
  """
44
- time.sleep(3)
45
  try:
46
  return """[
47
  {
 
41
  :param ccode:
42
  :return:
43
  """
44
+ # time.sleep(3)
45
  try:
46
  return """[
47
  {
main.py CHANGED
@@ -23,7 +23,8 @@ session_state_table = global_database_tables.session_state
23
  if session_state_table not in global_database_tables:
24
  # session_id stored in cookies
25
  # see EVAL_STATUS_x below for state
26
- session_state_table.create(id=int, session_id=str, state=int, submitted=datetime, completed=datetime, answer=str, pk='id')
 
27
  Session_State_cls = session_state_table.dataclass()
28
 
29
  EVAL_STATE_NEW=0
@@ -32,14 +33,21 @@ EVAL_STATE_TIMEDOUT=2
32
  EVAL_STATE_ANSWER=3
33
  EVAL_STATE_ERROR=4
34
 
 
 
 
 
 
 
35
 
36
  if "localhost" in SPACE_HOST:
37
  # Are we hacking locally?
38
  print("Localhost detected in SPACE_HOST. App started in debug+live mode!")
39
- app, rt = fast_app(debug=True, live=True)
 
40
  else:
41
- app, rt = fast_app(debug=False, live=False)
42
-
43
 
44
  CODE_AUGMENTATIONS=[
45
  ("DRY", "Don't repeat yourself."),
@@ -48,32 +56,61 @@ CODE_AUGMENTATIONS=[
48
  ("NAME", "Meaningful names in the code."),
49
  ]
50
 
51
- def format_code_review(code_js, html_id=""):
52
  list_of_citerias = []
53
  for caug_code, caug_txt in CODE_AUGMENTATIONS:
54
  crit_tag = [H3(caug_code), P(caug_txt)]
55
  list_of_citerias.extend(crit_tag)
56
  # yeah, I know . . .
57
  criteria_found = False
58
- for eval_line in code_js:
59
  if caug_code == eval_line["criteria"]:
60
- eval_txt = Pre(eval_line["explanation"])
61
  list_of_citerias.append(eval_txt)
62
  criteria_found = True
63
  if criteria_found is False:
64
  list_of_citerias.append(P("Not infringed"))
65
- return Div(*list_of_citerias, _id=html_id)
66
 
67
- def generate_default_eval_section(html_id=""):
68
  return Div(P("This is where criterias will show up once the code is evaluated"), _id=html_id)
69
 
70
- def generate_working_eval_section(html_id=""):
71
  return Div(P("Working . . ."), _id=html_id,
72
  hx_get=f"/render_answer",
73
- hx_trigger = "every 2s",
74
  hx_swap = "outerHTML",
75
  )
76
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77
  # How can I timeout? Well ... TBD.
78
  @threaded
79
  def call_gpt_and_store_result(pkid, code_to_check):
@@ -102,36 +139,68 @@ def call_gpt_and_store_result(pkid, code_to_check):
102
  import traceback
103
  traceback.print_exc()
104
 
105
- def get_rendered_answer(session_id):
106
- state_rows = session_state_table(limit=1, where=f"session_id == '{session_id}'", order_by="id DESC")
107
- # print(state_rows)
108
- if len(state_rows) <= 0:
109
- return generate_default_eval_section(html_id="prompt_response")
110
- state = state_rows[0]
111
- if state.state == EVAL_STATE_ANSWER:
112
- gpt_js_eval = json.loads(state.answer)
113
- return format_code_review(gpt_js_eval, html_id="prompt_response")
114
- if state.state == EVAL_STATE_QUERY:
115
- return generate_working_eval_section(html_id="prompt_response")
116
- return Div(P("There was an error:", P(state.answer)), _id="prompt_response")
117
 
118
 
119
- @rt("/render_answer")
120
- def get(session):
121
- if 'session_id' not in session: return "No session ID"
122
- session_id = session["session_id"]
123
- return get_rendered_answer(session_id)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
124
 
125
  @rt("/")
126
  def get(session):
127
  if 'session_id' not in session:
128
  session['session_id'] = str(uuid.uuid4())
129
- inp = Textarea(id="ccodetoeval", name="ccodetoeval", placeholder="Enter a piece of C code", rows=20)
130
- # eval_area = Pre("", id="prompt_response")
131
- # eval_area = format_code_review([], html_id="prompt_response")
132
- eval_area = generate_default_eval_section(html_id="prompt_response")
133
- add = Form(Group(inp, Button("Generate")), hx_post="/submit_to_eval", hx_swap="outerHTML", target_id='prompt_response')
134
- return Title('Image Generation Demo'), Main(H1('Magic Image Generation'), add, eval_area, cls='container')
 
 
 
 
 
 
 
 
 
 
 
 
135
 
136
 
137
  @rt("/submit_to_eval", methods="post")
@@ -142,13 +211,37 @@ def post(ccodetoeval:str, session):
142
  session_id=session_id,
143
  state=EVAL_STATE_QUERY,
144
  submitted=datetime.utcnow(),
 
145
  answer=""
146
  )
147
  # we insert and we get the new primary key
148
  session_obj = session_state_table.insert(session_obj)
149
  # will be executed in another thread with magic @threaded
150
  call_gpt_and_store_result(session_obj.id, ccodetoeval)
151
- return get_rendered_answer(session_id)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
152
 
153
 
154
  ## This code is for reference, for OAuth. BIG PITA, will be added later.
 
23
  if session_state_table not in global_database_tables:
24
  # session_id stored in cookies
25
  # see EVAL_STATUS_x below for state
26
+ session_state_table.create(id=int, session_id=str, state=int, submitted=datetime,
27
+ completed=datetime, answer=str, code=str, pk='id')
28
  Session_State_cls = session_state_table.dataclass()
29
 
30
  EVAL_STATE_NEW=0
 
33
  EVAL_STATE_ANSWER=3
34
  EVAL_STATE_ERROR=4
35
 
36
+ # Constants to name the various HTML ids in the code
37
+ HTML_SUBMIT_CODE_AREA = "submit_code_area"
38
+ HTML_RESULTS_AREA = "prompt_response"
39
+ HTML_CLEAR_FORM = "clear_the_form"
40
+
41
+ hdrs = (HighlightJS(langs=['python', 'javascript', 'html', 'css']),)
42
 
43
  if "localhost" in SPACE_HOST:
44
  # Are we hacking locally?
45
  print("Localhost detected in SPACE_HOST. App started in debug+live mode!")
46
+ app, rt = fast_app(debug=True, live=True, hdrs=hdrs)
47
+ REFRESH_TIME = 0.1
48
  else:
49
+ app, rt = fast_app(debug=False, live=False, hdrs=hdrs)
50
+ REFRESH_TIME = 1
51
 
52
  CODE_AUGMENTATIONS=[
53
  ("DRY", "Don't repeat yourself."),
 
56
  ("NAME", "Meaningful names in the code."),
57
  ]
58
 
59
+ def format_code_review(feedback_js, c_code, html_id=""):
60
  list_of_citerias = []
61
  for caug_code, caug_txt in CODE_AUGMENTATIONS:
62
  crit_tag = [H3(caug_code), P(caug_txt)]
63
  list_of_citerias.extend(crit_tag)
64
  # yeah, I know . . .
65
  criteria_found = False
66
+ for eval_line in feedback_js:
67
  if caug_code == eval_line["criteria"]:
68
+ eval_txt = P(eval_line["explanation"])
69
  list_of_citerias.append(eval_txt)
70
  criteria_found = True
71
  if criteria_found is False:
72
  list_of_citerias.append(P("Not infringed"))
73
+ return Div(html_render_code_output(c_code), *list_of_citerias, _id=html_id)
74
 
75
+ def html_default_results(html_id):
76
  return Div(P("This is where criterias will show up once the code is evaluated"), _id=html_id)
77
 
78
+ def html_waiting_for_results(html_id):
79
  return Div(P("Working . . ."), _id=html_id,
80
  hx_get=f"/render_answer",
81
+ hx_trigger = f"every {REFRESH_TIME}s",
82
  hx_swap = "outerHTML",
83
  )
84
 
85
+ def get_latest_eval_request_status(session_id):
86
+ state_rows = session_state_table(limit=1, where=f"session_id == '{session_id}'", order_by="id DESC")
87
+ if len(state_rows) <= 0:
88
+ return EVAL_STATE_NEW, None
89
+ state_obj = state_rows[0]
90
+ if state_obj.state in {EVAL_STATE_NEW, EVAL_STATE_QUERY, EVAL_STATE_ANSWER}:
91
+ return state_obj.state, state_obj
92
+ return EVAL_STATE_ERROR, state_obj
93
+
94
+
95
+ def html_error_results(message, html_id):
96
+ div = Div(P("There was an error:", P(message)), _id=html_id)
97
+ return div
98
+
99
+ def html_render_answer_from_db(session_id, html_id):
100
+ eval_request_status, state_obj = get_latest_eval_request_status(session_id)
101
+ # state_rows = session_state_table(limit=1, where=f"session_id == '{session_id}'", order_by="id DESC")
102
+ # print(eval_request_status, state_obj)
103
+ if eval_request_status == EVAL_STATE_NEW:
104
+ return html_default_results(html_id), #, html_render_inputbox(HTML_RESULTS_AREA, HTML_SUBMIT_CODE_AREA)
105
+ if eval_request_status == EVAL_STATE_ANSWER:
106
+ gpt_js_eval = json.loads(state_obj.answer)
107
+ return (format_code_review(gpt_js_eval, state_obj.code, html_id),
108
+ html_render_inputbox(target_html_id=HTML_RESULTS_AREA, region_html_id=HTML_SUBMIT_CODE_AREA)) #, html_render_code_output(HTML_SUBMIT_CODE_AREA, state_obj.code)
109
+ if eval_request_status == EVAL_STATE_QUERY:
110
+ return html_waiting_for_results(html_id),
111
+ return html_error_results(state_obj.answer, html_id), #, html_render_code_output(HTML_SUBMIT_CODE_AREA, state_obj.code)
112
+
113
+
114
  # How can I timeout? Well ... TBD.
115
  @threaded
116
  def call_gpt_and_store_result(pkid, code_to_check):
 
139
  import traceback
140
  traceback.print_exc()
141
 
 
 
 
 
 
 
 
 
 
 
 
 
142
 
143
 
144
+ def html_render_inputbox(target_html_id, region_html_id):
145
+ txtarea = Textarea(id="ccodetoeval", name="ccodetoeval", placeholder="Enter a piece of C code", rows=3)
146
+ form = Form(Group(txtarea, Button("Evaluate")),
147
+ hx_post="/submit_to_eval",
148
+ hx_swap="outerHTML",
149
+ target_id=target_html_id
150
+ )
151
+ return Div(form, _id=region_html_id, hx_swap_oob='true')
152
+
153
+ def html_render_code_output(code):
154
+ txtarea = Pre(Code(code))
155
+ return txtarea
156
+
157
+
158
+ def render_conditional_inputbox_results(session_id):
159
+ eval_request_status, _ = get_latest_eval_request_status(session_id)
160
+ pass
161
+
162
+ def html_render_clear_area_button(html_id):
163
+ button = Button("Clear form",
164
+ hx_get="/clear_area",
165
+ hx_swap="outerHTML",
166
+ target_id=HTML_RESULTS_AREA,
167
+ )
168
+ div = Div(button, _id=html_id, hx_swap_oob='true')
169
+ return div
170
+
171
+ def render_clear_area(session_id, html_id):
172
+ # return html_render_clear_area_button(html_id)
173
+ eval_request_status, _ = get_latest_eval_request_status(session_id)
174
+ if eval_request_status != EVAL_STATE_NEW:
175
+ print("clear button: render button")
176
+ return html_render_clear_area_button(html_id)
177
+ else:
178
+ print("clear button: render empty")
179
+ return Div(P(""), _id=html_id, hx_swap_oob='true')
180
+
181
 
182
  @rt("/")
183
  def get(session):
184
  if 'session_id' not in session:
185
  session['session_id'] = str(uuid.uuid4())
186
+ session_id = session["session_id"]
187
+ title = Title('C code review for students')
188
+ preamble = [H1("Evaluate your C code!"),
189
+ P("Enter your code in the textbox below and wait for answers."),
190
+ P("!! The data will be saved and maybe made public !!"),
191
+ ]
192
+
193
+ input_area = html_render_inputbox(target_html_id=HTML_RESULTS_AREA, region_html_id=HTML_SUBMIT_CODE_AREA)
194
+ results_area = html_render_answer_from_db(session_id, HTML_RESULTS_AREA)
195
+ clear_area = render_clear_area(session_id, HTML_CLEAR_FORM)
196
+ return title, Main(*preamble, input_area, results_area, clear_area)
197
+
198
+ @rt("/render_answer")
199
+ def get(session):
200
+ if 'session_id' not in session: return "No session ID"
201
+ session_id = session["session_id"]
202
+ answer_area = html_render_answer_from_db(session_id, HTML_RESULTS_AREA)
203
+ return answer_area
204
 
205
 
206
  @rt("/submit_to_eval", methods="post")
 
211
  session_id=session_id,
212
  state=EVAL_STATE_QUERY,
213
  submitted=datetime.utcnow(),
214
+ code=ccodetoeval,
215
  answer=""
216
  )
217
  # we insert and we get the new primary key
218
  session_obj = session_state_table.insert(session_obj)
219
  # will be executed in another thread with magic @threaded
220
  call_gpt_and_store_result(session_obj.id, ccodetoeval)
221
+ return (*html_render_answer_from_db(session_id, HTML_RESULTS_AREA),
222
+ render_clear_area(session_id, HTML_CLEAR_FORM)
223
+ )
224
+
225
+
226
+ @rt("/clear_area", methods="get")
227
+ def get(session):
228
+ if 'session_id' not in session: return P("Bad call. No session ID")
229
+ session_id = session["session_id"]
230
+ # insert a row to "cancel"/reset the current request
231
+ session_obj = Session_State_cls(
232
+ session_id=session_id,
233
+ state=EVAL_STATE_NEW,
234
+ submitted=datetime.utcnow(),
235
+ code="",
236
+ answer=""
237
+ )
238
+ session_state_table.insert(session_obj)
239
+ # re-issue forms
240
+ input_area = html_render_inputbox(target_html_id=HTML_RESULTS_AREA, region_html_id=HTML_SUBMIT_CODE_AREA)
241
+ results_area = html_render_answer_from_db(session_id, HTML_RESULTS_AREA)
242
+ clear_area = render_clear_area(session_id, HTML_CLEAR_FORM)
243
+ print(results_area)
244
+ return *results_area, input_area, clear_area
245
 
246
 
247
  ## This code is for reference, for OAuth. BIG PITA, will be added later.