Spaces:
Sleeping

raufoon commited on
Commit
3931bae
·
verified ·
1 Parent(s): 32a521d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -20
app.py CHANGED
@@ -1,6 +1,5 @@
1
  import gradio as gr
2
  import subprocess
3
-
4
  def valid(code:str) -> bool:
5
  import re
6
  strre = r"\"([^\"\r\n\\]*(\\.[^\"\r\n\\]*)*)\"|'([^'\r\n\\]*(\\.[^'\r\n\\]*)*)'"
@@ -10,18 +9,18 @@ def valid(code:str) -> bool:
10
 
11
  def run_code_with_inputs(code: str, inputs: str) -> str:
12
  """
13
- Runs the provided Pseudoku code as a console program with the given inputs.
14
-
15
  Parameters:
16
- code (str): Pseudoku code to execute.
17
  inputs (str): User inputs separated by newlines.
18
-
19
  Returns:
20
  str: Combined output from the program execution.
21
  """
22
- if not valid(code):
23
- raise Exception("Code contains keywords which violate Pseudoku's security protocol.")
24
-
25
  filename = 'my_program.pseudo'
26
  import os
27
  pseudokey = os.getenv("pseudokey")
@@ -31,17 +30,17 @@ def run_code_with_inputs(code: str, inputs: str) -> str:
31
  file_path = file.name
32
 
33
  input_data = inputs.replace("\\n", "\n")
34
-
35
  processes, outputs, errors = [], [], []
36
  processes.append(subprocess.run(
37
- ["./pseudoku", file_path, pseudokey, '--echo-inputs'],
38
  text=True,
39
  capture_output=True,
40
  timeout=10
41
  ))
42
  outputs.append(processes[-1].stdout)
43
  errors.append(processes[-1].stderr)
