Ramesh-vani's picture
Update app.py
3eafb49
raw
history blame
No virus
7.8 kB
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_interface = gradio.Interface(
fn=run_code,
inputs="text",
outputs="text",
examples=[
["ls -l"],
["pip install bs4"]
],
title="REST API ",
description="This is an AI powered REST API ",
article=""
)
gradio_interface.launch()