Spaces:
Runtime error
Runtime error
Haseeb commited on
Commit ·
c9f55e2
1
Parent(s): 8c2b5b9
Updated for READ_ONLY source
Browse files- bardcode_interpreter.py +95 -66
bardcode_interpreter.py
CHANGED
|
@@ -33,22 +33,22 @@ import io
|
|
| 33 |
BARD_FILE_SIZE_LIMIT = 10000
|
| 34 |
|
| 35 |
# Function to measure the accuracy of the code
|
| 36 |
-
|
| 37 |
-
|
| 38 |
def measure_accuracy(counter):
|
| 39 |
accuracy = 1 / (counter + 1)
|
| 40 |
accuracy_percentage = accuracy * 100
|
| 41 |
-
st.info(
|
| 42 |
-
f"Output has been fixed {counter} times with accuracy {accuracy_percentage:.0f}%")
|
| 43 |
|
| 44 |
|
| 45 |
def show_content(content):
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
|
|
|
| 49 |
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
| 52 |
|
| 53 |
|
| 54 |
# method to execute the bard coder process
|
|
@@ -139,6 +139,7 @@ def auto_bard_execute(prompt, code_file='code.txt', code_choices='code_choice',
|
|
| 139 |
stack_trace = traceback.format_exc()
|
| 140 |
st.info(stack_trace)
|
| 141 |
st.info(str(e))
|
|
|
|
| 142 |
|
| 143 |
|
| 144 |
# method to execute the bard coder process
|
|
@@ -229,23 +230,26 @@ def auto_bard_setup(prompt, code_file='code.txt', code_choices='code_choice', ex
|
|
| 229 |
|
| 230 |
|
| 231 |
def find_image_files(file_path):
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
|
|
|
|
|
|
|
|
|
| 249 |
return None
|
| 250 |
|
| 251 |
|
|
@@ -318,11 +322,14 @@ def is_code_safe(code):
|
|
| 318 |
|
| 319 |
|
| 320 |
def load_css(file_name):
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
|
|
|
|
|
|
|
|
|
| 326 |
|
| 327 |
|
| 328 |
def display_logo(logo_file: str, title: str):
|
|
@@ -384,11 +391,27 @@ def init_session_state():
|
|
| 384 |
if "file_char_count" not in st.session_state:
|
| 385 |
st.session_state.file_char_count = 0
|
| 386 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 387 |
|
| 388 |
if __name__ == "__main__":
|
| 389 |
try:
|
| 390 |
BardCoder.write_log("Starting the streamlit App")
|
| 391 |
-
|
|
|
|
| 392 |
# Load the CSS file named style.css
|
| 393 |
load_css("styles/style.css")
|
| 394 |
|
|
@@ -441,35 +464,39 @@ if __name__ == "__main__":
|
|
| 441 |
|
| 442 |
# Setting options for the application
|
| 443 |
with st.expander("Options"):
|
| 444 |
-
|
| 445 |
-
|
| 446 |
-
|
| 447 |
-
|
| 448 |
-
|
| 449 |
-
|
| 450 |
-
|
| 451 |
-
|
| 452 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 453 |
|
| 454 |
-
|
| 455 |
-
|
| 456 |
-
|
| 457 |
-
|
| 458 |
|
| 459 |
-
|
| 460 |
-
|
| 461 |
-
# To read file as string:
|
| 462 |
-
upload_data = stringio.read()
|
| 463 |
|
| 464 |
-
|
| 465 |
-
|
|
|
|
| 466 |
|
| 467 |
-
|
| 468 |
-
|
| 469 |
-
|
|
|
|
| 470 |
|
| 471 |
-
# Display a success message
|
| 472 |
-
st.success("File uploaded successfully.")
|
| 473 |
|
| 474 |
|
| 475 |
with st.expander("Settings"):
|
|
@@ -565,16 +592,18 @@ if __name__ == "__main__":
|
|
| 565 |
prompt += "\n" + "using Python use Pandas save the table in file called 'table.md'"
|
| 566 |
|
| 567 |
# Refine the prompt for harmful commands.
|
| 568 |
-
|
| 569 |
-
|
| 570 |
-
|
| 571 |
-
|
| 572 |
-
|
| 573 |
-
|
| 574 |
-
|
| 575 |
-
|
| 576 |
-
|
| 577 |
-
|
|
|
|
|
|
|
| 578 |
# Check if output is Graph,Chart request.
|
| 579 |
if 'graph' in prompt.lower() or 'chart' in prompt.lower():
|
| 580 |
image_file_graph = find_image_files(saved_file)
|
|
|
|
| 33 |
BARD_FILE_SIZE_LIMIT = 10000
|
| 34 |
|
| 35 |
# Function to measure the accuracy of the code
|
|
|
|
|
|
|
| 36 |
def measure_accuracy(counter):
|
| 37 |
accuracy = 1 / (counter + 1)
|
| 38 |
accuracy_percentage = accuracy * 100
|
| 39 |
+
st.info(f"Output has been fixed {counter} times with accuracy {accuracy_percentage:.0f}%")
|
|
|
|
| 40 |
|
| 41 |
|
| 42 |
def show_content(content):
|
| 43 |
+
try:
|
| 44 |
+
# Open the file and read its contents
|
| 45 |
+
with open(content, "r") as f:
|
| 46 |
+
markdown_text = f.read()
|
| 47 |
|
| 48 |
+
# Display the Markdown text in the app
|
| 49 |
+
st.markdown(markdown_text)
|
| 50 |
+
except Exception as e:
|
| 51 |
+
BardCoder.write_log(f"Error in showing content {e}")
|
| 52 |
|
| 53 |
|
| 54 |
# method to execute the bard coder process
|
|
|
|
| 139 |
stack_trace = traceback.format_exc()
|
| 140 |
st.info(stack_trace)
|
| 141 |
st.info(str(e))
|
| 142 |
+
BardCoder.add_log(str(e))
|
| 143 |
|
| 144 |
|
| 145 |
# method to execute the bard coder process
|
|
|
|
| 230 |
|
| 231 |
|
| 232 |
def find_image_files(file_path):
|
| 233 |
+
try:
|
| 234 |
+
# Create a regular expression for image files
|
| 235 |
+
image_regex = re.compile(r"\b\w+\.(png|jpg|jpeg|gif|bmp)", re.IGNORECASE)
|
| 236 |
+
|
| 237 |
+
# Open the code file
|
| 238 |
+
with open(file_path) as f:
|
| 239 |
+
# Read the lines
|
| 240 |
+
lines = f.readlines()
|
| 241 |
+
# Loop through the lines
|
| 242 |
+
for line in lines:
|
| 243 |
+
# Search for image files in the line
|
| 244 |
+
match = image_regex.search(line)
|
| 245 |
+
# If there is a match
|
| 246 |
+
if match:
|
| 247 |
+
# Get the image file name
|
| 248 |
+
image_file = match.group()
|
| 249 |
+
# Print the image file name
|
| 250 |
+
return image_file
|
| 251 |
+
except Exception as e:
|
| 252 |
+
BardCoder.write_log(f"Error in finding image files {e}")
|
| 253 |
return None
|
| 254 |
|
| 255 |
|
|
|
|
| 322 |
|
| 323 |
|
| 324 |
def load_css(file_name):
|
| 325 |
+
try:
|
| 326 |
+
# Open the file and read the content
|
| 327 |
+
with open(file_name) as fp:
|
| 328 |
+
css = fp.read()
|
| 329 |
+
# Use st.components.v1.html to load the CSS file
|
| 330 |
+
st.markdown(f'<style>{css}</style>', unsafe_allow_html=True)
|
| 331 |
+
except Exception as e:
|
| 332 |
+
BardCoder.write_log(f"Error in loading css {e}")
|
| 333 |
|
| 334 |
|
| 335 |
def display_logo(logo_file: str, title: str):
|
|
|
|
| 391 |
if "file_char_count" not in st.session_state:
|
| 392 |
st.session_state.file_char_count = 0
|
| 393 |
|
| 394 |
+
import os
|
| 395 |
+
from stat import S_IREAD, S_IRGRP, S_IROTH
|
| 396 |
+
|
| 397 |
+
def make_code_interpreter_read_only():
|
| 398 |
+
# Make code_interpreter.py read-only
|
| 399 |
+
# how to filename of this file
|
| 400 |
+
filename = __file__
|
| 401 |
+
BardCoder.add_log(f"Making {filename} read-only")
|
| 402 |
+
os.chmod(filename, S_IREAD|S_IRGRP|S_IROTH)
|
| 403 |
+
|
| 404 |
+
# Make all files in lib folder read-only
|
| 405 |
+
folder = "lib"
|
| 406 |
+
for filename in os.listdir(folder):
|
| 407 |
+
filepath = os.path.join(folder, filename)
|
| 408 |
+
os.chmod(filepath, S_IREAD|S_IRGRP|S_IROTH)
|
| 409 |
|
| 410 |
if __name__ == "__main__":
|
| 411 |
try:
|
| 412 |
BardCoder.write_log("Starting the streamlit App")
|
| 413 |
+
make_code_interpreter_read_only()
|
| 414 |
+
|
| 415 |
# Load the CSS file named style.css
|
| 416 |
load_css("styles/style.css")
|
| 417 |
|
|
|
|
| 464 |
|
| 465 |
# Setting options for the application
|
| 466 |
with st.expander("Options"):
|
| 467 |
+
try:
|
| 468 |
+
code_file = st.text_input("Filename for the generated code (without extension):", value="generated_code")
|
| 469 |
+
code_choices = st.text_input("Filename for code choices:", value="code_choices")
|
| 470 |
+
expected_output = st.text_input("Expected output (leave blank if none):")
|
| 471 |
+
exec_type = st.selectbox("Execution type:", ["single", "multiple"], index=0)
|
| 472 |
+
timeout_delay = st.number_input("Timeout (in seconds):", value=10)
|
| 473 |
+
|
| 474 |
+
# Adding the upload file option
|
| 475 |
+
uploaded_file = st.file_uploader("Choose a file")
|
| 476 |
+
if uploaded_file is not None:
|
| 477 |
+
|
| 478 |
+
# To read file as bytes:
|
| 479 |
+
bytes_data = uploaded_file.getvalue()
|
| 480 |
+
# get the file size
|
| 481 |
+
st.session_state.file_size = uploaded_file.size
|
| 482 |
|
| 483 |
+
# To convert to a string based IO:
|
| 484 |
+
stringio = StringIO(uploaded_file.getvalue().decode("utf-8"))
|
| 485 |
+
# To read file as string:
|
| 486 |
+
upload_data = stringio.read()
|
| 487 |
|
| 488 |
+
# Count the number of characters in the file
|
| 489 |
+
st.session_state.file_char_count = len(upload_data)
|
|
|
|
|
|
|
| 490 |
|
| 491 |
+
# write the file to uploads directory
|
| 492 |
+
with open("uploads/" + uploaded_file.name, "w") as f:
|
| 493 |
+
f.write(upload_data)
|
| 494 |
|
| 495 |
+
# Display a success message
|
| 496 |
+
st.success("File uploaded successfully.")
|
| 497 |
+
except Exception as e:
|
| 498 |
+
BardCoder.write_log(f"Error in options {e}")
|
| 499 |
|
|
|
|
|
|
|
| 500 |
|
| 501 |
|
| 502 |
with st.expander("Settings"):
|
|
|
|
| 592 |
prompt += "\n" + "using Python use Pandas save the table in file called 'table.md'"
|
| 593 |
|
| 594 |
# Refine the prompt for harmful commands.
|
| 595 |
+
try:
|
| 596 |
+
if prompt_safe:
|
| 597 |
+
# Run the auto bard setup process.
|
| 598 |
+
log_container = st.empty()
|
| 599 |
+
st.session_state.code_output, saved_file, status = auto_bard_setup(prompt, code_file, code_choices,expected_output, exec_type,timeout_delay)
|
| 600 |
+
else:
|
| 601 |
+
st.error(f"Cannot execute the prompt because of illegal command found '{command}'")
|
| 602 |
+
BardCoder.write_log(f"Cannot execute the prompt: '{prompt}' because of illegal command found '{command}'")
|
| 603 |
+
st.stop()
|
| 604 |
+
except Exception as e:
|
| 605 |
+
BardCoder.write_log(f"Error in auto bard setup {e}")
|
| 606 |
+
|
| 607 |
# Check if output is Graph,Chart request.
|
| 608 |
if 'graph' in prompt.lower() or 'chart' in prompt.lower():
|
| 609 |
image_file_graph = find_image_files(saved_file)
|