SilviuMatei commited on
Commit
1aa340c
·
verified ·
1 Parent(s): c9b8fea

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -50
app.py CHANGED
@@ -6,59 +6,39 @@ import yaml
6
  from tools.final_answer import FinalAnswerTool
7
 
8
  from Gradio_UI import GradioUI
9
- from bs4 import BeautifulSoup
 
 
10
 
11
  @tool
12
- def amazon_product_scraper(search_url: str) -> list:
13
  """
14
- Scrapes Amazon search results for product titles, prices, delivery fees, and links.
15
-
16
- Args:
17
- search_url: The URL of the Amazon search results page containing product listings.
18
-
19
  Returns:
20
- A list containing two elements:
21
- - A list of dictionaries, each with keys "Title", "Price", "Delivery", "Link", sorted by price.
22
- - A string containing a recommendation for the best deal.
23
  """
24
- headers = {
25
- "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36"
26
- }
27
-
28
- response = requests.get(search_url, headers=headers)
29
- if response.status_code != 200:
30
- return [], "Failed to retrieve Amazon results. Amazon may be blocking requests."
31
-
32
- soup = BeautifulSoup(response.text, 'html.parser')
33
-
34
- product_data = []
35
-
36
- for item in soup.select("div[data-asin]"):
37
- title_tag = item.select_one("h2 a")
38
- price_tag = item.select_one("span.a-price-whole")
39
- delivery_tag = item.select_one("span.s-align-children-center")
40
-
41
- if title_tag and price_tag:
42
- title = title_tag.text.strip()
43
- price = price_tag.text.strip().replace(',', '') # Normalize prices
44
- delivery = delivery_tag.text.strip() if delivery_tag else "Free"
45
- link = "https://www.amazon.com" + title_tag["href"]
46
-
47
- product_data.append({
48
- "Title": title,
49
- "Price": float(price) if price.isnumeric() else None,
50
- "Delivery": delivery,
51
- "Link": link
52
- })
53
-
54
- # Filter out products with no price and sort by price
55
- product_data = [p for p in product_data if p["Price"] is not None]
56
- product_data.sort(key=lambda x: x["Price"])
57
-
58
- # Recommendation logic
59
- best_deal = product_data[0] if product_data else None
60
- recommendation = ""
61
- if best_deal is not None:
62
- recommendation = f"Best deal: {best_deal['Title']} at ${best_deal['Price']} with {best_deal['Delivery']} (Link: {best_deal['Link']})"
63
 
64
- return product_data, recommendation
 
6
  from tools.final_answer import FinalAnswerTool
7
 
8
  from Gradio_UI import GradioUI
9
+
10
+ from selenium import webdriver
11
+ from selenium.webdriver.chrome.options import Options
12
 
13
  @tool
14
+ def get_device_info() -> str:
15
  """
16
+ Opens a headless browser and executes JavaScript to silently retrieve device information.
17
+
 
 
 
18
  Returns:
19
+ str: A string containing device details such as user agent, platform, and screen resolution.
 
 
20
  """
21
+ options = Options()
22
+ options.add_argument("--headless")
23
+ options.add_argument("--disable-gpu")
24
+ options.add_argument("--no-sandbox")
25
+ options.add_argument("--disable-dev-shm-usage")
26
+
27
+ driver = webdriver.Chrome(options=options)
28
+
29
+ script = """
30
+ return JSON.stringify({
31
+ userAgent: navigator.userAgent,
32
+ platform: navigator.platform,
33
+ language: navigator.language,
34
+ screenWidth: window.screen.width,
35
+ screenHeight: window.screen.height
36
+ });
37
+ """
38
+
39
+ driver.get("data:text/html,<script>document.title='device_info';</script>")
40
+ device_info = driver.execute_script(script)
41
+ driver.quit()
42
+
43
+ return device_info
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44