Spaces:
Sleeping
Sleeping
File size: 9,625 Bytes
26c6eb4 17f0653 f3fa710 17f0653 26c6eb4 17f0653 26c6eb4 17f0653 26c6eb4 17f0653 26c6eb4 17f0653 26c6eb4 17f0653 26c6eb4 17f0653 26c6eb4 17f0653 26c6eb4 17f0653 26c6eb4 17f0653 26c6eb4 f3fa710 26c6eb4 f3fa710 26c6eb4 17f0653 26c6eb4 17f0653 26c6eb4 f3fa710 26c6eb4 17f0653 26c6eb4 f3fa710 26c6eb4 f3fa710 17f0653 26c6eb4 17f0653 26c6eb4 17f0653 26c6eb4 17f0653 26c6eb4 17f0653 26c6eb4 17f0653 26c6eb4 17f0653 26c6eb4 17f0653 26c6eb4 17f0653 26c6eb4 17f0653 26c6eb4 17f0653 26c6eb4 17f0653 26c6eb4 17f0653 26c6eb4 17f0653 f3fa710 17f0653 f3fa710 17f0653 f3fa710 17f0653 f3fa710 17f0653 f3fa710 17f0653 f3fa710 17f0653 f3fa710 17f0653 f3fa710 17f0653 f3fa710 17f0653 26c6eb4 f3fa710 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 |
import gradio as gr
from openai import OpenAI
import os
import json
import threading
import time
import atexit
from novita_sandbox.code_interpreter import Sandbox
# -------------------------
# Global State
# -------------------------
client = OpenAI(
base_url="https://api.novita.ai/openai",
api_key=os.environ["NOVITA_API_KEY"],
)
model = "meta-llama/llama-3.3-70b-instruct"
sandbox = None
sandbox_timer = None
# -------------------------
# Sandbox Management
# -------------------------
def create_sandbox():
global sandbox
if sandbox is None:
sandbox = Sandbox.create(timeout=1200)
print("[DEBUG] Sandbox created.")
return "π’ Sandbox ON"
return "Sandbox already running."
def kill_sandbox():
global sandbox
if sandbox is not None:
try:
sandbox.kill()
except Exception:
pass
sandbox = None
print("[DEBUG] Sandbox killed.")
return "π΄ Sandbox OFF"
return "Sandbox already off."
def sandbox_auto_off():
print("[DEBUG] Auto-off countdown started (1200 sec).")
time.sleep(1200)
print("[DEBUG] Auto-off triggered.")
kill_sandbox()
# -------------------------
# Sandbox Usage Guard
# -------------------------
def require_sandbox():
if sandbox is None:
return "β Sandbox is OFF. Turn it ON to use this feature."
return None
# -------------------------
# Tool Functions
# -------------------------
def read_file(path: str):
print(f"[DEBUG] read_file called with path: {path}")
err = require_sandbox()
if err:
return err
try:
content = sandbox.files.read(path)
print(f"[DEBUG] read_file result: {content}")
return content
except Exception as e:
print(f"[DEBUG] read_file error: {e}")
return f"Error reading file: {e}"
def write_file(path: str, data: str):
print(f"[DEBUG] write_file called with path: {path}")
err = require_sandbox()
if err:
return err
try:
sandbox.files.write(path, data)
msg = f"File created successfully at {path}"
print(f"[DEBUG] {msg}")
return msg
except Exception as e:
print(f"[DEBUG] write_file error: {e}")
return f"Error writing file: {e}"
def write_files(files: list):
print(f"[DEBUG] write_files called with {len(files)} files")
err = require_sandbox()
if err:
return err
try:
sandbox.files.write_files(files)
msg = f"{len(files)} file(s) created successfully"
print(f"[DEBUG] {msg}")
return msg
except Exception as e:
print(f"[DEBUG] write_files error: {e}")
return f"Error writing multiple files: {e}"
def run_commands(command: str):
print(f"[DEBUG] run_commands called with command: {command}")
err = require_sandbox()
if err:
return err
try:
result = sandbox.commands.run(command)
print(f"[DEBUG] run_commands result: {result.stdout}")
return result.stdout
except Exception as e:
print(f"[DEBUG] run_commands error: {e}")
return f"Error running command: {e}"
# -------------------------
# Register tools
# -------------------------
tools = [
{
"type": "function",
"function": {
"name": "read_file",
"description": "Read contents of a file inside the sandbox",
"parameters": {
"type": "object",
"properties": {"path": {"type": "string"}},
"required": ["path"],
},
},
},
{
"type": "function",
"function": {
"name": "write_file",
"description": "Write a single file inside the sandbox",
"parameters": {
"type": "object",
"properties": {
"path": {"type": "string"},
"data": {"type": "string"},
},
"required": ["path", "data"],
},
},
},
{
"type": "function",
"function": {
"name": "write_files",
"description": "Write multiple files inside the sandbox",
"parameters": {
"type": "object",
"properties": {
"files": {
"type": "array",
"items": {
"type": "object",
"properties": {
"path": {"type": "string"},
"data": {"type": "string"},
},
"required": ["path", "data"],
},
}
},
"required": ["files"],
},
},
},
{
"type": "function",
"function": {
"name": "run_commands",
"description": "Run a shell command inside the sandbox",
"parameters": {
"type": "object",
"properties": {"command": {"type": "string"}},
"required": ["command"],
},
},
},
]
# -------------------------
# Chat + Tool Call Debug
# -------------------------
messages = []
def set_model(selected_model):
global model
model = selected_model
return f"β
Model switched to **{model}**"
def chat_fn(user_message, history):
global messages, model
messages.append({"role": "user", "content": user_message})
response = client.chat.completions.create(
model=model,
messages=messages,
tools=tools,
)
assistant_msg = response.choices[0].message
messages.append(assistant_msg)
# DEBUG tool call logging
if assistant_msg.tool_calls:
print(f"[DEBUG] Assistant requested {len(assistant_msg.tool_calls)} tool call(s).")
for tc in assistant_msg.tool_calls:
print(f"[DEBUG] Tool call detected: {tc.function.name} with args {tc.function.arguments}")
fn_name = tc.function.name
fn_args = json.loads(tc.function.arguments)
if fn_name == "read_file":
result = read_file(**fn_args)
elif fn_name == "write_file":
result = write_file(**fn_args)
elif fn_name == "write_files":
result = write_files(**fn_args)
elif fn_name == "run_commands":
result = run_commands(**fn_args)
else:
result = f"Unknown tool {fn_name}"
print(f"[DEBUG] Tool call result: {result}")
messages.append({
"tool_call_id": tc.id,
"role": "tool",
"content": str(result),
})
followup = client.chat.completions.create(
model=model,
messages=messages,
)
final_msg = followup.choices[0].message
messages.append(final_msg)
return final_msg.content
return assistant_msg.content
# -------------------------
# Command Interface
# -------------------------
def execute_command(command):
if not command.strip():
return "β οΈ Please enter a command."
output = run_commands(command)
return f"```bash\n{output}\n```"
# -------------------------
# UI
# -------------------------
with gr.Blocks(title="Novita Sandbox App") as demo:
gr.Markdown("## π§ Novita Sandbox Agent")
gr.Markdown("Interact with a Novita sandbox-enabled LLM with code execution capability.")
with gr.Row(equal_height=True):
# Chat
with gr.Column(scale=2):
gr.Markdown("### π¬ Chat Interface")
gr.ChatInterface(chat_fn)
# Controls
with gr.Column(scale=1):
gr.Markdown("### βοΈ Controls")
# Sandbox Switch
sandbox_switch = gr.Checkbox(label="Sandbox On/Off", value=False)
sandbox_status = gr.Markdown("π΄ Sandbox OFF")
def toggle_sandbox(is_on):
global sandbox_timer
if is_on:
msg = create_sandbox()
sandbox_timer = threading.Thread(target=sandbox_auto_off, daemon=True)
sandbox_timer.start()
return "π’ Sandbox ON"
else:
msg = kill_sandbox()
return "π΄ Sandbox OFF"
sandbox_switch.change(toggle_sandbox, inputs=sandbox_switch, outputs=sandbox_status)
# Model Selector
model_selector = gr.Dropdown(
label="Select Model",
choices=[
"meta-llama/llama-3.3-70b-instruct",
"deepseek/deepseek-v3.2-exp",
"qwen/qwen3-coder-30b-a3b-instruct",
"openai/gpt-oss-120b",
"moonshotai/kimi-k2-instruct",
],
value=model,
)
model_status = gr.Markdown(f"Current model: **{model}**")
model_selector.change(set_model, inputs=model_selector, outputs=model_status)
# Command Runner
command_input = gr.Textbox(label="Command", placeholder="e.g., ls")
run_btn = gr.Button("Run", variant="primary")
command_output = gr.Markdown("Command output here...")
run_btn.click(execute_command, inputs=command_input, outputs=command_output)
# Cleanup
atexit.register(lambda: (kill_sandbox(), print("[DEBUG] Sandbox terminated.")))
if __name__ == "__main__":
demo.launch()
|