Spaces:
Runtime error
Runtime error
Ramesh-vani
commited on
Commit
·
3a7bbac
1
Parent(s):
3fcc68b
Update app.py
Browse files
app.py
CHANGED
@@ -11,10 +11,148 @@ def run_command(command):
|
|
11 |
result = error.decode('utf-8')
|
12 |
return result
|
13 |
|
|
|
|
|
|
|
|
|
14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
|
16 |
gradio_interface = gradio.Interface(
|
17 |
-
fn=
|
18 |
inputs="text",
|
19 |
outputs="text",
|
20 |
examples=[
|
|
|
11 |
result = error.decode('utf-8')
|
12 |
return result
|
13 |
|
14 |
+
def run_code(request):
|
15 |
+
if request:
|
16 |
+
code = request.split('@')[0]
|
17 |
+
lang = request.split('@')[1]
|
18 |
|
19 |
+
if lang == 'python':
|
20 |
+
try:
|
21 |
+
with open('program.py', 'w') as f:
|
22 |
+
f.write(code)
|
23 |
+
command = f"python program.py"
|
24 |
+
process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
25 |
+
output, error = process.communicate()
|
26 |
+
if process.returncode == 0:
|
27 |
+
result = output.decode('utf-8')
|
28 |
+
else:
|
29 |
+
result = error.decode('utf-8')
|
30 |
+
except subprocess.CalledProcessError as e:
|
31 |
+
result = str(e)
|
32 |
+
elif lang == 'java':
|
33 |
+
try:
|
34 |
+
with open('Main.java', 'w') as f:
|
35 |
+
f.write(code)
|
36 |
+
compile_command = f"javac Main.java"
|
37 |
+
compile_process = subprocess.Popen(compile_command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
38 |
+
compile_output, compile_error = compile_process.communicate()
|
39 |
+
if compile_process.returncode == 0:
|
40 |
+
run_command = f"java Main"
|
41 |
+
run_process = subprocess.Popen(run_command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
42 |
+
run_output, run_error = run_process.communicate()
|
43 |
+
if run_process.returncode == 0:
|
44 |
+
result = run_output.decode('utf-8')
|
45 |
+
else:
|
46 |
+
result = run_error.decode('utf-8')
|
47 |
+
else:
|
48 |
+
result = compile_error.decode('utf-8')
|
49 |
+
except subprocess.CalledProcessError as e:
|
50 |
+
result = str(e)
|
51 |
+
elif lang == 'html':
|
52 |
+
result = ''
|
53 |
+
elif lang == 'js':
|
54 |
+
result = 'JS code cannot be executed directly.'
|
55 |
+
elif lang == 'css':
|
56 |
+
result = 'CSS code cannot be executed directly.'
|
57 |
+
elif lang == 'c':
|
58 |
+
try:
|
59 |
+
compile_command = f"gcc -o c_program -x c -"
|
60 |
+
compile_process = subprocess.Popen(compile_command, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
61 |
+
compile_output, compile_error = compile_process.communicate(input=code.encode('utf-8'))
|
62 |
+
if compile_process.returncode == 0:
|
63 |
+
run_command = f"./c_program"
|
64 |
+
run_process = subprocess.Popen(run_command, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
65 |
+
run_output, run_error = run_process.communicate()
|
66 |
+
if run_process.returncode == 0:
|
67 |
+
result = run_output.decode('utf-8')
|
68 |
+
else:
|
69 |
+
result = run_error.decode('utf-8')
|
70 |
+
else:
|
71 |
+
result = compile_error.decode('utf-8')
|
72 |
+
except subprocess.CalledProcessError as e:
|
73 |
+
result = str(e)
|
74 |
+
elif lang == 'cpp':
|
75 |
+
try:
|
76 |
+
compile_command = f"g++ -o cpp_program -x c++ -"
|
77 |
+
compile_process = subprocess.Popen(compile_command, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
78 |
+
compile_output, compile_error = compile_process.communicate(input=code.encode('utf-8'))
|
79 |
+
if compile_process.returncode == 0:
|
80 |
+
run_command = f"./cpp_program"
|
81 |
+
run_process = subprocess.Popen(run_command, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
82 |
+
run_output, run_error = run_process.communicate()
|
83 |
+
if run_process.returncode == 0:
|
84 |
+
result = run_output.decode('utf-8')
|
85 |
+
else:
|
86 |
+
result = run_error.decode('utf-8')
|
87 |
+
else:
|
88 |
+
result = compile_error.decode('utf-8')
|
89 |
+
except subprocess.CalledProcessError as e:
|
90 |
+
result = str(e)
|
91 |
+
elif lang == 'csharp':
|
92 |
+
try:
|
93 |
+
with open('Program.cs', 'w') as f:
|
94 |
+
f.write(code)
|
95 |
+
compile_command = f"csc Program.cs"
|
96 |
+
compile_process = subprocess.Popen(compile_command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
97 |
+
compile_output, compile_error = compile_process.communicate()
|
98 |
+
if compile_process.returncode == 0:
|
99 |
+
run_command = f"mono Program.exe"
|
100 |
+
run_process = subprocess.Popen(run_command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
101 |
+
run_output, run_error = run_process.communicate()
|
102 |
+
if run_process.returncode == 0:
|
103 |
+
result = run_output.decode('utf-8')
|
104 |
+
else:
|
105 |
+
result = run_error.decode('utf-8')
|
106 |
+
else:
|
107 |
+
result = compile_error.decode('utf-8')
|
108 |
+
except subprocess.CalledProcessError as e:
|
109 |
+
result = str(e)
|
110 |
+
elif lang == 'r':
|
111 |
+
try:
|
112 |
+
with open('script.R', 'w') as f:
|
113 |
+
f.write(code)
|
114 |
+
run_command = f"Rscript script.R"
|
115 |
+
run_process = subprocess.Popen(run_command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
116 |
+
run_output, run_error = run_process.communicate()
|
117 |
+
if run_process.returncode == 0:
|
118 |
+
result = run_output.decode('utf-8')
|
119 |
+
else:
|
120 |
+
result = run_error.decode('utf-8')
|
121 |
+
except subprocess.CalledProcessError as e:
|
122 |
+
result = str(e)
|
123 |
+
elif lang == 'php':
|
124 |
+
try:
|
125 |
+
with open('script.php', 'w') as f:
|
126 |
+
f.write(code)
|
127 |
+
run_command = f"php script.php"
|
128 |
+
run_process = subprocess.Popen(run_command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
129 |
+
run_output, run_error = run_process.communicate()
|
130 |
+
if run_process.returncode == 0:
|
131 |
+
result = run_output.decode('utf-8')
|
132 |
+
else:
|
133 |
+
result = run_error.decode('utf-8')
|
134 |
+
except subprocess.CalledProcessError as e:
|
135 |
+
result = str(e)
|
136 |
+
elif lang == 'nodejs':
|
137 |
+
try:
|
138 |
+
with open('script.js', 'w') as f:
|
139 |
+
f.write(code)
|
140 |
+
run_command = f"node script.js"
|
141 |
+
run_process = subprocess.Popen(run_command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
142 |
+
run_output, run_error = run_process.communicate()
|
143 |
+
if run_process.returncode == 0:
|
144 |
+
result = run_output.decode('utf-8')
|
145 |
+
else:
|
146 |
+
result = run_error.decode('utf-8')
|
147 |
+
except subprocess.CalledProcessError as e:
|
148 |
+
result = str(e)
|
149 |
+
else:
|
150 |
+
result = 'Language not supported yet.'
|
151 |
+
|
152 |
+
return result
|
153 |
|
154 |
gradio_interface = gradio.Interface(
|
155 |
+
fn=run_code,
|
156 |
inputs="text",
|
157 |
outputs="text",
|
158 |
examples=[
|