Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import subprocess
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
def upload_file(filepath):
|
| 6 |
+
name = Path(filepath).name
|
| 7 |
+
return [gr.UploadButton(visible=True), gr.DownloadButton(label=f"Download {name}", value=filepath, visible=True)]
|
| 8 |
+
|
| 9 |
+
def download_file():
|
| 10 |
+
return [gr.UploadButton(visible=True), gr.DownloadButton(visible=True)]
|
| 11 |
+
def sub_pattern_with_exclusion(fullpattern, excludedsubpattern, targetpattern, replacement, text):
|
| 12 |
+
import re
|
| 13 |
+
class emptymatch:
|
| 14 |
+
def __init__(self):
|
| 15 |
+
minus1 = lambda: -1
|
| 16 |
+
self.group = minus1; self.start = minus1; self.end = minus1;
|
| 17 |
+
|
| 18 |
+
matches1 = list(re.finditer(fullpattern, text))
|
| 19 |
+
matches2 = list(re.finditer(excludedsubpattern, text))
|
| 20 |
+
# print("Matches 1 and positions:", [(match.group(), match.start(), match.end(), match) for match in matches1])
|
| 21 |
+
# print("Matches 2 and positions:", [(match.group(), match.start(), match.end(), match) for match in matches2])
|
| 22 |
+
|
| 23 |
+
matches1.extend([emptymatch()]*(len(matches2)-len(matches1)))
|
| 24 |
+
matches2.extend([emptymatch()]*(len(matches1)-len(matches2)))
|
| 25 |
+
|
| 26 |
+
matches = []
|
| 27 |
+
for match1, match2 in zip(matches1, matches2):
|
| 28 |
+
if match1.start()>=0 and match1.start() == match2.start() and match1.end() == match2.end():
|
| 29 |
+
matches.append(match1)
|
| 30 |
+
# print("Common Matches and positions:", [(match.group(), match.start(), match.end(), match) for match in matches])
|
| 31 |
+
|
| 32 |
+
# print("Matches and positions:", [(match.group(), match.start(), match.end()) for match in matches])
|
| 33 |
+
|
| 34 |
+
modified_text = text
|
| 35 |
+
|
| 36 |
+
for match in reversed(matches):
|
| 37 |
+
modified_text = modified_text[:match.start()] + re.sub(targetpattern, replacement, match.group()) + modified_text[match.end():]
|
| 38 |
+
return modified_text
|
| 39 |
+
|
| 40 |
+
def run_code_with_inputs(code: str, inputs: str) -> str:
|
| 41 |
+
"""
|
| 42 |
+
Runs the provided Python code as a console program with the given inputs.
|
| 43 |
+
|
| 44 |
+
Parameters:
|
| 45 |
+
code (str): Python code to execute.
|
| 46 |
+
inputs (str): User inputs separated by newlines.
|
| 47 |
+
|
| 48 |
+
Returns:
|
| 49 |
+
str: Combined output from the program execution.
|
| 50 |
+
"""
|
| 51 |
+
filename = 'my_program.py'
|
| 52 |
+
echo = ''
|
| 53 |
+
fullpattern = r"\.? *input *\("
|
| 54 |
+
excludedsubpattern = r" *input *\("
|
| 55 |
+
targetpattern = "input"
|
| 56 |
+
replacement = "Input"
|
| 57 |
+
modified_code = sub_pattern_with_exclusion(fullpattern, excludedsubpattern, targetpattern, replacement, code)
|
| 58 |
+
modified_code = """
|
| 59 |
+
def Input(prompt=""):
|
| 60 |
+
res = input(prompt)
|
| 61 |
+
print(res)
|
| 62 |
+
return res
|
| 63 |
+
"""+modified_code
|
| 64 |
+
|
| 65 |
+
try:
|
| 66 |
+
# Create a temporary file for the user's code
|
| 67 |
+
with open(filename.removesuffix('.py')+'.py','wb') as file:
|
| 68 |
+
file.write(modified_code.encode('utf-8'))
|
| 69 |
+
file_path = file.name
|
| 70 |
+
|
| 71 |
+
# Prepare inputs
|
| 72 |
+
input_data = inputs.replace("\\n", "\n")
|
| 73 |
+
|
| 74 |
+
# Execute the Python script with the provided inputs
|
| 75 |
+
process = subprocess.run(
|
| 76 |
+
["python", file_path],
|
| 77 |
+
input=input_data,
|
| 78 |
+
text=True,
|
| 79 |
+
capture_output=True
|
| 80 |
+
)
|
| 81 |
+
|
| 82 |
+
# Collect output and errors
|
| 83 |
+
output = ''.join(echo)+process.stdout
|
| 84 |
+
errors = process.stderr
|
| 85 |
+
|
| 86 |
+
if errors:
|
| 87 |
+
return f"Errors:\n{errors}\n\nOutput:\n{output}"
|
| 88 |
+
return output
|
| 89 |
+
|
| 90 |
+
except Exception as e:
|
| 91 |
+
return f"An error occurred: {str(e)}"
|
| 92 |
+
|
| 93 |
+
# Define the Gradio interface
|
| 94 |
+
description = (
|
| 95 |
+
"This app allows you to input Python code for a console program "
|
| 96 |
+
"and provide a list of inputs separated by newlines. It will run the code "
|
| 97 |
+
"and return the entire program output."
|
| 98 |
+
)
|
| 99 |
+
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
with gr.Blocks() as demo:
|
| 103 |
+
# Add custom CSS for black background and white monospace text for the output box
|
| 104 |
+
|
| 105 |
+
gr.Interface(
|
| 106 |
+
fn=run_code_with_inputs,
|
| 107 |
+
inputs=[
|
| 108 |
+
gr.Code(label="Python Code", language="python", value="name=input(\"What's your name? \")\nprint(f\"Hello, {name}!\")"),
|
| 109 |
+
gr.Textbox(label="User Inputs", lines=5, value='Alice\nBob',placeholder="Enter inputs separated by newlines...")
|
| 110 |
+
],
|
| 111 |
+
outputs=gr.Code(
|
| 112 |
+
label="Program Output",
|
| 113 |
+
lines=10,
|
| 114 |
+
elem_id="output-box",
|
| 115 |
+
language=None
|
| 116 |
+
),
|
| 117 |
+
title="Python Console Runner",
|
| 118 |
+
description=description,
|
| 119 |
+
submit_btn=">_ Run"
|
| 120 |
+
)
|
| 121 |
+
|
| 122 |
+
|
| 123 |
+
# current_file = ['my_program.py']
|
| 124 |
+
# with gr.Row():
|
| 125 |
+
# t = gr.Textbox(label="File Name", lines=1, placeholder="Enter file name...", value="my_program.py", interactive=True)
|
| 126 |
+
# def get_file():
|
| 127 |
+
# subprocess.run(["mv", current_file[0], t.value], check=False)
|
| 128 |
+
# current_file[0] = t.value
|
| 129 |
+
# return f"/{current_file[0]}"
|
| 130 |
+
# d = gr.DownloadButton(label="Download .py file", value=get_file, visible=True)
|
| 131 |
+
|
| 132 |
+
demo.css = """
|
| 133 |
+
#output-box {
|
| 134 |
+
background-color: black;
|
| 135 |
+
font-family: monospace;
|
| 136 |
+
padding: 10px;
|
| 137 |
+
}
|
| 138 |
+
"""
|
| 139 |
+
if __name__ == "__main__":
|
| 140 |
+
demo.launch()
|