Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -28,11 +28,12 @@ if not os.path.exists(global_context_path):
|
|
| 28 |
with open(global_context_path, "w") as f:
|
| 29 |
f.write("# Global Context & Browser Workspace\nShared knowledge base accessible across all connected IP environments under /px.")
|
| 30 |
|
| 31 |
-
#
|
| 32 |
-
required_packages = ["gradio", "requests", "ollama", "beautifulsoup4"]
|
| 33 |
for package in required_packages:
|
| 34 |
try:
|
| 35 |
-
|
|
|
|
| 36 |
except ImportError:
|
| 37 |
print(f"Installing missing dependency: {package}...")
|
| 38 |
subprocess.check_call([sys.executable, "-m", "pip", "install", "-q", package])
|
|
@@ -144,12 +145,11 @@ def execute_tool(name, args, ip_folder, yolo_mode):
|
|
| 144 |
try:
|
| 145 |
if name == "bash":
|
| 146 |
cmd = args.get("command", "")
|
| 147 |
-
|
| 148 |
-
if not yolo_mode and any(danger in cmd for dangereux in ["rm -rf /", "mkfs", "dd if="]):
|
| 149 |
return "Error: Command blocked by YOLO Security Mode (Sandbox active)."
|
| 150 |
res = subprocess.run(cmd, shell=True, capture_output=True, text=True, cwd=ip_folder, timeout=30)
|
| 151 |
output = res.stdout if res.returncode == 0 else f"STDOUT:\n{res.stdout}\nSTDERR:\n{res.stderr}"
|
| 152 |
-
return output[:6000]
|
| 153 |
|
| 154 |
elif name == "write_file":
|
| 155 |
filepath = args.get("filepath")
|
|
@@ -168,7 +168,7 @@ def execute_tool(name, args, ip_folder, yolo_mode):
|
|
| 168 |
if not os.path.exists(safe_path):
|
| 169 |
return f"Error: File '{filepath}' not found."
|
| 170 |
with open(safe_path, "r", encoding="utf-8", errors="ignore") as f:
|
| 171 |
-
return f.read()[:15000]
|
| 172 |
|
| 173 |
elif name == "web_search":
|
| 174 |
query = args.get("query")
|
|
@@ -198,14 +198,12 @@ def predict(message, history, system_prompt, temperature, yolo_mode, request: gr
|
|
| 198 |
elif request.client:
|
| 199 |
client_ip = request.client.host
|
| 200 |
|
| 201 |
-
# Isolate session into /px/ips/<user_ip>/
|
| 202 |
safe_ip = client_ip.replace(":", "_").replace(".", "_")
|
| 203 |
ip_folder = os.path.join(IPS_DIR, safe_ip)
|
| 204 |
os.makedirs(ip_folder, exist_ok=True)
|
| 205 |
|
| 206 |
security_mode = "DISABLED (UNRESTRICTED / GOD-MODE)" if yolo_mode else "ENABLED (SECURE / SANDBOXED)"
|
| 207 |
|
| 208 |
-
# Load global shared context
|
| 209 |
global_text = ""
|
| 210 |
if os.path.exists(global_context_path):
|
| 211 |
with open(global_context_path, "r") as f:
|
|
@@ -239,7 +237,6 @@ def predict(message, history, system_prompt, temperature, yolo_mode, request: gr
|
|
| 239 |
with open(history_log_path, "a") as hf:
|
| 240 |
hf.write(f"[{timestamp}] User (YOLO: {yolo_mode} | Sec: {security_mode}): {message}\n")
|
| 241 |
|
| 242 |
-
# Autonomous Agent Tool Execution Loop (Multi-step reasoning)
|
| 243 |
max_turns = 5
|
| 244 |
turn = 0
|
| 245 |
accumulated_output = ""
|
|
@@ -262,9 +259,8 @@ def predict(message, history, system_prompt, temperature, yolo_mode, request: gr
|
|
| 262 |
accumulated_output += content
|
| 263 |
yield accumulated_output
|
| 264 |
|
| 265 |
-
# If model calls tools, execute them and feed back into context
|
| 266 |
if tool_calls:
|
| 267 |
-
messages.append(message_resp)
|
| 268 |
for tc in tool_calls:
|
| 269 |
func = tc.get("function", {})
|
| 270 |
func_name = func.get("name")
|
|
@@ -274,10 +270,8 @@ def predict(message, history, system_prompt, temperature, yolo_mode, request: gr
|
|
| 274 |
accumulated_output += status_msg
|
| 275 |
yield accumulated_output
|
| 276 |
|
| 277 |
-
# Run tool
|
| 278 |
tool_result = execute_tool(func_name, func_args, ip_folder, yolo_mode)
|
| 279 |
|
| 280 |
-
# Append tool result message
|
| 281 |
messages.append({
|
| 282 |
'role': 'tool',
|
| 283 |
'content': tool_result
|
|
@@ -286,7 +280,6 @@ def predict(message, history, system_prompt, temperature, yolo_mode, request: gr
|
|
| 286 |
accumulated_output += f"```output\n{tool_result}\n```\n"
|
| 287 |
yield accumulated_output
|
| 288 |
else:
|
| 289 |
-
# No more tool calls, agent finished task
|
| 290 |
break
|
| 291 |
except Exception as e:
|
| 292 |
yield accumulated_output + f"\n\n⚠️ **Agent Execution Error:** {str(e)}"
|
|
@@ -341,8 +334,7 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue="cyan", secondary_hue="blue"), c
|
|
| 341 |
gr.ChatInterface(
|
| 342 |
fn=predict,
|
| 343 |
additional_inputs=[system_prompt_input, temperature_slider, yolo_checkbox],
|
| 344 |
-
textbox=gr.Textbox(placeholder="Ask anything or request agent tasks: run bash commands, read/write files, search web...", container=False, scale=7)
|
| 345 |
-
theme="soft"
|
| 346 |
)
|
| 347 |
|
| 348 |
gr.Markdown(
|
|
|
|
| 28 |
with open(global_context_path, "w") as f:
|
| 29 |
f.write("# Global Context & Browser Workspace\nShared knowledge base accessible across all connected IP environments under /px.")
|
| 30 |
|
| 31 |
+
# Pin stable Gradio 4.x to prevent signature mismatch errors
|
| 32 |
+
required_packages = ["gradio==4.44.1", "requests", "ollama", "beautifulsoup4"]
|
| 33 |
for package in required_packages:
|
| 34 |
try:
|
| 35 |
+
pkg_name = package.split("==")[0]
|
| 36 |
+
__import__(pkg_name)
|
| 37 |
except ImportError:
|
| 38 |
print(f"Installing missing dependency: {package}...")
|
| 39 |
subprocess.check_call([sys.executable, "-m", "pip", "install", "-q", package])
|
|
|
|
| 145 |
try:
|
| 146 |
if name == "bash":
|
| 147 |
cmd = args.get("command", "")
|
| 148 |
+
if not yolo_mode and any(danger in cmd for danger in ["rm -rf /", "mkfs", "dd if="]):
|
|
|
|
| 149 |
return "Error: Command blocked by YOLO Security Mode (Sandbox active)."
|
| 150 |
res = subprocess.run(cmd, shell=True, capture_output=True, text=True, cwd=ip_folder, timeout=30)
|
| 151 |
output = res.stdout if res.returncode == 0 else f"STDOUT:\n{res.stdout}\nSTDERR:\n{res.stderr}"
|
| 152 |
+
return output[:6000]
|
| 153 |
|
| 154 |
elif name == "write_file":
|
| 155 |
filepath = args.get("filepath")
|
|
|
|
| 168 |
if not os.path.exists(safe_path):
|
| 169 |
return f"Error: File '{filepath}' not found."
|
| 170 |
with open(safe_path, "r", encoding="utf-8", errors="ignore") as f:
|
| 171 |
+
return f.read()[:15000]
|
| 172 |
|
| 173 |
elif name == "web_search":
|
| 174 |
query = args.get("query")
|
|
|
|
| 198 |
elif request.client:
|
| 199 |
client_ip = request.client.host
|
| 200 |
|
|
|
|
| 201 |
safe_ip = client_ip.replace(":", "_").replace(".", "_")
|
| 202 |
ip_folder = os.path.join(IPS_DIR, safe_ip)
|
| 203 |
os.makedirs(ip_folder, exist_ok=True)
|
| 204 |
|
| 205 |
security_mode = "DISABLED (UNRESTRICTED / GOD-MODE)" if yolo_mode else "ENABLED (SECURE / SANDBOXED)"
|
| 206 |
|
|
|
|
| 207 |
global_text = ""
|
| 208 |
if os.path.exists(global_context_path):
|
| 209 |
with open(global_context_path, "r") as f:
|
|
|
|
| 237 |
with open(history_log_path, "a") as hf:
|
| 238 |
hf.write(f"[{timestamp}] User (YOLO: {yolo_mode} | Sec: {security_mode}): {message}\n")
|
| 239 |
|
|
|
|
| 240 |
max_turns = 5
|
| 241 |
turn = 0
|
| 242 |
accumulated_output = ""
|
|
|
|
| 259 |
accumulated_output += content
|
| 260 |
yield accumulated_output
|
| 261 |
|
|
|
|
| 262 |
if tool_calls:
|
| 263 |
+
messages.append(message_resp)
|
| 264 |
for tc in tool_calls:
|
| 265 |
func = tc.get("function", {})
|
| 266 |
func_name = func.get("name")
|
|
|
|
| 270 |
accumulated_output += status_msg
|
| 271 |
yield accumulated_output
|
| 272 |
|
|
|
|
| 273 |
tool_result = execute_tool(func_name, func_args, ip_folder, yolo_mode)
|
| 274 |
|
|
|
|
| 275 |
messages.append({
|
| 276 |
'role': 'tool',
|
| 277 |
'content': tool_result
|
|
|
|
| 280 |
accumulated_output += f"```output\n{tool_result}\n```\n"
|
| 281 |
yield accumulated_output
|
| 282 |
else:
|
|
|
|
| 283 |
break
|
| 284 |
except Exception as e:
|
| 285 |
yield accumulated_output + f"\n\n⚠️ **Agent Execution Error:** {str(e)}"
|
|
|
|
| 334 |
gr.ChatInterface(
|
| 335 |
fn=predict,
|
| 336 |
additional_inputs=[system_prompt_input, temperature_slider, yolo_checkbox],
|
| 337 |
+
textbox=gr.Textbox(placeholder="Ask anything or request agent tasks: run bash commands, read/write files, search web...", container=False, scale=7)
|
|
|
|
| 338 |
)
|
| 339 |
|
| 340 |
gr.Markdown(
|