File size: 9,237 Bytes
a147ec9
 
1b9a790
a147ec9
1b9a790
 
 
 
 
a147ec9
 
 
 
 
 
 
 
 
 
 
0d02a6a
ed2aabe
 
76d7423
0a814fc
a147ec9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1b9a790
 
a147ec9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1b9a790
 
 
 
a147ec9
1b9a790
 
 
 
a147ec9
 
 
 
 
 
 
 
 
 
1b9a790
a147ec9
 
 
1b9a790
 
 
86789dc
9432621
1b9a790
 
 
9432621
 
 
b26e570
df6e4f5
a147ec9
 
1b9a790
af46d53
a147ec9
1b9a790
fc1b02b
4a85eab
 
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
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(code,language):
    
    
        code = code.strip()
        lang = language.strip().lower()

        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(label="command")
        command_output = gradio.Textbox(label="output")
        command_button = gradio.Button("submit command")
    with gradio.Tab("run code"):
        with gradio.Row():
            code_input = gradio.Textbox(label="code")
            language_input = gradio.Textbox(label="language")
            code_output = gradio.Textbox(label="output")
            
        code_button = gradio.Button("submit code")


    command_button.click(run_command, inputs=command_input, outputs=command_output,api_name='command')
    code_button.click(run_code, inputs=[code_input,language_input], outputs=code_output,api_name='code',concurrency_limit=2000)

if __name__ == "__main__":
    demo.launch()