Update app.py
Browse files
app.py
CHANGED
@@ -64,22 +64,46 @@ if prompt := st.chat_input("Ask Palm 2 anything..."):
|
|
64 |
with st.chat_message("assistant", avatar='🌴'):
|
65 |
st.markdown(response['content'])
|
66 |
|
|
|
|
|
67 |
if response['code']:
|
68 |
if exportToReplIt:
|
69 |
with st.status("Exporting replit..."):
|
70 |
-
|
71 |
try:
|
72 |
-
url = bard.export_replit(code=response['code'],program_lang=response['program_lang'])['url']
|
73 |
-
except error:
|
74 |
-
|
75 |
-
st.write('ERROR')
|
76 |
-
if not
|
77 |
st.title('Export to repl.it')
|
78 |
st.markdown(f'[link]({url})')
|
|
|
79 |
if code_interpreter:
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
84 |
|
|
|
|
|
|
|
85 |
st.session_state.messages.append({"role": "assistant", "content": response['content']})
|
|
|
64 |
with st.chat_message("assistant", avatar='🌴'):
|
65 |
st.markdown(response['content'])
|
66 |
|
67 |
+
MAX_ATTEMPTS = 3 # Define a maximum number of attempts to execute the code
|
68 |
+
|
69 |
if response['code']:
|
70 |
if exportToReplIt:
|
71 |
with st.status("Exporting replit..."):
|
72 |
+
fail = False
|
73 |
try:
|
74 |
+
url = bard.export_replit(code=response['code'], program_lang=response['program_lang'])['url']
|
75 |
+
except Exception as error:
|
76 |
+
fail = True
|
77 |
+
st.write('ERROR exporting to repl.it')
|
78 |
+
if not fail:
|
79 |
st.title('Export to repl.it')
|
80 |
st.markdown(f'[link]({url})')
|
81 |
+
|
82 |
if code_interpreter:
|
83 |
+
current_attempt = 0
|
84 |
+
current_code = response['code']
|
85 |
+
while current_attempt < MAX_ATTEMPTS:
|
86 |
+
try:
|
87 |
+
exec(current_code)
|
88 |
+
break # If successful, break out of the loop
|
89 |
+
except Exception as e:
|
90 |
+
st.write(f"ERROR {e}...")
|
91 |
+
|
92 |
+
with st.status("Rebuilding code..."): # Status indicating code is being rebuilt
|
93 |
+
# Ask the AI model to regenerate the code and mention the problem
|
94 |
+
new_prompt = f"{prompt} . There was an error executing the code: {e}. Can you provide a corrected version?"
|
95 |
+
new_response = predict(new_prompt)
|
96 |
+
|
97 |
+
with st.chat_message("assistant", avatar='🌴'):
|
98 |
+
st.markdown(new_response['content'])
|
99 |
+
|
100 |
+
st.session_state.messages.append({"role": "assistant", "content": new_response['content']})
|
101 |
+
|
102 |
+
# Update the current code with the new response and increment the attempt counter
|
103 |
+
current_code = new_response['code']
|
104 |
+
current_attempt += 1
|
105 |
|
106 |
+
if current_attempt == MAX_ATTEMPTS:
|
107 |
+
st.write("Reached maximum attempts. Unable to execute the code successfully.")
|
108 |
+
|
109 |
st.session_state.messages.append({"role": "assistant", "content": response['content']})
|