Spaces:
Runtime error
Runtime error
File size: 8,681 Bytes
51857fd f276882 c7228ae f276882 01531ac 57193ba f276882 3eafb49 f276882 3a7bbac 51857fd c7228ae 32eeb01 c7228ae b08fe9a c7228ae b08fe9a c7228ae 9969cd4 c7228ae b08fe9a c7228ae b08fe9a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
import gradio
import subprocess
def run_command(command):
process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output, error = process.communicate()
if process.returncode == 0:
result = output.decode('utf-8')
return result
else:
result = error.decode('utf-8')
return result
def run_code(command):
print(command)
if command is not None:
code = command.split('@')[0]
lang = command.split('@')[1]
if lang == 'python':
try:
with open('program.py', 'w') as f:
f.write(code)
command = f"python program.py"
process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output, error = process.communicate()
if process.returncode == 0:
result = output.decode('utf-8')
else:
result = error.decode('utf-8')
except subprocess.CalledProcessError as e:
result = str(e)
elif lang == 'java':
try:
with open('Main.java', 'w') as f:
f.write(code)
compile_command = f"javac Main.java"
compile_process = subprocess.Popen(compile_command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
compile_output, compile_error = compile_process.communicate()
if compile_process.returncode == 0:
run_command = f"java Main"
run_process = subprocess.Popen(run_command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
run_output, run_error = run_process.communicate()
if run_process.returncode == 0:
result = run_output.decode('utf-8')
else:
result = run_error.decode('utf-8')
else:
result = compile_error.decode('utf-8')
except subprocess.CalledProcessError as e:
result = str(e)
elif lang == 'html':
result = ''
elif lang == 'js':
result = 'JS code cannot be executed directly.'
elif lang == 'css':
result = 'CSS code cannot be executed directly.'
elif lang == 'c':
try:
compile_command = f"gcc -o c_program -x c -"
compile_process = subprocess.Popen(compile_command, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
compile_output, compile_error = compile_process.communicate(input=code.encode('utf-8'))
if compile_process.returncode == 0:
run_command = f"./c_program"
run_process = subprocess.Popen(run_command, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
run_output, run_error = run_process.communicate()
if run_process.returncode == 0:
result = run_output.decode('utf-8')
else:
result = run_error.decode('utf-8')
else:
result = compile_error.decode('utf-8')
except subprocess.CalledProcessError as e:
result = str(e)
elif lang == 'cpp':
try:
compile_command = f"g++ -o cpp_program -x c++ -"
compile_process = subprocess.Popen(compile_command, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
compile_output, compile_error = compile_process.communicate(input=code.encode('utf-8'))
if compile_process.returncode == 0:
run_command = f"./cpp_program"
run_process = subprocess.Popen(run_command, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
run_output, run_error = run_process.communicate()
if run_process.returncode == 0:
result = run_output.decode('utf-8')
else:
result = run_error.decode('utf-8')
else:
result = compile_error.decode('utf-8')
except subprocess.CalledProcessError as e:
result = str(e)
elif lang == 'csharp':
try:
with open('Program.cs', 'w') as f:
f.write(code)
compile_command = f"csc Program.cs"
compile_process = subprocess.Popen(compile_command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
compile_output, compile_error = compile_process.communicate()
if compile_process.returncode == 0:
run_command = f"mono Program.exe"
run_process = subprocess.Popen(run_command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
run_output, run_error = run_process.communicate()
if run_process.returncode == 0:
result = run_output.decode('utf-8')
else:
result = run_error.decode('utf-8')
else:
result = compile_error.decode('utf-8')
except subprocess.CalledProcessError as e:
result = str(e)
elif lang == 'r':
try:
with open('script.R', 'w') as f:
f.write(code)
run_command = f"Rscript script.R"
run_process = subprocess.Popen(run_command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
run_output, run_error = run_process.communicate()
if run_process.returncode == 0:
result = run_output.decode('utf-8')
else:
result = run_error.decode('utf-8')
except subprocess.CalledProcessError as e:
result = str(e)
elif lang == 'php':
try:
with open('script.php', 'w') as f:
f.write(code)
run_command = f"php script.php"
run_process = subprocess.Popen(run_command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
run_output, run_error = run_process.communicate()
if run_process.returncode == 0:
result = run_output.decode('utf-8')
else:
result = run_error.decode('utf-8')
except subprocess.CalledProcessError as e:
result = str(e)
elif lang == 'nodejs':
try:
with open('script.js', 'w') as f:
f.write(code)
run_command = f"node script.js"
run_process = subprocess.Popen(run_command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
run_output, run_error = run_process.communicate()
if run_process.returncode == 0:
result = run_output.decode('utf-8')
else:
result = run_error.decode('utf-8')
except subprocess.CalledProcessError as e:
result = str(e)
else:
result = 'Language not supported yet.'
return result
# gradio_interface1 = gradio.Interface(
# fn=run_code,
# inputs="text",
# outputs="text",
# title="REST API ",
# description="This is an AI powered languages API ",
# article=""
# )
# gradio_interface2 = gradio.Interface(
# fn=run_command,
# inputs="text",
# outputs="text",
# title="REST API ",
# description="This is an AI powered command REST API ",
# article=""
# )
# gradio_interface1.launch()
# gradio_interface2.launch()
with gradio.Blocks() as demo:
gradio.Markdown("run command or run code.")
with gradio.Tab("run command"):
command_input = gradio.Textbox()
command_output = gradio.Textbox()
command_button = gradio.Button("submit command")
with gradio.Tab("run code"):
with gradio.Row():
code_input = gradio.Textbox()
code_output = gradio.Textbox()
code_button = gradio.Button("submit code@lanuage)
command_button.click(run_command, inputs=command_input, outputs=command_output)
code_button.click(run_code, inputs=code_input, outputs=code_output)
if __name__ == "__main__":
demo.launch()
|