Spaces:
Running
Running
Delete pages/8_CodeSimulator.py
Browse files- pages/8_CodeSimulator.py +0 -57
pages/8_CodeSimulator.py
DELETED
@@ -1,57 +0,0 @@
|
|
1 |
-
import streamlit as st
|
2 |
-
import torch
|
3 |
-
import io
|
4 |
-
import sys
|
5 |
-
|
6 |
-
# Function to execute the input code and capture print statements
|
7 |
-
def execute_code(code):
|
8 |
-
# Redirect stdout to capture print statements
|
9 |
-
old_stdout = sys.stdout
|
10 |
-
sys.stdout = mystdout = io.StringIO()
|
11 |
-
|
12 |
-
global_vars = {"torch": torch}
|
13 |
-
local_vars = {}
|
14 |
-
try:
|
15 |
-
exec(code, global_vars, local_vars)
|
16 |
-
output = mystdout.getvalue()
|
17 |
-
except Exception as e:
|
18 |
-
output = str(e)
|
19 |
-
finally:
|
20 |
-
# Reset redirect.
|
21 |
-
sys.stdout = old_stdout
|
22 |
-
|
23 |
-
return output, local_vars
|
24 |
-
|
25 |
-
st.title('PyTorch Code Runner')
|
26 |
-
|
27 |
-
# Text area for inputting the PyTorch code
|
28 |
-
code_input = st.text_area("Enter your PyTorch code here", height=300, value="""# Create two tensors of different shapes
|
29 |
-
tensor_c = torch.tensor([1, 2, 3])
|
30 |
-
tensor_d = torch.tensor([[1], [2], [3]])
|
31 |
-
|
32 |
-
# Perform addition using broadcasting
|
33 |
-
tensor_broadcast_add = tensor_c + tensor_d
|
34 |
-
print("Broadcast Addition:\\n", tensor_broadcast_add)
|
35 |
-
|
36 |
-
# Perform element-wise multiplication using broadcasting
|
37 |
-
tensor_broadcast_mul = tensor_c * tensor_d
|
38 |
-
print("Broadcast Multiplication:\\n", tensor_broadcast_mul)
|
39 |
-
""")
|
40 |
-
|
41 |
-
# Button to execute the code
|
42 |
-
if st.button("Run Code"):
|
43 |
-
# Prepend the import statement
|
44 |
-
code_to_run = "import torch\n" + code_input
|
45 |
-
|
46 |
-
# Execute the code and capture the output
|
47 |
-
output, variables = execute_code(code_to_run)
|
48 |
-
|
49 |
-
# Display the output
|
50 |
-
st.subheader('Output')
|
51 |
-
st.text(output)
|
52 |
-
|
53 |
-
# Display returned variables
|
54 |
-
if variables:
|
55 |
-
st.subheader('Variables')
|
56 |
-
for key, value in variables.items():
|
57 |
-
st.text(f"{key}: {value}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|