Araeynn commited on
Commit
e05989c
1 Parent(s): 1bc6199

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -7
app.py CHANGED
@@ -2,7 +2,7 @@ import gradio as gr
2
  from io import StringIO
3
  import sys
4
  import os
5
- import threading
6
 
7
  original_stdout = sys.stdout
8
  sys.stdout = StringIO()
@@ -22,12 +22,20 @@ def execute_code(source, token, output_callback):
22
  sys.stdout = original_stdout
23
 
24
  def exc(source, token):
25
- def output_callback(output):
26
- yield output
27
-
28
- thread = threading.Thread(target=execute_code, args=(source, token, output_callback))
29
- thread.start()
30
- thread.join()
 
 
 
 
 
 
 
 
31
 
32
  app = gr.Interface(
33
  fn=exc,
 
2
  from io import StringIO
3
  import sys
4
  import os
5
+ import multiprocessing
6
 
7
  original_stdout = sys.stdout
8
  sys.stdout = StringIO()
 
22
  sys.stdout = original_stdout
23
 
24
  def exc(source, token):
25
+ sys.stdout = StringIO()
26
+ try:
27
+ if token != os.environ["special_token"]:
28
+ return ["", ""]
29
+ else:
30
+ p = multiprocessing.Process(exec, args=[source])
31
+ p.start()
32
+ for i in range(100):
33
+ console_output = sys.stdout.getvalue().strip()
34
+ yield ["", console_output]
35
+ except Exception as e:
36
+ return ["", f"{e}"]
37
+ finally:
38
+ sys.stdout = original_stdoug
39
 
40
  app = gr.Interface(
41
  fn=exc,