File size: 567 Bytes
4338994
 
 
 
213bfb5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4338994
213bfb5
4338994
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import gradio as gr
import io
import sys

class PersistentRunner:
    def __init__(self):
        self.globals = {}

    def run(self, code):
        old_stdout = sys.stdout
        sys.stdout = buffer = io.StringIO()

        try:
            exec(code, self.globals)
        except Exception as e:
            print(f"Error: {e}")
        finally:
            sys.stdout = old_stdout

        return buffer.getvalue()

runner = PersistentRunner()

def run(code):
    return runner.run(code)

demo = gr.Interface(fn=run, inputs="text", outputs="text")
demo.launch()