in output there is code copy in output, so llm will understand better ig
Browse files
app.py
CHANGED
@@ -1,54 +1,51 @@
|
|
1 |
import gradio as gr
|
2 |
import subprocess
|
|
|
3 |
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
subprocess.check_call(['pip', 'install', package])
|
16 |
-
installed_packages.append(package)
|
17 |
-
|
18 |
-
# Execute the script file with inputs and capture the output and error
|
19 |
-
input_values = '\n'.join(inputs.split(','))
|
20 |
try:
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
|
|
|
|
27 |
except subprocess.TimeoutExpired:
|
28 |
-
output =
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
import subprocess
|
3 |
+
import tempfile
|
4 |
|
5 |
+
|
6 |
+
class CodeExecutor:
|
7 |
+
def __init__(self):
|
8 |
+
self.temp_dir = tempfile.TemporaryDirectory()
|
9 |
+
|
10 |
+
def execute(self, code, inputs):
|
11 |
+
input_file = f"{self.temp_dir.name}/input.txt"
|
12 |
+
with open(input_file, "w") as f:
|
13 |
+
f.write(inputs)
|
14 |
+
|
15 |
+
output_file = f"{self.temp_dir.name}/output.txt"
|
|
|
|
|
|
|
|
|
|
|
16 |
try:
|
17 |
+
with open(output_file, "w") as f:
|
18 |
+
subprocess.run(
|
19 |
+
["python", "-c", code], check=True, timeout=5, stdin=open(input_file, "r"), stdout=f, stderr=f
|
20 |
+
)
|
21 |
+
with open(output_file, "r") as f:
|
22 |
+
output = f.read()
|
23 |
+
except subprocess.CalledProcessError as e:
|
24 |
+
output = e.output.decode("utf-8")
|
25 |
except subprocess.TimeoutExpired:
|
26 |
+
output = "Execution timed out."
|
27 |
+
except Exception as e:
|
28 |
+
output = str(e)
|
29 |
+
|
30 |
+
return f"User's Code:\n```python\n{code}\n```\n\nOutput:\n{output}"
|
31 |
+
|
32 |
+
|
33 |
+
def create_interface():
|
34 |
+
with gr.Blocks() as demo:
|
35 |
+
gr.Markdown("# Code Interpreter")
|
36 |
+
gr.Markdown("Enter Python code and inputs to execute.")
|
37 |
+
code_input = gr.Code(language="python", label="Code")
|
38 |
+
inputs_input = gr.Textbox(label="Inputs")
|
39 |
+
output_text = gr.Textbox(label="Output", lines=10)
|
40 |
+
run_button = gr.Button("Run")
|
41 |
+
run_button.click(
|
42 |
+
CodeExecutor().execute, inputs=[code_input, inputs_input], outputs=output_text
|
43 |
+
)
|
44 |
+
demo.inputs = [code_input, inputs_input]
|
45 |
+
demo.outputs = [output_text]
|
46 |
+
return demo
|
47 |
+
|
48 |
+
|
49 |
+
if __name__ == "__main__":
|
50 |
+
app = create_interface()
|
51 |
+
app.launch()
|
|