Spaces:
Running
Running
terryyz commited on
Commit ·
fe3f5b0
1
Parent(s): f1aac33
init
Browse filesFiles changed (16)
- .gitignoreAdded+208
- README.md+2-2
- api_config.yamlAdded+216
- app.pyAdded+777
- chat_state.pyAdded+259
- completion.pyAdded+1304
- conversation.pyAdded
- requirements.txtAdded+21
- sandbox/__init__.pyAdded
- sandbox/code_analyzer.pyAdded+935
- sandbox/code_runner.pyAdded+1612
- sandbox/constants.pyAdded+148
- sandbox/prompts.pyAdded+393
- sandbox/sandbox_manager.pyAdded+254
- sandbox/sandbox_state.pyAdded+83
- sandbox/sandbox_telemetry.pyAdded+221
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208# Byte-compiled / optimized / DLL files__pycache__/*.py[codz]*$py.class
# C extensions*.so
# Distribution / packaging.Pythonbuild/develop-eggs/dist/downloads/eggs/.eggs/lib/lib64/parts/sdist/var/wheels/share/python-wheels/*.egg-info/.installed.cfg*.eggMANIFESTrun.sh
# PyInstaller# Usually these files are written by a python script from a template# before PyInstaller builds the exe, so as to inject date/other infos into it.*.manifest*.spec
# Installer logspip-log.txtpip-delete-this-directory.txt
# Unit test / coverage reportshtmlcov/.tox/.nox/.coverage.coverage.*.cachenosetests.xmlcoverage.xml*.cover*.py.cover.hypothesis/.pytest_cache/cover/
# Translations*.mo*.pot
# Django stuff:*.loglocal_settings.pydb.sqlite3db.sqlite3-journal
# Flask stuff:instance/.webassets-cache
# Scrapy stuff:.scrapy
# Sphinx documentationdocs/_build/
# PyBuilder.pybuilder/target/
# Jupyter Notebook.ipynb_checkpoints
# IPythonprofile_default/ipython_config.py
# pyenv# For a library or package, you might want to ignore these files since the code is# intended to run in multiple environments; otherwise, check them in:# .python-version
# pipenv# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.# However, in case of collaboration, if having platform-specific dependencies or dependencies# having no cross-platform support, pipenv may install dependencies that don't work, or not# install all needed dependencies.#Pipfile.lock
# UV# Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.# This is especially recommended for binary packages to ensure reproducibility, and is more# commonly ignored for libraries.#uv.lock
# poetry# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.# This is especially recommended for binary packages to ensure reproducibility, and is more# commonly ignored for libraries.# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control#poetry.lock#poetry.toml
# pdm# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.# pdm recommends including project-wide configuration in pdm.toml, but excluding .pdm-python.# https://pdm-project.org/en/latest/usage/project/#working-with-version-control#pdm.lock#pdm.toml.pdm-python.pdm-build/
# pixi# Similar to Pipfile.lock, it is generally recommended to include pixi.lock in version control.#pixi.lock# Pixi creates a virtual environment in the .pixi directory, just like venv module creates one# in the .venv directory. It is recommended not to include this directory in version control..pixi
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm__pypackages__/
# Celery stuffcelerybeat-schedulecelerybeat.pid
# SageMath parsed files*.sage.py
# Environments.env.envrc.venvenv/venv/ENV/env.bak/venv.bak/
# Spyder project settings.spyderproject.spyproject
# Rope project settings.ropeproject
# mkdocs documentation/site
# mypy.mypy_cache/.dmypy.jsondmypy.json
# Pyre type checker.pyre/
# pytype static type analyzer.pytype/
# Cython debug symbolscython_debug/
# PyCharm# JetBrains specific template is maintained in a separate JetBrains.gitignore that can# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore# and can be added to the global gitignore or merged into this file. For a more nuclear# option (not recommended) you can uncomment the following to ignore the entire idea folder.#.idea/
# Abstra# Abstra is an AI-powered process automation framework.# Ignore directories containing user credentials, local state, and settings.# Learn more at https://abstra.io/docs.abstra/
# Visual Studio Code# Visual Studio Code specific template is maintained in a separate VisualStudioCode.gitignore # that can be found at https://github.com/github/gitignore/blob/main/Global/VisualStudioCode.gitignore# and can be added to the global gitignore or merged into this file. However, if you prefer, # you could uncomment the following to ignore the entire vscode folder# .vscode/
# Ruff stuff:.ruff_cache/
# PyPI configuration file.pypirc
# Cursor# Cursor is an AI-powered code editor. `.cursorignore` specifies files/directories to# exclude from AI features like autocomplete and code analysis. Recommended for sensitive data# refer to https://docs.cursor.com/context/ignore-files.cursorignore.cursorindexingignore
# Marimomarimo/_static/marimo/_lsp/__marimo__/123456789101112---title: Arenaemoji: 🚀colorFrom: pinkcolorTo: yellowsdk: gradiosdk_version: 5.44.1app_file: app.pypinned: falselicense: apache-2.0---123456789101112---title: BigCodeArenaemoji: 🚀colorFrom: pinkcolorTo: yellowsdk: gradiosdk_version: 5.44.1app_file: app.pypinned: truelicense: apache-2.0---
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216gpt-4o-mini-2024-07-18: model: gpt-4o-mini-2024-07-18 endpoints: null api_type: openai parallel: 32 max_tokens: 8192 temperature: 0.0
gpt-4o-2024-11-20: model: gpt-4o-2024-11-20 endpoints: null api_type: openai parallel: 32 max_tokens: 8192 temperature: 0.0
# o1-2024-12-17:# model: o1-2024-12-17# endpoints: null# api_type: openai# parallel: 32# max_tokens: 8192# temperature: 0.0
# o4-mini-2025-04-16:# model: o4-mini-2025-04-16# endpoints: null# api_type: openai_thinking# parallel: 32# max_tokens: 8192# temperature: 1.0
# o3-mini-2025-01-31:# model: o3-mini-2025-01-31# endpoints: null# api_type: openai_thinking# parallel: 32# max_tokens: 8192# temperature: 0.0
# gemini-2.0-flash-001:# model: google/gemini-2.0-flash-001# endpoints:# - api_base: https://openrouter.ai/api/v1# api_key: ${OPENROUTER_API_KEY}# api_type: openai# parallel: 32# max_tokens: 81920# temperature: 0.0
# gemini-2.5-pro:# model: google/gemini-2.5-pro# endpoints:# - api_base: https://openrouter.ai/api/v1# api_key: ${OPENROUTER_API_KEY}# api_type: openai# parallel: 32# max_tokens: 8192# temperature: 0.0
# gemini-2.5-flash:# model: google/gemini-2.5-flash# endpoints:# - api_base: https://openrouter.ai/api/v1# api_key: ${OPENROUTER_API_KEY}# api_type: openai# parallel: 32# max_tokens: 8192# temperature: 0.0
# claude35_haiku:# model: bedrock/anthropic.claude-3-5-haiku-20241022-v1:0# endpoints: null# api_type: litellm# parallel: 32# max_tokens: 8192# temperature: 0.0
# claude35_sonnet:# model: bedrock/anthropic.claude-3-5-sonnet-20241022-v2:0# endpoints: null# api_type: litellm# parallel: 32# max_tokens: 8192# temperature: 0.0
# claude37_sonnet:# model: bedrock/us.anthropic.claude-3-7-sonnet-20250219-v1:0# endpoints: null# api_type: litellm# parallel: 32# max_tokens: 8192# temperature: 0.0
# qwen3-coder:# model: qwen/qwen3-coder# endpoints:# - api_base: https://openrouter.ai/api/v1# api_key: ${OPENROUTER_API_KEY}# api_type: openai# parallel: 32# max_tokens: 8192# temperature: 0.0
# kimi-k2:# model: moonshotai/kimi-k2# endpoints:# - api_base: https://openrouter.ai/api/v1# api_key: ${OPENROUTER_API_KEY}# api_type: openai# parallel: 32# max_tokens: 8192# temperature: 0.0
# claude-4-sonnet:# model: bedrock/us.anthropic.claude-sonnet-4-20250514-v1:0# endpoints: null# api_type: litellm# parallel: 16# max_tokens: 8192# temperature: 0.0
# claude-4-opus:# model: bedrock/us.anthropic.claude-opus-4-20250514-v1:0# endpoints: null# api_type: litellm# parallel: 16# max_tokens: 8192# temperature: 0.0
# gpt-oss-120b:# model: openai/gpt-oss-120b# endpoints:# - api_base: https://openrouter.ai/api/v1# api_key: ${OPENROUTER_API_KEY}# api_type: openai_thinking# parallel: 32# max_tokens: 8192# temperature: 1.0
# gpt-oss-20b:# model: openai/gpt-oss-20b# endpoints:# - api_base: https://openrouter.ai/api/v1# api_key: ${OPENROUTER_API_KEY}# api_type: openai_thinking# parallel: 32# max_tokens: 8192# temperature: 1.0
# deepseek-chat-v3-0324:# model: deepseek/deepseek-chat-v3-0324# endpoints:# - api_base: https://openrouter.ai/api/v1# api_key: ${OPENROUTER_API_KEY}# api_type: openai# parallel: 32# max_tokens: 8192# temperature: 0.0
# deepseek-chat-v3.1:# model: deepseek-chat# endpoints:# - api_base: https://api.deepseek.com# api_key: ${DEEPSEEK_API_KEY}# api_type: openai# parallel: 32# max_tokens: 8192# temperature: 0.0
# glm-4.5:# model: z-ai/glm-4.5# endpoints:# - api_base: https://openrouter.ai/api/v1# api_key: ${OPENROUTER_API_KEY}# api_type: openai# parallel: 32# max_tokens: 8192# temperature: 0.0
# gpt-4.1-2025-04-14:# model: gpt-4.1-2025-04-14# endpoints: null# api_type: openai# parallel: 32# max_tokens: 8192# temperature: 0.0
# deepseek-r1-0528:# model: deepseek/deepseek-r1-0528# endpoints:# - api_base: https://openrouter.ai/api/v1# api_key: ${OPENROUTER_API_KEY}# api_type: openai_thinking# parallel: 32# max_tokens: 81920# temperature: 1.0
# gpt-5-2025-08-07:# model: gpt-5-2025-08-07# endpoints: null# api_type: openai_thinking# parallel: 32# max_tokens: 8192# temperature: 1.0
# grok-code:# model: x-ai/grok-code-fast-1# endpoints:# - api_base: https://openrouter.ai/api/v1# api_key: ${OPENROUTER_API_KEY}# api_type: openai_thinking# parallel: 32# max_tokens: 8192# temperature: 1.0123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777"""Simple BigCodeArena - A simplified AI coding battle arenaFocuses on core functionality: two models, automatic code extraction, and execution"""
import gradio as grfrom gradio_sandboxcomponent import SandboxComponent
# Import completion utilitiesfrom completion import make_config, registered_api_completion
# Import code extraction utilitiesfrom sandbox.code_analyzer import ( SandboxEnvironment, extract_code_from_markdown, )
# Import sandbox execution functionsfrom sandbox.code_runner import ( run_html_sandbox, run_react_sandbox, run_vue_sandbox, run_pygame_sandbox, run_gradio_sandbox, run_streamlit_sandbox, run_code_interpreter, run_c_code, run_cpp_code, run_java_code, run_golang_code, run_rust_code, mermaid_to_html)
# Create a proper sandbox state structuredef create_sandbox_state() -> dict: """Create a new sandbox state for a model""" return { 'enable_sandbox': True, 'enabled_round': 0, 'sandbox_run_round': 0, 'edit_round': 0, 'sandbox_environment': SandboxEnvironment.AUTO, 'auto_selected_sandbox_environment': None, 'sandbox_instruction': "Run the extracted code in the appropriate sandbox environment", 'code_to_execute': "", 'code_dependencies': ([], []), 'btn_list_length': 5, 'sandbox_id': None, 'chat_session_id': None, 'conv_id': None, "sandbox_output": None, "sandbox_error": None, }
def reset_sandbox_state(state: dict) -> dict: """Reset the sandbox state""" state['enabled_round'] = 0 state['sandbox_run_round'] = 0 state['edit_round'] = 0 state['auto_selected_sandbox_environment'] = None state['code_to_execute'] = "" state['code_dependencies'] = ([], []) state['sandbox_error'] = None state['sandbox_output'] = None state['sandbox_id'] = None state['conv_id'] = None state['chat_session_id'] = None return state
# Load API configurationdef load_api_config(): """Load API configuration from yaml file""" try: config = make_config("api_config.yaml") return config except Exception as e: print(f"Error loading API config: {e}") return {}
# Global variablesapi_config = load_api_config()available_models = list(api_config.keys()) if api_config else []
def get_random_models(): """Get two random models from available models""" if len(available_models) < 2: return available_models[0] if available_models else None, available_models[0] if available_models else None import random models = random.sample(available_models, 2) return models[0], models[1]
def create_chat_state(model_name: str) -> dict: """Create a new chat state for a model""" return { "model_name": model_name, "messages": [], "sandbox_state": create_sandbox_state() }
def generate_response_with_completion(state, temperature, max_tokens): """Generate response using the completion API system with full conversation history""" if state is None: return state, ""
# Get the last user message user_message = None for msg in reversed(state["messages"]): if msg["role"] == "user": user_message = msg["content"] break if not user_message: return state, ""
# Prepare messages for API call - include full conversation history messages = [] for msg in state["messages"]: if msg["role"] in ["user", "assistant"] and msg["content"] is not None: messages.append({"role": msg["role"], "content": msg["content"]}) # Get model config model_name = state["model_name"] if model_name not in api_config: print(f"Model {model_name} not found in config") return state, f"Error: Model {model_name} not configured" model_config = api_config[model_name] api_type = model_config.get("api_type", "openai") # retrieve the api completion function from register api_completion_func = registered_api_completion[api_type] # build arguments for api completions # Use the actual model identifier from config, not the display name actual_model = model_config.get("model", model_name) kwargs = { "model": actual_model, "temperature": temperature, "max_tokens": max_tokens, "api_dict": model_config.get("endpoints", [{}])[0] if model_config.get("endpoints") else None, "messages": messages, } output = api_completion_func(**kwargs) # Extract the answer from the response if isinstance(output, dict) and "answer" in output: response_text = output["answer"] return state, response_text else: error_msg = f"Error: Invalid response format from {api_type}" print(error_msg) return state, error_msg
def extract_and_execute_code(message, sandbox_state): """Extract code from message and prepare for execution""" if not message: return sandbox_state, "", "" # Extract code using the same logic as code_runner.py extract_result = extract_code_from_markdown( message=message, enable_auto_env=True ) if extract_result is None: return sandbox_state, "", "" code, code_language, code_dependencies, env_selection = extract_result # Update sandbox state (now a dictionary) sandbox_state['code_to_execute'] = code sandbox_state['code_dependencies'] = code_dependencies sandbox_state['auto_selected_sandbox_environment'] = env_selection return sandbox_state, code, str(env_selection)
def add_text_and_generate(state0, state1, text, temperature, max_tokens, model_a, model_b): """Add text and generate responses for both models""" if not text.strip(): return state0, state1, "", "", "", "", "", "", "", "", "", "", "", "" # Initialize states if needed if state0 is None or state1 is None: if state0 is None: state0 = create_chat_state(model_a) if state1 is None: state1 = create_chat_state(model_b) print(f"Models: {state0['model_name']} vs {state1['model_name']}") # Add user message to both states state0["messages"].append({"role": "user", "content": text}) state1["messages"].append({"role": "user", "content": text}) # Generate responses state0, response0 = generate_response_with_completion(state0, temperature, max_tokens) state1, response1 = generate_response_with_completion(state1, temperature, max_tokens) # Add the assistant responses to the message history state0["messages"].append({"role": "assistant", "content": response0}) state1["messages"].append({"role": "assistant", "content": response1}) # Format chat history for display chat0 = format_chat_history(state0["messages"]) chat1 = format_chat_history(state1["messages"]) # Extract code from responses for sandbox sandbox_state0 = state0.get("sandbox_state", create_sandbox_state()) sandbox_state1 = state1.get("sandbox_state", create_sandbox_state()) _, code0, env0 = extract_and_execute_code(response0, sandbox_state0) _, code1, env1 = extract_and_execute_code(response1, sandbox_state1) # Update sandbox states in the main states state0["sandbox_state"] = sandbox_state0 state1["sandbox_state"] = sandbox_state1 # Clear previous sandbox outputs when new message is sent sandbox_output0 = "" sandbox_output1 = "" sandbox_component_update0 = gr.update(visible=False) sandbox_component_update1 = gr.update(visible=False) # Also clear the sandbox view components to show fresh results sandbox_view_a = "" sandbox_view_b = "" if code0.strip(): # Get the dependencies from the sandbox state dependencies0 = sandbox_state0.get('code_dependencies', ([], [])) print(f"DEBUG: Running code0 with dependencies: {dependencies0}") sandbox_url0, sandbox_output0, sandbox_error0 = run_sandbox_code(sandbox_state0, code0, dependencies0) print(f"DEBUG: Code0 result - URL: {sandbox_url0}, Output: {sandbox_output0[:100] if sandbox_output0 else 'None'}, Error: {sandbox_error0[:100] if sandbox_error0 else 'None'}") # Check if this is a web-based environment that should use SandboxComponent env_type = sandbox_state0.get('auto_selected_sandbox_environment') or sandbox_state0.get('sandbox_environment') print(f"DEBUG: Model A environment type: {env_type}") # Use the URL directly from the function return if sandbox_url0: sandbox_component_update0 = gr.update(value=(sandbox_url0, True, []), visible=True) # Update sandbox view with output and errors if sandbox_output0: sandbox_view_a += f"# Output\n{sandbox_output0}" if sandbox_error0: sandbox_view_a += f"# Errors\n{sandbox_error0}" if code1.strip(): # Get the dependencies from the sandbox state dependencies1 = sandbox_state1.get('code_dependencies', ([], [])) print(f"DEBUG: Running code1 with dependencies: {dependencies1}") sandbox_url1, sandbox_output1, sandbox_error1 = run_sandbox_code(sandbox_state1, code1, dependencies1) print(f"DEBUG: Code1 result - URL: {sandbox_url1}, Output: {sandbox_output1[:100] if sandbox_output1 else 'None'}, Error: {sandbox_error1[:100] if sandbox_error1 else 'None'}") # Check if this is a web-based environment that should use SandboxComponent env_type = sandbox_state1.get('auto_selected_sandbox_environment') or sandbox_state1.get('sandbox_environment') print(f"DEBUG: Model B environment type: {env_type}") # Use the URL directly from the function return if sandbox_url1: sandbox_component_update1 = gr.update(value=(sandbox_url1, True, []), visible=True) if sandbox_output1: sandbox_view_b += f"## Output\n{sandbox_output1}" if sandbox_error1: sandbox_view_b += f"## Errors\n{sandbox_error1}" # Calculate conversation statistics turn_count_a = len([msg for msg in state0["messages"] if msg["role"] == "assistant" and msg["content"]]) turn_count_b = len([msg for msg in state1["messages"] if msg["role"] == "assistant" and msg["content"]]) # Format conversation statistics chat_stats_a = f"**Conversation:** {turn_count_a} turns | **Total Messages:** {len(state0['messages'])}" chat_stats_b = f"**Conversation:** {turn_count_b} turns | **Total Messages:** {len(state1['messages'])}" return state0, state1, chat0, chat1, response0, response1, code0, code1, env0, env1, sandbox_state0, sandbox_state1, sandbox_output0, sandbox_output1, sandbox_component_update0, sandbox_component_update1, chat_stats_a, chat_stats_b, sandbox_view_a, sandbox_view_b
def format_chat_history(messages): """Format messages for chat display with turn numbers""" formatted = [] for msg in messages: if msg["role"] == "user" and msg["content"]: # Add turn number to user messages formatted.append({ "role": "user", "content": msg['content'] }) elif msg["role"] == "assistant" and msg["content"]: # Add turn number to assistant messages formatted.append({ "role": "assistant", "content": msg['content'] }) return formatted
def clear_chat(state0, state1): """Clear chat history""" if state0 and "sandbox_state" in state0: reset_sandbox_state(state0["sandbox_state"]) if state1 and "sandbox_state" in state1: reset_sandbox_state(state1["sandbox_state"]) # Get current model names for display model_a, model_b = get_random_models() return None, None, "", "", "", "", "", "", "", "", "", "", "", "", gr.update(visible=False), gr.update(visible=False), "**Conversation:** 0 turns | **Total Messages:** 0", "**Conversation:** 0 turns | **Total Messages:** 0", "", "", f"**Model A:** {model_a}", f"**Model B:** {model_b}"
def run_sandbox_code(sandbox_state: dict, code: str, dependencies: tuple) -> tuple[str, str, str]: """Run code in the appropriate sandbox environment""" if not code.strip(): return "", "", "No code to run" # Update sandbox state sandbox_state['code_to_execute'] = code sandbox_state['code_dependencies'] = dependencies # Determine environment env = sandbox_state.get('auto_selected_sandbox_environment') or sandbox_state.get('sandbox_environment') try: if env == SandboxEnvironment.HTML: sandbox_url, sandbox_id, stderr = run_html_sandbox(code, dependencies, sandbox_state.get('sandbox_id')) sandbox_state['sandbox_id'] = sandbox_id return sandbox_url, "", stderr elif env == SandboxEnvironment.REACT: result = run_react_sandbox(code, dependencies, sandbox_state.get('sandbox_id')) sandbox_state['sandbox_id'] = result['sandbox_id'] return result['sandbox_url'], "", result['stderr'] elif env == SandboxEnvironment.VUE: result = run_vue_sandbox(code, dependencies, sandbox_state.get('sandbox_id')) sandbox_state['sandbox_id'] = result['sandbox_id'] return result['sandbox_url'], "", result['stderr'] elif env == SandboxEnvironment.PYGAME: result = run_pygame_sandbox(code, dependencies, sandbox_state.get('sandbox_id')) sandbox_state['sandbox_id'] = result['sandbox_id'] return result['sandbox_url'], "", result['stderr'] elif env == SandboxEnvironment.GRADIO: sandbox_url, sandbox_id, stderr = run_gradio_sandbox(code, dependencies, sandbox_state.get('sandbox_id')) sandbox_state['sandbox_id'] = sandbox_id return sandbox_url, "", stderr elif env == SandboxEnvironment.STREAMLIT: sandbox_url, sandbox_id, stderr = run_streamlit_sandbox(code, dependencies, sandbox_state.get('sandbox_id')) sandbox_state['sandbox_id'] = sandbox_id return sandbox_url, "", stderr elif env == SandboxEnvironment.MERMAID: # Convert Mermaid to HTML and run in HTML sandbox html_code = mermaid_to_html(code, theme='light') sandbox_url, sandbox_id, stderr = run_html_sandbox(html_code, dependencies, sandbox_state.get('sandbox_id')) sandbox_state['sandbox_id'] = sandbox_id return sandbox_url, "", stderr elif env == SandboxEnvironment.PYTHON_RUNNER: output, stderr = run_code_interpreter(code, 'python', dependencies) return "", output, stderr elif env == SandboxEnvironment.JAVASCRIPT_RUNNER: html_code = javascript_to_html(code) output, stderr = run_html_sandbox(html_code, dependencies, sandbox_state.get('sandbox_id')) return "", output, stderr elif env == SandboxEnvironment.C_RUNNER: output, stderr = run_c_code(code, sandbox_state.get('sandbox_id')) return "", output, stderr elif env == SandboxEnvironment.CPP_RUNNER: output, stderr = run_cpp_code(code, sandbox_state.get('sandbox_id')) return "", output, stderr elif env == SandboxEnvironment.JAVA_RUNNER: output, stderr = run_java_code(code, sandbox_state.get('sandbox_id')) return "", output, stderr elif env == SandboxEnvironment.GOLANG_RUNNER: output, stderr = run_golang_code(code, sandbox_state.get('sandbox_id')) return "", output, stderr elif env == SandboxEnvironment.RUST_RUNNER: output, stderr = run_rust_code(code, sandbox_state.get('sandbox_id')) return "", output, stderr else: # Fallback to Python runner output, stderr = run_code_interpreter(code, 'python', dependencies) return "", output, stderr except Exception as e: return "", "", str(e)
def build_ui(): """Build a UI for the coding arena with integrated sandbox""" # Get random models for this session model_a, model_b = get_random_models() with gr.Blocks(title="BigCodeArena") as demo: gr.Markdown("# BigCodeArena - Start Your Vibe Coding!") # Model display (non-interactive) with gr.Row(): with gr.Column(): model_display_a = gr.Markdown(f"**Model A:** {model_a}", visible=False) with gr.Column(): model_display_b = gr.Markdown(f"**Model B:** {model_b}", visible=False) # Sandbox section with tabs for each model - Collapsible and open by default with gr.Accordion("🏗️ Code Execution & Sandbox", open=True): with gr.Row(): # Model A Sandbox with gr.Column(): gr.Markdown("### Model A Sandbox") with gr.Tabs(): with gr.Tab("View"): sandbox_view_a = gr.Markdown("**Sandbox output will appear here automatically**") sandbox_component_a = SandboxComponent( value=("", False, []), label="Model A Sandbox", visible=False ) with gr.Tab("Code"): code_a = gr.Code( label="Extracted Code", language="python", lines=8, interactive=False ) # Model B Sandbox with gr.Column(): gr.Markdown("### Model B Sandbox") with gr.Tabs(): with gr.Tab("View"): sandbox_view_b = gr.Markdown("**Sandbox output will appear here automatically**") sandbox_component_b = SandboxComponent( value=("", False, []), label="Model B Sandbox", visible=False ) with gr.Tab("Code"): code_b = gr.Code( label="Extracted Code", language="python", lines=8, interactive=False )
# Main chat interface - Collapsible and hidden by default with gr.Accordion("💬 Chat Interface", open=False): with gr.Row(): with gr.Column(): gr.Markdown("## Model A") chatbot_a = gr.Chatbot( label="Model A", height=300, show_copy_button=True, type="messages" ) chat_stats_a = gr.Markdown("**Conversation:** 0 turns") with gr.Column(): gr.Markdown("## Model B") chatbot_b = gr.Chatbot( label="Model B", height=300, show_copy_button=True, type="messages" ) chat_stats_b = gr.Markdown("**Conversation:** 0 turns") # Input section with gr.Row(): text_input = gr.Textbox( label="Enter your coding prompt", placeholder="e.g., 'Write a Python function to calculate fibonacci numbers'", lines=1 ) # Control buttons with gr.Row(): send_btn = gr.Button("🚀 Send to Both Models", variant="primary", size="lg") clear_btn = gr.Button("🗑️ Clear Chat", variant="secondary") refresh_models_btn = gr.Button("🔄 New Random Models", variant="secondary") # Advanced Settings (Collapsible) with gr.Accordion("⚙️ Advanced Settings", open=False): with gr.Row(): with gr.Column(scale=1): temperature = gr.Slider( minimum=0.0, maximum=1.0, value=0.7, step=0.1, label="Temperature" ) with gr.Column(scale=1): max_tokens = gr.Slider( minimum=100, maximum=4000, value=1000, step=100, label="Max Tokens" ) # Event handlers # Create state variables for the run buttons state0_var = gr.State() state1_var = gr.State() # Create response components (hidden but needed for outputs) response_a = gr.Markdown("", visible=False) response_b = gr.Markdown("", visible=False) # Create a wrapper function that handles both the main execution and state update def send_and_update_state(state0, state1, text, temp, max_tok, model_a, model_b): print(f"DEBUG: send_and_update_state called with text: {text[:50] if text else 'None'}") # Call the main function result = add_text_and_generate(state0, state1, text, temp, max_tok, model_a, model_b) # Extract the state from the result new_state0, new_state1 = result[0], result[1] print(f"DEBUG: send_and_update_state returning new_state0: {type(new_state0)}, new_state1: {type(new_state1)}") # Return all the original outputs plus the updated state for run buttons # Make sure all outputs are properly formatted for their expected types return ( new_state0, # state0 new_state1, # state1 result[2], # chatbot_a (chat0) result[3], # chatbot_b (chat1) result[4], # response_a (response0) result[5], # response_b (response1) result[6], # code_a (code0) result[7], # code_b (code1) result[10], # sandbox_state0 result[11], # sandbox_state1 result[12], # sandbox_output0 result[13], # sandbox_output1 result[14], # sandbox_component_update0 result[15], # sandbox_component_update1 result[16], # chat_stats_a result[17], # chat_stats_b result[18], # sandbox_view_a result[19], # sandbox_view_b new_state0, # state0_var new_state1, # state1_var "", # Clear text input f"**Model A:** {model_a}", # Update model display A f"**Model B:** {model_b}", # Update model display B ) send_btn.click( fn=send_and_update_state, inputs=[ state0_var, # state0 state1_var, # state1 text_input, temperature, max_tokens, gr.State(model_a), # Use fixed model A gr.State(model_b) # Use fixed model B ], outputs=[ state0_var, # state0 state1_var, # state1 chatbot_a, chatbot_b, response_a, response_b, code_a, code_b, gr.State(), # sandbox_state0 gr.State(), # sandbox_state1 sandbox_view_a, # sandbox output for model A sandbox_view_b, # sandbox output for model B sandbox_component_a, # sandbox component for model A sandbox_component_b, # sandbox component for model B chat_stats_a, # Conversation statistics for model A chat_stats_b, # Conversation statistics for model B sandbox_view_a, # Sandbox view for model A sandbox_view_b, # Sandbox view for model B state0_var, # Updated state for run button A state1_var, # Updated state for run button B text_input, # Clear the text input after sending model_display_a, # Update model display A model_display_b, # Update model display B ] ) clear_btn.click( fn=clear_chat, inputs=[gr.State(), gr.State()], outputs=[ gr.State(None), gr.State(None), chatbot_a, chatbot_b, response_a, response_b, code_a, code_b, gr.State(None), gr.State(None), sandbox_view_a, sandbox_view_b, sandbox_component_a, sandbox_component_b, state0_var, # Reset state for run button A state1_var, # Reset state for run button B chat_stats_a, # Reset conversation statistics for model A chat_stats_b, # Reset conversation statistics for model B sandbox_view_a, # Reset sandbox view for model A sandbox_view_b, # Reset sandbox view for model B model_display_a, # Reset model display A model_display_b, # Reset model display B ] ) # Refresh models button handler def refresh_models(): new_model_a, new_model_b = get_random_models() return ( None, # Reset state0 None, # Reset state1 "", # Clear chat A "", # Clear chat B "", # Clear response A "", # Clear response B "", # Clear code A "", # Clear code B gr.State(None), # Reset sandbox state A gr.State(None), # Reset sandbox state B "", # Clear sandbox view A "", # Clear sandbox view B gr.update(visible=False), # Hide sandbox component A gr.update(visible=False), # Hide sandbox component B "**Conversation:** 0 turns | **Total Messages:** 0", # Reset stats A "**Conversation:** 0 turns | **Total Messages:** 0", # Reset stats B "", # Clear sandbox view A "", # Clear sandbox view B None, # Reset state0_var None, # Reset state1_var f"**Model A:** {new_model_a}", # Update model display A f"**Model B:** {new_model_b}", # Update model display B ) refresh_models_btn.click( fn=refresh_models, inputs=[], outputs=[ state0_var, state1_var, chatbot_a, chatbot_b, response_a, response_b, code_a, code_b, gr.State(None), gr.State(None), sandbox_view_a, sandbox_view_b, sandbox_component_a, sandbox_component_b, chat_stats_a, chat_stats_b, sandbox_view_a, sandbox_view_b, state0_var, state1_var, model_display_a, # Update model display A model_display_b, # Update model display B ] ) # Examples gr.Examples( examples=[ ["使用SVG绘制春节主题的动态图案,包括:1)一个红色的灯笼,带有金色的流苏 2)一个金色的福字,使用书法字体 3)背景添加一些烟花效果 4)在灯笼和福字周围添加一些祥云图案。确保图案布局美观,颜色搭配符合春节传统风格。"], ["SVGを使用して日本の伝統的な和柄パターンを描画してください。1)波紋(さざなみ)模様 2)市松模様 3)麻の葉模様 4)雷文(らいもん)模様を含めてください。色は伝統的な日本の色(藍色、朱色、金色など)を使用し、レイアウトはバランスよく配置してください。"], ["Write HTML with P5.js that simulates 25 particles in a vacuum space of a cylindrical container, bouncing within its boundaries. Use different colors for each ball and ensure they leave a trail showing their movement. Add a slow rotation of the container to give better view of what's going on in the scene. Make sure to create proper collision detection and physic rules to ensure particles remain in the container. Add an external spherical container. Add a slow zoom in and zoom out effect to the whole scene."], ["Write a Python script to scrape NVIDIA's stock price for the past month using the yfinance library. Clean the data and create an interactive visualization using Matplotlib. Include: 1) A candlestick chart showing daily price movements 2) A line chart with 7-day and 30-day moving averages. Add hover tooltips showing exact values and date. Make the layout professional with proper titles and axis labels."], ["Write a Python script that uses the Gradio library to create a functional calculator. The calculator should support basic arithmetic operations: addition, subtraction, multiplication, and division. It should have two input fields for numbers and a dropdown menu to select the operation."], ["Write a Todo list app using React.js. The app should allow users to add, delete, and mark tasks as completed. Include features like filtering tasks by status (completed, active), sorting tasks by priority, and displaying the total number of tasks."], ["Write a Python script using the Streamlit library to create a web application for uploading and displaying files. The app should allow users to upload files of type .csv or .txt. If a .csv file is uploaded, display its contents as a table using Streamlit's st.dataframe() method. If a .txt file is uploaded, display its content as plain text."], ["Write a Python function to solve the Trapping Rain Water problem. The function should take a list of non-negative integers representing the height of bars in a histogram and return the total amount of water trapped between the bars after raining. Use an efficient algorithm with a time complexity of O(n)."], ["Create a simple Pygame script for a game where the player controls a bouncing ball that changes direction when it collides with the edges of the window. Add functionality for the player to control a paddle using arrow keys, aiming to keep the ball from touching the bottom of the screen. Include basic collision detection and a scoring system that increases as the ball bounces off the paddle. You need to add clickable buttons to start the game, and reset the game."], ["Create a financial management Dashboard using Vue.js, focusing on local data handling without APIs. Include features like a clean dashboard for tracking income and expenses, dynamic charts for visualizing finances, and a budget planner. Implement functionalities for adding, editing, and deleting transactions, as well as filtering by date or category. Ensure responsive design and smooth user interaction for an intuitive experience."], ["Create a Mermaid diagram to visualize a flowchart of a user login process. Include the following steps: User enters login credentials; Credentials are validated; If valid, the user is directed to the dashboard; If invalid, an error message is shown, and the user can retry or reset the password."], ["Write a Python function to calculate the Fibonacci sequence up to n numbers. Then write test cases to verify the function works correctly for edge cases like negative numbers, zero, and large inputs."], ["Build an HTML page for a Kanban board with three columns with Vue.js: To Do, In Progress, and Done. Each column should allow adding, moving, and deleting tasks. Implement drag-and-drop functionality using Vue Draggable and persist the state using Vuex."], ["Develop a Streamlit app that takes a CSV file as input and provides: 1) Basic statistics about the data 2) Interactive visualizations using Plotly 3) A data cleaning interface with options to handle missing values 4) An option to download the cleaned data."], ["Write an HTML page with embedded JavaScript that creates an interactive periodic table. Each element should display its properties on hover and allow filtering by category (metals, non-metals, etc.). Include a search bar to find elements by name or symbol."], ["Here's a Python function that sorts a list of dictionaries by a specified key:\n\n```python\ndef sort_dicts(data, key):\n return sorted(data, key=lambda x: x[key])\n```\n\nWrite test cases to verify the function works correctly for edge cases like empty lists, missing keys, and different data types. If you use unittest, please use `unittest.main(argv=['first-arg-is-ignored'], exit=False)` to run the tests."], ["Create a React component for a fitness tracker that shows: 1) Daily step count 2) Calories burned 3) Distance walked 4) A progress bar for daily goals."], ["Build a Vue.js dashboard for monitoring server health. Include: 1) Real-time CPU and memory usage graphs 2) Disk space visualization 3) Network activity monitor 4) Alerts for critical thresholds."], ["Write a C program that calculates and prints the first 100 prime numbers in a formatted table with 10 numbers per row. Include a function to check if a number is prime and use it in your solution."], ["Write a C++ program that implements a simple calculator using object-oriented programming. Create a Calculator class with methods for addition, subtraction, multiplication, and division. Include error handling for division by zero."], ["Write a Rust program that generates and prints a Pascal's Triangle with 10 rows. Format the output to center-align the numbers in each row."], ["Write a Java program that simulates a simple bank account system. Create a BankAccount class with methods for deposit, withdrawal, and balance inquiry. Include error handling for insufficient funds and demonstrate its usage with a few transactions."], ["Write a Go program that calculates and prints the Fibonacci sequence up to the 50th number. Format the output in a table with 5 numbers per row and include the index of each Fibonacci number."], ["Write a C program that calculates and prints a histogram of letter frequencies from a predefined string. Use ASCII art to display the histogram vertically."], ["Write a C++ program that implements a simple stack data structure with push, pop, and peek operations. Demonstrate its usage by reversing a predefined string using the stack."], ["Write a Rust program that calculates and prints the first 20 happy numbers. Include a function to check if a number is happy and use it in your solution."], ["Write a Java program that implements a simple binary search algorithm. Create a sorted array of integers and demonstrate searching for different values, including cases where the value is found and not found."], ["Write a Go program that generates and prints a multiplication table from 1 to 12. Format the output in a neat grid with proper alignment."], ], example_labels=[ "🏮 春节主题图案", "🎎 日本の伝統的な和柄パターン", "🌐 Particles in a Spherical Container", "💹 NVIDIA Stock Analysis with Matplotlib", "🧮 Calculator with Gradio", "📝 Todo List App with React.js", "📂 File Upload Web App with Streamlit", "💦 Solve Trapping Rain Water Problem", "🎮 Pygame Bouncing Ball Game", "💳 Financial Dashboard with Vue.js", "🔑 User Login Process Flowchart", "🔢 Fibonacci Sequence with Tests", "📌 Vue Kanban Board", "🧹 Streamlit Data Cleaning App", "⚗️ Interactive Periodic Table with React", "📚 Dictionary Sorting Tests in Python", "🏋️♂️ Fitness Tracker with React", "🖥️ Vue Server Monitoring", "🔢 Prime Numbers in C", "🧮 OOP Calculator in C++", "🔷 Pascal's Triangle in Rust", "🏛️ Bank Account Simulation in Java", "🐰 Fibonacci Sequence in Go", "📊 Letter Frequency Histogram in C", "📦 Stack Implementation in C++", "😄 Happy Numbers in Rust", "🔎 Binary Search in Java", "✖️ Multiplication Table in Go", ], examples_per_page=100, label="Example Prompts", inputs=[text_input], ) return demo
def main(): """Main function to run the Simple BigCodeArena app""" print("🚀 Starting Simple BigCodeArena...") if available_models: print(f"🔍 Available models: {', '.join(available_models)}") # Get random models for this session model_a, model_b = get_random_models() print(f"🎲 Randomly selected models for this session:") print(f" Model A: {model_a}") print(f" Model B: {model_b}") else: print("⚠️ No models found in config!") # Build the UI demo = build_ui() # Launch the app demo.launch( server_name="0.0.0.0", server_port=7860, share=False, debug=True )
if __name__ == "__main__": main()123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259'''Chat State and Logging'''
import jsonimport osfrom typing import Any, Literal, Optionalfrom conversation import Conversation
import datetimeimport uuid
LOG_DIR = os.getenv("LOGDIR", "./logs")'''The default output dir of log files'''
class ModelChatState: ''' The state of a chat with a model. '''
is_vision: bool ''' Whether the model is vision based. '''
conv: Conversation ''' The conversation '''
conv_id: str ''' Unique identifier for the model conversation. Unique per chat per model. '''
chat_session_id: str ''' Unique identifier for the chat session. Unique per chat. The two battle models share the same chat session id. '''
skip_next: bool ''' Flag to indicate skipping the next operation. '''
model_name: str ''' Name of the model being used. '''
oai_thread_id: Optional[str] ''' Identifier for the OpenAI thread. '''
has_csam_image: bool ''' Indicates if a CSAM image has been uploaded. '''
regen_support: bool ''' Indicates if regeneration is supported for the model. '''
chat_start_time: datetime.datetime ''' Chat start time. '''
chat_mode: Literal['battle_anony', 'battle_named', 'direct'] ''' Chat mode. '''
curr_response_type: Literal['chat_multi', 'chat_single', 'regenerate_multi', 'regenerate_single'] | None ''' Current response type. Used for logging. '''
@staticmethod def create_chat_session_id() -> str: ''' Create a new chat session id. ''' return uuid.uuid4().hex
@staticmethod def create_battle_chat_states( model_name_1: str, model_name_2: str, chat_mode: Literal['battle_anony', 'battle_named'], is_vision: bool, ) -> tuple['ModelChatState', 'ModelChatState']: ''' Create two chat states for a battle. ''' chat_session_id = ModelChatState.create_chat_session_id() return ( ModelChatState(model_name_1, chat_mode, is_vision=is_vision, chat_session_id=chat_session_id), ModelChatState(model_name_2, chat_mode, is_vision=is_vision, chat_session_id=chat_session_id), )
def __init__(self, model_name: str, chat_mode: Literal['battle_anony', 'battle_named', 'direct'], is_vision: bool, chat_session_id: str | None = None, ): from fastchat.model.model_adapter import get_conversation_template
self.conv = get_conversation_template(model_name) self.conv_id = uuid.uuid4().hex # if no chat session id is provided, use the conversation id self.chat_session_id = chat_session_id if chat_session_id else self.conv_id self.chat_start_time = datetime.datetime.now() self.chat_mode = chat_mode
self.skip_next = False self.model_name = model_name self.oai_thread_id = None self.is_vision = is_vision
# NOTE(chris): This could be sort of a hack since it assumes the user only uploads one image. If they can upload multiple, we should store a list of image hashes. self.has_csam_image = False
self.regen_support = True if "browsing" in model_name: self.regen_support = False self.init_system_prompt(self.conv, is_vision)
def init_system_prompt(self, conv, is_vision): system_prompt = conv.get_system_message(is_vision) if len(system_prompt) == 0: return current_date = datetime.datetime.now().strftime("%Y-%m-%d") system_prompt = system_prompt.replace("{{currentDateTime}}", current_date)
current_date_v2 = datetime.datetime.now().strftime("%d %b %Y") system_prompt = system_prompt.replace("{{currentDateTimev2}}", current_date_v2)
current_date_v3 = datetime.datetime.now().strftime("%B %Y") system_prompt = system_prompt.replace("{{currentDateTimev3}}", current_date_v3) conv.set_system_message(system_prompt)
def set_response_type( self, response_type: Literal['chat_multi', 'chat_single', 'regenerate_multi', 'regenerate_single'] ): ''' Set the response type for the chat state. ''' self.curr_response_type = response_type
def to_gradio_chatbot(self): ''' Convert to a Gradio chatbot. ''' return self.conv.to_gradio_chatbot()
def get_conv_log_filepath(self, path_prefix: str): ''' Get the filepath for the conversation log.
Expected directory structure: softwarearenlog/ └── YEAR_MONTH_DAY/ ├── conv_logs/ └── sandbox_logs/ ''' date_str = self.chat_start_time.strftime('%Y_%m_%d') filepath = os.path.join( path_prefix, date_str, 'conv_logs', self.chat_mode, f"conv-log-{self.chat_session_id}.json" ) return filepath
def to_dict(self): base = self.conv.to_dict() base.update( { "chat_session_id": self.chat_session_id, "conv_id": self.conv_id, "chat_mode": self.chat_mode, "chat_start_time": self.chat_start_time, "model_name": self.model_name, } )
if self.is_vision: base.update({"has_csam_image": self.has_csam_image}) return base
def generate_vote_record( self, vote_type: str, ip: str ) -> dict[str, Any]: ''' Generate a vote record for telemertry. ''' data = { "tstamp": round(datetime.datetime.now().timestamp(), 4), "type": vote_type, "model": self.model_name, "state": self.to_dict(), "ip": ip, } return data
def generate_response_record( self, gen_params: dict[str, Any], start_ts: float, end_ts: float, ip: str ) -> dict[str, Any]: ''' Generate a vote record for telemertry. ''' data = { "tstamp": round(datetime.datetime.now().timestamp(), 4), "type": self.curr_response_type, "model": self.model_name, "start_ts": round(start_ts, 4), "end_ts": round(end_ts, 4), "gen_params": gen_params, "state": self.to_dict(), "ip": ip, } return data
def save_log_to_local( log_data: dict[str, Any], log_path: str, write_mode: Literal['overwrite', 'append'] = 'append'): ''' Save the log locally. ''' log_json = json.dumps(log_data, default=str) os.makedirs(os.path.dirname(log_path), exist_ok=True) with open(log_path, "w" if write_mode == 'overwrite' else 'a') as fout: fout.write(log_json + "\n")12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304import osimport jsonimport timeimport yamlimport randomimport shortuuid
import requestsfrom typing import Optionalimport boto3
from glob import globfrom tqdm import tqdm
# API setting constantsAPI_MAX_RETRY = 50API_RETRY_SLEEP = 10API_ERROR_OUTPUT = None
registered_api_completion = {}registered_engine_completion = {}
def register_api(api_type): def decorator(func): registered_api_completion[api_type] = func return func
return decorator
def register_engine(engine_type): def decorator(func): registered_engine_completion[engine_type] = func return func
return decorator
def load_questions(question_file: str): """Load questions from a file.""" questions = [] with open(question_file, "r") as ques_file: for line in ques_file: if line: questions.append(json.loads(line)) return questions
def load_model_answers(answer_dir: str): """Load model answers.
The return value is a python dict of type: Dict[model_name: str -> Dict[uid: int -> answer: dict]] """ if not os.path.exists(answer_dir): return {} filenames = [] for folder in os.listdir(answer_dir): if not os.path.isdir(os.path.join(answer_dir, folder)): continue if not os.path.exists(os.path.join(answer_dir, folder, "generation.jsonl")): print(f"WARNING: {folder} does not have generation.jsonl, skip it.") continue filenames.append(os.path.join(answer_dir, folder, "generation.jsonl"))
filenames.sort() model_answers = {} for filename in filenames: # Use parent directory name as model name model_name = os.path.basename(os.path.dirname(filename)) answer = {} with open(filename) as fin: for line in fin: line = json.loads(line) answer[line["uid"]] = line model_answers[model_name] = answer return model_answers
def load_model_judgements(answer_dir: str): """Load model judgements.
The return value is a python dict of type: Dict[model_name: str -> Dict[uid: int -> answer: dict]] """ filenames = glob(os.path.join(answer_dir, "*.jsonl")) filenames.sort() model_answers = {}
for filename in filenames: model_name = os.path.basename(filename)[:-6] answer = {} with open(filename) as fin: for line in fin: line = json.loads(line) answer[line["uid"]] = line model_answers[model_name] = answer
return model_answers
def load_model_answers_and_execution_results(data_dir: str): """Load model answers and execution results.
The return value is a python dict of type: Dict[model_name: str -> Dict[uid: int -> answer: dict]] """ filenames = [] for folder in os.listdir(data_dir): if not os.path.isdir(os.path.join(data_dir, folder)): continue if not os.path.exists(os.path.join(data_dir, folder, "execution_results.jsonl")): print(f"WARNING: {folder} does not have execution_results.jsonl, skip it.") continue filenames.append(os.path.join(data_dir, folder, "execution_results.jsonl"))
filenames.sort() model_answers = {}
for filename in filenames: # Use parent directory name as model name model_name = os.path.basename(os.path.dirname(filename)) answer = {} with open(filename) as fin: for line in fin: line = json.loads(line) answer[line["uid"]] = line model_answers[model_name] = answer
return model_answers
def load_id_to_model_answers(answer_dir: str): """Load model answers.
The return value is a python dict of type: Dict[model_name: str -> Dict[uid: int -> answer: dict]] """ filenames = glob(os.path.join(answer_dir, "*.jsonl")) filenames.sort() model_answers = {}
for filename in filenames: model_name = os.path.basename(filename)[:-6] with open(filename) as fin: for line in fin: line = json.loads(line) if line["uid"] in model_answers: model_answers[line["uid"]][model_name] = line else: model_answers[line["uid"]] = {model_name: line} return model_answers
def get_endpoint(endpoint_list): if endpoint_list is None: return None assert endpoint_list is not None # randomly pick one api_dict = random.choices( endpoint_list )[0] return api_dict
# load config args from config yaml filesdef make_config(config_file: str) -> dict: config_kwargs = {} with open(config_file, "r") as f: config_kwargs = yaml.load(f, Loader=yaml.SafeLoader)
return config_kwargs
@register_api("openai")def chat_completion_openai(model, messages, temperature, max_tokens, api_dict=None, **kwargs): import openai if api_dict: client = openai.OpenAI( base_url=api_dict["api_base"], api_key=api_dict["api_key"], ) else: client = openai.OpenAI() if api_dict and "model_name" in api_dict: model = api_dict["model_name"] output = API_ERROR_OUTPUT for _ in range(API_MAX_RETRY): try: completion = client.chat.completions.create( model=model, messages=messages, temperature=temperature, max_tokens=max_tokens, ) output = { "answer": completion.choices[0].message.content } break except openai.RateLimitError as e: print(type(e), e) time.sleep(API_RETRY_SLEEP) except openai.BadRequestError as e: print("=== DEBUG: OpenAI BadRequestError ===") print("Error type:", type(e)) print("Error message:", str(e)) print("=== Analyzing messages for image issues ===") for i, msg in enumerate(messages): print(f"Message {i} role: {msg.get('role', 'unknown')}") if "content" in msg: content = msg["content"] if isinstance(content, list): for j, item in enumerate(content): if isinstance(item, dict) and item.get("type") == "image_url": url = item.get("image_url", {}).get("url", "") if url.startswith("data:image/png;base64,"): base64_part = url[22:] # Remove "data:image/png;base64," prefix print(f" Image {j}: base64 length = {len(base64_part)}") if len(base64_part) < 50: print(f" *** ISSUE: Image {j} has very short/empty base64: '{url}'") elif url.startswith("data:image/"): print(f" Image {j}: Non-PNG data URL: {url[:50]}...") else: print(f" Image {j}: Unexpected URL format: {url[:50]}...") elif isinstance(item, dict) and item.get("type") == "text": text_content = item.get("text", "") print(f" Text {j}: {len(text_content)} chars") else: print(f" Content {j}: {type(item)} - {str(item)[:50]}...") else: print(f" Content: {type(content)} - {str(content)[:100]}...") print("=== End debug info ===") break except KeyError: print(type(e), e) break return output
@register_api("openai_streaming")def chat_completion_openai_streaming(model, messages, temperature, max_tokens, api_dict=None, **kwargs): """Streaming version of OpenAI completion that yields tokens as they arrive""" import openai if api_dict: client = openai.OpenAI( base_url=api_dict["api_base"], api_key=api_dict["api_key"], ) else: client = openai.OpenAI() if api_dict and "model_name" in api_dict: model = api_dict["model_name"] try: stream = client.chat.completions.create( model=model, messages=messages, temperature=temperature, max_tokens=max_tokens, stream=True ) for chunk in stream: if chunk.choices[0].delta.content is not None: yield chunk.choices[0].delta.content except Exception as e: print(f"Error in streaming completion: {e}") yield f"Error: {str(e)}"
@register_api("openai_thinking")def chat_completion_openai_thinking(model, messages, api_dict=None, **kwargs): import openai if api_dict: client = openai.OpenAI( api_key=api_dict["api_key"], base_url=api_dict["api_base"], ) else: client = openai.OpenAI() output = API_ERROR_OUTPUT for i in range(API_MAX_RETRY): try: completion = client.chat.completions.create( model=model, messages=messages, reasoning_effort=kwargs['reasoning_effort'] if 'reasoning_effort' in kwargs else 'medium', ) output = { "answer": completion.choices[0].message.content } break except openai.RateLimitError as e: print(type(e), e) time.sleep(API_RETRY_SLEEP) except openai.BadRequestError as e: print("=== DEBUG: OpenAI BadRequestError ===") print("Error type:", type(e)) print("Error message:", str(e)) print("=== Analyzing messages for image issues ===") for i, msg in enumerate(messages): print(f"Message {i} role: {msg.get('role', 'unknown')}") if "content" in msg: content = msg["content"] if isinstance(content, list): for j, item in enumerate(content): if isinstance(item, dict) and item.get("type") == "image_url": url = item.get("image_url", {}).get("url", "") if url.startswith("data:image/png;base64,"): base64_part = url[22:] # Remove "data:image/png;base64," prefix print(f" Image {j}: base64 length = {len(base64_part)}") if len(base64_part) < 50: print(f" *** ISSUE: Image {j} has very short/empty base64: '{url}'") elif url.startswith("data:image/"): print(f" Image {j}: Non-PNG data URL: {url[:50]}...") else: print(f" Image {j}: Unexpected URL format: {url[:50]}...") elif isinstance(item, dict) and item.get("type") == "text": text_content = item.get("text", "") print(f" Text {j}: {len(text_content)} chars") else: print(f" Content {j}: {type(item)} - {str(item)[:50]}...") else: print(f" Content: {type(content)} - {str(content)[:100]}...") print("=== End debug info ===") break except KeyError: print(type(e), e) break return output
@register_api("deepseek_reasoner")def chat_completion_deepseek_reasoner(messages, api_dict, **kwargs): import urllib.request
chat_endpoint_headers = { "User-Agent": "curl/8.7.1", "Authorization": "Bearer {}".format(api_dict['api_key']), "Content-Type": "application/json", "Accept": "application/json", } chat_endpoint_url = "https://api.deepseek.com/chat/completions" req_body = { "messages": messages, "model": "deepseek-reasoner", "stream": False, } req_data = json.dumps(req_body).encode("utf-8") output = API_ERROR_OUTPUT for i in range(API_MAX_RETRY): try: req = urllib.request.Request( chat_endpoint_url, headers = chat_endpoint_headers.copy(), data = req_data, ) with urllib.request.urlopen(req) as res: res_data = res.read() res_body = json.loads(res_data.decode("utf-8")) output = { "thought": res_body["choices"][0]["message"]["reasoning_content"], "answer": res_body["choices"][0]["message"]["content"], } break except Exception as e: print(type(e), e) time.sleep(API_RETRY_SLEEP) return output
@register_api("deepseek")def chat_completion_deepseek(messages, max_tokens, api_dict, **kwargs): import urllib.request
chat_endpoint_headers = { "User-Agent": "curl/8.7.1", "Authorization": "Bearer {}".format(api_dict['api_key']), "Content-Type": "application/json", "Accept": "application/json", } chat_endpoint_url = "https://api.deepseek.com/chat/completions" req_body = { "messages": messages, "model": "deepseek-chat", "stream": False, "max_tokens": max_tokens, } req_data = json.dumps(req_body).encode("utf-8") output = API_ERROR_OUTPUT for i in range(API_MAX_RETRY): try: req = urllib.request.Request( chat_endpoint_url, headers = chat_endpoint_headers.copy(), data = req_data, ) with urllib.request.urlopen(req) as res: res_data = res.read() res_body = json.loads(res_data.decode("utf-8")) output = { "answer": res_body["choices"][0]["message"]["content"], } break except Exception as e: print(type(e), e) time.sleep(API_RETRY_SLEEP) return output
@register_api("anthropic")def chat_completion_anthropic(model, messages, temperature, max_tokens, api_dict=None, **kwargs): import anthropic
if api_dict: api_key = api_dict["api_key"] else: api_key = os.environ["ANTHROPIC_API_KEY"]
sys_msg = "" if messages[0]["role"] == "system": sys_msg = messages[0]["content"] messages = messages[1:]
output = API_ERROR_OUTPUT for _ in range(API_MAX_RETRY): try: c = anthropic.Anthropic(api_key=api_key) response = c.messages.create( model=model, messages=messages, stop_sequences=[anthropic.HUMAN_PROMPT], max_tokens=max_tokens, temperature=temperature, system=sys_msg ) output = { "answer": response.content[0].text } break except anthropic.APIError as e: print(type(e), e) time.sleep(API_RETRY_SLEEP) return output
@register_api("anthropic_thinking")def chat_completion_anthropic_thinking(model, messages, max_tokens, budget_tokens, **kwargs): import anthropic
client = anthropic.Anthropic( timeout=1200, ) output = API_ERROR_OUTPUT for _ in range(API_MAX_RETRY): try: response = client.messages.create( model=model, max_tokens=max_tokens, thinking={ "type": "enabled", "budget_tokens": budget_tokens }, messages=messages, ) output = { "thought": response.content[0].thinking, "answer": response.content[1].text, } break except anthropic.APIError as e: print(type(e), e) time.sleep(API_RETRY_SLEEP)
return output
@register_api("mistral")def chat_completion_mistral(model, messages, temperature, max_tokens, **kwargs): from mistralai.client import MistralClient from mistralai.models.chat_completion import ChatMessage from mistralai.exceptions import MistralException
api_key = os.environ["MISTRAL_API_KEY"] client = MistralClient(api_key=api_key)
prompts = [ChatMessage(role=message["role"], content=message["content"]) for message in messages] output = API_ERROR_OUTPUT for _ in range(API_MAX_RETRY): try: chat_response = client.chat( model=model, messages=prompts, temperature=temperature, max_tokens=max_tokens, ) output = { "answer": chat_response.choices[0].message.content } break except MistralException as e: print(type(e), e) break
return output
@register_api("xai")def chat_completion_xai(model, messages, temperature, max_tokens, api_dict=None, **kwargs): import xai_sdk
client = xai_sdk.Client(api_key=api_dict['api_key'], api_host=api_dict['api_base']).compat output = API_ERROR_OUTPUT for _ in range(API_MAX_RETRY): try: stream = client.chat.completions.create( model=model, messages=messages, stream=True, max_tokens=max_tokens, temperature=temperature, top_p=0.95, ) output_text = "" for chunk in stream: if chunk.choices[0].delta.content: output_text += chunk.choices[0].delta.content output = { "answer": output_text } break except Exception as e: print(type(e), e) time.sleep(API_RETRY_SLEEP) return output
@register_api("litellm")def chat_completion_litellm(model, messages, temperature, max_tokens, api_dict=None, **kwargs): import litellm
output = API_ERROR_OUTPUT for _ in range(API_MAX_RETRY): try: response = litellm.completion( model=model, messages=messages, temperature=temperature, max_tokens=max_tokens, ) output = { "answer": response.choices[0].message.content } break except Exception as e: print(type(e), e) time.sleep(API_RETRY_SLEEP) return output
@register_api("litellm_streaming")def chat_completion_litellm_streaming(model, messages, temperature, max_tokens, api_dict=None, **kwargs): """Streaming version of litellm completion""" import litellm
try: response = litellm.completion( model=model, messages=messages, temperature=temperature, max_tokens=max_tokens, stream=True ) for chunk in response: if chunk.choices[0].delta.content is not None: yield chunk.choices[0].delta.content except Exception as e: print(f"Error in litellm streaming completion: {e}") yield f"Error: {str(e)}"
@register_api("anthropic_streaming")def chat_completion_anthropic_streaming(model, messages, temperature, max_tokens, api_dict=None, **kwargs): """Streaming version of Anthropic completion""" import anthropic if api_dict: client = anthropic.Anthropic(api_key=api_dict["api_key"]) else: client = anthropic.Anthropic() try: # Convert messages to Anthropic format system_message = "" conversation_messages = [] for msg in messages: if msg["role"] == "system": system_message = msg["content"] else: conversation_messages.append(msg) stream = client.messages.create( model=model, max_tokens=max_tokens, temperature=temperature, system=system_message if system_message else None, messages=conversation_messages, stream=True ) for chunk in stream: if chunk.type == "content_block_delta" and chunk.delta.text: yield chunk.delta.text except Exception as e: print(f"Error in Anthropic streaming completion: {e}") yield f"Error: {str(e)}"
@register_api("gemini")def http_completion_gemini(model, messages, **kwargs): import requests api_key = os.environ["GEMINI_API_KEY"] safety_settings = [ { "category": "HARM_CATEGORY_HARASSMENT", "threshold": "BLOCK_NONE" }, { "category": "HARM_CATEGORY_HATE_SPEECH", "threshold": "BLOCK_NONE" }, { "category": "HARM_CATEGORY_SEXUALLY_EXPLICIT", "threshold": "BLOCK_NONE" }, { "category": "HARM_CATEGORY_DANGEROUS_CONTENT", "threshold": "BLOCK_NONE" }, ]
sys_prompt = None if messages[0]["role"] == "system": sys_prompt = { "parts":[ {"text": messages[0]["content"]} ] } messages = messages[1:] role_map = {"user": "user", "assistant": "model"} conv = [{"parts":[{"text":turn["content"]}], "role":role_map[turn["role"]]} for turn in messages] json_request = { "contents": conv, "safetySettings": safety_settings, "systemInstruction": sys_prompt, }
if "temperature" in kwargs and "max_tokens" in kwargs: gen_config = { "temperature": kwargs["temperature"], "maxOutputTokens": kwargs["max_tokens"], } json_request["generationConfig"] = gen_config elif "temperature" in kwargs: gen_config = { "temperature": kwargs["temperature"], } json_request["generationConfig"] = gen_config elif "max_tokens" in kwargs: gen_config = { "maxOutputTokens": kwargs["max_tokens"], } json_request["generationConfig"] = gen_config output = API_ERROR_OUTPUT for _ in range(API_MAX_RETRY): try: response = requests.post( f"https://generativelanguage.googleapis.com/v1beta/models/{model}:generateContent?key={api_key}", json=json_request, ) except Exception as e: print(f"**API REQUEST ERROR** Reason: {e}.") time.sleep(API_RETRY_SLEEP) if response.status_code != 200: print(f"**API REQUEST ERROR** Reason: status code {response.status_code}.") time.sleep(API_RETRY_SLEEP) try: output = { "answer": response.json()["candidates"][0]["content"]["parts"][0]["text"], } except KeyError as e: print(type(e), e) print(response.json()) return output
@register_api("vertex")def vertex_completion_gemini(model, messages, project_id, regions, **kwargs): import requests import subprocess output = API_ERROR_OUTPUT # Obtain the access token using gcloud CLI access_token = subprocess.check_output( ["gcloud", "auth", "application-default", "print-access-token"], text=True ).strip()
if messages[0]["role"] == "system": data = { "systemInstruction": { "role": "system", # ignored by vertexi api (04/18/2025) "parts": [{ "text": messages[0]["content"] }] }, } messages = messages[1:] else: data = {} role_map = { "user": "user", "assistant": "model" } messages = [{"parts":[{"text":turn["content"]}], "role":role_map[turn["role"]]} for turn in messages]
url = ( f"https://us-central1-aiplatform.googleapis.com/v1/projects/" f"{project_id}/locations/{regions}/publishers/google/models/" f"{model}:generateContent" ) headers = { "Authorization": f"Bearer {access_token}", "Content-Type": "application/json", }
data = data | { "contents": messages, } if "temperature" in kwargs or "max_tokens" in kwargs: gen_config = {} if "temperature" in kwargs: gen_config["temperature"] = kwargs["temperature"] if "max_tokens" in kwargs: gen_config["maxOutputTokens"] = kwargs["max_tokens"] data["generationConfig"] = gen_config
response = requests.post(url, json=data, headers=headers) try: output = { "answer": response.json()["candidates"][0]["content"]["parts"][0]["text"], } except KeyError as e: print(type(e), e) print(response.json()) return output
@register_api("cohere")def chat_completion_cohere(model, messages, temperature, max_tokens, **kwargs): import cohere
co = cohere.Client(os.environ["COHERE_API_KEY"]) assert len(messages) > 0
template_map = {"system":"SYSTEM", "assistant":"CHATBOT", "user":"USER"}
assert messages[-1]["role"] == "user" prompt = messages[-1]["content"]
if len(messages) > 1: history = [] for message in messages[:-1]: history.append({"role":template_map[message["role"]], "message":message["content"]}) else: history = None
output = API_ERROR_OUTPUT for _ in range(API_MAX_RETRY): try: response = co.chat( message=prompt, model=model, temperature=temperature, max_tokens=max_tokens, chat_history=history, ) output = { "answer": response.text } break except cohere.core.api_error.ApiError as e: print(type(e), e) raise except Exception as e: print(type(e), e) break return output
@register_api("meta")def chat_completion_meta(model, messages, temperature, max_tokens, api_dict, **kwargs): assert api_dict texts = [{"role": m["role"], "text": m["content"]} for m in messages] output = "" for _ in range(API_MAX_RETRY): try: res = requests.post( f"{api_dict['api_base']}/chat_stream_completions?access_token={api_dict['api_key']}", stream=True, headers={"Content-Type": "application/json"}, json={ "model": model, "chunks_delimited": True, "messages": texts, "options": { "max_tokens": max_tokens, "generation_algorithm": "top_p", "top_p": 1, "temperature": temperature, }, }, timeout=30, ) if res.status_code == 200: for line in res.iter_lines(): if line: part = json.loads(line.decode("utf-8")) if "text" in part: output += part["text"] break else: print(f"**API REQUEST ERROR** Code: {res.status_code}") time.sleep(API_RETRY_SLEEP) except Exception as e: print("**API REQUEST ERROR** Reason: Unknown.") time.sleep(API_RETRY_SLEEP) continue return { "answer": output }
def batch_submit_sglang( executor, tokenizer, temperature, max_tokens, all_context, max_context_length=None, end_think_token=None,): print(f"DEBUG: sglang_completion_qwq: max_context_length: {max_context_length}") sampling_params = { "temperature": temperature, "skip_special_tokens": False, "max_new_tokens": max_tokens - 1, "no_stop_trim": True, } batch_prompt_token_ids = [] batch_uids =[] uid_to_prompt = {} uid_to_response = {} for context in all_context: prompt_token_ids = tokenizer.apply_chat_template( context['turns'], add_generation_prompt=True, tokenize=True, ) if max_context_length and (len(prompt_token_ids) + max_tokens) > max_context_length: print(f"DEBUG: sglang_completion_qwq: context length ({len(prompt_token_ids) + max_tokens}) > max_context_length ({max_context_length}), skip this context") continue batch_prompt_token_ids.append(prompt_token_ids) batch_uids.append(context['uid']) uid_to_prompt[context['uid']] = context['turns'] err_msg = f"ERROR: len(batch_prompt_token_ids): {len(batch_prompt_token_ids)} != len(batch_uids): {len(batch_uids)}" assert len(batch_prompt_token_ids) == len(batch_uids), err_msg _ = executor.submit( prompt_token_ids=batch_prompt_token_ids, sampling_params=[sampling_params] * len(batch_uids), keys=batch_uids, ) for request in tqdm(executor.as_completed(), total=len(batch_uids)): uid = request.key() result = request.result() raw_response = tokenizer.decode( result['output_ids'], skip_special_tokens=True, ) if end_think_token: thought, _, ans = raw_response.partition(end_think_token) if ans == "": uid_to_response[uid] = {"thought": thought, "answer": raw_response} else: uid_to_response[uid] = {"thought": thought, "answer": ans} else: uid_to_response[uid] = {"answer": raw_response} # assert len(uid_to_response) == len(all_context), f"ERROR: len output ({len(uid_to_response)}) != len input ({len(all_context)})" return uid_to_response
def _infer_cuda_tp_world_size(): cuda_devices = os.environ.get("CUDA_VISIBLE_DEVICES", None) if cuda_devices is None: tp_world_size = 8 else: tp_world_size = len(cuda_devices.split(",")) return tp_world_size
def download_model(model: str, max_workers: int = 64): import subprocess env = os.environ.copy() env["HF_HUB_ENABLE_HF_TRANSFER"] = "0" cmd = [ "huggingface-cli", "download", f"--max-workers={max_workers}", model ] try: subprocess.run(cmd, env=env, check=True) print(f"Successfully downloaded model '{model}' with {max_workers} max workers.") except subprocess.CalledProcessError as e: print(f"Error occurred while downloading the model: {e}")
@register_engine("sglang")def sglang_completion( model, batch_context, answer_file, temperature, max_tokens=32768, end_think_token=None, **kwargs,): from transformers import AutoTokenizer from utils.sglang_server import SGLangServerExecutor import re
tokenizer = AutoTokenizer.from_pretrained(model) uids = [context['uid'] for context in batch_context] prompts = [context['instruction'] for context in batch_context] code_envs = [context['environment'] for context in batch_context] processed_context = [ { "uid": uids[i], "turns": [{ "content": prompts[i], "role": "user", }] } for i in tqdm(range(len(uids))) ] download_model(model=model) server_args = { "model_path": model, "dtype": "auto", "tp_size": _infer_cuda_tp_world_size(), "mem_fraction_static": 0.7, "max_prefill_tokens": max_tokens, "max_workers": 256, "server_port": 30000, } executor = SGLangServerExecutor( **server_args, ) print(f"DEBUG: sglang_completion: model: {model}") uid_to_response = batch_submit_sglang( executor=executor, tokenizer=tokenizer, temperature=temperature, max_tokens=max_tokens, all_context=processed_context, end_think_token=end_think_token, ) executor.join() print("DEBUG: sglang_completion: done, sleep 10 seconds...") time.sleep(10) num_null = sum( [uid_to_response[uid]['answer'] is None for uid in uids if uid in uid_to_response] ) print(f"Number of null responses: {num_null}") records = [] for i, context in enumerate(processed_context): uid = context['uid'] if uid not in uid_to_response: continue
answer_data = uid_to_response[uid]
record = { "uid": uid, "ans_id": shortuuid.uuid(), "model": kwargs.get("model_display_name", model), "messages": context['turns'] + [ {"content": answer_data, "role": "assistant"} ], "environment": code_envs[i], "tstamp": time.time(), "metadata": {}, }
records.append(record)
with open(answer_file, 'w', encoding='utf-8') as f: for rec in records: f.write(json.dumps(rec, ensure_ascii=True) + '\n')
@register_api("aws_claude")def chat_completion_aws_bedrock_claude(messages, api_dict=None, aws_region="us-west-2", **kwargs): """ Call AWS Bedrock API for chat completion Args: model (str): Model ID conv (object): Conversation object containing messages temperature (float): Temperature parameter for response generation max_tokens (int): Maximum tokens in response api_dict (dict, optional): API configuration dictionary aws_region (str, optional): AWS region, defaults to "us-west-2" Returns: str: Generated response text or error message """ # Configure AWS client if api_dict provided if api_dict is not None: bedrock_rt_client = boto3.client( service_name='bedrock-runtime', region_name=aws_region, aws_access_key_id=api_dict.get('aws_access_key_id'), aws_secret_access_key=api_dict.get('aws_secret_access_key') ) else: bedrock_rt_client = boto3.client( service_name='bedrock-runtime', region_name=aws_region,) output = API_ERROR_OUTPUT #get kwargs from settings temperature= kwargs["temperature"] max_tokens= kwargs["max_tokens"] model = kwargs["model_id"] sys_msg = "" if messages[0]["role"] == "system": sys_msg = messages[0]["content"] messages = messages[1:] else: prompt = messages[0]['content']
# Retry logic for API calls for _ in range(API_MAX_RETRY): try: # Prepare request body prompt_json = { "system": sys_msg, "messages": messages, "max_tokens": max_tokens, "temperature": temperature, "anthropic_version": "bedrock-2023-05-31", "stop_sequences": ["Human"] }
# Call Bedrock API response = bedrock_rt_client.invoke_model( body=json.dumps(prompt_json), modelId=model, accept='application/json', contentType='application/json' ) # Parse response response_body = json.loads(response.get('body').read()) output = {"answer":response_body.get("content")[0].get("text")} break except Exception as e: print(type(e), e) time.sleep(API_RETRY_SLEEP) return output
@register_api("aws_mistral")def chat_completion_aws_bedrock_mistral(messages, api_dict=None, aws_region="us-west-2", **kwargs): """ Call AWS Bedrock API for chat completion Args: model (str): Model ID conv (object): Conversation object containing messages temperature (float): Temperature parameter for response generation max_tokens (int): Maximum tokens in response api_dict (dict, optional): API configuration dictionary aws_region (str, optional): AWS region, defaults to "us-west-2" Returns: str: Generated response text or error message """ # Configure AWS client if api_dict provided if api_dict is not None: bedrock_rt_client = boto3.client( service_name='bedrock-runtime', region_name=aws_region, aws_access_key_id=api_dict.get('aws_access_key_id'), aws_secret_access_key=api_dict.get('aws_secret_access_key') ) else: bedrock_rt_client = boto3.client( service_name='bedrock-runtime', region_name=aws_region,) output = API_ERROR_OUTPUT
#get kwargs from settings temperature= kwargs["temperature"] max_tokens= kwargs["max_tokens"] model = kwargs["model_id"] # Retry logic for API calls for _ in range(API_MAX_RETRY): try: ## =============== Format prompt ================ prompt = "\n".join([content for message in messages for content in message["content"]]) formatted_prompt = f"<s>[INST] {prompt.strip()} [/INST]" body = { "prompt": formatted_prompt, "max_tokens": max_tokens, "stop": ["Human:"], "temperature": temperature, }
# Call Bedrock API response = bedrock_rt_client.invoke_model( body=json.dumps(body), modelId=model, accept='application/json', contentType='application/json' ) # Parse response response_body = json.loads(response.get('body').read()) if "pixtral-large" in model: #us.mistral.pixtral-large-2502-v1:0 output = {"answer": response_body.get("choices")[0].get("message").get("content")} else: output = {"answer": response_body.get("outputs")[0].get("text")} break except Exception as e: print(type(e), e) time.sleep(API_RETRY_SLEEP) return output
@register_api("mistral_streaming")def chat_completion_mistral_streaming(model, messages, temperature, max_tokens, api_dict=None, **kwargs): """Streaming version of Mistral completion""" import openai if api_dict: client = openai.OpenAI( base_url=api_dict["api_base"], api_key=api_dict["api_key"], ) else: client = openai.OpenAI() try: stream = client.chat.completions.create( model=model, messages=messages, temperature=temperature, max_tokens=max_tokens, stream=True ) for chunk in stream: if chunk.choices[0].delta.content is not None: yield chunk.choices[0].delta.content except Exception as e: print(f"Error in Mistral streaming completion: {e}") yield f"Error: {str(e)}"
@register_api("gemini_streaming")def chat_completion_gemini_streaming(model, messages, **kwargs): """Streaming version of Gemini completion""" import google.generativeai as genai try: # Configure the API genai.configure(api_key=os.environ.get("GEMINI_API_KEY")) # Create model model_genai = genai.GenerativeModel(model) # Convert messages to Gemini format conversation = model_genai.start_chat(history=[]) # Get the last user message last_user_message = None for msg in messages: if msg["role"] == "user": last_user_message = msg["content"] if not last_user_message: yield "Error: No user message found" return # Stream the response response = conversation.send_message(last_user_message, stream=True) for chunk in response: if chunk.text: yield chunk.text except Exception as e: print(f"Error in Gemini streaming completion: {e}") yield f"Error: {str(e)}"123456789101112131415161718192021# Core dependencies for BigCodeArenagradiogradio-sandboxcomponent# HTTP and API handlingrequests# AWS SDKboto3# YAML configurationPyYAML# Progress bars and utilitiestqdm# UUID generationshortuuid>=1.0.11# Image processing (for vision models)Pillow>=10.0.0# Optional: FastChat (if using conversation templates)# fastchat>=0.2.0# Development and testing (optional)pytest>=7.4.0black>=23.0.0flake8>=6.0.0123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935'''Module for analyzing code snippets to determine the environments, dependencies, and other information needed to run the code.'''
from enum import StrEnumfrom typing import Any, Generator, TypeAlias, TypedDict, Set
import base64
import astfrom tree_sitter import Language, Node, Parserimport tree_sitter_javascriptimport tree_sitter_typescriptimport sysimport re
class SandboxEnvironment(StrEnum): AUTO = 'Auto'
# Web UI Frameworks HTML = 'HTML' REACT = 'React' VUE = 'Vue' GRADIO = 'Gradio' STREAMLIT = 'Streamlit' PYGAME = 'PyGame' MERMAID = 'Mermaid'
# Runner PYTHON_RUNNER = 'Python Runner' JAVASCRIPT_RUNNER = 'Javascript Runner'
# Compiler C_RUNNER = 'C Runner' CPP_RUNNER = 'C++ Runner' # CSHARP_RUNNER = 'C# Runner' JAVA_RUNNER = 'Java Runner' RUST_RUNNER = 'Rust Runner' GOLANG_RUNNER = 'Golang Runner'
def extract_python_imports(code: str) -> list[str]: ''' Extract Python package imports using AST parsing. Returns a list of top-level package names. ''' try: tree = ast.parse(code) except SyntaxError: return []
packages: Set[str] = set()
for node in ast.walk(tree): try: if isinstance(node, ast.Import): for name in node.names: # Get the top-level package name from any dotted path # e.g., 'foo.bar.baz' -> 'foo' if name.name: # Ensure there's a name packages.add(name.name.split('.')[0])
elif isinstance(node, ast.ImportFrom): # Skip relative imports (those starting with dots) if node.level == 0 and node.module: # Get the top-level package name # e.g., from foo.bar import baz -> 'foo' packages.add(node.module.split('.')[0])
# Also check for common dynamic import patterns elif isinstance(node, ast.Call): if isinstance(node.func, ast.Name) and node.func.id == 'importlib': # Handle importlib.import_module('package') if len(node.args) > 0 and isinstance(node.args[0], ast.Str): packages.add(node.args[0].s.split('.')[0]) elif isinstance(node.func, ast.Attribute) and isinstance(node.func.value, ast.Name): # Handle __import__('package') and importlib.import_module('package') if node.func.value.id == 'importlib' and node.func.attr == 'import_module': if len(node.args) > 0 and isinstance(node.args[0], ast.Str): packages.add(node.args[0].s.split('.')[0]) elif node.func.attr == '__import__': if len(node.args) > 0 and isinstance(node.args[0], ast.Str): packages.add(node.args[0].s.split('.')[0]) except Exception as e: print(f"Error processing node {type(node)}: {e}") continue
# Filter out standard library modules using sys.stdlib_module_names std_libs = set(sys.stdlib_module_names)
return list(packages - std_libs)
def extract_js_imports(code: str) -> list[str]: ''' Extract npm package imports using Tree-sitter for robust parsing. Handles both JavaScript and TypeScript code, including Vue SFC. Returns a list of package names. ''' try: # For Vue SFC, extract the script section first script_match = re.search(r'<script.*?>(.*?)</script>', code, re.DOTALL) if script_match: code = script_match.group(1).strip()
# Initialize parsers with language modules ts_parser = Parser(Language(tree_sitter_typescript.language_tsx())) js_parser = Parser(Language(tree_sitter_javascript.language()))
# Try parsing as TypeScript first, then JavaScript code_bytes = bytes(code, "utf8") try: tree = ts_parser.parse(code_bytes) except Exception as e: print(f"TypeScript parsing failed: {e}") try: tree = js_parser.parse(code_bytes) except Exception as e: print(f"JavaScript parsing failed: {e}") tree = None
if tree is None: raise Exception("Both TypeScript and JavaScript parsing failed")
packages: Set[str] = set()
def extract_package_name(node: Node) -> str | None: """Extract npm package name from string or template string. Returns None for local aliases like @/ or relative paths.""" if node.type in ['string', 'string_fragment']: pkg_path = code[node.start_byte:node.end_byte].strip('"\'') if pkg_path.startswith('.') or pkg_path.startswith('/') or pkg_path.startswith('@/'): return None # relative, absolute, or alias path
# Scoped npm package: @scope/package/... if pkg_path.startswith('@'): parts = pkg_path.split('/') if len(parts) >= 2: return '/'.join(parts[:2])
# Regular npm package: "lodash/cloneDeep" -> "lodash" return pkg_path.split('/')[0]
elif node.type == 'template_string': content = '' has_template_var = False for child in node.children: if child.type == 'string_fragment': content += code[child.start_byte:child.end_byte] elif child.type == 'template_substitution': has_template_var = True
if not content or content.startswith('.') or content.startswith('/') or content.startswith('@/'): return None
if has_template_var: if content.endswith('-literal'): return 'package-template-literal' return None
if content.startswith('@'): parts = content.split('/') if len(parts) >= 2: return '/'.join(parts[:2]) return content.split('/')[0]
return None
def visit_node(node: Node) -> None: if node.type == 'import_statement': # Handle ES6 imports string_node = node.child_by_field_name('source') if string_node: pkg_name = extract_package_name(string_node) if pkg_name: packages.add(pkg_name)
elif node.type == 'export_statement': # Handle re-exports source = node.child_by_field_name('source') if source: pkg_name = extract_package_name(source) if pkg_name: packages.add(pkg_name)
elif node.type == 'call_expression': # Handle require calls and dynamic imports func_node = node.child_by_field_name('function') if func_node and func_node.text: func_name = func_node.text.decode('utf8') if func_name in ['require', 'import']: args = node.child_by_field_name('arguments') if args and args.named_children: arg = args.named_children[0] pkg_name = extract_package_name(arg) if pkg_name: packages.add(pkg_name)
# Recursively visit children for child in node.children: visit_node(child)
visit_node(tree.root_node) return list(packages)
except Exception as e: print(f"Tree-sitter parsing failed: {e}") # Fallback to basic regex parsing if tree-sitter fails packages: Set[str] = set()
# First try to extract script section for Vue SFC script_match = re.search(r'<script.*?>(.*?)</script>', code, re.DOTALL) if script_match: code = script_match.group(1).strip()
# Look for imports import_patterns = [ # dynamic imports r'(?:import|require)\s*\(\s*[\'"](@?[\w-]+(?:/[\w-]+)*)[\'"]', # static imports r'(?:import|from)\s+[\'"](@?[\w-]+(?:/[\w-]+)*)[\'"]', # require statements r'require\s*\(\s*[\'"](@?[\w-]+(?:/[\w-]+)*)[\'"]', ]
for pattern in import_patterns: matches = re.finditer(pattern, code) for match in matches: pkg_name = match.group(1) if not pkg_name.startswith('.'): if pkg_name.startswith('@'): parts = pkg_name.split('/') if len(parts) >= 2: packages.add('/'.join(parts[:2])) else: packages.add(pkg_name.split('/')[0])
return list(packages)
def determine_python_environment(code: str, imports: list[str]) -> SandboxEnvironment | None: ''' Determine Python sandbox environment based on imports and AST analysis. ''' try: tree = ast.parse(code) for node in ast.walk(tree): # Check for specific framework usage patterns if isinstance(node, ast.Name) and node.id == 'gr': return SandboxEnvironment.GRADIO elif isinstance(node, ast.Name) and node.id == 'st': return SandboxEnvironment.STREAMLIT except SyntaxError: pass
# Check imports for framework detection if 'pygame' in imports: return SandboxEnvironment.PYGAME elif 'gradio' in imports: return SandboxEnvironment.GRADIO elif 'streamlit' in imports: return SandboxEnvironment.STREAMLIT # elif 'nicegui' in imports: # return SandboxEnvironment.NICEGUI
return SandboxEnvironment.PYTHON_RUNNER
def determine_jsts_environment(code: str, imports: list[str]) -> SandboxEnvironment | None: ''' Determine JavaScript/TypeScript sandbox environment based on imports and AST analysis. ''' # First check for Vue SFC structure if '<template>' in code or '<script setup' in code: return SandboxEnvironment.VUE
# Check imports for framework detection react_packages = {'react', '@react', 'next', '@next'} vue_packages = {'vue', '@vue', 'nuxt', '@nuxt'}
if any(pkg in react_packages for pkg in imports): return SandboxEnvironment.REACT elif any(pkg in vue_packages for pkg in imports): return SandboxEnvironment.VUE
try: ts_parser = Parser(Language(tree_sitter_typescript.language_tsx())) tree = ts_parser.parse(bytes(code, "utf8"))
def has_framework_patterns(node: Node) -> tuple[bool, str]: # React JSX patterns if node.type in ['jsx_element', 'jsx_self_closing_element']: return True, 'react' # Vue <template> AST element elif node.type == 'template_element': return True, 'vue' # Vue template string with restricted patterns elif node.type == 'template_string': content = code[node.start_byte:node.end_byte] vue_patterns = [ r'\bv-(if|else|for|bind|on|model|show|html|text)=', # Vue directives r'@(?:click|change|input|submit|keyup|keydown)\s*=', # Event bindings r':(?:class|style|src|href|value|disabled|checked)\s*=' # Attribute bindings ] if any(re.search(p, content) for p in vue_patterns): return True, 'vue' return False, ''
cursor = tree.walk()
def visit_node() -> SandboxEnvironment | None: is_framework, framework = has_framework_patterns(cursor.node) if is_framework: return SandboxEnvironment.REACT if framework == 'react' else SandboxEnvironment.VUE if cursor.goto_first_child(): while True: result = visit_node() if result: return result if not cursor.goto_next_sibling(): break cursor.goto_parent() return None
result = visit_node() if result: return result
# More targeted Vue detection vue_patterns = [ r'export\s+default\s+{[\s\S]*?(components|props|emits|data|methods|computed|watch)\s*:', r'defineComponent\s*\(', r'Vue\.extend\s*\(', r'createApp\s*\(', r'\b(ref|reactive|computed|watch|onMounted|onUnmounted|provide|inject)\s*\(', r'defineProps\s*\(', r'defineEmits\s*\(', r'<[a-zA-Z][^>]+\s+(v-(if|else|for|bind|on|model|show|html|text)|@|:)[^>]*>' # in tag context ]
for pattern in vue_patterns: if re.search(pattern, code, re.MULTILINE): return SandboxEnvironment.VUE
except Exception as e: print(f"Tree-sitter parsing error: {e}")
return SandboxEnvironment.JAVASCRIPT_RUNNER
def detect_js_ts_code_lang(code: str) -> str: ''' Detect whether code is JavaScript or TypeScript using Tree-sitter AST parsing. Handles Vue SFC, React, and regular JS/TS files.
Args: code (str): The code to analyze
Returns: str: 'typescript' if TypeScript patterns are found, 'javascript' otherwise ''' # Quick check for explicit TypeScript in Vue SFC if '<script lang="ts">' in code or '<script lang="typescript">' in code: return 'typescript'
try: # Initialize TypeScript parser ts_parser = Parser(Language(tree_sitter_typescript.language_tsx()))
# Parse the code tree = ts_parser.parse(bytes(code, "utf8"))
def has_typescript_patterns(node: Node) -> bool: # Check for TypeScript-specific syntax if node.type in { 'type_annotation', # Type annotations 'type_alias_declaration', # type Foo = ... 'interface_declaration', # interface Foo 'enum_declaration', # enum Foo 'implements_clause', # implements Interface 'type_parameter', # Generic type parameters 'type_assertion', # Type assertions 'type_predicate', # Type predicates in functions 'type_arguments', # Generic type arguments 'readonly_type', # readonly keyword 'mapped_type', # Mapped types 'conditional_type', # Conditional types 'union_type', # Union types 'intersection_type', # Intersection types 'tuple_type', # Tuple types 'optional_parameter', # Optional parameters 'decorator', # Decorators 'ambient_declaration', # Ambient declarations 'declare_statement', # declare keyword 'accessibility_modifier', # private/protected/public }: return True
# Check for type annotations in variable declarations if node.type == 'variable_declarator': for child in node.children: if child.type == 'type_annotation': return True
# Check for return type annotations in functions if node.type in {'function_declaration', 'method_definition', 'arrow_function'}: for child in node.children: if child.type == 'type_annotation': return True
return False
# Walk the AST to find TypeScript patterns cursor = tree.walk()
def visit_node() -> bool: if has_typescript_patterns(cursor.node): return True
# Check children if cursor.goto_first_child(): while True: if visit_node(): return True if not cursor.goto_next_sibling(): break cursor.goto_parent()
return False
if visit_node(): return 'typescript'
except Exception as e: print(f"Tree-sitter parsing error: {e}") # Fallback to basic checks if parsing fails pass
return 'javascript'
def extract_inline_pip_install_commands(code: str) -> tuple[list[str], str]: ''' Extracts pip install commands from inline code comments and returns both the packages and cleaned code. This is useful for cases where pip install commands are written as comments in the code or Jupyter notebook-style !pip install commands.
Args: code (str): The code to analyze.
Returns: tuple[list[str], str]: A tuple containing: 1. List of Python packages extracted from pip install commands in comments 2. Code with the pip install comments removed ''' python_packages = [] cleaned_lines = []
# Regex patterns to match pip install commands in comments and Jupyter-style commands pip_patterns = [ # Comments with pip install r'#\s*(?:pip|pip3|python -m pip)\s+install\s+(?:(?:--upgrade|--user|--no-cache-dir|-U)\s+)*([^-\s][\w\-\[\]<>=~\.]+(?:\s+[^-\s][\w\-\[\]<>=~\.]+)*)', # Jupyter-style !pip install r'!\s*(?:pip|pip3|python -m pip)\s+install\s+(?:(?:--upgrade|--user|--no-cache-dir|-U)\s+)*([^-\s][\w\-\[\]<>=~\.]+(?:\s+[^-\s][\w\-\[\]<>=~\.]+)*)', # Requirements file style pip install r'(?:#|!)\s*(?:pip|pip3|python -m pip)\s+install\s+(?:-r\s+[\w\-\.\/]+\s+)*([^-\s][\w\-\[\]<>=~\.]+(?:\s+[^-\s][\w\-\[\]<>=~\.]+)*)' ]
# Process each line for line in code.splitlines(): matched = False for pattern in pip_patterns: match = re.search(pattern, line) if match: matched = True # Extract packages from the command pkgs = match.group(1).strip().split() # Clean package names (remove version specifiers) cleaned_pkgs = [pkg.split('==')[0].split('>=')[0].split('<=')[ 0].split('~=')[0] for pkg in pkgs] python_packages.extend(cleaned_pkgs)
# Remove the pip install command from the line cleaned_line = line[:match.start()].rstrip() if cleaned_line: # Only add non-empty lines cleaned_lines.append(cleaned_line) break
if not matched: cleaned_lines.append(line)
# Remove duplicates while preserving order python_packages = list(dict.fromkeys(python_packages))
return python_packages, '\n'.join(cleaned_lines)
def extract_js_from_html_script_tags(code: str) -> list[str]: ''' Extract JavaScript package names from HTML script tags. Handles both CDN script tags and inline scripts.
Args: code: HTML code containing script tags
Returns: list[str]: List of package names ''' packages: Set[str] = set()
# Extract packages from CDN script tags script_patterns = [ # unpkg.com pattern r'<script[^>]*src="https?://unpkg\.com/(@?[^@/"]+(?:/[^@/"]+)?(?:@[^/"]+)?)[^"]*"[^>]*>', # cdn.jsdelivr.net pattern - explicitly handle /npm/ in the path r'<script[^>]*src="https?://cdn\.jsdelivr\.net/npm/(@?[^@/"]+(?:/[^@/"]+)?(?:@[^/"]+)?)[^"]*"[^>]*>', # Generic CDN pattern for any domain - exclude common path components r'<script[^>]*src="https?://(?!(?:[^"]+/)?(?:npm|dist|lib|build|umd|esm|cjs|min)/)[^"]+?/(@?[\w-]+)(?:/[^"]*)?[^"]*"[^>]*>', ]
seen_packages = set() # Track packages we've already added to avoid duplicates for pattern in script_patterns: matches = re.finditer(pattern, code, re.IGNORECASE) for match in matches: pkg_name = match.group(1) if pkg_name.startswith('@'): # Handle scoped packages parts = pkg_name.split('/') if len(parts) >= 2: pkg_name = '/'.join(parts[:2]) else: # Remove version and path components from package name pkg_name = pkg_name.split('/')[0].split('@')[0]
# Skip common path components and duplicates if pkg_name and pkg_name not in seen_packages and not pkg_name.lower() in {'npm', 'dist', 'lib', 'build', 'umd', 'esm', 'cjs', 'min'}: seen_packages.add(pkg_name) packages.add(pkg_name)
# Extract packages from inline scripts script_tags = re.finditer( r'<script[^>]*>(.*?)</script>', code, re.DOTALL | re.IGNORECASE) for script in script_tags: script_content = script.group(1) # Check for ES module imports with full URLs es_module_patterns = [ # Match imports from CDN URLs, being careful to extract only the package name r'import\s+[\w\s{},*]+\s+from\s+[\'"]https?://[^/]+/npm/([^/@"\s]+)[@/][^"]*[\'"]', ] found_cdn_import = False for pattern in es_module_patterns: matches = re.finditer(pattern, script_content) for match in matches: pkg_name = match.group(1) if pkg_name and pkg_name not in seen_packages and not pkg_name.lower() in {'npm', 'dist', 'lib', 'build', 'umd', 'esm', 'cjs', 'min', 'https', 'http'}: seen_packages.add(pkg_name) packages.add(pkg_name) found_cdn_import = True
# Only check for regular imports if we didn't find a CDN import if not found_cdn_import: # Remove any URL imports before passing to extract_js_imports cleaned_content = re.sub( r'import\s+[\w\s{},*]+\s+from\s+[\'"]https?://[^"]+[\'"]', '', script_content) packages.update(extract_js_imports(cleaned_content))
return list(packages)
def extract_code_from_markdown(message: str, enable_auto_env: bool = False) -> tuple[str, str, tuple[list[str], list[str]], SandboxEnvironment | None] | None: ''' Extracts code from a markdown message by parsing code blocks directly. Determines sandbox environment based on code content and frameworks used.
Returns: tuple[str, str, tuple[list[str], list[str]], SandboxEnvironment | None]: A tuple: 1. code - the longest code block found 2. code language 3. sandbox python and npm dependencies (extracted using static analysis) 4. sandbox environment determined from code content ''' code_block_regex = r'```(?P<code_lang>[\w\+\#\-\.]*)?[ \t]*\r?\n?(?P<code>.*?)```' matches = list(re.finditer(code_block_regex, message, re.DOTALL))
if not matches: return None
# Define a low-priority list for certain languages low_priority_languages = ['bash', 'shell', 'sh', 'zsh', 'powershell', 'pwsh', '']
# Find the main code block by avoiding low-priority languages main_code = None main_code_lang = None max_length = 0
for match in matches: code = match.group('code').strip() code_lang = (match.group('code_lang') or '').lower() if code_lang not in low_priority_languages and len(code) > max_length: main_code = code main_code_lang = code_lang max_length = len(code)
# Fallback to the longest code block if no main code was found if not main_code: longest_match = max(matches, key=lambda m: len(m.group('code'))) main_code = longest_match.group('code').strip() main_code_lang = (longest_match.group('code_lang') or '').lower()
# Define language prefixes for each environment python_prefixes = ['py', 'ipython', 'pygame', 'gradio', 'streamlit'] vue_prefixes = ['vue'] react_prefixes = ['react', 'next'] js_prefixes = ['js', 'javascript', 'jsx', 'coffee', 'ecma', 'node', 'es', 'svelte'] html_prefixes = ['html', 'xhtml', 'htm'] ts_prefixes = ['ts', 'typescript', 'tsx'] mermaid_prefixes = ['mermaid', 'mmd'] c_prefixes = ['c'] cpp_prefixes = ['cpp', 'c++'] go_prefixes = ['go', 'golang'] java_prefixes = ['java'] rust_prefixes = ['rust'] csharp_prefixes = ['cs', 'csharp', 'dotnet']
# Extract package dependencies from the main program python_packages: list[str] = [] npm_packages: list[str] = []
# Helper function to check if any prefix matches def matches_prefix(lang: str, prefixes: list[str]) -> bool: return any(lang.lower().startswith(prefix) for prefix in prefixes)
if matches_prefix(main_code_lang, python_prefixes): python_packages = extract_python_imports(main_code) extra_python_packages, main_code = extract_inline_pip_install_commands( main_code) python_packages.extend(extra_python_packages) sandbox_env_name = determine_python_environment( main_code, python_packages) elif matches_prefix(main_code_lang, vue_prefixes): npm_packages = extract_js_imports(main_code) sandbox_env_name = SandboxEnvironment.VUE main_code_lang = detect_js_ts_code_lang(main_code) elif matches_prefix(main_code_lang, react_prefixes): npm_packages = extract_js_imports(main_code) sandbox_env_name = SandboxEnvironment.REACT main_code_lang = detect_js_ts_code_lang(main_code) elif ('<!DOCTYPE html>' in main_code and ('<head' in main_code or '<body' in main_code)) or (main_code.strip().startswith('<svg')) or (not matches_prefix(main_code_lang, [*react_prefixes, *vue_prefixes, *js_prefixes, *ts_prefixes]) and ('<html' in main_code or '<!DOCTYPE html>' in main_code)): npm_packages = extract_js_from_html_script_tags(main_code) sandbox_env_name = SandboxEnvironment.HTML main_code_lang = 'html' elif matches_prefix(main_code_lang, js_prefixes): main_code_lang = 'javascript' npm_packages = extract_js_imports(main_code) sandbox_env_name = determine_jsts_environment(main_code, npm_packages) elif matches_prefix(main_code_lang, ts_prefixes): main_code_lang = 'typescript' npm_packages = extract_js_imports(main_code) sandbox_env_name = determine_jsts_environment(main_code, npm_packages) elif matches_prefix(main_code_lang, html_prefixes): main_code_lang = detect_js_ts_code_lang(main_code) npm_packages = extract_js_imports(main_code) sandbox_env_name = determine_jsts_environment(main_code, npm_packages) elif matches_prefix(main_code_lang, mermaid_prefixes): main_code_lang = 'markdown' sandbox_env_name = SandboxEnvironment.MERMAID elif matches_prefix(main_code_lang, cpp_prefixes): main_code_lang = 'cpp' sandbox_env_name = SandboxEnvironment.CPP_RUNNER elif matches_prefix(main_code_lang, go_prefixes): main_code_lang = 'go' sandbox_env_name = SandboxEnvironment.GOLANG_RUNNER elif matches_prefix(main_code_lang, java_prefixes): main_code_lang = 'java' sandbox_env_name = SandboxEnvironment.JAVA_RUNNER elif matches_prefix(main_code_lang, rust_prefixes): main_code_lang = 'rust' sandbox_env_name = SandboxEnvironment.RUST_RUNNER elif main_code_lang == 'c': main_code_lang = 'c' sandbox_env_name = sandbox_env_name = SandboxEnvironment.C_RUNNER else: sandbox_env_name = None
all_python_packages: Set[str] = set(python_packages) all_npm_packages: Set[str] = set(npm_packages)
for match in matches: code = match.group('code').strip() if code != main_code: install_python_packages, install_npm_packages = extract_installation_commands( code) all_python_packages.update(install_python_packages) all_npm_packages.update(install_npm_packages)
if not main_code_lang: main_code_lang = 'markdown' return main_code, main_code_lang, (list(all_python_packages), list(all_npm_packages)), sandbox_env_name
def create_placeholder_svg_data_url(width: int, height: int) -> str: ''' Create a data URL for a placeholder image with given dimensions. Uses SVG to create an elegant placeholder.
Args: width: Width of the placeholder image height: Height of the placeholder image
Returns: str: Data URL containing the SVG image ''' # Create SVG with gradient background and text # Use simpler SVG structure for better browser compatibility svg = f'''<svg width="{width}" height="{height}" xmlns="http://www.w3.org/2000/svg"> <rect width="100%" height="100%" fill="#f3f4f6"/> <text x="50%" y="50%" font-family="Arial, sans-serif" font-size="{max(12, min(width, height) // 8)}" fill="#6b7280" text-anchor="middle" dominant-baseline="middle"> {width} × {height} </text> </svg>'''
# Convert to base64 data URL try: encoded_svg = base64.b64encode(svg.encode('utf-8')).decode('utf-8') return f'data:image/svg+xml;base64,{encoded_svg}' except Exception as e: print(f'Error encoding SVG: {e}') # Fallback to a simple colored div return f'data:image/svg+xml;base64,PHN2ZyB3aWR0aD0ie3dpZHRofSIgaGVpZ2h0PSJ7aGVpZ2h0fSIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIj48cmVjdCB3aWR0aD0iMTAwJSIgaGVpZ2h0PSIxMDAlIiBmaWxsPSIjZjNmNGY2Ii8+PC9zdmc+'
def replace_placeholder_urls(code: str) -> str: ''' Replace placeholder image URLs with SVG data URLs. Only replaces exact matches of "/api/placeholder/{width}/{height}".
Args: code: The source code containing placeholder URLs
Returns: str: Code with placeholder URLs replaced with data URLs '''
def replacer(match: re.Match) -> str: try: # Extract width and height from the URL using capturing groups width = int(match.group(1)) height = int(match.group(2)) # Validate dimensions if width <= 0 or height <= 0: print(f'Warning: Invalid dimensions {width}x{height}, using default 100x100') width, height = 100, 100 elif width > 10000 or height > 10000: print(f'Warning: Dimensions {width}x{height} are very large, capping at 1000x1000') width, height = min(width, 1000), min(height, 1000) print(f'Replacing placeholder URL with SVG: {width}x{height}') data_url = create_placeholder_svg_data_url(width, height) return data_url except Exception as e: print(f'Error replacing placeholder URL: {e}') # Return a simple fallback return 'data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMTAwIiBoZWlnaHQ9IjEwMCIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIj48cmVjdCB3aWR0aD0iMTAwJSIgaGVpZ2h0PSIxMDAlIiBmaWxsPSIjZjNmNGY2Ii8+PC9zdmc+'
# Regular expression pattern to match placeholder URLs pattern = r'/api/placeholder/(\d+)/(\d+)'
try: # Replace all occurrences result = re.sub(pattern, replacer, code) print(f'Placeholder URL replacement completed successfully') return result except Exception as e: print(f'Error during placeholder URL replacement: {e}') return code # Return original code if replacement fails
def extract_installation_commands(code: str) -> tuple[list[str], list[str]]: ''' Extracts package installation commands from the code block, preserving version information.
Args: code (str): The code block to analyze.
Returns: tuple[list[str], list[str]]: A tuple containing two lists: 1. Python packages from pip install commands (with versions if specified). 2. npm packages from npm install commands (with versions if specified). ''' python_packages = [] npm_packages = []
# Process the code line by line to handle both pip and npm commands lines = code.split('\n') for line in lines: line = line.strip()
# Skip empty lines and comments if not line or line.startswith('#'): continue
# Handle pip install commands if any(x in line for x in ['pip install', 'pip3 install', 'python -m pip install']): # Remove the command part and any flags parts = line.split('install', 1)[1].strip() # Handle flags at the start while parts.startswith(('-', '--')): parts = parts.split(None, 1)[1]
# Split by whitespace, respecting quotes current = '' in_quotes = False quote_char = None packages = []
for char in parts: if char in '"\'': if not in_quotes: in_quotes = True quote_char = char elif char == quote_char: in_quotes = False quote_char = None elif char.isspace() and not in_quotes: if current: packages.append(current) current = '' else: current += char if current: packages.append(current)
# Add packages, stripping quotes and ignoring flags for pkg in packages: pkg = pkg.strip('"\'') if pkg and not pkg.startswith(('-', '--')) and not pkg == '-r': python_packages.append(pkg)
# Handle npm/yarn install commands elif any(x in line for x in ['npm install', 'npm i', 'yarn add']): # Remove the command part and any flags if 'yarn add' in line: parts = line.split('add', 1)[1] else: parts = line.split('install', 1)[ 1] if 'install' in line else line.split('i', 1)[1] parts = parts.strip()
# Handle flags at the start while parts.startswith(('-', '--')): parts = parts.split(None, 1)[1] if ' ' in parts else ''
# Process each package for pkg in parts.split(): if pkg.startswith(('-', '--')) or pkg in ('install', 'i', 'add'): continue
if pkg.startswith('@'): # Handle scoped packages (e.g., @types/node@16.0.0) if '@' in pkg[1:]: # Has version pkg_parts = pkg.rsplit('@', 1) base_pkg = pkg_parts[0] # @scope/name version = pkg_parts[1] # version npm_packages.append(f"{base_pkg}@{version}") else: npm_packages.append(pkg) else: npm_packages.append(pkg)
# Remove duplicates while preserving order python_packages = list(dict.fromkeys(python_packages)) npm_packages = list(dict.fromkeys(npm_packages))
# Filter out npm command words npm_packages = [p for p in npm_packages if p not in ( 'npm', 'install', 'i', 'add')]
return python_packages, npm_packages
def validate_dependencies(dependencies: list) -> tuple[bool, str]: """ Validate dependency list format and values. Allows empty rows but validates format when package name is specified. """ if not dependencies: return True, ""
valid_types = ["python", "npm"] for dep in dependencies: # Skip validation for empty rows if len(dep) != 3: return False, "Each dependency must have type, package and version fields"
dep_type, pkg_name, version = dep
# Skip empty rows if not pkg_name.strip(): continue
if dep_type.lower() not in valid_types: return False, f"Invalid dependency type: {dep_type}"
# Validate version format if specified if version.strip(): if dep_type.lower() == "python": # Check for valid pip version specifiers if not any(op in version for op in ['==', '>=', '<=', '~=', '>', '<']) and version.lower() != "latest": return False, f"Invalid Python version format for {pkg_name}: {version}" elif dep_type.lower() == "npm": # Check for valid npm version format (starts with @ or valid semver-like) if not (version.startswith('@') or version.lower() == "latest"): return False, f"Invalid NPM version format for {pkg_name}: {version}"
return True, ""
def extract_java_class_name(java_code: str) -> str: ''' Extract the class name from Java code. ''' match = re.search(r'public\s+class\s+(\w+)', java_code) return match.group(1) if match else "Main"1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612'''Run generated code in a sandbox environment.
Gradio will interact with this module.'''
from typing import Any, Generator, Literal, TypeAlias, TypedDict, Setimport uuidimport gradio as gr
import base64from e2b_code_interpreter import Sandbox as CodeSandboxfrom gradio_sandboxcomponent import SandboxComponent
from sandbox.sandbox_state import ChatbotSandboxStatefrom sandbox.code_analyzer import SandboxEnvironment, extract_code_from_markdown, extract_installation_commands, extract_java_class_name, extract_js_imports, extract_python_imports, replace_placeholder_urls, validate_dependenciesfrom sandbox.prompts import ( DEFAULT_C_CODE_RUN_SANDBOX_INSTRUCTION, DEFAULT_CPP_CODE_RUN_SANDBOX_INSTRUCTION, DEFAULT_GOLANG_CODE_RUN_SANDBOX_INSTRUCTION, DEFAULT_GRADIO_SANDBOX_INSTRUCTION, DEFAULT_HTML_SANDBOX_INSTRUCTION, DEFAULT_JAVA_CODE_RUN_SANDBOX_INSTRUCTION, DEFAULT_JAVASCRIPT_RUNNER_INSTRUCTION, DEFAULT_MERMAID_SANDBOX_INSTRUCTION, DEFAULT_PYGAME_SANDBOX_INSTRUCTION, DEFAULT_PYTHON_RUNNER_INSTRUCTION, DEFAULT_REACT_SANDBOX_INSTRUCTION, DEFAULT_RUST_CODE_RUN_SANDBOX_INSTRUCTION, DEFAULT_STREAMLIT_SANDBOX_INSTRUCTION, DEFAULT_VUE_SANDBOX_INSTRUCTION, GENERAL_SANDBOX_INSTRUCTION)from sandbox.sandbox_telemetry import log_sandbox_telemetry_gradio_fn
from .constants import CODE_RUN_TIMEOUT_SECONDS, E2B_API_KEY, SANDBOX_TEMPLATE_ID, SANDBOX_NGINX_PORTfrom .sandbox_manager import get_sandbox_app_url, create_sandbox, install_npm_dependencies, install_pip_dependencies, reuse_or_create_sandbox, run_background_command_with_timeout, run_command_in_sandbox
SUPPORTED_SANDBOX_ENVIRONMENTS: list[str] = [ env.value for env in SandboxEnvironment ]
WEB_UI_SANDBOX_ENVIRONMENTS = [ SandboxEnvironment.HTML, SandboxEnvironment.REACT, SandboxEnvironment.VUE, SandboxEnvironment.GRADIO, SandboxEnvironment.STREAMLIT, # SandboxEnvironment.NICEGUI, SandboxEnvironment.PYGAME, SandboxEnvironment.MERMAID]'''Sandbox environments that can be rendered in the web UI.'''
VALID_GRADIO_CODE_LANGUAGES = [ 'python', 'c', 'cpp', 'markdown', 'json', 'html', 'css', 'javascript', 'jinja2', 'typescript', 'yaml', 'dockerfile', 'shell', 'r', 'sql', 'sql-msSQL', 'sql-mySQL', 'sql-mariaDB', 'sql-sqlite', 'sql-cassandra', 'sql-plSQL', 'sql-hive', 'sql-pgSQL', 'sql-gql', 'sql-gpSQL', 'sql-sparkSQL', 'sql-esper']'''Languages that gradio code component can render.'''
RUN_CODE_BUTTON_HTML = "<button style='background-color: #4CAF50; border: none; color: white; padding: 10px 24px; text-align: center; text-decoration: none; display: inline-block; font-size: 16px; margin: 4px 2px; cursor: pointer; border-radius: 12px;'>Click to Run in Sandbox</button>"'''Button in the chat to run the code in the sandbox.'''
DEFAULT_SANDBOX_INSTRUCTIONS: dict[SandboxEnvironment, str] = { SandboxEnvironment.AUTO: GENERAL_SANDBOX_INSTRUCTION.strip(), SandboxEnvironment.PYTHON_RUNNER: DEFAULT_PYTHON_RUNNER_INSTRUCTION.strip(), SandboxEnvironment.JAVASCRIPT_RUNNER: DEFAULT_JAVASCRIPT_RUNNER_INSTRUCTION.strip(), SandboxEnvironment.HTML: DEFAULT_HTML_SANDBOX_INSTRUCTION.strip(), SandboxEnvironment.REACT: DEFAULT_REACT_SANDBOX_INSTRUCTION.strip(), SandboxEnvironment.VUE: DEFAULT_VUE_SANDBOX_INSTRUCTION.strip(), SandboxEnvironment.GRADIO: DEFAULT_GRADIO_SANDBOX_INSTRUCTION.strip(), SandboxEnvironment.STREAMLIT: DEFAULT_STREAMLIT_SANDBOX_INSTRUCTION.strip(), SandboxEnvironment.PYGAME: DEFAULT_PYGAME_SANDBOX_INSTRUCTION.strip(), SandboxEnvironment.MERMAID: DEFAULT_MERMAID_SANDBOX_INSTRUCTION.strip(), # Runners SandboxEnvironment.C_RUNNER: DEFAULT_C_CODE_RUN_SANDBOX_INSTRUCTION, SandboxEnvironment.CPP_RUNNER: DEFAULT_CPP_CODE_RUN_SANDBOX_INSTRUCTION, SandboxEnvironment.JAVA_RUNNER: DEFAULT_JAVA_CODE_RUN_SANDBOX_INSTRUCTION, SandboxEnvironment.GOLANG_RUNNER: DEFAULT_GOLANG_CODE_RUN_SANDBOX_INSTRUCTION, SandboxEnvironment.RUST_RUNNER: DEFAULT_RUST_CODE_RUN_SANDBOX_INSTRUCTION,
}
SandboxGradioSandboxComponents: TypeAlias = tuple[ gr.Markdown | Any, # sandbox_output_md SandboxComponent | Any, # sandbox_ui gr.Code | Any, # sandbox_code Any]'''Gradio components for the sandbox.'''
class CodeRunResult(TypedDict): ''' The result of running the code in the sandbox. ''' sandbox_id: str ''' The sandbox id to run the code. ''' sandbox_url: str ''' The sandbox url to access the rendered results. ''' is_run_success: bool ''' Whether the code run is successful. ''' stderr: str ''' The stderr output from the sandbox. '''
def create_chatbot_sandbox_state(btn_list_length: int = 5) -> ChatbotSandboxState: ''' Create a new sandbox state for a chatbot. ''' return { 'enable_sandbox': True, # Always enabled 'enabled_round': 0, 'sandbox_run_round': 0, 'edit_round': 0, 'sandbox_environment': SandboxEnvironment.AUTO, 'auto_selected_sandbox_environment': None, 'sandbox_instruction': DEFAULT_SANDBOX_INSTRUCTIONS[SandboxEnvironment.AUTO], 'code_to_execute': "", 'code_language': None, 'code_dependencies': ([], []), 'btn_list_length': btn_list_length, 'sandbox_id': None, 'chat_session_id': None, 'conv_id': None, "sandbox_output": None, "sandbox_error": None, }
def set_sandbox_state_ids( sandbox_state: ChatbotSandboxState, conv_id: str, chat_session_id: str,) -> ChatbotSandboxState: ''' Set the conv_id and chat_session_id in the sandbox state. ''' sandbox_state['conv_id'] = conv_id sandbox_state['chat_session_id'] = chat_session_id return sandbox_state
def reset_sandbox_state(state: ChatbotSandboxState) -> ChatbotSandboxState: ''' Reset the sandbox state. Used when the chatbot session is reset. ''' # reset rounds state['enabled_round'] = 0 state['sandbox_run_round'] = 0 state['edit_round'] = 0
# state['sandbox_environment'] = SandboxEnvironment.AUTO state['auto_selected_sandbox_environment'] = None state['sandbox_instruction'] = DEFAULT_SANDBOX_INSTRUCTIONS[SandboxEnvironment.AUTO] state['code_to_execute'] = "" state['code_language'] = None state['code_dependencies'] = ([], []) state['sandbox_error'] = None state['sandbox_output'] = None
# reset ids state['sandbox_id'] = None state['conv_id'] = None state['chat_session_id'] = None
return state
def update_sandbox_config_multi( enable_sandbox: bool, sandbox_environment: SandboxEnvironment, *states: ChatbotSandboxState) -> list[ChatbotSandboxState]: ''' Fn to update sandbox config. ''' return [ update_sandbox_config(enable_sandbox, sandbox_environment, state) for state in states ]
def update_sandbox_state_system_prompt(sandbox_state: ChatbotSandboxState, system_prompt: str): if sandbox_state['enabled_round'] == 0: sandbox_state['sandbox_instruction'] = system_prompt return sandbox_state
def update_sandbox_config( enable_sandbox: bool, sandbox_environment: SandboxEnvironment, state: ChatbotSandboxState) -> ChatbotSandboxState: ''' Fn to update sandbox config for single model. ''' state["enable_sandbox"] = enable_sandbox state["sandbox_environment"] = sandbox_environment state['sandbox_instruction'] = DEFAULT_SANDBOX_INSTRUCTIONS.get(sandbox_environment, None) return state
def update_visibility(visible): return [gr.update(visible=visible)] *14
def update_visibility_for_single_model(visible: bool, component_cnt: int): return [gr.update(visible=visible)] * component_cnt
def mermaid_to_html(mermaid_code: str, theme: str = 'default') -> str: """ Convert Mermaid diagram code to a minimal HTML document.
Args: mermaid_code: The Mermaid diagram syntax theme: Theme name ('default', 'dark', 'forest', 'neutral', etc.)
Returns: str: Complete HTML document with embedded Mermaid diagram """ html_template = f'''<!DOCTYPE html><html><head> <meta charset="UTF-8"> <script src="https://cdn.jsdelivr.net/npm/mermaid@10.6.1/dist/mermaid.min.js"></script> <script> mermaid.initialize({{ startOnLoad: true, theme: '{theme}', securityLevel: 'loose', fontFamily: 'Arial, sans-serif' }}); </script></head><body> <div class="mermaid">{mermaid_code} </div></body></html>''' return html_template
def javascript_to_html(javascript_code: str) -> str: """ Convert JavaScript code to a minimal HTML document that executes the code.
Args: javascript_code: The JavaScript code to embed
Returns: str: Complete HTML document with embedded JavaScript code """ html_template = f'''<!DOCTYPE html><html><head> <meta charset="UTF-8"> <title>JavaScript Code Execution</title> <style> body {{ font-family: Arial, sans-serif; margin: 20px; background-color: #f5f5f5; }} #output {{ background-color: white; border: 1px solid #ddd; border-radius: 4px; padding: 15px; margin-top: 20px; white-space: pre-wrap; font-family: monospace; }} .error {{ color: red; }} .log {{ color: black; }} </style></head><body> <h1>JavaScript Code Execution</h1> <div id="output"></div> <script> // Override console methods to capture output const outputDiv = document.getElementById('output'); const originalConsole = {{}}; ['log', 'error', 'warn', 'info'].forEach(function(method) {{ originalConsole[method] = console[method]; console[method] = function(...args) {{ const message = args.map(function(arg) {{ return typeof arg === 'object' ? JSON.stringify(arg, null, 2) : String(arg); }}).join(' '); const span = document.createElement('span'); span.className = method === 'error' ? 'error' : 'log'; span.textContent = '[' + method.toUpperCase() + '] ' + message + '\\n'; outputDiv.appendChild(span); // Also call original console method originalConsole[method].apply(console, args); }}; }}); // Capture uncaught errors window.addEventListener('error', function(e) {{ const span = document.createElement('span'); span.className = 'error'; span.textContent = '[ERROR] ' + e.message + ' at line ' + e.lineno + '\\n'; outputDiv.appendChild(span); }}); try {{ // Execute the user's JavaScript code{javascript_code} }} catch (error) {{ console.error('Execution error:', error.message); }} </script></body></html>''' return html_template
def render_result(result): if result.png: if isinstance(result.png, str): img_str = result.png else: img_str = base64.b64encode(result.png).decode() return f"" elif result.jpeg: if isinstance(result.jpeg, str): img_str = result.jpeg else: img_str = base64.b64encode(result.jpeg).decode() return f"" elif result.svg: if isinstance(result.svg, str): svg_data = result.svg else: svg_data = result.svg.decode() svg_base64 = base64.b64encode(svg_data.encode()).decode() return f"" elif result.html: return result.html elif result.markdown: return f"```markdown\n{result.markdown}\n```" elif result.latex: return f"```latex\n{result.latex}\n```" elif result.json: return f"```json\n{result.json}\n```" elif result.javascript: return result.javascript # Return raw JavaScript else: return str(result)
def run_code_interpreter(code: str, code_language: str | None, code_dependencies: tuple[list[str], list[str]]) -> tuple[str, str]: """ Executes the provided code within a sandboxed environment and returns the output.
Args: code (str): The code to be executed. """ sandbox = CodeSandbox( api_key=E2B_API_KEY, )
sandbox.commands.run("pip install uv", timeout=60 * 3, on_stderr=lambda message: print(message),) stderrs = []
python_dependencies, npm_dependencies = code_dependencies pip_install_errs = install_pip_dependencies(sandbox, python_dependencies) npm_install_errs = install_npm_dependencies(sandbox, npm_dependencies)
stderrs.extend(pip_install_errs) stderrs.extend(npm_install_errs)
execution = sandbox.run_code( code=code, language=code_language )
# collect stdout, stderr from sandbox stdout = "\n".join(execution.logs.stdout) stderr = "\n".join(execution.logs.stderr) if execution.error: stderr += f"\n{execution.error.name}: {execution.error.value}" output = "" if stdout: output += f"### Stdout:\n```markdown\n{stdout}\n```\n\n"
stderrs.append(stderr)
results = [] for result in execution.results: if result.html or result.javascript: # TODO: fix this continue # with open('html_code.html', 'w') as f: # f.write(result.html) # url, _ = run_html_sandbox(result.html, ([], extract_js_imports(result.html))) else: rendered_result = render_result(result) results.append(rendered_result) if results: output += "\n### Results:\n" + "\n".join(results)
stderrs = '\n'.join(stderrs) return output, "" if output else stderrs
def run_html_sandbox(code: str, code_dependencies: tuple[list[str], list[str]], existing_sandbox_id: str | None = None) -> tuple[str, str, str]: """ Executes the provided code within a sandboxed environment and returns the output. Supports both React and Vue.js rendering in HTML files.
Args: code (str): The code to be executed. code_dependencies: Tuple of (python_deps, npm_deps)
Returns: tuple: (sandbox_url, sandbox_id, stderr) """ sandbox = reuse_or_create_sandbox(sandbox_id=existing_sandbox_id) project_root = "~/html_app" sandbox.files.make_dir(project_root)
# HTML does not support dependencies for now # _, npm_dependencies = code_dependencies # install_npm_dependencies(sandbox, npm_dependencies, project_root=project_root)
# replace placeholder URLs with SVG data URLs code = replace_placeholder_urls(code)
file_path = f"{project_root}/index.html" sandbox.files.write(file_path, code, "user", 60)
sandbox_url = get_sandbox_app_url(sandbox, 'html') return (sandbox_url, sandbox.sandbox_id, '')
def run_react_sandbox(code: str, code_dependencies: tuple[list[str], list[str]], existing_sandbox_id: str | None = None) -> CodeRunResult: """ Executes the provided code within a sandboxed environment and returns the output.
Args: code (str): The code to be executed.
Returns: url for remote sandbox """ project_root = "~/react_app" sandbox = reuse_or_create_sandbox(sandbox_id=existing_sandbox_id)
stderrs: list[str] = [] # to collect errors
_, npm_dependencies = code_dependencies if npm_dependencies: print(f"Installing NPM dependencies...: {npm_dependencies}") install_errs = install_npm_dependencies(sandbox, npm_dependencies, project_root=project_root) stderrs.extend(install_errs) print("NPM dependencies installed. " + "Errors: " + str(install_errs))
# replace placeholder URLs with SVG data URLs code = replace_placeholder_urls(code)
# set up the sandbox print("Setting up sandbox directory structure...") file_path = "~/react_app/src/App.tsx" sandbox.files.write(file_path, code, "user", 60) print("Code files written successfully.")
is_run_success, _, build_stderrs = run_command_in_sandbox( sandbox=sandbox, command="npm run build --loglevel=error -- --mode development --logLevel error", working_directory=project_root, ) stderrs.extend(build_stderrs)
sandbox_url = get_sandbox_app_url(sandbox, 'react') return { 'sandbox_id': sandbox.sandbox_id, 'sandbox_url': sandbox_url, 'is_run_success': is_run_success, 'stderr': '\n'.join(stderrs), }
def run_vue_sandbox(code: str, code_dependencies: tuple[list[str], list[str]], existing_sandbox_id: str | None = None) -> CodeRunResult: """ Executes the provided Vue code within a sandboxed environment and returns the output.
Args: code (str): The Vue code to be executed.
Returns: url for remote sandbox """ sandbox = reuse_or_create_sandbox(sandbox_id=existing_sandbox_id) project_root = "~/vue_app"
stderrs: list[str] = [] # to collect errors
# replace placeholder URLs with SVG data URLs code = replace_placeholder_urls(code)
# Set up the sandbox file_path = "~/vue_app/src/App.vue" sandbox.files.write(file_path, code, "user", 60)
_, npm_dependencies = code_dependencies if npm_dependencies: print(f"Installing NPM dependencies...: {npm_dependencies}") install_errs = install_npm_dependencies(sandbox, npm_dependencies, project_root=project_root) stderrs.extend(install_errs) print("NPM dependencies installed. " + "Errors: " + str(install_errs))
is_run_success, _, build_stderrs = run_command_in_sandbox( sandbox=sandbox, command="npm run build --loglevel=error -- --mode development --logLevel error", working_directory=project_root, ) stderrs.extend(build_stderrs)
sandbox_url = get_sandbox_app_url(sandbox, 'vue') return { 'sandbox_id': sandbox.sandbox_id, 'sandbox_url': sandbox_url, 'is_run_success': is_run_success, 'stderr': '\n'.join(stderrs), }
def run_pygame_sandbox(code: str, code_dependencies: tuple[list[str], list[str]], existing_sandbox_id: str | None = None) -> CodeRunResult: """ Executes the provided code within a sandboxed environment and returns the output.
Args: code (str): The code to be executed.
Returns: url for remote sandbox """ sandbox = reuse_or_create_sandbox(sandbox_id=existing_sandbox_id) project_root = "~/pygame_app" file_path = f"{project_root}/main.py"
stderrs = []
sandbox.files.write(file_path, code, "user", 60)
python_dependencies, _ = code_dependencies install_errs = install_pip_dependencies(sandbox, python_dependencies) stderrs.extend(install_errs)
# build the pygame code is_run_success, _, build_stderrs = run_command_in_sandbox( sandbox=sandbox, command="pygbag --build ~/pygame_app", ) stderrs.extend(build_stderrs)
sandbox_url = get_sandbox_app_url(sandbox, 'pygame') return { 'sandbox_id': sandbox.sandbox_id, 'sandbox_url': sandbox_url, 'is_run_success': is_run_success, 'stderr': '\n'.join(stderrs), }
def run_gradio_sandbox(code: str, code_dependencies: tuple[list[str], list[str]], existing_sandbox_id: str | None = None) -> tuple[str, str, str]: """ Executes the provided code within a sandboxed environment and returns the output.
Args: code (str): The code to be executed.
Returns: url for remote sandbox and sandbox id """ sandbox = reuse_or_create_sandbox(sandbox_id=existing_sandbox_id)
file_path = "~/gradio_app/main.py" sandbox.files.write(file_path, code, "user", 60)
stderrs = []
python_dependencies, _ = code_dependencies install_stderr = install_pip_dependencies(sandbox, python_dependencies) stderrs.extend(install_stderr) stderr = run_background_command_with_timeout( sandbox, f"python {file_path}", timeout=10, ) stderrs.append(stderr)
sandbox_url = 'https://' + sandbox.get_host(7860)
return (sandbox_url, sandbox.sandbox_id, '\n'.join(stderrs))
def run_streamlit_sandbox(code: str, code_dependencies: tuple[list[str], list[str]], existing_sandbox_id: str | None = None) -> tuple[str, str, str]: sandbox = reuse_or_create_sandbox(sandbox_id=existing_sandbox_id)
stderrs = []
sandbox.files.make_dir('mystreamlit') file_path = "~/mystreamlit/app.py" sandbox.files.write(file_path, code, "user", 60)
python_dependencies, _ = code_dependencies install_stderr = install_pip_dependencies(sandbox, python_dependencies) stderrs.extend(install_stderr)
stderr = run_background_command_with_timeout( sandbox, r"sudo kill -9 $(ss -lptn 'sport = :8501' | grep -oP '(?<=pid=)\d+'); streamlit run ~/mystreamlit/app.py --server.port 8501 --server.headless true", timeout=8, ) stderrs.append(stderr)
host = sandbox.get_host(port=8501) url = f"https://{host}" return (url, sandbox.sandbox_id, '\n'.join(stderrs))
def run_c_code(code: str, existing_sandbox_id: str | None = None) -> tuple[str, str]: """ Executes the provided C code within a sandboxed environment and returns the output.
Args: code (str): The C code to be executed.
Returns: tuple: (stdout, stderr) """ sandbox = reuse_or_create_sandbox(sandbox_id=existing_sandbox_id)
file_path = "~/main.c" sandbox.files.write(file_path, code, "user", 60)
is_success, stdouts, stderrs = run_command_in_sandbox( sandbox=sandbox, command=f"gcc {file_path} -o ~/main && ./main", timeout=CODE_RUN_TIMEOUT_SECONDS, )
# collect stdout, stderr from sandbox stdout = "\n".join(stdouts) stderr = "\n".join(stderrs) return stdout, stderr
def run_cpp_code(code: str, existing_sandbox_id: str | None = None) -> tuple[str, str]: """ Executes the provided C++ code within a sandboxed environment and returns the output.
Args: code (str): The C++ code to be executed.
Returns: tuple: (stdout, stderr) """ sandbox = reuse_or_create_sandbox(sandbox_id=existing_sandbox_id)
file_path = "~/main.cpp" sandbox.files.write(file_path, code, "user", 60)
is_success, stdouts, stderrs = run_command_in_sandbox( sandbox=sandbox, command=f"g++ {file_path} -o ~/main && ./main", timeout=CODE_RUN_TIMEOUT_SECONDS, )
# collect stdout, stderr from sandbox stdout = "\n".join(stdouts) stderr = "\n".join(stderrs) return stdout, stderr
def run_java_code(code: str, existing_sandbox_id: str | None = None) -> tuple[str, str]: """ Executes the provided Java code within a sandboxed environment and returns the output.
Args: code (str): The Java code to be executed.
Returns: tuple: (stdout, stderr) """ sandbox = reuse_or_create_sandbox(sandbox_id=existing_sandbox_id)
class_name = extract_java_class_name(code) file_path = f"~/{class_name}.java" sandbox.files.write(file_path, code, "user", 60)
is_success, stdouts, stderrs = run_command_in_sandbox( sandbox=sandbox, command=f"javac {file_path} && java {class_name}", timeout=CODE_RUN_TIMEOUT_SECONDS, )
# collect stdout, stderr from sandbox stdout = "\n".join(stdouts) stderr = "\n".join(stderrs) return stdout, stderr
def run_golang_code(code: str, existing_sandbox_id: str | None = None) -> tuple[str, str]: """ Executes the provided Go code within a sandboxed environment and returns the output.
Args: code (str): The Go code to be executed
Returns: tuple: (stdout, stderr) """ sandbox = reuse_or_create_sandbox(sandbox_id=existing_sandbox_id)
file_path = "~/main.go" sandbox.files.write(file_path, code, "user", 60)
is_success, stdouts, stderrs = run_command_in_sandbox( sandbox=sandbox, command=f"go run {file_path}", timeout=CODE_RUN_TIMEOUT_SECONDS, )
# collect stdout, stderr from sandbox stdout = "\n".join(stdouts) stderr = "\n".join(stderrs) return stdout, stderr
# def run_csharp_code(code: str, existing_sandbox_id: str | None = None) -> tuple[str, str]:# """# Executes the provided C# code within a sandboxed environment and returns the output.
# Args:# code (str): The C# code to be executed
# Returns:# tuple: (stdout, stderr)# """# sandbox = reuse_or_create_sandbox(sandbox_id=existing_sandbox_id)
# file_path = "~/main.cs"# sandbox.files.write(file_path, code, "user", 60)
# is_success, stdouts, stderrs = run_command_in_sandbox(# sandbox=sandbox,# command=f"mcs {file_path} && mono main.exe",# timeout=CODE_RUN_TIMEOUT_SECONDS,# )
# # collect stdout, stderr from sandbox# stdout = "\n".join(stdouts)# stderr = "\n".join(stderrs)# return stdout, stderr
def run_rust_code(code: str, existing_sandbox_id: str | None = None) -> tuple[str, str]: """ Executes the provided Rust code within a sandboxed environment and returns the output.
Args: code (str): The Rust code to be executed
Returns: tuple: (stdout, stderr) """ sandbox = reuse_or_create_sandbox(sandbox_id=existing_sandbox_id)
file_path = "~/main.rs" sandbox.files.write(file_path, code, "user", 60)
is_success, stdouts, stderrs = run_command_in_sandbox( sandbox=sandbox, command=f"rustc {file_path} && ./main", timeout=CODE_RUN_TIMEOUT_SECONDS, )
# collect stdout, stderr from sandbox stdout = "\n".join(stdouts) stderr = "\n".join(stderrs) return stdout, stderr
def on_edit_code( state, sandbox_state: ChatbotSandboxState, sandbox_output_md: gr.Markdown, sandbox_ui: SandboxComponent, sandbox_code: str, sandbox_dependency: gr.Dataframe,) -> Generator[tuple[Any, Any, Any, Any], None, None]: ''' Gradio Handler when code is edited manually by users. ''' if sandbox_state['enable_sandbox'] is False: yield None, None, None, None return if len(sandbox_code.strip()) == 0 or sandbox_code == sandbox_state['code_to_execute']: yield gr.skip(), gr.skip(), gr.skip(), gr.skip() return sandbox_state['code_to_execute'] = sandbox_code
# Extract packages from imports (without versions) python_deps_from_imports = set(extract_python_imports(sandbox_code)) npm_deps_from_imports = set(extract_js_imports(sandbox_code))
# Get existing dependencies with versions from state existing_python_deps, existing_npm_deps = sandbox_state["code_dependencies"]
# Create dictionaries to track package versions python_deps_dict = {} # pkg_name -> version npm_deps_dict = {} # pkg_name -> version
# First add existing dependencies with their specific versions for dep in existing_python_deps: pkg_name = dep.split('==')[0].split('>=')[0].split('<=')[0].split('~=')[0] version = dep[len(pkg_name):] if version: # If it has a specific version python_deps_dict[pkg_name] = version elif pkg_name in python_deps_from_imports: # Only keep packages that are still imported python_deps_dict[pkg_name] = "latest"
for dep in existing_npm_deps: if '@' in dep and not dep.startswith('@'): pkg_name = dep.split('@')[0] version = '@' + dep.split('@')[1] elif '@' in dep[1:]: # Handle scoped packages pkg_name, version = dep.rsplit('@', 1) version = '@' + version else: pkg_name = dep version = "latest" if version != "latest": # If it has a specific version npm_deps_dict[pkg_name] = version elif pkg_name in npm_deps_from_imports: # Only keep packages that are still imported npm_deps_dict[pkg_name] = "latest"
# Add new dependencies from imports with "latest" if not already present for dep in python_deps_from_imports: if dep not in python_deps_dict: python_deps_dict[dep] = "latest"
for dep in npm_deps_from_imports: if dep not in npm_deps_dict: npm_deps_dict[dep] = "latest"
# Convert to dataframe format dependencies = []
# Add Python packages for pkg_name, version in python_deps_dict.items(): dependencies.append(["python", pkg_name, version])
# Add NPM packages for pkg_name, version in npm_deps_dict.items(): dependencies.append(["npm", pkg_name, version])
# If no dependencies found, provide default empty rows if not dependencies: dependencies = [["python", "", ""], ["npm", "", ""]]
# Update dependencies in sandbox state sandbox_state["code_dependencies"] = ( [f"{pkg}{ver}" if ver != "latest" else pkg for pkg, ver in python_deps_dict.items()], [f"{pkg}{ver}" if ver != "latest" else pkg for pkg, ver in npm_deps_dict.items()] )
yield ( gr.skip(), # sandbox_output_md gr.skip(), # sandbox_ui gr.skip(), # sandbox_code gr.update(value=dependencies), # sandbox_dependency ) yield from on_run_code( state, sandbox_state, sandbox_output_md, sandbox_ui, sandbox_code, sandbox_dependency, )
def on_edit_dependency( state, sandbox_state: ChatbotSandboxState, sandbox_dependency: gr.Dataframe, sandbox_output_md: gr.Markdown, sandbox_ui: SandboxComponent, sandbox_code: str,) -> Generator[tuple[Any, Any, Any, Any], None, None]: """ Gradio Handler when dependencies are edited manually by users. Handles version specifications and dependency removal. """ if sandbox_state["enable_sandbox"] is False: yield None, None, None, None return
# Validate dependencies format is_valid, error_msg = validate_dependencies(sandbox_dependency) if not is_valid: yield ( gr.Markdown(f"Invalid dependencies: {error_msg}"), gr.skip(), gr.skip(), sandbox_dependency, # Return original dataframe ) return
# Convert dataframe format to separate python and npm lists python_deps = [] npm_deps = [] for dep in sandbox_dependency: dep_type, pkg_name, version = dep pkg_name = pkg_name.strip() version = version.strip()
# Skip empty rows if not pkg_name: continue
if dep_type.lower() == "python": # Handle Python package with version if version and version.lower() != "latest": if not any(op in version for op in ["==", ">=", "<=", "~=", ">", "<"]): python_deps.append(f"{pkg_name}=={version}") else: python_deps.append(f"{pkg_name}{version}") else: python_deps.append(pkg_name)
elif dep_type.lower() == "npm": # Handle NPM package with version if version and version.lower() != "latest": if not version.startswith("@"): version = "@" + version npm_deps.append(f"{pkg_name}{version}") else: npm_deps.append(pkg_name)
# Update sandbox state with new dependencies sandbox_state["code_dependencies"] = (python_deps, npm_deps)
# increase edit round sandbox_state['edit_round'] += 1
# First yield: Update UI with success message yield ( gr.Markdown("Dependencies updated successfully"), gr.skip(), # sandbox_ui gr.skip(), # sandbox_code sandbox_dependency, # Return the same dataframe )
# Second yield: Run code with new dependencies yield from on_run_code( state, sandbox_state, sandbox_output_md, sandbox_ui, sandbox_code, sandbox_dependency, )
def on_click_code_message_run( state, sandbox_state: ChatbotSandboxState, sandbox_output_md: gr.Markdown, sandbox_ui: SandboxComponent, sandbox_code: str, sandbox_dependency: gr.Dataframe, evt: gr.SelectData) -> Generator[SandboxGradioSandboxComponents, None, None]: ''' Gradio Handler when run code button in message is clicked. Update Sandbox components. ''' print("on_click_code_message_run")
if sandbox_state['enable_sandbox'] is False: yield None, None, None, None return if not evt.value.endswith(RUN_CODE_BUTTON_HTML): yield gr.skip(), gr.skip(), gr.skip(), gr.skip() return
message = evt.value.replace(RUN_CODE_BUTTON_HTML, "").strip() extract_result = extract_code_from_markdown( message=message, enable_auto_env=sandbox_state['sandbox_environment'] == SandboxEnvironment.AUTO ) if extract_result is None: yield gr.skip(), gr.skip(), gr.skip(), gr.skip() return
code, code_language, code_dependencies, env_selection = extract_result
# As sandbox is reused, no need to skip # if sandbox_state['code_to_execute'] == code and sandbox_state['code_language'] == code_language: # # skip if no changes # yield gr.skip(), gr.skip(), gr.skip(), gr.skip() # return
if code_language == 'tsx': code_language = 'typescript' code_language = code_language.lower() gradio_code_language = code_language.lower() if code_language and code_language.lower( # ensure gradio supports the code language ) in VALID_GRADIO_CODE_LANGUAGES else None
python_deps, npm_deps = code_dependencies
# Convert to dataframe format dependencies = []
# Add Python packages with versions for dep in python_deps: # Check if package has version specifier if any(op in dep for op in ['==', '>=', '<=', '~=']): # Split on first occurrence of version operator pkg_name = dep.split('==')[0].split('>=')[0].split('<=')[0].split('~=')[0] version = dep[len(pkg_name):] # Get everything after package name dependencies.append(["python", pkg_name, version]) else: dependencies.append(["python", dep, "latest"])
# Add NPM packages with versions for dep in npm_deps: # Check if package has version specifier if '@' in dep and not dep.startswith('@'): # Handle non-scoped packages with version pkg_name, version = dep.split('@', 1) dependencies.append(["npm", pkg_name, '@' + version]) elif '@' in dep[1:]: # Handle scoped packages with version # Split on last @ for scoped packages pkg_parts = dep.rsplit('@', 1) dependencies.append(["npm", pkg_parts[0], '@' + pkg_parts[1]]) else: dependencies.append(["npm", dep, "latest"])
# If no dependencies found, provide default empty rows if not dependencies: dependencies = [["python", "", ""], ["npm", "", ""]]
sandbox_state['code_to_execute'] = code sandbox_state['code_language'] = code_language sandbox_state["code_dependencies"] = code_dependencies if sandbox_state['sandbox_environment'] == SandboxEnvironment.AUTO: sandbox_state['auto_selected_sandbox_environment'] = env_selection
# reset edit round sandbox_state['edit_round'] = 0
yield ( gr.skip(), # sandbox_output_md gr.skip(), # sandbox_ui gr.update(value=code, language=gradio_code_language), # sandbox_code gr.update(value=dependencies) # sandbox_dependency )
yield from on_run_code( state, sandbox_state, sandbox_output_md, sandbox_ui, sandbox_code, sandbox_dependency, )
def on_run_code( state, sandbox_state: ChatbotSandboxState, sandbox_output_md: gr.Markdown, sandbox_ui: SandboxComponent, sandbox_code: str, sandbox_dependency: gr.Dataframe,) -> Generator[tuple[Any, Any, Any, Any], None, None]: ''' gradio fn when run code button is clicked. Update Sandbox components. ''' print("on_run_code")
if sandbox_state['enable_sandbox'] is False: yield None, None, None, None return
# validate e2b api key if not E2B_API_KEY: raise ValueError("E2B_API_KEY is not set in env vars.")
# hide and change value of the current sandbox UI to force refresh the sandbox # otherwise the sandbox might not change if the url is same yield ( gr.skip(), SandboxComponent( value=('', False, []), label="Example", visible=False, ), gr.skip(), gr.skip(), )
code, code_language = sandbox_state['code_to_execute'], sandbox_state['code_language'] if code is None or code_language is None: yield None, None, None, None return
gradio_code_language = code_language.lower() if code_language and code_language.lower( # ensure gradio supports the code language ) in VALID_GRADIO_CODE_LANGUAGES else None
# Use dependencies from sandbox_state instead of re-extracting code_dependencies = sandbox_state['code_dependencies'] python_deps, npm_deps = code_dependencies
# Helper function to extract package name without version def get_base_package_name(pkg: str) -> str: # For Python packages if any(op in pkg for op in ['==', '>=', '<=', '~=', '>', '<']): return pkg.split('==')[0].split('>=')[0].split('<=')[0].split('~=')[0].split('>')[0].split('<')[0] # For NPM packages if '@' in pkg and not pkg.startswith('@'): return pkg.split('@')[0] elif '@' in pkg[1:]: # Handle scoped packages return pkg.rsplit('@', 1)[0] return pkg
# Helper function to extract version from package string def get_package_version(pkg: str) -> str: # For Python packages if any(op in pkg for op in ['==', '>=', '<=', '~=', '>', '<']): base_name = get_base_package_name(pkg) return pkg[len(base_name):] # For NPM packages if '@' in pkg and not pkg.startswith('@'): return '@' + pkg.split('@', 1)[1] elif '@' in pkg[1:]: # Handle scoped packages _, version = pkg.rsplit('@', 1) return '@' + version return "latest"
# Create unified dependency dictionaries to avoid duplicates python_deps_dict = {} # pkg_name -> version npm_deps_dict = {} # pkg_name -> version
# Process Python dependencies for dep in python_deps: base_name = get_base_package_name(dep) version = get_package_version(dep) # Only update if we don't have a version yet or if we're replacing 'latest' if base_name not in python_deps_dict or python_deps_dict[base_name] == "latest": python_deps_dict[base_name] = version
# Process NPM dependencies for dep in npm_deps: base_name = get_base_package_name(dep) version = get_package_version(dep) # Only update if we don't have a version yet or if we're replacing 'latest' if base_name not in npm_deps_dict or npm_deps_dict[base_name] == "latest": npm_deps_dict[base_name] = version
# Convert unified dictionaries to dataframe format dependencies = [] for pkg_name, version in python_deps_dict.items(): dependencies.append(["python", pkg_name, version]) for pkg_name, version in npm_deps_dict.items(): dependencies.append(["npm", pkg_name, version])
# If no dependencies found, provide default empty rows if not dependencies: dependencies = [["python", "", ""], ["npm", "", ""]]
# Initialize output with loading message markdown_output_text = "### Sandbox Execution Log\n\n" yield ( gr.Markdown( value=markdown_output_text + "🔄 Initializing sandbox environment...", visible=True ), SandboxComponent(visible=False), gr.Code(value=code, language=gradio_code_language, visible=True), gr.update(value=dependencies, visible=True), # Update with unified dependencies )
# Use auto_selected_sandbox_environment only when in AUTO mode, otherwise use sandbox_environment sandbox_env = ( sandbox_state['auto_selected_sandbox_environment'] if sandbox_state['sandbox_environment'] == SandboxEnvironment.AUTO else sandbox_state['sandbox_environment'] )
def update_markdown_output(message: str, clear_output: bool = False): nonlocal markdown_output_text if clear_output: markdown_output_text = "" markdown_output_text += f"\n{message}" return ( gr.Markdown(value=markdown_output_text, visible=True, sanitize_html=False), gr.skip(), gr.skip(), gr.skip() # Always include dependencies update )
sandbox_id: str | None = sandbox_state["sandbox_id"] # the sandbox id sandbox_output: str = "" # stdout from sandbox sandbox_error: str = "" # stderr from sandbox print(f"sandbox_env: {sandbox_env}") match sandbox_env: case SandboxEnvironment.HTML: yield update_markdown_output("🔄 Setting up HTML sandbox...") sandbox_url, sandbox_id, sandbox_error = run_html_sandbox( code=code, code_dependencies=code_dependencies, existing_sandbox_id=sandbox_state['sandbox_id'], ) if sandbox_error: yield update_markdown_output("❌ HTML sandbox failed to run!", clear_output=True) yield update_markdown_output(f"### Stderr:\n```markdown\n{sandbox_error}\n```\n\n") else: yield update_markdown_output("✅ HTML sandbox is ready!", clear_output=True) yield ( gr.Markdown(value=markdown_output_text, visible=True), SandboxComponent( value=(sandbox_url, True, []), label="Example", visible=True, key="newsandbox", ), gr.skip(), gr.skip(), ) case SandboxEnvironment.REACT: yield update_markdown_output("🔄 Setting up React sandbox...") code_run_result = run_react_sandbox( code=code, code_dependencies=code_dependencies, existing_sandbox_id=sandbox_state['sandbox_id'], ) sandbox_id, sandbox_error = code_run_result['sandbox_id'], code_run_result['stderr'] if code_run_result['is_run_success'] is False and sandbox_error: yield update_markdown_output("❌ React sandbox failed to run!", clear_output=True) yield update_markdown_output(f"### Stderr:\n```markdown\n{sandbox_error}\n```\n\n") else: yield update_markdown_output("✅ React sandbox is ready!", clear_output=True) yield ( gr.Markdown(value=markdown_output_text, visible=True), SandboxComponent( value=(code_run_result['sandbox_url'], True, []), label="Example", visible=True, key="newsandbox", ), gr.skip(), gr.skip(), ) case SandboxEnvironment.VUE: yield update_markdown_output("🔄 Setting up Vue sandbox...") code_run_result = run_vue_sandbox( code=code, code_dependencies=code_dependencies, existing_sandbox_id=sandbox_state['sandbox_id'], ) sandbox_id, sandbox_error = code_run_result['sandbox_id'], code_run_result['stderr'] if code_run_result['is_run_success'] is False and code_run_result['stderr']: yield update_markdown_output("❌ Vue sandbox failed to run!", clear_output=True) yield update_markdown_output(f"### Stderr:\n```markdown\n{code_run_result['stderr']}\n```\n\n") else: yield update_markdown_output("✅ Vue sandbox is ready!", clear_output=True) yield ( gr.Markdown(value=markdown_output_text, visible=True), SandboxComponent( value=(code_run_result['sandbox_url'], True, []), label="Example", visible=True, key="newsandbox", ), gr.skip(), gr.skip(), ) case SandboxEnvironment.PYGAME: yield update_markdown_output("🔄 Setting up PyGame sandbox...") code_run_result = run_pygame_sandbox( code=code, code_dependencies=code_dependencies, existing_sandbox_id=sandbox_state['sandbox_id'], ) sandbox_id, sandbox_error = code_run_result['sandbox_id'], code_run_result['stderr'] if code_run_result['is_run_success'] is False and code_run_result['stderr']: yield update_markdown_output("❌ PyGame sandbox failed to run!", clear_output=True) yield update_markdown_output(f"### Stderr:\n```markdown\n{code_run_result['stderr']}\n```\n\n") else: yield update_markdown_output("✅ PyGame sandbox is ready!", clear_output=True) yield ( gr.Markdown(value=markdown_output_text, visible=True), SandboxComponent( value=(code_run_result['sandbox_url'], True, []), label="Example", visible=True, key="newsandbox", ), gr.skip(), gr.skip(), ) case SandboxEnvironment.GRADIO: yield update_markdown_output("🔄 Setting up Gradio sandbox...") sandbox_url, sandbox_id, sandbox_error = run_gradio_sandbox( code=code, code_dependencies=code_dependencies, existing_sandbox_id=sandbox_state['sandbox_id'], ) if sandbox_error: yield update_markdown_output("❌ Gradio sandbox failed to run!", clear_output=True) yield update_markdown_output(f"### Stderr:\n```markdown\n{sandbox_error}\n```\n\n") else: yield update_markdown_output("✅ Gradio sandbox is ready!", clear_output=True) yield ( gr.Markdown(value=markdown_output_text, visible=True), SandboxComponent( value=(sandbox_url, True, []), label="Example", visible=True, key="newsandbox", ), gr.skip(), gr.skip(), ) case SandboxEnvironment.STREAMLIT: yield update_markdown_output("🔄 Setting up Streamlit sandbox...") sandbox_url, sandbox_id, sandbox_error = run_streamlit_sandbox( code=code, code_dependencies=code_dependencies, existing_sandbox_id=sandbox_state['sandbox_id'], ) if sandbox_error: yield update_markdown_output("❌ Streamlit sandbox failed to run!", clear_output=True) yield update_markdown_output(f"### Stderr:\n```markdown\n{sandbox_error}\n```\n\n") else: yield update_markdown_output("✅ Streamlit sandbox is ready!", clear_output=True) yield ( gr.Markdown(value=markdown_output_text, visible=True), SandboxComponent( value=(sandbox_url, True, []), label="Example", visible=True, key="newsandbox", ), gr.skip(), gr.skip(), ) case SandboxEnvironment.MERMAID: yield update_markdown_output("🔄 Setting up Mermaid visualization...") # Convert Mermaid to HTML at execution time html_code = mermaid_to_html(code, theme='light') sandbox_url, sandbox_id, sandbox_error = run_html_sandbox( code=html_code, code_dependencies=code_dependencies, existing_sandbox_id=sandbox_state['sandbox_id'], ) if sandbox_error: yield update_markdown_output("❌ Mermaid visualization failed to render!", clear_output=True) yield update_markdown_output(f"### Stderr:\n```markdown\n{sandbox_error}\n```\n\n") else: yield update_markdown_output("✅ Mermaid visualization is ready!", clear_output=True) yield ( gr.Markdown(value=markdown_output_text, visible=True), SandboxComponent( value=(sandbox_url, True, []), label="Mermaid Diagram", visible=True, key="newsandbox", ), gr.skip(), gr.skip(), ) case SandboxEnvironment.PYTHON_RUNNER: yield update_markdown_output("🔄 Running Python Runner...", clear_output=True) sandbox_output, sandbox_error = run_code_interpreter( code=code, code_language='python', code_dependencies=code_dependencies ) if sandbox_error: yield update_markdown_output("❌ Python Runner failed to run!", clear_output=True) yield update_markdown_output(f"### Stderr:\n```markdown\n{sandbox_error}\n```\n\n") else: yield update_markdown_output("✅ Code execution is ready!", clear_output=True) yield ( gr.Markdown( value=markdown_output_text + "\n\n" + sandbox_output, sanitize_html=False, visible=True, ), SandboxComponent( value=("", False, []), label="Example", visible=False, key="newsandbox", ), gr.skip(), gr.skip(), ) case SandboxEnvironment.JAVASCRIPT_RUNNER: yield update_markdown_output("🔄 Running JavaScript Runner...", clear_output=True) sandbox_output, sandbox_error = run_code_interpreter( code=code, code_language='javascript', code_dependencies=code_dependencies ) if sandbox_error: yield update_markdown_output("❌ JavaScript Runner failed to run!", clear_output=True) yield update_markdown_output(f"### Stderr:\n```markdown\n{sandbox_error}\n```\n\n") else: yield update_markdown_output("✅ Code execution is ready!", clear_output=True) yield ( gr.Markdown( value=markdown_output_text + "\n\n" + sandbox_output, sanitize_html=False, visible=True, ), SandboxComponent( value=("", False, []), label="Example", visible=False, key="newsandbox", ), gr.skip(), gr.skip(), ) case SandboxEnvironment.C_RUNNER: yield update_markdown_output("🔄 Running C Runner...", clear_output=True) sandbox_output, sandbox_error = run_c_code( code=code, existing_sandbox_id=sandbox_state['sandbox_id'] ) if sandbox_error: yield update_markdown_output("❌ C Runner failed to run!", clear_output=True) yield update_markdown_output(f"### Stderr:\n```markdown\n{sandbox_error}\n```\n\n") else: yield update_markdown_output("✅ Code execution is ready!", clear_output=True) yield ( gr.Markdown( value=markdown_output_text + "\n\n" + f"```markdown\n{sandbox_output}\n```", sanitize_html=False, visible=True, ), SandboxComponent( value=("", False, []), label="Example", visible=False, key="newsandbox", ), gr.skip(), gr.skip(), ) case SandboxEnvironment.CPP_RUNNER: yield update_markdown_output("🔄 Running C++ Runner...", clear_output=True) sandbox_output, sandbox_error = run_cpp_code( code=code, existing_sandbox_id=sandbox_state['sandbox_id'] ) if sandbox_error: yield update_markdown_output("❌ C++ Runner failed to run!", clear_output=True) yield update_markdown_output(f"### Stderr:\n```markdown\n{sandbox_error}\n```\n\n") else: yield update_markdown_output("✅ Code execution is ready!", clear_output=True) yield ( gr.Markdown( value=markdown_output_text + "\n\n" + f"```markdown\n{sandbox_output}\n```", sanitize_html=False, visible=True, ), SandboxComponent( value=("", False, []), label="Example", visible=False, key="newsandbox", ), gr.skip(), gr.skip(), ) case SandboxEnvironment.JAVA_RUNNER: yield update_markdown_output("🔄 Running Java Runner...", clear_output=True) sandbox_output, sandbox_error = run_java_code( code=code, existing_sandbox_id=sandbox_state['sandbox_id'] ) if sandbox_error: yield update_markdown_output("❌ Java Runner failed to run!", clear_output=True) yield update_markdown_output(f"### Stderr:\n```markdown\n{sandbox_error}\n```\n\n") else: yield update_markdown_output("✅ Code execution is ready!", clear_output=True) yield ( gr.Markdown( value=markdown_output_text + "\n\n" + f"```markdown\n{sandbox_output}\n```", sanitize_html=False, visible=True, ), SandboxComponent( value=("", False, []), label="Example", visible=False, key="newsandbox", ), gr.skip(), gr.skip(), ) case SandboxEnvironment.GOLANG_RUNNER: yield update_markdown_output("🔄 Running Go Runner...", clear_output=True) sandbox_output, sandbox_error = run_golang_code( code=code, existing_sandbox_id=sandbox_state['sandbox_id'] ) if sandbox_error: yield update_markdown_output("❌ Go Runner failed to run!", clear_output=True) yield update_markdown_output(f"### Stderr:\n```markdown\n{sandbox_error}\n```\n\n") else: yield update_markdown_output("✅ Code execution is ready!", clear_output=True) yield ( gr.Markdown( value=markdown_output_text + "\n\n" + f"```markdown\n{sandbox_output}\n```", sanitize_html=False, visible=True, ), SandboxComponent( value=("", False, []), label="Example", visible=False, key="newsandbox", ), gr.skip(), gr.skip(), ) # case SandboxEnvironment.CSHARP_RUNNER: # yield update_markdown_output("🔄 Running C# Runner...", clear_output=True) # output, stderr = run_csharp_code( # code=code, existing_sandbox_id=sandbox_state['sandbox_id'] # ) # yield update_markdown_output("✅ Code execution is ready!", clear_output=True) # if output: # yield update_markdown_output(f"### Stdout:\n```markdown\n{output}\n```\n\n") # if stderr: # yield update_markdown_output(f"### Stderr:\n```markdown\n{stderr}\n```\n\n") case SandboxEnvironment.RUST_RUNNER: yield update_markdown_output("🔄 Running Rust Runner...", clear_output=True) sandbox_output, sandbox_error = run_rust_code( code=code, existing_sandbox_id=sandbox_state['sandbox_id'] ) if sandbox_error: yield update_markdown_output("❌ Rust Runner failed to run!", clear_output=True) yield update_markdown_output(f"### Stderr:\n```markdown\n{sandbox_error}\n```\n\n") else: yield update_markdown_output("✅ Code execution is ready!", clear_output=True) yield ( gr.Markdown( value=markdown_output_text + "\n\n" + f"```markdown\n{sandbox_output}\n```", sanitize_html=False, visible=True, ), SandboxComponent( value=("", False, []), label="Example", visible=False, key="newsandbox", ), gr.skip(), gr.skip(), ) case _: yield ( gr.Markdown(value=code, visible=True), SandboxComponent( value=("", False, []), label="Example", visible=False, key="newsandbox", ), gr.skip(), gr.skip(), )
sandbox_state['sandbox_run_round'] += 1 sandbox_state["sandbox_output"] = sandbox_output # record sandbox output if exists sandbox_state["sandbox_error"] = sandbox_error # record sandbox error if exists # generate a random sandbox id if not exists as some code runners might not return sandbox id sandbox_state['sandbox_id'] = sandbox_id if sandbox_id else str(uuid.uuid4()) log_sandbox_telemetry_gradio_fn( sandbox_state=sandbox_state, sandbox_ui_value=None, )
print("on_run_code done")123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148'''Constants for sandbox.'''
import os
E2B_API_KEY = os.environ.get("E2B_API_KEY")'''API key for the e2b API.'''
AZURE_BLOB_STORAGE_CONNECTION_STRING = os.environ.get("AZURE_STORAGE_CONNECTION_STRING")'''API key for the Azure Blob Storage.'''
AZURE_BLOB_STORAGE_CONTAINER_NAME = "softwarearenalogs"'''Contianer name for the Azure Blob Storage.'''
SANDBOX_TEMPLATE_ID: str = "bxq9sha9l55ytsyfturr"'''Template ID for the sandbox.'''
SANDBOX_NGINX_PORT: int = 8000'''Nginx port for the sandbox.'''
SANDBOX_TIMEOUT_SECONDS: int = 1 * 60'''Timeout in seconds for created sandboxes to expire.'''
CODE_RUN_TIMEOUT_SECONDS: int = 60'''Timeout in seconds for code execution.'''
SANDBOX_RETRY_COUNT: int = 3'''Number of times to retry the sandbox creation.'''
INSTALLED_PYPI_PACKAGES = [ "boto3", "botocore", "urllib3", "setuptools", "requests", "certifi", "idna", "charset-normalizer", "packaging", "typing-extensions", "python-dateutil", "aiobotocore", "s3transfer", "grpcio-status", "pyyaml", "six", "fsspec", "s3fs", "numpy", "wheel", "pip", "cryptography", "awscli", "pydantic", "cffi", "attrs", "google-api-core", "pycparser", "pandas", "importlib-metadata", "jmespath", "click", "zipp", "rsa", "pyasn1", "markupsafe", "pytz", "colorama", "protobuf", "platformdirs", "jinja2", "rich", "tomli", "pytest", "pydantic-core", "pyjwt", "pluggy", "aiohttp", "virtualenv", "jsonschema", "googleapis-common-protos", "cachetools", "google-auth", "filelock", "wrapt", "sqlalchemy", "docutils", "pyasn1-modules", "pyarrow", "greenlet", "iniconfig", "pygments", "annotated-types", "yarl", "requests-oauthlib", "tzdata", "psutil", "multidict", "pyparsing", "requests-toolbelt", "exceptiongroup", "werkzeug", "soupsieve", "oauthlib", "beautifulsoup4", "frozenlist", "more-itertools", "distlib", "tomlkit", "pathspec", "aiosignal", "grpcio", "tqdm", "scipy", "async-timeout", "pillow", "isodate", "anyio", "sortedcontainers", "decorator", "markdown-it-py", "deprecated", "mypy-extensions", "sniffio", "httpx", "coverage", "openpyxl", "flask", "rpds-py", "et-xmlfile"]123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393'''Prompts for the sandbox.'''
GENERAL_SANDBOX_INSTRUCTION = """\You are an expert Software Engineer, UI/UX designer, and product manager. Your task is to generate self-contained, executable code for a single file or block that can run directly in a sandbox environment. Feel free to ask questions or explain your reasoning.If you do a great job based on the instructions, you will be rewarded with a high salary and a promotion.
Your code must be written using one of these supported development frameworks and environments:- React (JavaScript/TypeScript)- Vue (JavaScript/TypeScript)- HTML (Vanilla HTML)- Gradio (Python)- Streamlit (Python)- PyGame (Python)- Mermaid (Markdown)- Python Runner- JavaScript Runner- Command Line Code Runner (C/C++/Go/Java/Rust)
All web framework code (React, Vue, HTML) must be directly rendered in a browser and immediately executable without additional setup. DO NOT create separate CSS filesPython-based frameworks should be directly executable in a browser environment.The code to be executed in Runners must be plain Python or JavaScript programs that do not require web UI frameworks or standard user input.
The code must be in the markdown format:```<language><code>```
Before you begin writing any code, you must follow these fundamental rules:- You are NOT allowed to start directly with a code block. Before writing code, ALWAYS think carefully step-by-step- Your response must contain a clear explanation of the solution you are providing- ALWAYS generate complete, self-contained code in a single file- You CAN NOT split your program into multiple files or multiple code blocks- If you use any external libraries, make sure to specify them for the installation command in either `pip install` or `npm install`- You prefer JavaScript over HTML- Each code block must be completely independent. If modifications are needed, the entire code block must be rewritten- When fetching data, you MUST use external libraries and packages, and avoid using placeholder URLs or URLs that require API keys- Make sure the program is functional by creating a state when needed and having no required props- Make sure to include all necessary code in one file- There are no additional files in the local file system, unless you create them inside the same program- Do not touch project dependencies files like package.json, package-lock.json, requirements.txt, etc
When developing with React or Vue components, follow these specific requirements:- Use TypeScript or JavaScript as the language- DO NOT use gray text color on a white background- Make sure it can run by itself by using a default export at the end of the file- DO NOT CALL `ReactDOM.render()` AT THE END OF THE FILE- Use Tailwind classes for styling. DO NOT USE ARBITRARY VALUES (e.g. 'h-[600px]'). Make sure to use a consistent color palette- If you use any imports from React like `useState` or `useEffect`, make sure to import them directly- Use Tailwind margin and padding classes to style the components and ensure proper spacing- Various npm packages are available to be imported, e.g. `import { LineChart, XAxis, ... } from "recharts"` & `<LineChart ...><XAxis dataKey="name"> ...`- Images from the web are not allowed, but you can use placeholder images by specifying the width and height like so `<img src="/api/placeholder/400/320" alt="placeholder" />`
For Python development, you must follow these constraints:- For any programs that require user inputs, you MUST USE `gradio` or `streamlit`- Choose suitable PyPI packages to be imported, e.g., `import pandas`- Avoid using libraries that require desktop GUI interfaces, with the exceptions of `pygame`, `gradio`, and `streamlit` which are explicitly supported- For PyGame applications, you have to write the main function as an async function like:```pythonimport asyncioimport pygame
async def main(): global game_state while game_state: game_state(pygame.event.get()) pygame.display.update() await asyncio.sleep(0) # it must be called on every frame
if __name__ == "__main__": asyncio.run(main())```
For HTML development, ensure that:- All HTML code must be self-contained in a single file- Include any necessary CSS and JavaScript within the HTML file- Ensure the code is directly executable in a browser environment- Images from the web are not allowed, but you can use placeholder images by specifying the width and height like so `<img src="/api/placeholder/400/320" alt="placeholder" />`
For Mermaid development:- Write Mermaid diagrams directly using ```mermaid code blocks, e.g.:```mermaidgraph TD; A-->B;```
For Command Line Code Runner (C/C++/Go/Java/Rust), ensure that:- ALWAYS generate complete, self-contained code in a single file. Avoid non-standard libraries.- Your code should be able to be compiled and run directly.- Your code must complete the task without any user inputs. It should not be long running.- You should provide example test cases in the code and output the result to stdout or stderr.
The code must be in the markdown format:```<language><code>```"""
DEFAULT_PYTHON_RUNNER_INSTRUCTION = """You are an expert Software Engineer. Your task is to generate self-contained, executable Python code that can run directly in a code interpreter environment.
Before you begin writing any code, you must follow these fundamental rules:- You are NOT allowed to start directly with a code block. Before writing code, ALWAYS think carefully step-by-step- Your response must contain a clear explanation of the solution you are providing- ALWAYS generate complete, self-contained code in a single file- If you use any external libraries, make sure to specify them for installation with `pip install`- Make sure to include all necessary code in one file- Make sure it does not require any user inputs- Choose suitable PyPI packages to be imported, e.g., `import pandas`
The code must be in the markdown format:```python<code>```
You can output in stdout, stderr, or render images, plots, and tables."""
DEFAULT_JAVASCRIPT_RUNNER_INSTRUCTION = """You are an expert Software Engineer. Your task is to generate self-contained JavaScript code that can run directly in a code interpreter environment.
Before you begin writing any code, you must follow these fundamental rules:- You are NOT allowed to start directly with a code block. Before writing code, ALWAYS think carefully step-by-step- Your response must contain a clear explanation of the solution you are providing- ALWAYS generate complete, self-contained code in a single file- If you use any external libraries, make sure to specify them for installation with `npm install`- Make sure to include all necessary code in one file- Ensure the code is self-contained and does not rely on browser-specific APIs
The code must be in the markdown format:```javascript<code>```
You can output in stdout, stderr, or render images, plots, and tables."""
DEFAULT_HTML_SANDBOX_INSTRUCTION = """You are an expert Software Engineer and UI/UX designer. Your task is to generate self-contained HTML code that can run directly in a browser environment.
Before you begin writing any code, you must follow these fundamental rules:- You are NOT allowed to start directly with a code block. Before writing code, ALWAYS think carefully step-by-step- Your response must contain a clear explanation of the solution you are providing- ALWAYS generate complete, self-contained code in a single file- Include any necessary CSS and JavaScript within the HTML file- If you use any external libraries, make sure to specify them- Make sure the program is functional by creating a state when needed- Images from the web are not allowed, but you can use placeholder images by specifying the width and height like so `<img src="/api/placeholder/400/320" alt="placeholder" />`
The code must be in the markdown format:```html<code>```
For HTML development, ensure that:- All HTML code must be self-contained in a single file- Include any necessary CSS and JavaScript within the HTML file- Ensure the code is directly executable in a browser environment- Images from the web are not allowed, but you can use placeholder images by specifying the width and height like so `<img src="/api/placeholder/400/320" alt="placeholder" />`"""
DEFAULT_REACT_SANDBOX_INSTRUCTION = """You are an expert Software Engineer and UI/UX designer. Your task is to generate a self-contained React component using TypeScript that can run directly in a browser environment.
Before you begin writing any code, you must follow these fundamental rules:- You are NOT allowed to start directly with a code block. Before writing code, ALWAYS think carefully step-by-step- Your response must contain a clear explanation of the solution you are providing- ALWAYS generate complete, self-contained code in a single file- If you use any external libraries, make sure to specify them for installation with `npm install`- Make sure the program is functional by creating a state when needed and having no required props- Make sure it can run by itself by using a default export at the end of the file- DO NOT CALL `ReactDOM.render()` AT THE END OF THE FILE- Use Tailwind classes for styling. DO NOT USE ARBITRARY VALUES (e.g. 'h-[600px]'). Make sure to use a consistent color palette- If you use any imports from React like `useState` or `useEffect`, make sure to import them directly- Images from the web are not allowed, but you can use placeholder images by specifying the width and height like so `<img src="/api/placeholder/400/320" alt="placeholder" />`
The code must be in the markdown format:```typescript<code>```
When developing with React components, follow these specific requirements:- Use TypeScript or JavaScript as the language- DO NOT use gray text color on a white background- Make sure it can run by itself by using a default export at the end of the file- DO NOT CALL `ReactDOM.render()` AT THE END OF THE FILE- Use Tailwind classes for styling. DO NOT USE ARBITRARY VALUES (e.g. 'h-[600px]'). Make sure to use a consistent color palette- If you use any imports from React like `useState` or `useEffect`, make sure to import them directly- Use Tailwind margin and padding classes to style the components and ensure proper spacing- Various npm packages are available to be imported, e.g. `import { LineChart, XAxis, ... } from "recharts"` & `<LineChart ...><XAxis dataKey="name"> ...`- Images from the web are not allowed, but you can use placeholder images by specifying the width and height like so `<img src="/api/placeholder/400/320" alt="placeholder" />`"""
DEFAULT_VUE_SANDBOX_INSTRUCTION = """You are an expert Software Engineer and UI/UX designer. Your task is to generate a self-contained Vue.js component using TypeScript that can run directly in a browser environment.
Before you begin writing any code, you must follow these fundamental rules:- You are NOT allowed to start directly with a code block. Before writing code, ALWAYS think carefully step-by-step- Your response must contain a clear explanation of the solution you are providing- ALWAYS generate complete, self-contained code in a single file- If you use any external libraries, make sure to specify them for installation with `npm install`- Make sure the program is functional by creating a state when needed and having no required props- The component should be a simple custom page in a styled `<div>` element- Do not include <NuxtWelcome /> or reference any external components- Use Tailwind classes for styling. DO NOT USE ARBITRARY VALUES (e.g. 'h-[600px]'). Make sure to use a consistent color palette- Images from the web are not allowed, but you can use placeholder images by specifying the width and height like so `<img src="/api/placeholder/400/320" alt="placeholder" />`
The code must be in the markdown format:```vue<code>```
When developing with Vue components, follow these specific requirements:- Use Vue 3's Composition API with <script setup> syntax for better TypeScript integration- Use TypeScript for type safety and better developer experience- Properly type all props, emits, and refs using Vue 3's type system- Use defineProps, defineEmits, and other Vue 3 macros correctly- Implement reactive state management using ref() or reactive() from Vue- Follow Vue 3's best practices for component organization and lifecycle management- Use computed properties for derived state- Handle component events using proper Vue 3 event handling syntax- Use Tailwind classes for styling with a consistent design system- Ensure components are responsive using Tailwind's responsive classes- Use Vue's built-in transition and animation systems when needed- Follow proper Vue 3 security practices (e.g., v-html only when necessary)- Implement proper error handling and loading states- Add comments explaining complex logic or component structure- Use async/await for asynchronous operations- Ensure the component is accessible following ARIA best practices"""
DEFAULT_PYGAME_SANDBOX_INSTRUCTION = """You are an expert Software Engineer and UI/UX designer. Your task is to generate self-contained PyGame code that can run directly in a browser environment.
Before you begin writing any code, you must follow these fundamental rules:- You are NOT allowed to start directly with a code block. Before writing code, ALWAYS think carefully step-by-step- Your response must contain a clear explanation of the solution you are providing- ALWAYS generate complete, self-contained code in a single file- If you use any external libraries, make sure to specify them for installation with `pip install`- Make sure it does not require any user inputs- Write the main function as an async function like:
```pythonimport asyncioimport pygame
async def main(): global game_state while game_state: game_state(pygame.event.get()) pygame.display.update() await asyncio.sleep(0) # it must be called on every frame
if __name__ == "__main__": asyncio.run(main())```
The code must be in the markdown format:```python<code>```"""
DEFAULT_GRADIO_SANDBOX_INSTRUCTION = """You are an expert Software Engineer and UI/UX designer. Your task is to generate self-contained Gradio application code that can run directly in a browser environment.
Before you begin writing any code, you must follow these fundamental rules:- You are NOT allowed to start directly with a code block. Before writing code, ALWAYS think carefully step-by-step- Your response must contain a clear explanation of the solution you are providing- ALWAYS generate complete, self-contained code in a single file- If you use any external libraries, make sure to specify them for installation with `pip install`- Make sure it does not require any user inputs- Choose suitable PyPI packages to be imported, e.g., `import pandas`
The code must be in the markdown format:```python<code>```"""
DEFAULT_STREAMLIT_SANDBOX_INSTRUCTION = """You are an expert Software Engineer and UI/UX designer. Your task is to generate self-contained Streamlit application code that can run directly in a browser environment.
Before you begin writing any code, you must follow these fundamental rules:- You are NOT allowed to start directly with a code block. Before writing code, ALWAYS think carefully step-by-step- Your response must contain a clear explanation of the solution you are providing- ALWAYS generate complete, self-contained code in a single file- If you use any external libraries, make sure to specify them for installation with `pip install`- Make sure it does not require any user inputs- Choose suitable PyPI packages to be imported, e.g., `import pandas`- The app should automatically reload when changes are made
The code must be in the markdown format:```python<code>```"""
DEFAULT_MERMAID_SANDBOX_INSTRUCTION = """You are an expert Software Engineer. Your task is to generate self-contained Mermaid diagram code that can be rendered directly.
Before you begin writing any code, you must follow these fundamental rules:- You are NOT allowed to start directly with a code block. Before writing code, ALWAYS think carefully step-by-step- Your response must contain a clear explanation of the solution you are providing- ALWAYS generate complete, self-contained code in a single file
The code must be in the markdown format:```mermaid<code>```
Example:```mermaidgraph TD; A-->B;```"""
DEFAULT_C_CODE_RUN_SANDBOX_INSTRUCTION = """You are an expert Software Engineer. Your task is to generate self-contained C code that can run directly in a code runner environment.
Ensure that:- ALWAYS generate complete, self-contained code in a single file. Avoid non-standard libraries.- Your code should be able to be compiled and run directly.- Your code must complete the task without any user inputs. It should not be long running.- You should provide example test cases in the code and output the result to stdout or stderr.
The code must be in the markdown format:```c<code>```"""
DEFAULT_CPP_CODE_RUN_SANDBOX_INSTRUCTION = """You are an expert Software Engineer. Your task is to generate self-contained C++ code that can run directly in a code runner environment.
Ensure that:- ALWAYS generate complete, self-contained code in a single file. Avoid non-standard libraries.- Your code should be able to be compiled and run directly.- Your code must complete the task without any user inputs. It should not be long running.- You should provide example test cases in the code and output the result to stdout or stderr.
The code must be in the markdown format:```cpp<code>```"""
DEFAULT_JAVA_CODE_RUN_SANDBOX_INSTRUCTION = """You are an expert Software Engineer. Your task is to generate self-contained Java code that can run directly in a code runner environment.
Ensure that:- ALWAYS generate complete, self-contained code in a single file. Avoid non-standard libraries.- Your code should be able to be compiled and run directly.- Your code must complete the task without any user inputs. It should not be long running.- You should provide example test cases in the code and output the result to stdout or stderr.
The code must be in the markdown format:```java<code>```"""
DEFAULT_GOLANG_CODE_RUN_SANDBOX_INSTRUCTION = """You are an expert Software Engineer. Your task is to generate self-contained Go code that can run directly in a code runner environment.
Ensure that:- ALWAYS generate complete, self-contained code in a single file. Avoid non-standard libraries.- Your code should be able to be compiled and run directly.- Your code must complete the task without any user inputs. It should not be long running.- You should provide example test cases in the code and output the result to stdout or stderr.
The code must be in the markdown format:```go<code>```"""
DEFAULT_RUST_CODE_RUN_SANDBOX_INSTRUCTION = """You are an expert Software Engineer. Your task is to generate self-contained Rust code that can run directly in a code runner environment.
Ensure that:- ALWAYS generate complete, self-contained code in a single file. Avoid non-standard libraries.- Your code should be able to be compiled and run directly.- Your code must complete the task without any user inputs. It should not be long running.- You should provide example test cases in the code and output the result to stdout or stderr.
The code must be in the markdown format:```rust<code>```"""123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254'''Facades for interacting with the e2b sandbox.'''
import jsonfrom typing import Literalfrom e2b import Sandboxfrom e2b.sandbox.commands.command_handle import CommandExitExceptionfrom e2b.exceptions import TimeoutExceptionimport timeimport threadingfrom httpcore import ReadTimeoutimport queue
from .constants import E2B_API_KEY, SANDBOX_TEMPLATE_ID, SANDBOX_NGINX_PORT, SANDBOX_RETRY_COUNT, SANDBOX_TIMEOUT_SECONDS, INSTALLED_PYPI_PACKAGES
def create_sandbox(template: str = SANDBOX_TEMPLATE_ID) -> Sandbox: ''' Create a new sandbox. Will retry if the sandbox creation fails. ''' print("Creating new sandbox...") for attempt in range(1, SANDBOX_RETRY_COUNT + 1): try: return Sandbox( api_key=E2B_API_KEY, domain="e2b-foxtrot.dev", template=template, timeout=SANDBOX_TIMEOUT_SECONDS, ) except Exception as e: if attempt < SANDBOX_RETRY_COUNT: time.sleep(1 * attempt) else: raise e raise RuntimeError("Failed to create sandbox after maximum attempts")
def reuse_or_create_sandbox(sandbox_id: str | None, template: str = SANDBOX_TEMPLATE_ID) -> Sandbox: ''' Reuse an existing sandbox if it is running, otherwise create a new sandbox. ''' sandbox = None
if sandbox_id is not None: try: sandbox = Sandbox.connect( sandbox_id=sandbox_id, api_key=E2B_API_KEY, ) if not sandbox.is_running(request_timeout=5): sandbox = None except Exception as e: pass
if sandbox is not None: sandbox.set_timeout(timeout=SANDBOX_TIMEOUT_SECONDS) else: sandbox = create_sandbox(template=template)
return sandbox
def run_command_in_sandbox( sandbox: Sandbox, command: str, working_directory: str | None = None, timeout: int = 60, print_output: bool = True, ) -> tuple[bool, list[str], list[str]]: ''' Run a command in the sandbox. Return whether the command was successful and the stdout and stderr output. ''' is_run_success = False stdouts: list[str] = [] stderrs: list[str] = []
try: command_result = sandbox.commands.run( cmd=command, cwd=working_directory, timeout=timeout, request_timeout=timeout + 5, on_stdout=lambda message: stdouts.append(message), on_stderr=lambda message: stderrs.append(message), ) if command_result and command_result.exit_code == 0: is_run_success = True except Exception as e: stderrs.append(str(e)) is_run_success = False
if print_output: print(f"Command: {command}") for stdout in stdouts: print(stdout) for stderr in stderrs: print(stderr)
return is_run_success, stdouts, stderrs
def install_pip_dependencies(sandbox: Sandbox, dependencies: list[str]) -> list[str]: ''' Install pip dependencies in the sandbox.
Return errors if any. ''' install_errors = [] if not dependencies: return install_errors for dependency in dependencies: if dependency not in INSTALLED_PYPI_PACKAGES: try: sandbox.commands.run( f"uv pip install --system {dependency}", timeout=60 * 3, on_stdout=lambda message: print(message), on_stderr=lambda message: print(message), ) except Exception as e: install_errors.append(f"Error during installing pip package {dependency}: {str(e)}") continue
return install_errors
def parse_npm_package_name(package) -> tuple[str, str | None]: '''abc@123 -> abc, 123''' return package.split("@")[0], package.split("@")[1] if "@" in package else None
def is_npm_package_installed(package: str, installed_packages: dict[str, str | None]) -> bool: package_name, package_version = parse_npm_package_name(package) return package_name in installed_packages and (package_version is None or installed_packages[package_name] == package_version)
def get_installed_npm_packages(sandbox: Sandbox, project_root: str) -> dict[str, str | None]: installed_packages_raw = [] sandbox.commands.run( "npm list --depth=0 --json", cwd=project_root, timeout=30, on_stdout=lambda message: installed_packages_raw.append(message), ) lines = [json.loads(line) for line in installed_packages_raw if line.strip()] if not lines: return {} package_data = lines[-1] dependencies = package_data.get("dependencies", {}) return { dep_name: details.get("version") for dep_name, details in dependencies.items() }
def install_npm_dependencies(sandbox: Sandbox, dependencies: list[str], project_root: str = '~') -> list[str]: ''' Install npm dependencies in the sandbox.
Return errors if any. ''' install_errors = [] if not dependencies: return install_errors
installed_packages: dict[str, str | None] = get_installed_npm_packages( sandbox, project_root)
dependencies_to_install = [dependency for dependency in dependencies if not is_npm_package_installed( dependency, installed_packages)]
for dependency in dependencies_to_install: try: sandbox.commands.run( f"npm install {dependency} --prefer-offline --no-audit --no-fund --legacy-peer-deps", cwd=project_root, timeout=60 * 3, on_stdout=lambda message: print(message), on_stderr=lambda message: print(message), ) except Exception as e: install_errors.append(f"Error during installing npm package {dependency}:" + str(e)) continue
return install_errors
def run_background_command_with_timeout( sandbox: Sandbox, command: str, cwd: str = "~", timeout: int = 5,) -> str: """ Run a command in the background and wait for a short time to check for startup errors.
Args: sandbox: The sandbox instance command: The command to run cwd: The working directory for the command timeout: How long to wait for startup errors (in seconds)
Returns: str: Any error output collected during startup """ stderr = ""
cmd = sandbox.commands.run( command, timeout=60 * 3, # Overall timeout for the command cwd=cwd, background=True, )
def wait_for_command(result_queue): nonlocal stderr try: result = cmd.wait() if result.stderr: stderr += result.stderr result_queue.put(stderr) except ReadTimeout: result_queue.put(stderr) except CommandExitException as e: stderr += "".join(e.stderr) result_queue.put(stderr) except TimeoutException: return
result_queue = queue.Queue() wait_thread = threading.Thread( target=wait_for_command, args=(result_queue,)) wait_thread.daemon = True # Make thread daemon so it won't prevent program exit wait_thread.start()
try: return result_queue.get(timeout=timeout) except queue.Empty: return stderr
def get_sandbox_app_url( sandbox: Sandbox, app_type: Literal["react", "vue", "html", "pygame"]) -> str: ''' Get the URL for the app in the sandbox with container wrapper. ''' return f"https://{sandbox.get_host(port=SANDBOX_NGINX_PORT)}/container/?app={app_type}"1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283'''Chatbot state.'''
from sandbox.code_analyzer import SandboxEnvironmentfrom typing import TypedDict
class ChatbotSandboxState(TypedDict): ''' Chatbot sandbox state in gr.state. ''' enable_sandbox: bool ''' Whether the code sandbox is enabled. ''' sandbox_instruction: str | None ''' The sandbox instruction to display. '''
enabled_round: int ''' The chat round after which the sandbox is enabled. ''' sandbox_run_round: int ''' How many rounds the sandbox has been run inside the session. ''' edit_round: int ''' How many rounds the code has been edited. Starting from 0, incrementing each time the code is edited. Refreshed when running a generated code. '''
sandbox_environment: SandboxEnvironment | None ''' The sandbox environment to run the code. ''' auto_selected_sandbox_environment: SandboxEnvironment | None ''' The sandbox environment selected automatically. ''' code_to_execute: str | None ''' The code to execute in the sandbox. ''' code_language: str | None ''' The code language to execute in the sandbox. ''' code_dependencies: tuple[list[str], list[str]] ''' The code dependencies for the sandbox (python, npm). '''
sandbox_output: str | None ''' The sandbox output. ''' sandbox_error: str | None ''' The sandbox error. '''
sandbox_id: str | None ''' The remote e2b sandbox id. None if not run yet. ''' chat_session_id: str | None ''' The chat session id, unique per chat. The two battle models share the same chat session id. ''' conv_id: str | None ''' The conv id, unique per chat per model. '''
btn_list_length: int ''' Count of Gradio user interface buttons. '''123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221'''Module for logging the sandbox interactions and state.'''from concurrent.futures import ThreadPoolExecutorimport jsonimport osfrom typing import Any, List, Literal, Optional, TypedDictimport datetime
from chat_state import LOG_DIRfrom sandbox.sandbox_state import ChatbotSandboxState
from azure.storage.blob import BlobServiceClient
from sandbox.constants import AZURE_BLOB_STORAGE_CONNECTION_STRING, AZURE_BLOB_STORAGE_CONTAINER_NAME
class SandboxLog(TypedDict): ''' The schema of the sandbox log stored. ''' sandbox_state: ChatbotSandboxState user_interaction_records: Optional[List[Any]]
def upload_data_to_azure_storage( data: bytes, blob_name: str, write_mode: Literal['overwrite', 'append'], connection_string: str | None = AZURE_BLOB_STORAGE_CONNECTION_STRING, container_name: str = AZURE_BLOB_STORAGE_CONTAINER_NAME, ) -> None: ''' Upload data to Azure Blob Storage. ''' if not connection_string: raise ValueError("AZURE_STORAGE_CONNECTION_STRING is not set")
blob_service_client = BlobServiceClient.from_connection_string(connection_string) container_client = blob_service_client.get_container_client(container_name)
if write_mode == "overwrite": container_client.upload_blob( name=blob_name, data=data, overwrite=True ) elif write_mode == "append": blob_client = container_client.get_blob_client(blob=blob_name) if not blob_client.exists(): blob_client.upload_blob(data, blob_type="AppendBlob") else: blob_client.append_block(data) else: raise ValueError("Unsupported write_mode. Use 'w' for overwrite or 'a' for append.")
def get_sandbox_log_blob_name(filename: str) -> str: date_str = datetime.datetime.now().strftime('%Y_%m_%d') blob_name = f"{date_str}/sandbox_logs/{filename}" return blob_name
def get_conv_log_filepath( date: datetime.date, chat_mode: Literal['battle_anony', 'battle_named', 'direct'], chat_session_id: str,) -> str: ''' Get the filepath for the conversation log.
Expected directory structure: softwarearenlog/ └── YEAR_MONTH_DAY/ ├── conv_logs/ │ ├── battle_anony/ │ │ └── CHATSESSIONID.json │ ├── battle_named/ │ │ └── CHATSESSIONID.json │ └── direct/ │ └── CHATSESSIONID.json ''' date_str = date.strftime('%Y_%m_%d') filepath = os.path.join( date_str, 'conv_logs', chat_mode, f"{chat_session_id}.json" ) return filepath
def get_sandbox_log_filepath( date: datetime.date, chat_mode: Literal['battle_anony', 'battle_named', 'direct'], chat_session_id: str,) -> str: ''' Get the filepath for the conversation log.
Expected directory structure: softwarearenlog/ └── YEAR_MONTH_DAY/ ├── conv_logs/ └── sandbox_logs/ ├── battle/ │ └── sandbox-records-SESSIONID-A-B-EDITID.json ├── side-by-side/ │ └── sandbox-records-SESSIONID-A-B-EDITID.json └── direct/ └── sandbox-records-SESSIONID-A-B-EDITID.json ''' date_str = date.strftime('%Y_%m_%d') filepath = os.path.join( date_str, 'sandbox_logs', chat_mode, f"{chat_session_id}.json" ) return filepath
def get_conv_log_blob_name(filename: str) -> str: date_str = datetime.datetime.now().strftime('%Y_%m_%d') blob_name = f"{date_str}/conv_logs/{filename}" return blob_name
_executor = ThreadPoolExecutor(max_workers=20)def save_conv_log_to_azure_storage( filename: str, log_data: dict[str, Any], write_mode: Literal['overwrite', 'append'] = 'append', use_async: bool = True) -> None: try: if AZURE_BLOB_STORAGE_CONNECTION_STRING: blob_name = get_conv_log_blob_name(filename) log_json: str = json.dumps( obj=log_data, default=str, )
def _run_upload(): upload_data_to_azure_storage( str.encode(log_json + "\n"), blob_name, write_mode )
if use_async: _executor.submit(_run_upload) else: _run_upload() except Exception as e: print(f"Error uploading conv log to Azure Blob Storage: {e}")
def get_sandbox_log_filename(sandbox_state: ChatbotSandboxState) -> str: return ( '-'.join( [ "sandbox-logs", f"{sandbox_state['conv_id']}", # chat conv id f"{sandbox_state['enabled_round']}", # current chat round f"{sandbox_state['sandbox_run_round']}", # current sandbox round ] ) + ".json" )
def upsert_sandbox_log(filename: str, data: str) -> None: filepath = os.path.join( LOG_DIR, datetime.datetime.now().strftime('%Y_%m_%d'), # current date as 2025_02_02 'sandbox_logs', filename ) # create directory if not exists os.makedirs(os.path.dirname(filepath), exist_ok=True) with open(filepath, "w") as fout: fout.write(data)
def create_sandbox_log(sandbox_state: ChatbotSandboxState, user_interaction_records: list[Any] | None) -> SandboxLog: return { "sandbox_state": sandbox_state, "user_interaction_records": user_interaction_records, }
def log_sandbox_telemetry_gradio_fn( sandbox_state: ChatbotSandboxState, sandbox_ui_value: tuple[str, bool, list[Any]] | None) -> None: if sandbox_state is None: return sandbox_id = sandbox_state['sandbox_id'] user_interaction_records = sandbox_ui_value[2] if sandbox_ui_value else None if sandbox_id is None: return
log_json = create_sandbox_log(sandbox_state, user_interaction_records) log_data = json.dumps( log_json, indent=2, default=str, ensure_ascii=False ) filename = get_sandbox_log_filename(sandbox_state) upsert_sandbox_log(filename=filename, data=log_data)
# # Upload to Azure Blob Storage # if AZURE_BLOB_STORAGE_CONNECTION_STRING: # try: # blob_name = get_sandbox_log_blob_name(filename) # upload_data_to_azure_storage( # data=str.encode(log_data), # blob_name=blob_name, # write_mode='overwrite' # ) # except Exception as e: # print(f"Error uploading sandbox log to Azure Blob Storage: {e}").gitignoreAdded+208
README.md+2-2
api_config.yamlAdded+216
app.pyAdded+777
chat_state.pyAdded+259
completion.pyAdded+1304
conversation.pyAdded
requirements.txtAdded+21
sandbox/__init__.pyAdded
sandbox/code_analyzer.pyAdded+935
sandbox/code_runner.pyAdded+1612
sandbox/constants.pyAdded+148
sandbox/prompts.pyAdded+393
sandbox/sandbox_manager.pyAdded+254
sandbox/sandbox_state.pyAdded+83
sandbox/sandbox_telemetry.pyAdded+221