File size: 7,736 Bytes
51857fd
f276882
 
 
01531ac
57193ba
 
 
 
 
 
 
f276882
3eafb49
 
 
 
 
f276882
3a7bbac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51857fd
1878582
3a7bbac
57193ba
 
32eeb01
3fcc68b
 
 
1878582
51857fd
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
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",
    
    title="REST API ",
    description="This is an AI powered REST API ",
    article=""
)
gradio_interface.launch()