ml-visoft commited on
Commit
08fdde3
1 Parent(s): 3630362

Some code examples.

Browse files
Files changed (2) hide show
  1. eval_code.py +71 -1
  2. main.py +36 -1
eval_code.py CHANGED
@@ -101,7 +101,7 @@ void func(int* arr) {
101
  arr[7] = 7 * 7; printf("%d ", arr[7]);
102
  arr[8] = 8 * 8; printf("%d ", arr[8]);
103
  arr[9] = 9 * 9; printf("%d ", arr[9]);
104
- printf("\n");
105
  }
106
 
107
  int main() {
@@ -131,6 +131,76 @@ Your output:
131
  """
132
 
133
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
134
  def get_the_openai_client(openai_key):
135
  if openai_key is None or openai_key == "" or openai_key == "(none)":
136
  return None
 
101
  arr[7] = 7 * 7; printf("%d ", arr[7]);
102
  arr[8] = 8 * 8; printf("%d ", arr[8]);
103
  arr[9] = 9 * 9; printf("%d ", arr[9]);
104
+ printf("\\n");
105
  }
106
 
107
  int main() {
 
131
  """
132
 
133
 
134
+ CODE_EVAL_EXAMPLES = [
135
+ {"name":"Hello World",
136
+ "code":"""
137
+ #include <stdio.h>
138
+ int main() {
139
+ printf("Hello, World!");
140
+ return 0;
141
+ }
142
+ """,
143
+ "eval":"[]"},
144
+ {"name":"Bookstore DB",
145
+ "code":"""
146
+ #include <stdio.h>
147
+ #include <stdlib.h>
148
+ #include <string.h>
149
+
150
+ #define A 100
151
+ #define B 50
152
+
153
+ int main() {
154
+ char x1[A] = "The Great Gatsby";
155
+ char y1[B] = "F. Scott Fitzgerald";
156
+ int z1 = 1925;
157
+
158
+ char* p1 = (char*)malloc(A * sizeof(char));
159
+ char* p2 = (char*)malloc(B * sizeof(char));
160
+ int* p3 = (int*)malloc(sizeof(int));
161
+
162
+ if (p1 != NULL && p2 != NULL && p3 != NULL) {
163
+ strncpy(p1, x1, A);
164
+ strncpy(p2, y1, B);
165
+ *p3 = z1;
166
+
167
+ printf("Title: %s\\n", p1);
168
+ printf("Author: %s\\n", p2);
169
+ printf("Year: %d\\n", *p3);
170
+ }
171
+
172
+ free(p1);
173
+ free(p2);
174
+ free(p3);
175
+
176
+ return 0;
177
+ }""",
178
+ "eval":"""[
179
+ {
180
+ "criteria": "DRY",
181
+ "explanation": "The memory allocation and initialization for ``p1``, ``p2``, and ``p3`` are repetitive. Consider creating a function like ``allocateMemory`` to handle this."
182
+ },
183
+ {
184
+ "criteria": "SRP",
185
+ "explanation": "The ``main`` function handles memory allocation, data copying, and printing. You should separate these responsibilities into different functions like ``allocateMemory``, ``copyData``, and ``printData``."
186
+ },
187
+ {
188
+ "criteria": "NAME",
189
+ "explanation": "``x1`` should be called ``title``, ``y1`` should be called ``author``, ``z1`` should be called ``year``, ``p1`` should be called ``titlePtr``, ``p2`` should be called ``authorPtr``, and ``p3`` should be called ``yearPtr``."
190
+ }
191
+ ]"""},
192
+ ]
193
+
194
+
195
+ def get_enhanced_sample_example(example_id):
196
+ example_id = min(example_id, len(CODE_EVAL_EXAMPLES)-1)
197
+ example = copy.deepcopy(CODE_EVAL_EXAMPLES[example_id])
198
+ gpt_eval = json.loads(example["eval"])
199
+ enhanced_eval = add_evaluation_fields_on_js_answer(gpt_eval)
200
+ example["eval"] = enhanced_eval
201
+ return example
202
+
203
+
204
  def get_the_openai_client(openai_key):
205
  if openai_key is None or openai_key == "" or openai_key == "(none)":
206
  return None
main.py CHANGED
@@ -616,6 +616,40 @@ def post(session, ccodetoeval:str):
616
  return tl_html_results_and_feedback_area(session), render_clear_area(session_id, HTML_CLEAR_FORM)
617
 
618
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
619
 
620
  ########## CLEAR FORM
621
 
@@ -901,10 +935,11 @@ def get(session):
901
  ]
902
 
903
  input_area = tl_html_render_inputbox(session, target_html_id=HTML_RESULTS_AREA, region_html_id=HTML_SUBMIT_CODE_AREA)
 
904
  results_feedback_area = tl_html_results_and_feedback_area(session, show_submit_form=False)
905
  clear_area = render_clear_area(session_id, HTML_CLEAR_FORM)
906
  # print(session)
907
- return title, Main( *preamble, input_area, results_feedback_area, clear_area)
908
 
909
 
910
  serve()
 
616
  return tl_html_results_and_feedback_area(session), render_clear_area(session_id, HTML_CLEAR_FORM)
617
 
618
 
619
+ def tl_html_get_samples_section():
620
+ button_row = []
621
+ head = H4("Don't have any ideas? Try one of the samples below:")
622
+ no_examples = len(eval_code.CODE_EVAL_EXAMPLES)
623
+ for k in range(no_examples):
624
+ example = eval_code.get_enhanced_sample_example(k)
625
+ button = Button(example["name"], hx_get=f"/load_example/{k}", target_id=HTML_RESULTS_AREA)
626
+ button_row.append(button)
627
+ div_buttons = Div(*button_row, _class="row")
628
+ div = Div(head, div_buttons, style="border: 1px solid black;padding: 10px; margin 1px;")
629
+ return div
630
+
631
+
632
+ @rt("/load_example/{example_id}", methods="get")
633
+ def get(session, example_id:int):
634
+ session_id = session["session_id"]
635
+ save_to_storage(
636
+ storage.NavigationEvent(event_type="/submit_to_eval", event_session_id=session_id, event_params={"example_id":example_id})
637
+ )
638
+ example = eval_code.get_enhanced_sample_example(example_id)
639
+
640
+ qe_obj = Question_Evaluation_cls(code_text=example["code"], answer_eval_text=example["eval"], submitted=0)
641
+ qe_obj = question_evaluation_table.insert(qe_obj)
642
+ session_obj = Session_State_cls(
643
+ session_id=session_id,
644
+ state=EVAL_STATE_ANSWER,
645
+ submitted_date=datetime.isoformat(datetime.utcnow()),
646
+ evaluated_date=datetime.isoformat(datetime.utcnow()),
647
+ current_qeval=qe_obj.id,
648
+ )
649
+ session_state_table.insert(session_obj)
650
+
651
+ return tl_html_results_and_feedback_area(session)
652
+
653
 
654
  ########## CLEAR FORM
655
 
 
935
  ]
936
 
937
  input_area = tl_html_render_inputbox(session, target_html_id=HTML_RESULTS_AREA, region_html_id=HTML_SUBMIT_CODE_AREA)
938
+ samples_area = tl_html_get_samples_section()
939
  results_feedback_area = tl_html_results_and_feedback_area(session, show_submit_form=False)
940
  clear_area = render_clear_area(session_id, HTML_CLEAR_FORM)
941
  # print(session)
942
+ return title, Main( *preamble, input_area, samples_area, results_feedback_area, clear_area)
943
 
944
 
945
  serve()