Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -38,7 +38,7 @@ from langchain.memory import ConversationBufferMemory
|
|
38 |
from langchain.chains import ConversationalRetrievalChain
|
39 |
from templates import css, bot_template, user_template
|
40 |
from io import BytesIO
|
41 |
-
|
42 |
|
43 |
# page config and sidebar declares up front allow all other functions to see global class variables
|
44 |
st.set_page_config(page_title="Python AI Pair Programmer", layout="wide")
|
@@ -56,6 +56,7 @@ with col1:
|
|
56 |
context = {}
|
57 |
|
58 |
|
|
|
59 |
def create_file(filename, prompt, response, should_save=True):
|
60 |
if not should_save:
|
61 |
return
|
@@ -63,52 +64,36 @@ def create_file(filename, prompt, response, should_save=True):
|
|
63 |
# Extract base filename without extension
|
64 |
base_filename, ext = os.path.splitext(filename)
|
65 |
|
66 |
-
#
|
67 |
-
combined_content = ""
|
68 |
-
|
69 |
-
# Add Prompt with markdown title and emoji
|
70 |
-
combined_content += "# Prompt π\n" + prompt + "\n\n"
|
71 |
-
|
72 |
-
# Add Response with markdown title and emoji
|
73 |
combined_content += "# Response π¬\n" + response + "\n\n"
|
74 |
|
75 |
-
#
|
76 |
-
resources = re.findall(r"```([\s\S]*?)```", response)
|
77 |
for resource in resources:
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
try:
|
92 |
-
exec(cleaned_code, context)
|
93 |
-
code_output = sys.stdout.getvalue()
|
94 |
-
combined_content += f"```\n{code_output}\n```\n\n"
|
95 |
-
realtimeEvalResponse = "# Code Results π\n" + "```" + code_output + "```\n\n"
|
96 |
-
st.write(realtimeEvalResponse)
|
97 |
-
|
98 |
-
except Exception as e:
|
99 |
-
combined_content += f"```python\nError executing Python code: {e}\n```\n\n"
|
100 |
-
|
101 |
-
# Restore the original standard output
|
102 |
-
sys.stdout = original_stdout
|
103 |
-
else:
|
104 |
-
# Add non-Python resources with markdown and emoji
|
105 |
-
combined_content += "# Resource π οΈ\n" + "```" + resource + "```\n\n"
|
106 |
|
107 |
# Save the combined content to a Markdown file
|
108 |
if should_save:
|
109 |
-
with open(
|
110 |
file.write(combined_content)
|
111 |
|
|
|
|
|
|
|
|
|
|
|
112 |
|
113 |
|
114 |
# Read it aloud
|
|
|
38 |
from langchain.chains import ConversationalRetrievalChain
|
39 |
from templates import css, bot_template, user_template
|
40 |
from io import BytesIO
|
41 |
+
from contextlib import redirect_stdout
|
42 |
|
43 |
# page config and sidebar declares up front allow all other functions to see global class variables
|
44 |
st.set_page_config(page_title="Python AI Pair Programmer", layout="wide")
|
|
|
56 |
context = {}
|
57 |
|
58 |
|
59 |
+
|
60 |
def create_file(filename, prompt, response, should_save=True):
|
61 |
if not should_save:
|
62 |
return
|
|
|
64 |
# Extract base filename without extension
|
65 |
base_filename, ext = os.path.splitext(filename)
|
66 |
|
67 |
+
combined_content = "# Prompt π\n" + prompt + "\n\n"
|
|
|
|
|
|
|
|
|
|
|
|
|
68 |
combined_content += "# Response π¬\n" + response + "\n\n"
|
69 |
|
70 |
+
# Extract Python code blocks from the response
|
71 |
+
resources = re.findall(r"```python([\s\S]*?)```", response)
|
72 |
for resource in resources:
|
73 |
+
combined_content += "# Code Results π\n"
|
74 |
+
|
75 |
+
# Execute the Python code and capture output
|
76 |
+
with io.StringIO() as buf:
|
77 |
+
with redirect_stdout(buf):
|
78 |
+
try:
|
79 |
+
# Remove the 'python' keyword from the code block
|
80 |
+
cleaned_code = re.sub(r'^\s*python', '', resource, flags=re.IGNORECASE | re.MULTILINE)
|
81 |
+
exec(cleaned_code.strip())
|
82 |
+
code_output = buf.getvalue()
|
83 |
+
combined_content += f"```\n{code_output}\n```\n\n"
|
84 |
+
except Exception as e:
|
85 |
+
combined_content += f"```python\nError executing Python code: {e}\n```\n\n"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
86 |
|
87 |
# Save the combined content to a Markdown file
|
88 |
if should_save:
|
89 |
+
with open(filename, 'w') as file:
|
90 |
file.write(combined_content)
|
91 |
|
92 |
+
# Create a Base64 encoded link for the file
|
93 |
+
with open(filename, 'rb') as file:
|
94 |
+
encoded_file = base64.b64encode(file.read()).decode()
|
95 |
+
href = f'<a href="data:file/markdown;base64,{encoded_file}" download="{filename}">Download File π</a>'
|
96 |
+
st.markdown(href, unsafe_allow_html=True)
|
97 |
|
98 |
|
99 |
# Read it aloud
|