Update app.py
Browse files
app.py
CHANGED
|
@@ -2,16 +2,27 @@ import gradio as gr
|
|
| 2 |
import io
|
| 3 |
import sys
|
| 4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
def run(code):
|
| 6 |
-
|
| 7 |
-
sys.stdout = buffer = io.StringIO()
|
| 8 |
-
try:
|
| 9 |
-
exec(code, {})
|
| 10 |
-
except Exception as e:
|
| 11 |
-
print(f"Error: {e}")
|
| 12 |
-
finally:
|
| 13 |
-
sys.stdout = old_stdout
|
| 14 |
-
return buffer.getvalue()
|
| 15 |
|
| 16 |
demo = gr.Interface(fn=run, inputs="text", outputs="text")
|
| 17 |
demo.launch()
|
|
|
|
| 2 |
import io
|
| 3 |
import sys
|
| 4 |
|
| 5 |
+
class PersistentRunner:
|
| 6 |
+
def __init__(self):
|
| 7 |
+
self.globals = {}
|
| 8 |
+
|
| 9 |
+
def run(self, code):
|
| 10 |
+
old_stdout = sys.stdout
|
| 11 |
+
sys.stdout = buffer = io.StringIO()
|
| 12 |
+
|
| 13 |
+
try:
|
| 14 |
+
exec(code, self.globals)
|
| 15 |
+
except Exception as e:
|
| 16 |
+
print(f"Error: {e}")
|
| 17 |
+
finally:
|
| 18 |
+
sys.stdout = old_stdout
|
| 19 |
+
|
| 20 |
+
return buffer.getvalue()
|
| 21 |
+
|
| 22 |
+
runner = PersistentRunner()
|
| 23 |
+
|
| 24 |
def run(code):
|
| 25 |
+
return runner.run(code)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
demo = gr.Interface(fn=run, inputs="text", outputs="text")
|
| 28 |
demo.launch()
|