# Fix app.py to handle Hugging Face secrets properly app_py = '''"""Swarm CLI - Gradio Web Interface for Hugging Face Spaces""" import os import sys import json import gradio as gr # Handle Hugging Face secrets if os.path.exists("/run/secrets/"): # Hugging Face secrets are mounted here for secret_file in os.listdir("/run/secrets/"): with open(f"/run/secrets/{secret_file}", "r") as f: os.environ[secret_file] = f.read().strip() from config import Config from agents.orchestrator import AgentOrchestrator from utils import UI # Initialize Config.validate() orchestrator = AgentOrchestrator() CSS = """ .swarm-header { text-align: center; padding: 20px; } .swarm-title { font-size: 2em; font-weight: bold; color: #6366f1; } .swarm-subtitle { color: #6b7280; margin-top: 10px; } .tool-badge { display: inline-block; padding: 2px 8px; margin: 2px; border-radius: 12px; font-size: 0.8em; background: #e0e7ff; color: #4338ca; } .message-user { background: #f3f4f6; padding: 10px; border-radius: 8px; } .message-agent { background: #e0e7ff; padding: 10px; border-radius: 8px; } """ def process_message(message, history): """Process user message through swarm""" if not message.strip(): return "", history history = history or [] history.append([message, "⏳ Processing..."]) yield "", history try: result = orchestrator.run(message) response = result.get("response", "No response generated") meta = f"\n\n---\n*Tools: {', '.join(result.get('tools_used', []))} | Model: {result.get('model_used', 'unknown')}*" history[-1][1] = response + meta yield "", history except Exception as e: history[-1][1] = f"❌ Error: {str(e)}" yield "", history def get_tool_list(): """Return formatted tool list""" tools = list(orchestrator.tool_registry.keys()) categories = {} for t in tools: cat = t.split("_")[0] categories.setdefault(cat, []).append(t) html = "
" for cat, tools in sorted(categories.items()): html += f"

🔧 {cat.upper()}

" for t in sorted(tools): html += f"{t}" html += "

" html += "
" return html def get_stats(): """Get system stats""" from tools.system_tools import SystemTools st = SystemTools() info = st.system_monitor("all") return json.dumps(info, indent=2) # Build Gradio interface with gr.Blocks(css=CSS, title="Swarm CLI") as demo: gr.HTML("""
🐝 Swarm CLI
Multi-Agent AI with 100+ Tools
""") with gr.Tab("Chat"): chatbot = gr.Chatbot( height=500, bubble_full_width=False, avatar_images=("👤", "🐝") ) msg_input = gr.Textbox( placeholder="Ask me anything... (e.g., 'List files in sandbox', 'Search web for Python tutorials', 'Calculate 2^10')", show_label=False, lines=2 ) with gr.Row(): submit_btn = gr.Button("🚀 Send", variant="primary") clear_btn = gr.Button("🗑️ Clear") with gr.Tab("Tools"): tool_display = gr.HTML(get_tool_list()) refresh_tools = gr.Button("🔄 Refresh") with gr.Tab("System"): stats_output = gr.Code(language="json", label="System Status") refresh_stats = gr.Button("🔄 Refresh Stats") with gr.Tab("Settings"): gr.Markdown("### Configuration") gr.Code(value=open(".env.example").read() if os.path.exists(".env.example") else "No .env.example found", language="bash", label="Environment Variables") gr.Markdown("Place your API keys in a `.env` file or set them as environment variables.") # Event handlers submit_btn.click( process_message, inputs=[msg_input, chatbot], outputs=[msg_input, chatbot] ) msg_input.submit( process_message, inputs=[msg_input, chatbot], outputs=[msg_input, chatbot] ) clear_btn.click(lambda: (None, []), outputs=[msg_input, chatbot]) refresh_tools.click(get_tool_list, outputs=tool_display) refresh_stats.click(get_stats, outputs=stats_output) # Launch if __name__ == "__main__": demo.launch(server_name="0.0.0.0", server_port=7860) ''' with open(f"{base_dir}/app.py", "w") as f: f.write(app_py) print("app.py updated with HF secrets support")