Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import subprocess
|
| 3 |
+
|
| 4 |
+
def run_code_with_inputs(code: str, inputs: str) -> str:
|
| 5 |
+
"""
|
| 6 |
+
Runs the provided Pseudoku code as a console program with the given inputs.
|
| 7 |
+
|
| 8 |
+
Parameters:
|
| 9 |
+
code (str): Pseudoku code to execute.
|
| 10 |
+
inputs (str): User inputs separated by newlines.
|
| 11 |
+
|
| 12 |
+
Returns:
|
| 13 |
+
str: Combined output from the program execution.
|
| 14 |
+
"""
|
| 15 |
+
filename = 'my_program.pseudo'
|
| 16 |
+
|
| 17 |
+
try:
|
| 18 |
+
with open(filename.removesuffix('.pseudo')+'.pseudo','wb') as file:
|
| 19 |
+
file.write(code.encode('utf-8'))
|
| 20 |
+
file_path = file.name
|
| 21 |
+
|
| 22 |
+
input_data = inputs.replace("\\n", "\n")
|
| 23 |
+
|
| 24 |
+
processes, outputs, inputs = [], [], []
|
| 25 |
+
processes.append(subprocess.run(
|
| 26 |
+
["pseudoku", file_path],
|
| 27 |
+
text=True,
|
| 28 |
+
capture_output=True,
|
| 29 |
+
timeout=10
|
| 30 |
+
))
|
| 31 |
+
outputs.append(processes[-1].stdout)
|
| 32 |
+
errors.append(processes[-1].stderr)
|
| 33 |
+
|
| 34 |
+
processes, outputs, inputs = [], [], []
|
| 35 |
+
processes.append(subprocess.run(
|
| 36 |
+
["fpc", file_path.removesuffix('.pseudo')+'.pas'],
|
| 37 |
+
input=input_data,
|
| 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 |
+
if any(errors):
|
| 46 |
+
return f"Errors:\n{errors}\n\nOutput:\n{output}"
|
| 47 |
+
return output
|
| 48 |
+
|
| 49 |
+
except Exception as e:
|
| 50 |
+
return f"An error occurred: {str(e)}"
|
| 51 |
+
|
| 52 |
+
# Define the Gradio interface
|
| 53 |
+
description = (
|
| 54 |
+
"This app allows you to input Python code for a console program "
|
| 55 |
+
"and provide a list of inputs separated by newlines. It will run the code "
|
| 56 |
+
"and return the entire program output."
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
with gr.Blocks(theme=gr.themes.Default(text_size=gr.themes.sizes.text_lg)) as demo:
|
| 62 |
+
gr.Interface(
|
| 63 |
+
fn=run_code_with_inputs,
|
| 64 |
+
inputs=[
|
| 65 |
+
gr.Code(label="Python Code", lines=20, language="typescript", value="""DECLARE name
|
| 66 |
+
INPUT name
|
| 67 |
+
OUTPUT "Hello, ",name,'!' """),
|
| 68 |
+
gr.Code(label="User Inputs", lines=5, value='Alice\nBob', language=None, elem_id="input-box"),
|
| 69 |
+
],
|
| 70 |
+
outputs=gr.Code(
|
| 71 |
+
label="Program Output",
|
| 72 |
+
lines=20,
|
| 73 |
+
elem_id="output-box",
|
| 74 |
+
language=None
|
| 75 |
+
),
|
| 76 |
+
title="Pseudoku Console - by raufoon",
|
| 77 |
+
description=description,
|
| 78 |
+
submit_btn=">_ Run",
|
| 79 |
+
clear_btn=None
|
| 80 |
+
)
|
| 81 |
+
|
| 82 |
+
demo.css = """
|
| 83 |
+
#output-box, #input-box {
|
| 84 |
+
background-color: black;
|
| 85 |
+
font-family: monospace;
|
| 86 |
+
padding: 10px;
|
| 87 |
+
}
|
| 88 |
+
"""
|
| 89 |
+
if __name__ == "__main__":
|
| 90 |
+
subprocess.run("""wget https://downloads.freepascal.org/fpc/dist/3.2.2/x86_64-linux/fpc-3.2.2.x86_64-linux.tar -O fpc.tar
|
| 91 |
+
tar -xvf fpc.tar
|
| 92 |
+
cd fpc-3.2.2.x86_64-linux
|
| 93 |
+
./install.sh --prefix=$HOME/fpc
|
| 94 |
+
export PATH=$HOME/fpc/bin:$PATH"
|
| 95 |
+
""", shell=True, text=True)
|
| 96 |
+
demo.launch()
|