tahamajs's picture
download
raw
2.95 kB
"""
Execute Self-Consistency CoT notebook cells and capture outputs
"""
import sys
import os
import json
import io
from contextlib import redirect_stdout, redirect_stderr
# Set up path
sys.path.insert(0, os.path.abspath("."))
# Suppress matplotlib display warnings
import matplotlib
matplotlib.use("Agg")
def execute_code_cell(code, global_vars):
"""Execute a code cell and capture output"""
output_buffer = io.StringIO()
error_buffer = io.StringIO()
try:
with redirect_stdout(output_buffer), redirect_stderr(error_buffer):
exec(code, global_vars)
output = output_buffer.getvalue()
error = error_buffer.getvalue()
if error:
return {
"name": "stderr",
"output_type": "stream",
"text": error.split("\n"),
}
elif output:
return {
"name": "stdout",
"output_type": "stream",
"text": output.split("\n"),
}
else:
return None
except Exception as e:
return {
"ename": type(e).__name__,
"evalue": str(e),
"output_type": "error",
"traceback": [str(e)],
}
def main():
# Read notebook
with open("notebooks/01_Self_Consistency_CoT.ipynb", "r") as f:
nb = json.load(f)
# Initialize global variables for execution
global_vars = {}
print("=" * 80)
print("EXECUTING SELF-CONSISTENCY COT NOTEBOOK")
print("=" * 80)
# Execute each code cell
cell_number = 1
for i, cell in enumerate(nb["cells"]):
if cell["cell_type"] == "code":
print(f"\n[Executing Cell {cell_number}]")
# Get code from cell
if isinstance(cell["source"], list):
code = "".join(cell["source"])
else:
code = cell["source"]
# Execute and capture output
output = execute_code_cell(code, global_vars)
# Add output to cell
if output:
cell["outputs"] = [output]
if output.get("output_type") == "stream":
print("".join(output["text"][:10])) # Print first 10 lines
elif output.get("output_type") == "error":
print(f"❌ Error: {output['ename']}: {output['evalue']}")
else:
cell["outputs"] = []
# Set execution count
cell["execution_count"] = cell_number
cell_number += 1
print(f"✅ Cell {cell_number-1} executed")
# Save executed notebook
output_path = "notebooks/01_Self_Consistency_CoT_executed.ipynb"
with open(output_path, "w") as f:
json.dump(nb, f, indent=2)
print("\n" + "=" * 80)
print(f"✅ Notebook executed and saved to: {output_path}")
print("=" * 80)
if __name__ == "__main__":
main()

Xet Storage Details

Size:
2.95 kB
·
Xet hash:
cdeff70ddb7602c066fb22419a1d086054f2c640145efa24961801c52adf45cf

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.