Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -10,15 +10,17 @@ from dotenv import load_dotenv
|
|
| 10 |
# Load environment variables
|
| 11 |
load_dotenv()
|
| 12 |
|
| 13 |
-
# Import browser-use components
|
| 14 |
try:
|
| 15 |
from langchain_openai import ChatOpenAI
|
|
|
|
|
|
|
| 16 |
from browser_use import Agent
|
| 17 |
except ImportError as e:
|
| 18 |
print(f"Import error: {e}")
|
| 19 |
-
print("Make sure to install browser-use and langchain-
|
| 20 |
|
| 21 |
-
app = Flask(__name__, static_folder='static')
|
| 22 |
CORS(app) # Enable CORS for all routes
|
| 23 |
|
| 24 |
# Global variables for managing async operations
|
|
@@ -43,8 +45,15 @@ def get_or_create_eventloop():
|
|
| 43 |
async def run_browser_task(task, model="gpt-4o-mini"):
|
| 44 |
"""Run a browser automation task using browser-use"""
|
| 45 |
try:
|
| 46 |
-
# Initialize the LLM
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
# Create the agent
|
| 50 |
agent = Agent(task=task, llm=llm)
|
|
@@ -64,12 +73,12 @@ async def run_browser_task(task, model="gpt-4o-mini"):
|
|
| 64 |
"task": task
|
| 65 |
}
|
| 66 |
|
| 67 |
-
@app.route('/')
|
| 68 |
def home():
|
| 69 |
"""Serve the HTML interface"""
|
| 70 |
-
return send_from_directory('static', 'index.html')
|
| 71 |
|
| 72 |
-
@app.route('/api')
|
| 73 |
def api_docs():
|
| 74 |
"""API documentation endpoint"""
|
| 75 |
return jsonify({
|
|
@@ -90,14 +99,14 @@ def api_docs():
|
|
| 90 |
"model": "gpt-4o-mini (optional, default)"
|
| 91 |
},
|
| 92 |
"example": {
|
| 93 |
-
"task": "Go to google.com and search for 'Python programming'",
|
| 94 |
"model": "gpt-4o-mini"
|
| 95 |
}
|
| 96 |
}
|
| 97 |
}
|
| 98 |
})
|
| 99 |
|
| 100 |
-
@app.route('/health')
|
| 101 |
def health():
|
| 102 |
"""Health check endpoint"""
|
| 103 |
return jsonify({
|
|
@@ -106,7 +115,7 @@ def health():
|
|
| 106 |
"version": "1.0.0"
|
| 107 |
})
|
| 108 |
|
| 109 |
-
@app.route('/status')
|
| 110 |
def status():
|
| 111 |
"""Get server status and configuration"""
|
| 112 |
return jsonify({
|
|
@@ -114,38 +123,41 @@ def status():
|
|
| 114 |
"environment": {
|
| 115 |
"python_version": sys.version,
|
| 116 |
"has_openai_key": bool(os.getenv("OPENAI_API_KEY")),
|
| 117 |
-
"has_anthropic_key": bool(os.getenv("ANTHROPIC_API_KEY"))
|
|
|
|
| 118 |
},
|
| 119 |
"supported_models": [
|
| 120 |
"gpt-4o",
|
| 121 |
"gpt-4o-mini",
|
| 122 |
"gpt-4-turbo",
|
| 123 |
"claude-3-sonnet-20240229",
|
| 124 |
-
"claude-3-haiku-20240307"
|
|
|
|
|
|
|
| 125 |
]
|
| 126 |
})
|
| 127 |
|
| 128 |
-
@app.route('/run-task', methods=['POST'])
|
| 129 |
def run_task():
|
| 130 |
"""Run a browser automation task"""
|
| 131 |
try:
|
| 132 |
# Get request data
|
| 133 |
data = request.get_json()
|
| 134 |
|
| 135 |
-
if not data or 'task' not in data:
|
| 136 |
return jsonify({
|
| 137 |
"success": False,
|
| 138 |
-
"error": "Missing 'task' in request body"
|
| 139 |
}), 400
|
| 140 |
|
| 141 |
-
task = data['task']
|
| 142 |
-
model = data.get('model', 'gpt-4o-mini')
|
| 143 |
|
| 144 |
# Validate API keys
|
| 145 |
-
if not os.getenv("OPENAI_API_KEY") and not os.getenv("ANTHROPIC_API_KEY"):
|
| 146 |
return jsonify({
|
| 147 |
"success": False,
|
| 148 |
-
"error": "No API keys configured. Please set OPENAI_API_KEY or
|
| 149 |
}), 500
|
| 150 |
|
| 151 |
# Get the event loop
|
|
@@ -173,12 +185,13 @@ def run_task():
|
|
| 173 |
"error": f"Server error: {str(e)}"
|
| 174 |
}), 500
|
| 175 |
|
| 176 |
-
if __name__ == '__main__':
|
| 177 |
print("Starting Browser-Use Server...")
|
| 178 |
print("Make sure you have set your API keys:")
|
| 179 |
print("- OPENAI_API_KEY for OpenAI models")
|
| 180 |
print("- ANTHROPIC_API_KEY for Anthropic models")
|
|
|
|
| 181 |
|
| 182 |
# Run the Flask app
|
| 183 |
-
app.run(host='0.0.0.0', port=7860, debug=False)
|
| 184 |
|
|
|
|
| 10 |
# Load environment variables
|
| 11 |
load_dotenv()
|
| 12 |
|
| 13 |
+
# Import browser-use components and LLM libraries
|
| 14 |
try:
|
| 15 |
from langchain_openai import ChatOpenAI
|
| 16 |
+
from langchain_anthropic import ChatAnthropic
|
| 17 |
+
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 18 |
from browser_use import Agent
|
| 19 |
except ImportError as e:
|
| 20 |
print(f"Import error: {e}")
|
| 21 |
+
print("Make sure to install browser-use, langchain-openai, langchain-anthropic, and langchain-google-genai")
|
| 22 |
|
| 23 |
+
app = Flask(__name__, static_folder=\'static\')
|
| 24 |
CORS(app) # Enable CORS for all routes
|
| 25 |
|
| 26 |
# Global variables for managing async operations
|
|
|
|
| 45 |
async def run_browser_task(task, model="gpt-4o-mini"):
|
| 46 |
"""Run a browser automation task using browser-use"""
|
| 47 |
try:
|
| 48 |
+
# Initialize the LLM based on the model name
|
| 49 |
+
if model.startswith(\'gpt\'):
|
| 50 |
+
llm = ChatOpenAI(model=model, temperature=0.1)
|
| 51 |
+
elif model.startswith(\'claude\'):
|
| 52 |
+
llm = ChatAnthropic(model=model, temperature=0.1)
|
| 53 |
+
elif model.startswith(\'gemini\'):
|
| 54 |
+
llm = ChatGoogleGenerativeAI(model=model, temperature=0.1)
|
| 55 |
+
else:
|
| 56 |
+
raise ValueError(f"Unsupported model: {model}")
|
| 57 |
|
| 58 |
# Create the agent
|
| 59 |
agent = Agent(task=task, llm=llm)
|
|
|
|
| 73 |
"task": task
|
| 74 |
}
|
| 75 |
|
| 76 |
+
@app.route(\'/\')
|
| 77 |
def home():
|
| 78 |
"""Serve the HTML interface"""
|
| 79 |
+
return send_from_directory(\'static\', \'index.html\')
|
| 80 |
|
| 81 |
+
@app.route(\'/api\')
|
| 82 |
def api_docs():
|
| 83 |
"""API documentation endpoint"""
|
| 84 |
return jsonify({
|
|
|
|
| 99 |
"model": "gpt-4o-mini (optional, default)"
|
| 100 |
},
|
| 101 |
"example": {
|
| 102 |
+
"task": "Go to google.com and search for \'Python programming\'",
|
| 103 |
"model": "gpt-4o-mini"
|
| 104 |
}
|
| 105 |
}
|
| 106 |
}
|
| 107 |
})
|
| 108 |
|
| 109 |
+
@app.route(\'/health\')
|
| 110 |
def health():
|
| 111 |
"""Health check endpoint"""
|
| 112 |
return jsonify({
|
|
|
|
| 115 |
"version": "1.0.0"
|
| 116 |
})
|
| 117 |
|
| 118 |
+
@app.route(\'/status\')
|
| 119 |
def status():
|
| 120 |
"""Get server status and configuration"""
|
| 121 |
return jsonify({
|
|
|
|
| 123 |
"environment": {
|
| 124 |
"python_version": sys.version,
|
| 125 |
"has_openai_key": bool(os.getenv("OPENAI_API_KEY")),
|
| 126 |
+
"has_anthropic_key": bool(os.getenv("ANTHROPIC_API_KEY")),
|
| 127 |
+
"has_google_api_key": bool(os.getenv("GOOGLE_API_KEY"))
|
| 128 |
},
|
| 129 |
"supported_models": [
|
| 130 |
"gpt-4o",
|
| 131 |
"gpt-4o-mini",
|
| 132 |
"gpt-4-turbo",
|
| 133 |
"claude-3-sonnet-20240229",
|
| 134 |
+
"claude-3-haiku-20240307",
|
| 135 |
+
"gemini-pro",
|
| 136 |
+
"gemini-1.5-pro-latest"
|
| 137 |
]
|
| 138 |
})
|
| 139 |
|
| 140 |
+
@app.route(\'/run-task\', methods=[\'POST\'])
|
| 141 |
def run_task():
|
| 142 |
"""Run a browser automation task"""
|
| 143 |
try:
|
| 144 |
# Get request data
|
| 145 |
data = request.get_json()
|
| 146 |
|
| 147 |
+
if not data or \'task\' not in data:
|
| 148 |
return jsonify({
|
| 149 |
"success": False,
|
| 150 |
+
"error": "Missing \'task\' in request body"
|
| 151 |
}), 400
|
| 152 |
|
| 153 |
+
task = data[\'task\']
|
| 154 |
+
model = data.get(\'model\', \'gpt-4o-mini\')
|
| 155 |
|
| 156 |
# Validate API keys
|
| 157 |
+
if not os.getenv("OPENAI_API_KEY") and not os.getenv("ANTHROPIC_API_KEY") and not os.getenv("GOOGLE_API_KEY"):
|
| 158 |
return jsonify({
|
| 159 |
"success": False,
|
| 160 |
+
"error": "No API keys configured. Please set OPENAI_API_KEY, ANTHROPIC_API_KEY, or GOOGLE_API_KEY environment variable."
|
| 161 |
}), 500
|
| 162 |
|
| 163 |
# Get the event loop
|
|
|
|
| 185 |
"error": f"Server error: {str(e)}"
|
| 186 |
}), 500
|
| 187 |
|
| 188 |
+
if __name__ == \'__main__\':
|
| 189 |
print("Starting Browser-Use Server...")
|
| 190 |
print("Make sure you have set your API keys:")
|
| 191 |
print("- OPENAI_API_KEY for OpenAI models")
|
| 192 |
print("- ANTHROPIC_API_KEY for Anthropic models")
|
| 193 |
+
print("- GOOGLE_API_KEY for Google Gemini models")
|
| 194 |
|
| 195 |
# Run the Flask app
|
| 196 |
+
app.run(host=\'0.0.0.0\', port=7860, debug=False)
|
| 197 |
|