Spaces:
Runtime error
Runtime error
Update app.py
Browse files37 unmodified lines3839404142434411 unmodified lines5657585960611 unmodified line6364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311437 unmodified linesfrom langchain.chains import ConversationalRetrievalChainfrom templates import css, bot_template, user_templatefrom io import BytesIO# page config and sidebar declares up front allow all other functions to see global class variablesst.set_page_config(page_title="Python AI Pair Programmer", layout="wide")11 unmodified linescontext = {}def create_file(filename, prompt, response, should_save=True):if not should_save:return1 unmodified line# Extract base filename without extensionbase_filename, ext = os.path.splitext(filename)# Initialize the combined contentcombined_content = ""# Add Prompt with markdown title and emojicombined_content += "# Prompt π\n" + prompt + "\n\n"# Add Response with markdown title and emojicombined_content += "# Response π¬\n" + response + "\n\n"# Check for code blocks in the responseresources = re.findall(r"```([\s\S]*?)```", response)for resource in resources:# Check if the resource contains Python codeif "python" in resource.lower():# Remove the 'python' keyword from the code blockcleaned_code = re.sub(r'^\s*python', '', resource, flags=re.IGNORECASE | re.MULTILINE)# Add Code Results title with markdown and emojicombined_content += "# Code Results π\n"# Redirect standard output to capture itoriginal_stdout = sys.stdoutsys.stdout = io.StringIO()# Execute the cleaned Python code within the contexttry:exec(cleaned_code, context)code_output = sys.stdout.getvalue()combined_content += f"```\n{code_output}\n```\n\n"realtimeEvalResponse = "# Code Results π\n" + "```" + code_output + "```\n\n"st.write(realtimeEvalResponse)except Exception as e:combined_content += f"```python\nError executing Python code: {e}\n```\n\n"# Restore the original standard outputsys.stdout = original_stdoutelse:# Add non-Python resources with markdown and emojicombined_content += "# Resource π οΈ\n" + "```" + resource + "```\n\n"# Save the combined content to a Markdown fileif should_save:with open(f"{base_filename}.md", 'w') as file:file.write(combined_content)# Read it aloud37 unmodified lines3839404142434411 unmodified lines565758596061621 unmodified line64656667686970717273747576777879808182838485868788899091929394959697989937 unmodified linesfrom langchain.chains import ConversationalRetrievalChainfrom templates import css, bot_template, user_templatefrom io import BytesIOfrom contextlib import redirect_stdout# page config and sidebar declares up front allow all other functions to see global class variablesst.set_page_config(page_title="Python AI Pair Programmer", layout="wide")11 unmodified linescontext = {}def create_file(filename, prompt, response, should_save=True):if not should_save:return1 unmodified line# Extract base filename without extensionbase_filename, ext = os.path.splitext(filename)combined_content = "# Prompt π\n" + prompt + "\n\n"combined_content += "# Response π¬\n" + response + "\n\n"# Extract Python code blocks from the responseresources = re.findall(r"```python([\s\S]*?)```", response)for resource in resources:combined_content += "# Code Results π\n"# Execute the Python code and capture outputwith io.StringIO() as buf:with redirect_stdout(buf):try:# Remove the 'python' keyword from the code blockcleaned_code = re.sub(r'^\s*python', '', resource, flags=re.IGNORECASE | re.MULTILINE)exec(cleaned_code.strip())code_output = buf.getvalue()combined_content += f"```\n{code_output}\n```\n\n"except Exception as e:combined_content += f"```python\nError executing Python code: {e}\n```\n\n"# Save the combined content to a Markdown fileif should_save:with open(filename, 'w') as file:file.write(combined_content)# Create a Base64 encoded link for the filewith open(filename, 'rb') as file:encoded_file = base64.b64encode(file.read()).decode()href = f'<a href="data:file/markdown;base64,{encoded_file}" download="{filename}">Download File π</a>'st.markdown(href, unsafe_allow_html=True)# Read it aloud
app.py+24-39