Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -12,20 +12,25 @@ from datetime import datetime
|
|
12 |
from dotenv import load_dotenv
|
13 |
from huggingface_hub import login
|
14 |
import tempfile
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
|
16 |
# Load environment variables
|
17 |
load_dotenv()
|
18 |
hf_token = os.getenv("HF_TOKEN")
|
19 |
gemini_api_key = os.getenv("GEMINI_API_KEY")
|
20 |
|
21 |
-
if hf_token:
|
22 |
-
login(hf_token, add_to_git_credential=False)
|
23 |
-
else:
|
24 |
raise ValueError("HF_TOKEN environment variable not set.")
|
25 |
-
|
26 |
if not gemini_api_key:
|
27 |
raise ValueError("GEMINI_API_KEY environment variable not set.")
|
28 |
|
|
|
|
|
29 |
# Define tools
|
30 |
@tool
|
31 |
def search_item_ctrl_f(text: str, nth_result: int = 1) -> str:
|
@@ -57,17 +62,21 @@ def close_popups() -> str:
|
|
57 |
"""
|
58 |
webdriver.ActionChains(driver).send_keys(Keys.ESCAPE).perform()
|
59 |
|
60 |
-
# Initialize Chrome driver
|
61 |
-
|
62 |
-
chrome_options.
|
63 |
-
chrome_options.add_argument("--
|
64 |
-
chrome_options.add_argument("--
|
65 |
-
chrome_options.add_argument("--
|
66 |
-
chrome_options.add_argument("--
|
67 |
-
chrome_options.add_argument("--
|
68 |
-
chrome_options.add_argument("--
|
69 |
-
|
70 |
-
driver = helium.start_chrome(headless=True, options=chrome_options)
|
|
|
|
|
|
|
|
|
71 |
|
72 |
# Screenshot callback
|
73 |
def save_screenshot(memory_step: ActionStep, agent: CodeAgent) -> Image.Image:
|
@@ -85,6 +94,7 @@ def save_screenshot(memory_step: ActionStep, agent: CodeAgent) -> Image.Image:
|
|
85 |
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
86 |
screenshot_path = f"{screenshot_dir}/screenshot_step_{current_step}_{timestamp}.png"
|
87 |
image.save(screenshot_path)
|
|
|
88 |
url_info = f"Current url: {driver.current_url}"
|
89 |
memory_step.observations = (
|
90 |
url_info if memory_step.observations is None else memory_step.observations + "\n" + url_info
|
@@ -156,9 +166,14 @@ def run_agent(url: str, request: str):
|
|
156 |
)
|
157 |
return agent_output, screenshot_path
|
158 |
except Exception as e:
|
|
|
159 |
return f"Error: {str(e)}", None
|
160 |
finally:
|
161 |
-
|
|
|
|
|
|
|
|
|
162 |
|
163 |
# Gradio interface
|
164 |
with gr.Blocks() as demo:
|
@@ -174,4 +189,5 @@ with gr.Blocks() as demo:
|
|
174 |
outputs=[output_text, output_image]
|
175 |
)
|
176 |
|
177 |
-
|
|
|
|
12 |
from dotenv import load_dotenv
|
13 |
from huggingface_hub import login
|
14 |
import tempfile
|
15 |
+
import logging
|
16 |
+
|
17 |
+
|
18 |
+
# Set up logging
|
19 |
+
logging.basicConfig(level=logging.INFO)
|
20 |
+
logger = logging.getLogger(__name__)
|
21 |
|
22 |
# Load environment variables
|
23 |
load_dotenv()
|
24 |
hf_token = os.getenv("HF_TOKEN")
|
25 |
gemini_api_key = os.getenv("GEMINI_API_KEY")
|
26 |
|
27 |
+
if not hf_token:
|
|
|
|
|
28 |
raise ValueError("HF_TOKEN environment variable not set.")
|
|
|
29 |
if not gemini_api_key:
|
30 |
raise ValueError("GEMINI_API_KEY environment variable not set.")
|
31 |
|
32 |
+
login(hf_token, add_to_git_credential=False)
|
33 |
+
|
34 |
# Define tools
|
35 |
@tool
|
36 |
def search_item_ctrl_f(text: str, nth_result: int = 1) -> str:
|
|
|
62 |
"""
|
63 |
webdriver.ActionChains(driver).send_keys(Keys.ESCAPE).perform()
|
64 |
|
65 |
+
# Initialize Chrome driver with error handling
|
66 |
+
try:
|
67 |
+
chrome_options = webdriver.ChromeOptions()
|
68 |
+
chrome_options.add_argument("--force-device-scale-factor=1")
|
69 |
+
chrome_options.add_argument("--window-size=1000,1350")
|
70 |
+
chrome_options.add_argument("--disable-pdf-viewer")
|
71 |
+
chrome_options.add_argument("--no-sandbox")
|
72 |
+
chrome_options.add_argument("--disable-dev-shm-usage")
|
73 |
+
chrome_options.add_argument("--window-position=0,0")
|
74 |
+
chrome_options.add_argument("--headless=new")
|
75 |
+
driver = helium.start_chrome(headless=True, options=chrome_options)
|
76 |
+
logger.info("Chrome driver initialized successfully.")
|
77 |
+
except Exception as e:
|
78 |
+
logger.error(f"Failed to initialize Chrome driver: {str(e)}")
|
79 |
+
raise
|
80 |
|
81 |
# Screenshot callback
|
82 |
def save_screenshot(memory_step: ActionStep, agent: CodeAgent) -> Image.Image:
|
|
|
94 |
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
95 |
screenshot_path = f"{screenshot_dir}/screenshot_step_{current_step}_{timestamp}.png"
|
96 |
image.save(screenshot_path)
|
97 |
+
logger.info(f"Saved screenshot to: {screenshot_path}")
|
98 |
url_info = f"Current url: {driver.current_url}"
|
99 |
memory_step.observations = (
|
100 |
url_info if memory_step.observations is None else memory_step.observations + "\n" + url_info
|
|
|
166 |
)
|
167 |
return agent_output, screenshot_path
|
168 |
except Exception as e:
|
169 |
+
logger.error(f"Agent execution failed: {str(e)}")
|
170 |
return f"Error: {str(e)}", None
|
171 |
finally:
|
172 |
+
try:
|
173 |
+
driver.quit()
|
174 |
+
logger.info("Chrome driver closed.")
|
175 |
+
except:
|
176 |
+
logger.warning("Failed to close Chrome driver.")
|
177 |
|
178 |
# Gradio interface
|
179 |
with gr.Blocks() as demo:
|
|
|
189 |
outputs=[output_text, output_image]
|
190 |
)
|
191 |
|
192 |
+
if __name__ == "__main__":
|
193 |
+
demo.launch()
|