Update app.py
Browse filesGet any product name or price from any amazon link, Generate images by text, Get current Time zone.
app.py
CHANGED
|
@@ -5,6 +5,11 @@ import pytz
|
|
| 5 |
import yaml
|
| 6 |
from tools.final_answer import FinalAnswerTool
|
| 7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
from Gradio_UI import GradioUI
|
| 9 |
|
| 10 |
# Below is an example of a tool that does nothing. Amaze us with your creativity !
|
|
@@ -33,6 +38,45 @@ def get_current_time_in_timezone(timezone: str) -> str:
|
|
| 33 |
except Exception as e:
|
| 34 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
final_answer = FinalAnswerTool()
|
| 38 |
|
|
@@ -42,7 +86,7 @@ final_answer = FinalAnswerTool()
|
|
| 42 |
model = HfApiModel(
|
| 43 |
max_tokens=2096,
|
| 44 |
temperature=0.5,
|
| 45 |
-
model_id='
|
| 46 |
custom_role_conversions=None,
|
| 47 |
)
|
| 48 |
|
|
@@ -50,12 +94,26 @@ custom_role_conversions=None,
|
|
| 50 |
# Import tool from Hub
|
| 51 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 52 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
with open("prompts.yaml", 'r') as stream:
|
| 54 |
prompt_templates = yaml.safe_load(stream)
|
| 55 |
|
| 56 |
agent = CodeAgent(
|
| 57 |
model=model,
|
| 58 |
-
tools=[final_answer], ## add your tools here (don't remove final answer)
|
| 59 |
max_steps=6,
|
| 60 |
verbosity_level=1,
|
| 61 |
grammar=None,
|
|
|
|
| 5 |
import yaml
|
| 6 |
from tools.final_answer import FinalAnswerTool
|
| 7 |
|
| 8 |
+
import requests
|
| 9 |
+
from bs4 import BeautifulSoup
|
| 10 |
+
from transformers import pipeline
|
| 11 |
+
|
| 12 |
+
|
| 13 |
from Gradio_UI import GradioUI
|
| 14 |
|
| 15 |
# Below is an example of a tool that does nothing. Amaze us with your creativity !
|
|
|
|
| 38 |
except Exception as e:
|
| 39 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 40 |
|
| 41 |
+
@tool
|
| 42 |
+
def get_productName_and_prices_from_amazon_link(Amazon_link: str) -> str:
|
| 43 |
+
"""A tool that gets the product name and price from provided amazon link.
|
| 44 |
+
Args:
|
| 45 |
+
Amazon_link: Amazon product link (e.g., 'https://www.amazon.co.uk/s-Oliver-Mens-Weste-Outdoor-Gilet/dp/B0B5HCKFX4?ref_=ast_sto_dp&psc=1').
|
| 46 |
+
"""
|
| 47 |
+
try:
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
# Amazon product URL
|
| 51 |
+
url = Amazon_link
|
| 52 |
+
|
| 53 |
+
# Set headers to mimic a real browser
|
| 54 |
+
headers = {
|
| 55 |
+
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36"
|
| 56 |
+
}
|
| 57 |
+
|
| 58 |
+
# Send a request to the webpage
|
| 59 |
+
response = requests.get(url, headers=headers)
|
| 60 |
+
|
| 61 |
+
# Parse the page
|
| 62 |
+
soup = BeautifulSoup(response.content, "html.parser")
|
| 63 |
+
|
| 64 |
+
# Extract product title
|
| 65 |
+
title = soup.find("span", {"id": "productTitle"}).text.strip()
|
| 66 |
+
|
| 67 |
+
# Extract product price (may vary based on availability)
|
| 68 |
+
price_element = soup.find("span", class_="a-offscreen")
|
| 69 |
+
price = price_element.text.strip() if price_element else "Price not available"
|
| 70 |
+
|
| 71 |
+
# Print results
|
| 72 |
+
print(f"Product Name: {title}")
|
| 73 |
+
print(f"Price: {price}")
|
| 74 |
+
|
| 75 |
+
return f"The Product Name is: {title} and the Price is:{price}"
|
| 76 |
+
except Exception as e:
|
| 77 |
+
return f"Error fetching Product name and price for this link{url}"
|
| 78 |
+
|
| 79 |
+
|
| 80 |
|
| 81 |
final_answer = FinalAnswerTool()
|
| 82 |
|
|
|
|
| 86 |
model = HfApiModel(
|
| 87 |
max_tokens=2096,
|
| 88 |
temperature=0.5,
|
| 89 |
+
model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud',# it is possible that this model may be overloaded
|
| 90 |
custom_role_conversions=None,
|
| 91 |
)
|
| 92 |
|
|
|
|
| 94 |
# Import tool from Hub
|
| 95 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 96 |
|
| 97 |
+
|
| 98 |
+
# Load a text-generation model
|
| 99 |
+
text_generator = pipeline("text-generation", model="meta-llama/Llama-3-8B-Instruct")
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
@tool
|
| 103 |
+
def generate_text(prompt: str) -> str:
|
| 104 |
+
"""A tool that generates text based on a given prompt."""
|
| 105 |
+
output = text_generator(prompt, max_length=100)
|
| 106 |
+
return output[0]["generated_text"]
|
| 107 |
+
|
| 108 |
+
|
| 109 |
+
|
| 110 |
+
|
| 111 |
with open("prompts.yaml", 'r') as stream:
|
| 112 |
prompt_templates = yaml.safe_load(stream)
|
| 113 |
|
| 114 |
agent = CodeAgent(
|
| 115 |
model=model,
|
| 116 |
+
tools=[final_answer,my_custom_tool,get_current_time_in_timezone,get_productName_and_prices_from_amazon_link,image_generation_tool,generate_text], ## add your tools here (don't remove final answer)
|
| 117 |
max_steps=6,
|
| 118 |
verbosity_level=1,
|
| 119 |
grammar=None,
|