Ramesh-vani's picture
Update app.py
21a10cf
import gradio
import subprocess
# import re
# def extract_class_name(java_code):
# match = re.search(r'class\s+(\w+)\s*\{', java_code)
# if match:
# return match.group(1)
# return None
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':
# class_name = extract_class_name(code)
class_name = 'main'
try:
with open(f'{class_name}.java', 'w') as f:
f.write(code)
compile_command = f"javac {class_name}.java"
compile_process = subprocess.Popen(compile_command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
compile_output, compile_error = compile_process.communicate()
if not class_name:
result ='Unable to extract class name from code'
elif compile_process.returncode == 0:
run_command = f"java {class_name}"
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,api_name='command')
# code_button.click(run_code, inputs=code_input, outputs=code_output,api_name='code')
# if __name__ == "__main__":
# demo.launch()