|
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() |
|
|