xd11yggy commited on
Commit
84a1f31
·
verified ·
1 Parent(s): de066cc

in output there is code copy in output, so llm will understand better ig

Browse files
Files changed (1) hide show
  1. app.py +46 -49
app.py CHANGED
@@ -1,54 +1,51 @@
1
  import gradio as gr
2
  import subprocess
 
3
 
4
- def run_code(code, inputs, packages):
5
- try:
6
- # Create a temporary Python script file
7
- with open('temp_script.py', 'w') as script_file:
8
- script_file.write(code)
9
-
10
- # Install packages if specified
11
- installed_packages = []
12
- if packages:
13
- for package in packages.split(','):
14
- package = package.strip()
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
- output = subprocess.check_output(['python', 'temp_script.py'], input=input_values.encode(), stderr=subprocess.STDOUT, timeout=5)
22
- output = output.decode('utf-8')
23
- error = ''
24
- except subprocess.CalledProcessError as cpe:
25
- output = cpe.output.decode('utf-8')
26
- error = f'Error: {output}'
 
 
27
  except subprocess.TimeoutExpired:
28
- output = ''
29
- error = 'Error: Execution timed out after 5 seconds'
30
- except Exception as e:
31
- output = ''
32
- error = str(e)
33
- finally:
34
- # Uninstall packages if they were installed
35
- if installed_packages:
36
- subprocess.check_call(['pip', 'uninstall', '-y'] + installed_packages) # Added closing parenthesis here
37
- # subprocess.check_call(['pip', 'uninstall', '-y'] + installed_packages) can also be written as
38
- # subprocess.check_call(['pip', 'uninstall', '-y', *installed_packages])
39
-
40
- return output, error
41
-
42
- demo = gr.Blocks()
43
- with demo:
44
- gr.Markdown("# Code Interpreter")
45
- gr.Markdown("Enter your Python code, inputs separated by commas, and optionally specify packages to install")
46
- code_input = gr.Code(language="python", label="Code")
47
- inputs_input = gr.Textbox(label="Inputs (separated by commas)", placeholder="e.g. 1,2,3")
48
- packages_input = gr.Textbox(label="Packages (optional, separated by commas)", placeholder="e.g. numpy,pandas")
49
- output_text = gr.Textbox(label="Output")
50
- error_text = gr.Textbox(label="Error")
51
-
52
- gr.Button("Run").click(fn=run_code, inputs=[code_input, inputs_input, packages_input], outputs=[output_text, error_text])
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()