Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -64,45 +64,57 @@ def create_file(filename, prompt, response, should_save=True):
|
|
64 |
# Extract base filename without extension
|
65 |
base_filename, ext = os.path.splitext(filename)
|
66 |
|
67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
68 |
combined_content += "# Response π¬\n" + response + "\n\n"
|
69 |
|
70 |
-
#
|
71 |
-
|
72 |
-
|
73 |
-
resources = re.findall(r"```python([\s\S]*?)```", response)
|
74 |
for resource in resources:
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
96 |
# Save the combined content to a Markdown file
|
97 |
if should_save:
|
98 |
-
with open(
|
99 |
file.write(combined_content)
|
100 |
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
|
107 |
|
108 |
# Read it aloud
|
|
|
64 |
# Extract base filename without extension
|
65 |
base_filename, ext = os.path.splitext(filename)
|
66 |
|
67 |
+
# Initialize the combined content
|
68 |
+
combined_content = ""
|
69 |
+
|
70 |
+
# Add Prompt with markdown title and emoji
|
71 |
+
combined_content += "# Prompt π\n" + prompt + "\n\n"
|
72 |
+
|
73 |
+
# Add Response with markdown title and emoji
|
74 |
combined_content += "# Response π¬\n" + response + "\n\n"
|
75 |
|
76 |
+
# Check for code blocks in the response
|
77 |
+
resources = re.findall(r"```([\s\S]*?)```", response)
|
|
|
|
|
78 |
for resource in resources:
|
79 |
+
# Check if the resource contains Python code
|
80 |
+
if "python" in resource.lower():
|
81 |
+
# Remove the 'python' keyword from the code block
|
82 |
+
cleaned_code = re.sub(r'^\s*python', '', resource, flags=re.IGNORECASE | re.MULTILINE)
|
83 |
+
|
84 |
+
# Add Code Results title with markdown and emoji
|
85 |
+
combined_content += "# Code Results π\n"
|
86 |
+
|
87 |
+
# Redirect standard output to capture it
|
88 |
+
original_stdout = sys.stdout
|
89 |
+
sys.stdout = io.StringIO()
|
90 |
+
|
91 |
+
# Execute the cleaned Python code within the context
|
92 |
+
try:
|
93 |
+
exec(cleaned_code, context)
|
94 |
+
code_output = sys.stdout.getvalue()
|
95 |
+
combined_content += f"```\n{code_output}\n```\n\n"
|
96 |
+
realtimeEvalResponse = "# Code Results π\n" + "```" + code_output + "```\n\n"
|
97 |
+
st.write(realtimeEvalResponse)
|
98 |
+
|
99 |
+
except Exception as e:
|
100 |
+
combined_content += f"```python\nError executing Python code: {e}\n```\n\n"
|
101 |
+
|
102 |
+
# Restore the original standard output
|
103 |
+
sys.stdout = original_stdout
|
104 |
+
else:
|
105 |
+
# Add non-Python resources with markdown and emoji
|
106 |
+
combined_content += "# Resource π οΈ\n" + "```" + resource + "```\n\n"
|
107 |
+
|
108 |
# Save the combined content to a Markdown file
|
109 |
if should_save:
|
110 |
+
with open(f"{base_filename}.md", 'w') as file:
|
111 |
file.write(combined_content)
|
112 |
|
113 |
+
# Create a Base64 encoded link for the file
|
114 |
+
with open(f"{base_filename}.md", 'rb') as file:
|
115 |
+
encoded_file = base64.b64encode(file.read()).decode()
|
116 |
+
href = f'<a href="data:file/markdown;base64,{encoded_file}" download="{filename}">Download File π</a>'
|
117 |
+
st.markdown(href, unsafe_allow_html=True)
|
118 |
|
119 |
|
120 |
# Read it aloud
|