Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from dotenv import load_dotenv
|
| 3 |
+
from e2b import Sandbox
|
| 4 |
+
from huggingface_hub import InferenceClient # Import the InferenceClient
|
| 5 |
+
import gradio as gr
|
| 6 |
+
|
| 7 |
+
# Load environment variables from .env file
|
| 8 |
+
load_dotenv()
|
| 9 |
+
|
| 10 |
+
# --- E2B Setup ---
|
| 11 |
+
e2b_api_key = os.getenv("E2B_API_KEY")
|
| 12 |
+
if not e2b_api_key:
|
| 13 |
+
print("WARNING: E2B_API_KEY not found. Cannot run locally without it.")
|
| 14 |
+
|
| 15 |
+
# --- LLM (Hugging Face Inference API) Setup ---
|
| 16 |
+
hf_token = os.getenv("HF_TOKEN") # Get Hugging Face token
|
| 17 |
+
if not hf_token:
|
| 18 |
+
print("WARNING: HF_TOKEN not found. Cannot run locally without it. Inference might be limited.")
|
| 19 |
+
|
| 20 |
+
# Choose the model you want to use from Hugging Face
|
| 21 |
+
# Make sure it's a text generation or chat model.
|
| 22 |
+
# Examples: "mistralai/Mistral-7B-Instruct-v0.2", "meta-llama/Llama-2-7b-chat-hf", "Qwen/Qwen1.5-7B-Chat"
|
| 23 |
+
HF_MODEL_ID = "mistralai/Mistral-7B-Instruct-v0.2" # <-- **Choose your model here**
|
| 24 |
+
|
| 25 |
+
# Initialize the Inference Client
|
| 26 |
+
try:
|
| 27 |
+
llm_client = InferenceClient(model=HF_MODEL_ID, token=hf_token)
|
| 28 |
+
llm_client_available = True
|
| 29 |
+
except Exception as e:
|
| 30 |
+
print(f"Could not initialize Hugging Face Inference Client: {e}")
|
| 31 |
+
llm_client_available = False
|
| 32 |
+
llm_client = None # Ensure client is None if initialization fails
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
def run_agent_task(user_input: str):
|
| 36 |
+
"""
|
| 37 |
+
Processes user input, interacts with Hugging Face Inference API and E2B, and returns results.
|
| 38 |
+
"""
|
| 39 |
+
output = ""
|
| 40 |
+
|
| 41 |
+
if not e2b_api_key:
|
| 42 |
+
return "Error: E2B API key not configured."
|
| 43 |
+
if not llm_client_available or llm_client is None:
|
| 44 |
+
return "Error: Hugging Face Inference Client not initialized. Check HF_TOKEN or model ID."
|
| 45 |
+
|
| 46 |
+
# Start an E2B sandbox session
|
| 47 |
+
try:
|
| 48 |
+
with Sandbox(api_key=e2b_api_key, template="base") as sandbox:
|
| 49 |
+
output += "E2B Sandbox started successfully.\n"
|
| 50 |
+
|
| 51 |
+
# 1. Formulate a prompt for the LLM
|
| 52 |
+
# Use the same prompt structure, adapting slightly if needed for chat models
|
| 53 |
+
prompt_content = f"""
|
| 54 |
+
You are a computer agent connected to a sandboxed environment.
|
| 55 |
+
The user wants you to perform the following task: {user_input}
|
| 56 |
+
|
| 57 |
+
Based on the task, decide what command(s) to run in the bash terminal within the sandbox.
|
| 58 |
+
Output only the bash command(s), nothing else. If no command is needed, output 'NO_COMMAND'.
|
| 59 |
+
For example:
|
| 60 |
+
User: List files in the current directory
|
| 61 |
+
Agent: ls -l
|
| 62 |
+
|
| 63 |
+
User: Calculate 2+2
|
| 64 |
+
Agent: echo $((2+2))
|
| 65 |
+
|
| 66 |
+
User: Greet me
|
| 67 |
+
Agent: NO_COMMAND
|
| 68 |
+
|
| 69 |
+
Now, based on the user task: {user_input}
|
| 70 |
+
Output the bash command(s) or 'NO_COMMAND':
|
| 71 |
+
"""
|
| 72 |
+
|
| 73 |
+
# 2. Call the Hugging Face Inference API
|
| 74 |
+
try:
|
| 75 |
+
# Use the .chat method which is suitable for instruction-following models
|
| 76 |
+
# It takes messages in the OpenAI format
|
| 77 |
+
response = llm_client.chat(
|
| 78 |
+
messages=[
|
| 79 |
+
{"role": "system", "content": "You are a helpful assistant that outputs bash commands or NO_COMMAND."},
|
| 80 |
+
{"role": "user", "content": prompt_content}
|
| 81 |
+
],
|
| 82 |
+
max_tokens=100, # Adjust as needed
|
| 83 |
+
temperature=0.1, # Lower temperature often helps with predictable output like commands
|
| 84 |
+
# Add other parameters as needed (e.g., top_p)
|
| 85 |
+
)
|
| 86 |
+
|
| 87 |
+
# Extract the content from the response
|
| 88 |
+
# The response structure depends on the method and model, .chat returns a ChatCompletion object
|
| 89 |
+
command_to_run = response.choices[0].message.content.strip()
|
| 90 |
+
|
| 91 |
+
output += f"LLM ({HF_MODEL_ID}) decided to run: `{command_to_run}`\n"
|
| 92 |
+
|
| 93 |
+
except Exception as e:
|
| 94 |
+
output += f"An exception occurred calling Hugging Face Inference API: {e}\n"
|
| 95 |
+
command_to_run = "NO_COMMAND" # Prevent execution on LLM error
|
| 96 |
+
|
| 97 |
+
# 3. Execute the command in the E2B sandbox (if not NO_COMMAND)
|
| 98 |
+
# This part remains the same as it uses the E2B SDK
|
| 99 |
+
if command_to_run and command_to_run != "NO_COMMAND":
|
| 100 |
+
try:
|
| 101 |
+
proc = sandbox.process.start(command_to_run)
|
| 102 |
+
process_output = proc.wait()
|
| 103 |
+
|
| 104 |
+
if process_output.stdout:
|
| 105 |
+
output += "--- Command Output (stdout) ---\n"
|
| 106 |
+
output += process_output.stdout + "\n"
|
| 107 |
+
if process_output.stderr:
|
| 108 |
+
output += "--- Command Output (stderr) ---\n"
|
| 109 |
+
output += process_output.stderr + "\n"
|
| 110 |
+
|
| 111 |
+
output += f"Command exited with code: {process_output.exit_code}\n"
|
| 112 |
+
|
| 113 |
+
except Exception as e:
|
| 114 |
+
output += f"Error executing command in sandbox: {e}\n"
|
| 115 |
+
elif command_to_run == "NO_COMMAND":
|
| 116 |
+
output += "LLM decided no command was necessary.\n"
|
| 117 |
+
else:
|
| 118 |
+
output += "LLM returned an empty command.\n"
|
| 119 |
+
|
| 120 |
+
output += "E2B Sandbox session ended.\n"
|
| 121 |
+
|
| 122 |
+
except Exception as e:
|
| 123 |
+
output += f"An error occurred with the E2B sandbox: {e}\n"
|
| 124 |
+
output += "Please check your E2B API key and try again.\n"
|
| 125 |
+
|
| 126 |
+
return output
|
| 127 |
+
|
| 128 |
+
|
| 129 |
+
# Define the Gradio interface - This part is unchanged
|
| 130 |
+
interface = gr.Interface(
|
| 131 |
+
fn=run_agent_task,
|
| 132 |
+
inputs=gr.Textbox(lines=2, placeholder="Enter your task for the agent here..."),
|
| 133 |
+
outputs=gr.Textbox(lines=10, label="Agent Output", interactive=False),
|
| 134 |
+
title=f"E2B Computer Agent Demo (using {HF_MODEL_ID})", # Update title
|
| 135 |
+
description="Enter a task for the agent to perform in a sandboxed environment using E2B and a Hugging Face model via Inference API.",
|
| 136 |
+
)
|
| 137 |
+
|
| 138 |
+
# This is the line Hugging Face Spaces will look for
|
| 139 |
+
if __name__ == "__main__":
|
| 140 |
+
interface.launch()
|