Trisha Tomy commited on
Commit
229832a
·
1 Parent(s): d40d89b

logging in

Browse files
Files changed (1) hide show
  1. app.py +35 -7
app.py CHANGED
@@ -18,16 +18,21 @@ async def initialize_runner():
18
  global _runner
19
  if _runner is None:
20
  logger.info("Initializing Proxy-lite Runner...")
 
 
21
  hf_api_token = os.environ.get("HF_API_TOKEN")
22
  if not hf_api_token:
23
  logger.error("HF_API_TOKEN environment variable not set. Cannot initialize Runner.")
24
  raise ValueError("HF_API_TOKEN environment variable not set. Please set it as a Space secret.")
25
 
 
26
  config = RunnerConfig.from_dict({
27
  "environment": {
28
  "name": "webbrowser",
 
 
29
  "homepage": "https://dwd000006jia1mae.lightning.force.com/lightning/setup/AccountForecastSettings/home",
30
- "headless": True,
31
  "launch_args": ["--no-sandbox", "--disable-setuid-sandbox"]
32
  },
33
  "solver": {
@@ -69,20 +74,43 @@ def run_async_task(coro):
69
  @app.route('/run_proxy_task', methods=['POST'])
70
  def run_proxy_task_endpoint():
71
  data = request.json
72
- task = data.get('task')
73
- if not task:
 
 
74
  logger.warning("Received request without 'task' field. Returning 400.")
75
  return jsonify({"error": "No 'task' provided in request body"}), 400
76
 
77
- logger.info(f"Received task for proxy-lite: '{task}'")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78
  try:
79
  runner = run_async_task(initialize_runner())
80
- result = run_async_task(runner.run(task))
 
81
 
82
  logger.info(f"Proxy-lite task completed. Output: {result[:200]}...")
83
  return jsonify({"output": result})
84
  except Exception as e:
85
- logger.exception(f"Error processing task '{task}':")
86
  return jsonify({"error": f"An error occurred: {str(e)}. Check logs for details."}), 500
87
 
88
  @app.route('/')
@@ -95,4 +123,4 @@ if __name__ == '__main__':
95
  logger.error("HF_API_TOKEN environment variable is not set. Please set it for local testing.")
96
  exit(1)
97
  logger.info("Starting Flask development server on 0.0.0.0:7860...")
98
- app.run(host='0.0.0.0', port=7860, debug=True)
 
18
  global _runner
19
  if _runner is None:
20
  logger.info("Initializing Proxy-lite Runner...")
21
+
22
+ # Retrieve Hugging Face API token from environment variables
23
  hf_api_token = os.environ.get("HF_API_TOKEN")
24
  if not hf_api_token:
25
  logger.error("HF_API_TOKEN environment variable not set. Cannot initialize Runner.")
26
  raise ValueError("HF_API_TOKEN environment variable not set. Please set it as a Space secret.")
27
 
28
+ # Define RunnerConfig
29
  config = RunnerConfig.from_dict({
30
  "environment": {
31
  "name": "webbrowser",
32
+ # The homepage is already set to the Account Forecast Settings page.
33
+ # The agent will start here, but still needs to handle login if not already logged in.
34
  "homepage": "https://dwd000006jia1mae.lightning.force.com/lightning/setup/AccountForecastSettings/home",
35
+ "headless": True, # Keep headless for production environments
36
  "launch_args": ["--no-sandbox", "--disable-setuid-sandbox"]
37
  },
38
  "solver": {
 
74
  @app.route('/run_proxy_task', methods=['POST'])
75
  def run_proxy_task_endpoint():
76
  data = request.json
77
+ # The 'task' from the user request will now be the dynamic instruction
78
+ request_task_instruction = data.get('task')
79
+
80
+ if not request_task_instruction:
81
  logger.warning("Received request without 'task' field. Returning 400.")
82
  return jsonify({"error": "No 'task' provided in request body"}), 400
83
 
84
+ logger.info(f"Received user request task: '{request_task_instruction}'")
85
+
86
+ # Retrieve Salesforce credentials from environment variables (Hugging Face Space secrets)
87
+ salesforce_username = os.environ.get("SALESFORCE_USERNAME")
88
+ salesforce_password = os.environ.get("SALESFORCE_PASSWORD")
89
+
90
+ if not salesforce_username or not salesforce_password:
91
+ logger.error("Salesforce credentials (SALESFORCE_USERNAME, SALESFORCE_PASSWORD) environment variables not set.")
92
+ return jsonify({"error": "Salesforce credentials not configured. Please set SALESFORCE_USERNAME and SALESFORCE_PASSWORD as Space secrets."}), 500
93
+
94
+ # Construct the full task for the proxy-lite agent,
95
+ # combining login instructions with the dynamic task from the user.
96
+ # The agent is expected to handle the login flow if needed.
97
+ agent_task = (
98
+ f"Log in to Salesforce. The username is '{salesforce_username}' and the password is '{salesforce_password}'. "
99
+ f"Once logged in, {request_task_instruction}. "
100
+ f"Report the final status of the requested action."
101
+ )
102
+
103
+ logger.info(f"Executing agent task: '{agent_task[:200]}...'")
104
+
105
  try:
106
  runner = run_async_task(initialize_runner())
107
+ # Pass the constructed agent_task to the runner
108
+ result = run_async_task(runner.run(agent_task))
109
 
110
  logger.info(f"Proxy-lite task completed. Output: {result[:200]}...")
111
  return jsonify({"output": result})
112
  except Exception as e:
113
+ logger.exception(f"Error processing Salesforce task: {e}")
114
  return jsonify({"error": f"An error occurred: {str(e)}. Check logs for details."}), 500
115
 
116
  @app.route('/')
 
123
  logger.error("HF_API_TOKEN environment variable is not set. Please set it for local testing.")
124
  exit(1)
125
  logger.info("Starting Flask development server on 0.0.0.0:7860...")
126
+ app.run(host='0.0.0.0', port=7860, debug=True)