Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# coding: utf-8
|
| 2 |
+
|
| 3 |
+
# Author: Du Mingzhe (mingzhe@nus.edu.sg)
|
| 4 |
+
# Date: 2025-03-01
|
| 5 |
+
|
| 6 |
+
import time
|
| 7 |
+
import utils
|
| 8 |
+
import requests
|
| 9 |
+
import pandas as pd
|
| 10 |
+
import streamlit as st
|
| 11 |
+
from code_editor import code_editor
|
| 12 |
+
|
| 13 |
+
lang_map = {
|
| 14 |
+
"Python": ["python", "python", "# Don't Worry, You Can't Break It. We Promise.\n"],
|
| 15 |
+
"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"],
|
| 16 |
+
"Java": ["java", "java", "// Don't Worry, You Can't Break It. We Promise.\n"],
|
| 17 |
+
"JavaScript": ["javascript", "javascript", "// Don't Worry, You Can't Break It. We Promise.\n"],
|
| 18 |
+
"Golang": ["golang", "go", "// Don't Worry, You Can't Break It. We Promise.\n"]
|
| 19 |
+
}
|
| 20 |
+
|
| 21 |
+
def post_task(lang, code, libs=None, timeout=30, memory_profile=False):
|
| 22 |
+
url = 'https://monolith.cool/execute'
|
| 23 |
+
data = {'language': lang, 'code': code, 'libraries': libs, 'timeout': timeout, 'run_memory_profile': memory_profile}
|
| 24 |
+
response = requests.post(url, json=data)
|
| 25 |
+
return response.json()
|
| 26 |
+
|
| 27 |
+
def get_result(task_id):
|
| 28 |
+
url = f'https://monolith.cool/results/{task_id}'
|
| 29 |
+
response = requests.get(url)
|
| 30 |
+
return response.json()
|
| 31 |
+
|
| 32 |
+
# Title
|
| 33 |
+
st.title("_Monolith_ is :blue[cool]")
|
| 34 |
+
|
| 35 |
+
# Language
|
| 36 |
+
lang = st.selectbox("Language?", utils.lang_map.keys(), help="the language for submission.")
|
| 37 |
+
language = utils.lang_map[lang][0]
|
| 38 |
+
|
| 39 |
+
# Libraries
|
| 40 |
+
lib_str = st.text_input("Library?", placeholder="Package A, Package B, ... , Package N", help="if any libraries are needed. Seperate with a comma.")
|
| 41 |
+
libraries = [lib.strip() for lib in lib_str.split(",")] if lib_str else None
|
| 42 |
+
|
| 43 |
+
# Memory Profile
|
| 44 |
+
memory_profile = st.checkbox("Memory Profile?", help="Enable memory profiling for the code execution.")
|
| 45 |
+
|
| 46 |
+
# Timeout
|
| 47 |
+
timeout = st.number_input("Timeout?", min_value=1, max_value=120, value=30, help="the maximum time allowed for execution.")
|
| 48 |
+
|
| 49 |
+
# Code Editor
|
| 50 |
+
editor_buttons = [{
|
| 51 |
+
"name": "Submit",
|
| 52 |
+
"feather": "Play",
|
| 53 |
+
"primary": True,
|
| 54 |
+
"hasText": True,
|
| 55 |
+
"showWithIcon": True,
|
| 56 |
+
"commands": ["submit"],
|
| 57 |
+
"style": {"bottom": "0.44rem","right": "0.4rem"}
|
| 58 |
+
}]
|
| 59 |
+
code_prompt = utils.lang_map[lang][2]
|
| 60 |
+
response_dict = code_editor(code_prompt, lang=utils.lang_map[lang][0], height=[15,15], options={"wrap": False}, buttons=editor_buttons)
|
| 61 |
+
|
| 62 |
+
if response_dict['type'] == 'submit':
|
| 63 |
+
code = response_dict['text']
|
| 64 |
+
my_bar = st.progress(0, text=f"Code Execution starts")
|
| 65 |
+
with st.spinner('Ok, give me a sec...'):
|
| 66 |
+
response = utils.post_task(language, code, libraries, 30, memory_profile)
|
| 67 |
+
task_id = response['task_id']
|
| 68 |
+
st.write(f"Task ID: {task_id}")
|
| 69 |
+
|
| 70 |
+
for ts in range(timeout+1):
|
| 71 |
+
time.sleep(1)
|
| 72 |
+
response = utils.get_result(task_id)
|
| 73 |
+
if response['status'] in ['done', 'error', 'timeout']:
|
| 74 |
+
break
|
| 75 |
+
my_bar.progress(ts / timeout, text=f"Running ({ts}/{timeout}) s...")
|
| 76 |
+
|
| 77 |
+
if response['output_dict'] and 'stdout' in response['output_dict']:
|
| 78 |
+
st.success(response['output_dict']['stdout'])
|
| 79 |
+
|
| 80 |
+
if response['output_dict'] and 'stderr' in response['output_dict']:
|
| 81 |
+
st.warning(response['output_dict']['stderr'])
|
| 82 |
+
|
| 83 |
+
if response['status'] == "done":
|
| 84 |
+
if memory_profile:
|
| 85 |
+
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")
|
| 86 |
+
st.area_chart(pd.DataFrame(response['output_dict']['log'], columns=["timestemp", "memory"]), x='timestemp', y='memory')
|
| 87 |
+
else:
|
| 88 |
+
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")
|
| 89 |
+
|
| 90 |
+
else:
|
| 91 |
+
st.error(response)
|