Download app.py from TAIRED/Ai-Agent: direct link, hf CLI and curl.
- Browser
- Download file 2.18 kB
-
https://huggingface.co/spaces/TAIRED/Ai-Agent/resolve/main/app.py
- Command line
-
hf download hf://spaces/TAIRED/Ai-Agent/app.py
-
curl -L -o app.py https://huggingface.co/spaces/TAIRED/Ai-Agent/resolve/main/app.py
2.18 kB
| import spaces | |
| import subprocess | |
| import os | |
| import gradio as gr | |
| from smolagents import CodeAgent, InferenceClientModel | |
| from smolagents.tools import Tool | |
| class CloudTerminalTool(Tool): | |
| name = "terminal" | |
| description = "Executes bash shell commands directly in the Linux container environment." | |
| inputs = {"command": {"type": "string", "description": "The exact shell command to execute."}} | |
| output_type = "string" | |
| def forward(self, command: str) -> str: | |
| try: | |
| res = subprocess.run( | |
| command, | |
| shell=True, | |
| capture_output=True, | |
| text=True, | |
| timeout=120 | |
| ) | |
| out = res.stdout if res.stdout else "" | |
| err = f"\n[stderr]\n{res.stderr}" if res.stderr else "" | |
| return (out + err).strip() if (out or err) else "Command executed successfully (no output)." | |
| except subprocess.TimeoutExpired: | |
| return "Error: Command timed out after 120 seconds." | |
| except Exception as e: | |
| return f"Execution error: {str(e)}" | |
| # Using a standard free inference model client configuration | |
| model = InferenceClientModel( | |
| model_id="Qwen/Qwen2.5-Coder-32B-Instruct", | |
| token=os.getenv("HF_TOKEN") | |
| ) | |
| agent = CodeAgent(tools=[CloudTerminalTool()], model=model) | |
| def run_agent_task(prompt): | |
| if not prompt.strip(): | |
| return "Please enter a task or command." | |
| try: | |
| result = agent.run(prompt) | |
| return str(result) | |
| except Exception as e: | |
| return f"Agent Runtime Error:\n{str(e)}" | |
| with gr.Blocks(title="Cloud Agent Terminal") as demo: | |
| gr.Markdown("## Free Cloud AI Agent with Terminal Access") | |
| with gr.Column(): | |
| task_input = gr.Textbox( | |
| label="Agent Prompt / Task", | |
| placeholder="e.g. Run 'uname -a' or list files", | |
| lines=2 | |
| ) | |
| run_btn = gr.Button("Execute Task", variant="primary") | |
| output_box = gr.Textbox(label="Terminal Output", lines=14, interactive=False) | |
| run_btn.click( | |
| fn=run_agent_task, | |
| inputs=task_input, | |
| outputs=output_box | |
| ) | |
| demo.launch() | |