File size: 4,517 Bytes
f7d84c8
 
 
 
 
 
 
 
 
 
 
 
3280b6c
84fa8b4
6c28b98
01069fe
2b63191
fbdbc13
01069fe
f7d84c8
 
35854a5
f7d84c8
57fc842
f7d84c8
 
 
 
c91255a
d24fd70
bea6aa5
 
f7d84c8
 
575fe88
8657254
 
f7d84c8
 
 
49efbbb
f7d84c8
 
528e6bc
f7d84c8
 
 
 
 
 
 
 
 
 
 
 
 
 
575fe88
18983a7
f7d84c8
 
 
986a10a
fc99f8c
18983a7
f7d84c8
 
 
 
 
 
 
 
 
9d11d82
528e6bc
f7d84c8
f7f5c45
f7d84c8
 
 
 
 
f0c23a5
 
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
# coding: utf-8

# Author: Du Mingzhe ([email protected])
# Date: 2025-03-01

import time
import requests
import pandas as pd
import streamlit as st
from code_editor import code_editor

lang_map = {
    "Python": ["python", "python", "# Don't Worry, You Can't Break It. We Promise.\nprint('Hello World')\n"],
    "CPP": ["c_cpp", "cpp", "// Don't Worry, You Can't Break It. We Promise.\n// For Cpp, please make sure the program lasts at least 1 ms.\n#include <iostream>\nint main() {\n    std::cout << \"Hello, World!\" << std::endl;\n    return 0;\n}\n"],
    "Java": ["java", "java", "// Don't Worry, You Can't Break It. We Promise.\nclass HelloWorld {\n    public static void main(String[] args) {\n        System.out.println(\"Hello, World!\"); \n    }\n}\n"],
    "JavaScript": ["javascript", "javascript", "// Don't Worry, You Can't Break It. We Promise.\nconsole.log('Hello World!');\n"],
    "Golang": ["golang", "go", "// Don't Worry, You Can't Break It. We Promise.\npackage main\n\nimport \"fmt\"\n\nfunc main() {\n    fmt.Println(\"Hello, World!\")\n}\n"],
    "Ruby": ["ruby", "ruby", "# Don't Worry, You Can't Break It. We Promise.\nputs 'Hello World'\n"],
    "Rust": ["rust", "rust", "// Don't Worry, You Can't Break It. We Promise.\nfn main() {\n\tprintln!('Hello World!'); \n}\n"],
}

def post_task(lang, code, libs=None, timeout=30, run_profiling=False):
    url = 'https://monolith.cool/execute'
    data = {'language': lang, 'code': code, 'libraries': libs, 'timeout': timeout, 'run_profiling': run_profiling}
    response = requests.post(url, json=data)
    return response.json()

# Title
st.title("_Monolith_ is :blue[Cool] :sunglasses:")
st.markdown("[![GitHub Badge](https://img.shields.io/badge/GitHub-Elfsong/Monolith-blue)](https://github.com/Elfsong/Monolith)")
# st.write("Please feel free to have a try. Should you have any question, contact [email protected]")
# st.write("[UPDATE] Hosting the service is costly, so I’ve enabled the spot instance feature. DM me if you need access.")

# Language
lang = st.selectbox("Language?", lang_map.keys(), help="the language for submission.")
editor_language = lang_map[lang][0]
monolith_language = lang_map[lang][1]

# Libraries
lib_str = st.text_input("Library?", placeholder="Package A, Package B, ... , Package N", help="if any libraries are needed. Seperate with a comma.")
libraries = [lib.strip() for lib in lib_str.split(",")] if lib_str else []

# Memory Profile
profiling = st.checkbox("Time/Memory Integral?", help="Enable time-memory integral profiling for the code execution.")

# Timeout
timeout = st.number_input("Timeout?", min_value=1, max_value=120, value=30, help="the maximum time allowed for execution.")

# Code Editor
editor_buttons = [{
    "name": "Submit", 
    "feather": "Play",
    "primary": True, 
    "hasText": True, 
    "showWithIcon": True, 
    "commands": ["submit"], 
    "style": {"bottom": "0.44rem","right": "0.4rem"}
}]
code_prompt = lang_map[lang][2]
response_dict = code_editor(code_prompt, lang=editor_language, height=[15,15], options={"wrap": False}, buttons=editor_buttons)

if response_dict['type'] == 'submit':
    code = response_dict['text']

    with st.spinner('Ok, give me a sec...', show_time=True):
        response = post_task(monolith_language, code, libraries, timeout, profiling)
        task_id = response['task_id']
        st.write(f"Task ID: {task_id}")
    
    if response['output_dict'] and 'stdout' in response['output_dict']:
        st.success(response['output_dict']['stdout'])
    
    if response['output_dict'] and 'stderr' in response['output_dict']:
        st.warning(response['output_dict']['stderr'])
    
    if response['status'] == "success":
        if profiling:
            st.write(f"**Execution Time:** :blue[{response['output_dict']['duration']}] ms, **Peak Memory:** :blue[{response['output_dict']['peak_memory']}] kb, **Integral:** :blue[{response['output_dict']['integral']}] kb*ms")
            # st.write(response)
            st.area_chart(pd.DataFrame(response['output_dict']['log'], columns=["timestemp", "memory"]), x='timestemp', y='memory')
        else:
            st.write(f"**Elapsed Time:** :blue[{response['output_dict']['time_v']['elapsed_time_seconds']}], **System Time:** :blue[{response['output_dict']['time_v']['system_time']}], **User Time:** :blue[{response['output_dict']['time_v']['user_time']}], **Peak Memory:** :blue[{response['output_dict']['time_v']['max_resident_set_kb']}] kb")

    else:
        st.error(response)