Spaces:
Running
Running
Create 30_PyTorchPlayground.py
Browse files
pages/30_PyTorchPlayground.py
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import torch
|
| 3 |
+
import numpy as np
|
| 4 |
+
import io
|
| 5 |
+
import sys
|
| 6 |
+
|
| 7 |
+
# Function to execute the input code and capture print statements
|
| 8 |
+
def execute_code(code):
|
| 9 |
+
# Redirect stdout to capture print statements
|
| 10 |
+
old_stdout = sys.stdout
|
| 11 |
+
sys.stdout = mystdout = io.StringIO()
|
| 12 |
+
|
| 13 |
+
global_vars = {"torch": torch, "np": np}
|
| 14 |
+
local_vars = {}
|
| 15 |
+
try:
|
| 16 |
+
exec(code, global_vars, local_vars)
|
| 17 |
+
output = mystdout.getvalue()
|
| 18 |
+
except Exception as e:
|
| 19 |
+
output = str(e)
|
| 20 |
+
finally:
|
| 21 |
+
# Reset redirect.
|
| 22 |
+
sys.stdout = old_stdout
|
| 23 |
+
|
| 24 |
+
return output, local_vars
|
| 25 |
+
|
| 26 |
+
st.title('PyTorch Code Runner')
|
| 27 |
+
|
| 28 |
+
# Text area for inputting the PyTorch code
|
| 29 |
+
code_input = st.text_area("Enter your PyTorch code here", height=300)
|
| 30 |
+
|
| 31 |
+
# Button to execute the code
|
| 32 |
+
if st.button("Run Code"):
|
| 33 |
+
# Prepend the import statement
|
| 34 |
+
code_to_run = "import torch\nimport numpy as np\n" + code_input
|
| 35 |
+
|
| 36 |
+
# Execute the code and capture the output
|
| 37 |
+
output, variables = execute_code(code_to_run)
|
| 38 |
+
|
| 39 |
+
# Display the output
|
| 40 |
+
st.subheader('Output')
|
| 41 |
+
st.text(output)
|