Skip to content

Commit e4784da

Browse files
committed
Fix code execution and add auto-fix functionality
1 parent c18eebf commit e4784da

File tree

2 files changed

+39
-21
lines changed

2 files changed

+39
-21
lines changed

libs/general_utils.py

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ class GeneralUtils:
2121
def execute_code(self, compiler_mode: str):
2222
code_language = st.session_state.code_language
2323
generated_code = st.session_state.generated_code
24-
24+
code_output = None
25+
2526
if not generated_code or len(generated_code.strip()) == 0 or not code_language or len(code_language.strip()) == 0:
2627
st.toast("Generated code is empty. Cannot execute an empty code.", icon="❌")
2728
logger.error("Error in code execution: Generated code is empty.")
@@ -40,37 +41,37 @@ def execute_code(self, compiler_mode: str):
4041
return html_content
4142

4243
else:
43-
output = self.run_code(generated_code, code_language)
44+
code_output = self.run_code(generated_code, code_language)
4445

4546
# Check for errors in code execution
46-
if "error" in output.lower() or "exception" in output.lower() or "SyntaxError" in output.lower() or "NameError" in output.lower():
47+
if "error" in code_output.lower() or "exception" in code_output.lower() or "SyntaxError" in code_output.lower() or "NameError" in code_output.lower():
4748

48-
logger.error(f"Error in code execution: {output}")
49+
logger.error(f"Error in code execution: {code_output}")
4950
response = st.session_state.sequential_chain({'code_topic': generated_code})
5051
fixed_code = response['code_fix']
5152
st.code(fixed_code, language=code_language.lower())
5253

5354
with st.expander('Message History'):
5455
st.info(st.session_state.memory.buffer)
5556
logger.warning(f"Trying to run fixed code: {fixed_code}")
56-
output = self.run_code(code=fixed_code, language=code_language)
57-
logger.warning(f"Fixed code output: {output}")
58-
logger.info(f"Execution Output: '{output}' and session output: '{st.session_state.code_output}'")
57+
code_output = self.run_code(code=fixed_code, language=code_language)
58+
logger.warning(f"Fixed code output: {code_output}")
59+
logger.info(f"Execution Output: '{code_output}' and session output: '{st.session_state.code_output}'")
5960

6061
# check for expected output
6162
if (st.session_state.code_output is not None and st.session_state.code_output != "" and len(st.session_state.code_output) > 0):
62-
if (output == st.session_state.code_output):
63-
st.toast("Output:\n" + output, icon="🔥")
63+
if (code_output == st.session_state.code_output):
64+
st.toast("Output:\n" + code_output, icon="🔥")
6465
else:
6566
st.toast("Error the expected output doesnt match the generated output:\n'" + st.session_state.code_output + "'\n", icon="❌")
66-
logger.info(f"Execution Output: {output}")
67-
return output
67+
logger.info(f"Execution Output: {code_output}")
68+
return code_output
6869

69-
except Exception as e:
70-
st.toast("Error in code execution:", icon="❌")
70+
except Exception as exception:
71+
st.toast("Error in code execution: " + code_output, icon="❌")
7172
# Output the stack trace
72-
st.toast(traceback.format_exc(), icon="❌")
7373
logger.error(f"Error in code execution: {traceback.format_exc()}")
74+
return code_output
7475

7576
# Generate Dynamic HTML for JDoodle Compiler iFrame Embedding.
7677
def generate_dynamic_html(self,language, code_prompt):

script.py

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,11 @@ def initialize_session_state():
6666
st.session_state.palm_langchain = None
6767
if "gemini_langchain" not in st.session_state:
6868
st.session_state.gemini_langchain = None
69-
69+
if "code_fix_instructions" not in st.session_state:
70+
st.session_state.code_fix_instructions = None
71+
if "session_state.sequential_chain" not in st.session_state:
72+
st.session_state.sequential_chain = None
73+
7074
# Initialize session state for Vertex AI
7175
if "vertexai" not in st.session_state:
7276
st.session_state["vertexai"] = {
@@ -323,11 +327,12 @@ def main():
323327
# Input box for entering the prompt
324328
st.session_state.code_prompt = st.text_area("Enter Prompt", height=200, placeholder=placeholder,label_visibility='hidden')
325329

326-
with st.expander("Input/Output Options"):
330+
with st.expander("Input Options"):
327331
with st.container():
328332
st.session_state.code_input = st.text_input("Input (Stdin)", placeholder="Input (Stdin)", label_visibility='collapsed',value=st.session_state.code_input)
329333
st.session_state.code_output = st.text_input("Output (Stdout)", placeholder="Output (Stdout)", label_visibility='collapsed',value=st.session_state.code_output)
330-
334+
st.session_state.code_fix_instructions = st.text_input("Fix instructions", placeholder="Fix instructions", label_visibility='collapsed',value=st.session_state.code_fix_instructions)
335+
331336
# Set the input and output to None if the input and output is empty
332337
if st.session_state.code_input and st.session_state.code_output:
333338
if len(st.session_state.code_input) == 0:
@@ -344,22 +349,22 @@ def main():
344349

345350
with st.form('code_controls_form'):
346351
# Create columns for alignment
347-
file_name_col, save_code_col,generate_code_col,run_code_col = st.columns(4)
352+
file_name_col, save_code_col,generate_code_col,run_code_col,fix_code_col = st.columns(5)
348353

349354
# Input Box (for entering the file name) in the first column
350355
with file_name_col:
351356
code_file = st.text_input("File name", value="", placeholder="File name", label_visibility='collapsed')
352357

353358
# Save Code button in the second column
354359
with save_code_col:
355-
download_code_submitted = st.form_submit_button("Download Code")
360+
download_code_submitted = st.form_submit_button("Download")
356361
if download_code_submitted:
357362
file_format = "text/plain"
358363
st.session_state.download_link = general_utils.generate_download_link(st.session_state.generated_code, code_file,file_format,True)
359364

360365
# Generate Code button in the third column
361366
with generate_code_col:
362-
button_label = "Generate Code" if st.session_state["vertexai"]["model_name"] == "code-bison" else "Complete Code"
367+
button_label = "Generate" if st.session_state["vertexai"]["model_name"] == "code-bison" else "Complete"
363368
generate_submitted = st.form_submit_button(button_label)
364369

365370
if generate_submitted:
@@ -415,9 +420,21 @@ def main():
415420
st.session_state.generated_code = ""
416421
logger.error(f"Please select a valid AI option selected '{st.session_state.ai_option}' option")
417422

423+
# Fix Code button in the fourth column
424+
with fix_code_col:
425+
fix_submitted = st.form_submit_button("Auto Fix")
426+
if fix_submitted:
427+
if len(st.session_state.code_fix_instructions) == 0:
428+
st.toast("Missing fix instructions", icon="❌")
429+
logger.warning("Missing fix instructions")
430+
431+
logger.info(f"Fixing code with instructions: {st.session_state.code_fix_instructions}")
432+
st.session_state.generated_code = st.session_state.palm_langchain.fix_generated_code(st.session_state.generated_code, st.session_state.code_language,st.session_state.code_fix_instructions)
433+
434+
418435
# Run Code button in the fourth column
419436
with run_code_col:
420-
execute_submitted = st.form_submit_button("Execute Code")
437+
execute_submitted = st.form_submit_button("Execute")
421438
if execute_submitted:
422439
st.session_state.output = general_utils.execute_code(st.session_state.compiler_mode)
423440

0 commit comments

Comments
 (0)