fix queue
Browse files
app.py
CHANGED
@@ -1,22 +1,27 @@
|
|
|
|
1 |
import gradio as gr
|
2 |
import subprocess
|
3 |
import tempfile
|
4 |
import shutil
|
5 |
import traceback
|
6 |
import importlib.util
|
|
|
7 |
|
8 |
class CodeExecutor:
|
9 |
def __init__(self):
|
10 |
self.temp_dir = tempfile.TemporaryDirectory()
|
|
|
11 |
|
12 |
def _install_packages(self, packages):
|
13 |
for package in packages:
|
14 |
try:
|
15 |
spec = importlib.util.find_spec(package)
|
16 |
if spec is None:
|
17 |
-
subprocess.check_call(["pip", "install", package], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
18 |
except subprocess.CalledProcessError as e:
|
19 |
raise Exception(f"Error installing package {package}: {e}")
|
|
|
|
|
20 |
|
21 |
def _execute_code(self, code, inputs):
|
22 |
temp_file = f"{self.temp_dir.name}/temp.py"
|
@@ -33,7 +38,9 @@ class CodeExecutor:
|
|
33 |
process.stdin.write(input_value.encode())
|
34 |
process.stdin.write(b"\n")
|
35 |
process.stdin.close()
|
36 |
-
process.wait()
|
|
|
|
|
37 |
except Exception as e:
|
38 |
error.write(traceback.format_exc())
|
39 |
|
@@ -45,26 +52,25 @@ class CodeExecutor:
|
|
45 |
return f"User Code:\n{code}\n\nInputs:\n{', '.join(inputs)}\n\nOutput:\n{output_text}"
|
46 |
|
47 |
def execute(self, code, inputs, packages):
|
|
|
48 |
try:
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
packages = [p.strip() for p in packages.split(",")]
|
53 |
-
self._install_packages(packages)
|
54 |
-
input_values = [i.strip() for i in inputs.split(",")] if inputs else []
|
55 |
-
output = self._execute_code(code, input_values)
|
56 |
-
return output
|
57 |
except Exception as e:
|
58 |
-
|
59 |
finally:
|
60 |
-
|
|
|
|
|
61 |
|
62 |
def __del__(self):
|
|
|
63 |
shutil.rmtree(self.temp_dir.name)
|
64 |
|
65 |
def wrapper_execute(code, inputs, packages):
|
66 |
executor = CodeExecutor()
|
67 |
-
return executor.execute(code, inputs, packages)
|
68 |
|
69 |
def create_interface():
|
70 |
with gr.Blocks() as demo:
|
|
|
1 |
+
import concurrent.futures
|
2 |
import gradio as gr
|
3 |
import subprocess
|
4 |
import tempfile
|
5 |
import shutil
|
6 |
import traceback
|
7 |
import importlib.util
|
8 |
+
import time
|
9 |
|
10 |
class CodeExecutor:
|
11 |
def __init__(self):
|
12 |
self.temp_dir = tempfile.TemporaryDirectory()
|
13 |
+
self.executor = concurrent.futures.ThreadPoolExecutor(max_workers=1)
|
14 |
|
15 |
def _install_packages(self, packages):
|
16 |
for package in packages:
|
17 |
try:
|
18 |
spec = importlib.util.find_spec(package)
|
19 |
if spec is None:
|
20 |
+
subprocess.check_call(["pip", "install", package], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, timeout=10)
|
21 |
except subprocess.CalledProcessError as e:
|
22 |
raise Exception(f"Error installing package {package}: {e}")
|
23 |
+
except subprocess.TimeoutExpired:
|
24 |
+
raise Exception(f"Timed out installing package {package}")
|
25 |
|
26 |
def _execute_code(self, code, inputs):
|
27 |
temp_file = f"{self.temp_dir.name}/temp.py"
|
|
|
38 |
process.stdin.write(input_value.encode())
|
39 |
process.stdin.write(b"\n")
|
40 |
process.stdin.close()
|
41 |
+
process.wait(timeout=10)
|
42 |
+
except subprocess.TimeoutExpired:
|
43 |
+
raise Exception("Timed out executing code")
|
44 |
except Exception as e:
|
45 |
error.write(traceback.format_exc())
|
46 |
|
|
|
52 |
return f"User Code:\n{code}\n\nInputs:\n{', '.join(inputs)}\n\nOutput:\n{output_text}"
|
53 |
|
54 |
def execute(self, code, inputs, packages):
|
55 |
+
future = self.executor.submit(self._execute_code, code, inputs)
|
56 |
try:
|
57 |
+
output = future.result(timeout=10)
|
58 |
+
except concurrent.futures.TimeoutError:
|
59 |
+
raise Exception("Timed out waiting for code execution")
|
|
|
|
|
|
|
|
|
|
|
60 |
except Exception as e:
|
61 |
+
raise Exception(f"Error: {str(e)}")
|
62 |
finally:
|
63 |
+
if packages:
|
64 |
+
self._install_packages(packages.split(","))
|
65 |
+
return output
|
66 |
|
67 |
def __del__(self):
|
68 |
+
self.executor.shutdown(wait=False)
|
69 |
shutil.rmtree(self.temp_dir.name)
|
70 |
|
71 |
def wrapper_execute(code, inputs, packages):
|
72 |
executor = CodeExecutor()
|
73 |
+
return executor.execute(code, inputs.split(","), packages)
|
74 |
|
75 |
def create_interface():
|
76 |
with gr.Blocks() as demo:
|