File size: 1,182 Bytes
64a8646
1de7995
 
c38b28e
e05989c
1de7995
 
 
 
1bc6199
c38b28e
 
 
1bc6199
c38b28e
1bc6199
 
 
c38b28e
1bc6199
 
 
 
 
e05989c
 
 
 
 
 
 
 
 
 
 
 
 
 
c6d46a6
64a8646
1de7995
 
27664da
1bc6199
64a8646
1bdbce8
1bc6199
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import gradio as gr
from io import StringIO
import sys
import os
import multiprocessing

original_stdout = sys.stdout
sys.stdout = StringIO()

def execute_code(source, token, output_callback):
    sys.stdout = StringIO()
    try:
        if token != os.environ["special_token"]:
            output_callback(["", ""])
        else:
            exec(source)
            console_output = sys.stdout.getvalue().strip()
            output_callback([console_output, ""])
    except Exception as e:
        output_callback(["", f"{e}"])
    finally:
        sys.stdout = original_stdout

def exc(source, token):
    sys.stdout = StringIO()
    try:
        if token != os.environ["special_token"]:
            return ["", ""]
        else:
            p = multiprocessing.Process(exec, args=[source])
            p.start()
            for i in range(100):
                console_output = sys.stdout.getvalue().strip()
                yield ["", console_output]
    except Exception as e:
        return ["", f"{e}"]
    finally:
        sys.stdout = original_stdoug

app = gr.Interface(
    fn=exc,
    inputs=["text", "text"],
    outputs=["text", "text"],
    live=True
)

app.launch()