44
-
45
  processes.append(subprocess.run(
46
  ["fpc", file_path.removesuffix('.pseudo')+'.pas'],
47
  text=True,
@@ -52,7 +51,7 @@ def run_code_with_inputs(code: str, inputs: str) -> str:
52
  errors.append(processes[-1].stderr)
53
 
54
  binfile = './'+file_path.removesuffix('.pseudo')
55
- print(file_path, os.path.exists(binfile))
56
  subprocess.run(["chmod", "x+", binfile])
57
  processes.append(subprocess.run(
58
  [binfile],
@@ -63,7 +62,7 @@ def run_code_with_inputs(code: str, inputs: str) -> str:
63
  ))
64
  outputs.append(processes[-1].stdout)
65
  errors.append(processes[-1].stderr)
66
-
67
  if any(errors):
68
  nl='\n'
69
  return f"Errors:\n{nl.join(errors)}\n\nOutput:\n{nl.join(outputs)}"
@@ -74,16 +73,15 @@ def run_code_with_inputs(code: str, inputs: str) -> str:
74
 
75
  # Define the Gradio interface
76
  description = (
77
- "This app allows you to input Pesudoku code for a console program "
78
- "and provide a list of inputs separated by newlines. It will run the code "
79
- "and return the entire program output."
80
  )
81
 
82
  with gr.Blocks(theme=gr.themes.Default(text_size=gr.themes.sizes.text_lg)) as demo:
83
  gr.Interface(
84
  fn=run_code_with_inputs,
85
  inputs=[
86
- gr.Code(label="Python Code", lines=20, language="typescript", value="""DECLARE name: STRING
87
  INPUT name
88
  OUTPUT "Hello, ",name,'!' """),
89
  gr.Code(label="User Inputs", lines=5, value='Alice\nBob', language=None, elem_id="input-box"),
@@ -95,12 +93,12 @@ OUTPUT "Hello, ",name,'!' """),
95
  elem_id="output-box",
96
  language=None
97
  ),
98
- title="Pseudoku Console - by raufoon",
99
  description=description,
100
  submit_btn=">_ Run",
101
  clear_btn=None
102
  )
103
-
104
  demo.css = """
105
  #output-box, #input-box {
106
  background-color: black;
@@ -109,7 +107,7 @@ OUTPUT "Hello, ",name,'!' """),
109
  }
110
  """
111
  if __name__ == "__main__":
112
- subprocess.run("""chmod +x ./pseudoku
113
  wget https://downloads.freepascal.org/fpc/dist/3.2.2/x86_64-linux/fpc-3.2.2.x86_64-linux.tar -O fpc.tar
114
  tar -xvf fpc.tar
115
  cd fpc-3.2.2.x86_64-linux
 
1
  import gradio as gr
2
  import subprocess
 
3
  def valid(code:str) -> bool:
4
  import re
5
  strre = r"\"([^\"\r\n\\]*(\\.[^\"\r\n\\]*)*)\"|'([^'\r\n\\]*(\\.[^'\r\n\\]*)*)'"
 
9
 
10
  def run_code_with_inputs(code: str, inputs: str) -> str:
11
  """
12
+ Runs the provided Pseudocode as a console program with the given inputs.
13
+
14
  Parameters:
15
+ code (str): pseudocode to execute.
16
  inputs (str): User inputs separated by newlines.
17
+
18
  Returns:
19
  str: Combined output from the program execution.
20
  """
21
+ if not valid(code):
22
+ raise Exception("Code contains keywords which violate Pseudocode+'s security protocol.")
23
+
24
  filename = 'my_program.pseudo'
25
  import os
26
  pseudokey = os.getenv("pseudokey")
 
30
  file_path = file.name
31
 
32
  input_data = inputs.replace("\\n", "\n")
33
+
34
  processes, outputs, errors = [], [], []
35
  processes.append(subprocess.run(
36
+ ["./pseudocode", file_path, pseudokey, '--echo-inputs'],
37
  text=True,
38
  capture_output=True,
39
  timeout=10
40
  ))
41
  outputs.append(processes[-1].stdout)
42
  errors.append(processes[-1].stderr)
43
+
44
  processes.append(subprocess.run(
45
  ["fpc", file_path.removesuffix('.pseudo')+'.pas'],
46
  text=True,
 
51
  errors.append(processes[-1].stderr)
52
 
53
  binfile = './'+file_path.removesuffix('.pseudo')
54
+ # print(file_path, os.path.exists(binfile))
55
  subprocess.run(["chmod", "x+", binfile])
56
  processes.append(subprocess.run(
57
  [binfile],
 
62
  ))
63
  outputs.append(processes[-1].stdout)
64
  errors.append(processes[-1].stderr)
65
+
66
  if any(errors):
67
  nl='\n'
68
  return f"Errors:\n{nl.join(errors)}\n\nOutput:\n{nl.join(outputs)}"
 
73
 
74
  # Define the Gradio interface
75
  description = (
76
+ """# Welcome to Pseudocode+!
77
+ ### Pseudocode+ runs pseudocode based on the [CAIE Pseudocode Guide](https://www.cambridgeinternational.org/Images/697401-2026-pseudocode-guide-for-teachers.pdf), applicable for exams on 2025-2029."""
 
78
  )
79
 
80
  with gr.Blocks(theme=gr.themes.Default(text_size=gr.themes.sizes.text_lg)) as demo:
81
  gr.Interface(
82
  fn=run_code_with_inputs,
83
  inputs=[
84
+ gr.Code(label="Pseudocode", lines=20, language="typescript", value="""DECLARE name: STRING
85
  INPUT name
86
  OUTPUT "Hello, ",name,'!' """),
87
  gr.Code(label="User Inputs", lines=5, value='Alice\nBob', language=None, elem_id="input-box"),
 
93
  elem_id="output-box",
94
  language=None
95
  ),
96
+ title="Pseudocode+ | by raufoon",
97
  description=description,
98
  submit_btn=">_ Run",
99
  clear_btn=None
100
  )
101
+
102
  demo.css = """
103
  #output-box, #input-box {
104
  background-color: black;
 
107
  }
108
  """
109
  if __name__ == "__main__":
110
+ subprocess.run("""chmod +x ./pseudocode
111
  wget https://downloads.freepascal.org/fpc/dist/3.2.2/x86_64-linux/fpc-3.2.2.x86_64-linux.tar -O fpc.tar
112
  tar -xvf fpc.tar
113
  cd fpc-3.2.2.x86_64-linux