Spaces:
Sleeping
Sleeping
Update app.py
Browse filesThe template now includes my additional tools with their respective functionalities. /Michael Cid
app.py
CHANGED
|
@@ -1,22 +1,68 @@
|
|
| 1 |
-
from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
|
| 2 |
import datetime
|
| 3 |
import requests
|
| 4 |
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 !
|
| 11 |
@tool
|
| 12 |
-
def
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
Args:
|
| 16 |
-
|
| 17 |
-
arg2: the second argument
|
| 18 |
"""
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
@tool
|
| 22 |
def get_current_time_in_timezone(timezone: str) -> str:
|
|
@@ -33,29 +79,52 @@ 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 |
|
| 39 |
# If the agent does not answer, the model is overloaded, please use another model or the following Hugging Face Endpoint that also contains qwen2.5 coder:
|
| 40 |
-
# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
|
| 41 |
|
| 42 |
model = HfApiModel(
|
| 43 |
-
max_tokens=2096,
|
| 44 |
-
temperature=0.5,
|
| 45 |
-
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloaded
|
| 46 |
-
custom_role_conversions=None,
|
| 47 |
)
|
| 48 |
|
| 49 |
-
|
| 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=[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
max_steps=6,
|
| 60 |
verbosity_level=1,
|
| 61 |
grammar=None,
|
|
@@ -65,5 +134,4 @@ agent = CodeAgent(
|
|
| 65 |
prompt_templates=prompt_templates
|
| 66 |
)
|
| 67 |
|
| 68 |
-
|
| 69 |
GradioUI(agent).launch()
|
|
|
|
| 1 |
+
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
|
| 2 |
import datetime
|
| 3 |
import requests
|
| 4 |
import pytz
|
| 5 |
import yaml
|
| 6 |
from tools.final_answer import FinalAnswerTool
|
|
|
|
| 7 |
from Gradio_UI import GradioUI
|
| 8 |
|
|
|
|
| 9 |
@tool
|
| 10 |
+
def weather_forecaster(location: str, date: str) -> str:
|
| 11 |
+
"""
|
| 12 |
+
Provides the weather forecast for a specific location and date.
|
| 13 |
+
|
| 14 |
+
Args:
|
| 15 |
+
location: The city or region for which the weather forecast is needed.
|
| 16 |
+
date: The date for which the weather forecast is needed (format: YYYY-MM-DD).
|
| 17 |
+
"""
|
| 18 |
+
# Implementation would involve calling a weather API to get the forecast.
|
| 19 |
+
return f"The weather in {location} on {date} will be..."
|
| 20 |
+
|
| 21 |
+
@tool
|
| 22 |
+
def language_translator(text: str, target_language: str) -> str:
|
| 23 |
+
"""
|
| 24 |
+
Translates a given text into the specified target language.
|
| 25 |
+
|
| 26 |
+
Args:
|
| 27 |
+
text: The text to be translated.
|
| 28 |
+
target_language: The language code (e.g., 'es' for Spanish) to translate the text into.
|
| 29 |
+
"""
|
| 30 |
+
# Implementation would involve using a translation API to convert the text.
|
| 31 |
+
return f"Translated text: {text} in {target_language}..."
|
| 32 |
+
|
| 33 |
+
@tool
|
| 34 |
+
def stock_price_checker(stock_symbol: str) -> str:
|
| 35 |
+
"""
|
| 36 |
+
Retrieves the current stock price for a given stock symbol.
|
| 37 |
+
|
| 38 |
Args:
|
| 39 |
+
stock_symbol: The stock symbol (e.g., 'AAPL' for Apple Inc.).
|
|
|
|
| 40 |
"""
|
| 41 |
+
# Implementation would involve calling a financial API to get the stock price.
|
| 42 |
+
return f"The current price of {stock_symbol} is..."
|
| 43 |
+
|
| 44 |
+
@tool
|
| 45 |
+
def news_headlines(topic: str, num_articles: int) -> str:
|
| 46 |
+
"""
|
| 47 |
+
Fetches the latest news headlines for a specific topic.
|
| 48 |
+
|
| 49 |
+
Args:
|
| 50 |
+
topic: The topic for which news headlines are needed.
|
| 51 |
+
num_articles: The number of articles to retrieve.
|
| 52 |
+
"""
|
| 53 |
+
# Implementation would involve calling a news API to get the headlines.
|
| 54 |
+
return f"Top {num_articles} headlines for {topic}..."
|
| 55 |
+
|
| 56 |
+
@tool
|
| 57 |
+
def recipe_finder(ingredients: list) -> str:
|
| 58 |
+
"""
|
| 59 |
+
Finds recipes based on the provided list of ingredients.
|
| 60 |
+
|
| 61 |
+
Args:
|
| 62 |
+
ingredients: A list of ingredients to use in the recipe.
|
| 63 |
+
"""
|
| 64 |
+
# Implementation would involve searching a recipe database or API.
|
| 65 |
+
return f"Recipes using {', '.join(ingredients)}..."
|
| 66 |
|
| 67 |
@tool
|
| 68 |
def get_current_time_in_timezone(timezone: str) -> str:
|
|
|
|
| 79 |
except Exception as e:
|
| 80 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 81 |
|
| 82 |
+
@tool
|
| 83 |
+
def get_latest_tech_news(topic: str, max_results: int = 5) -> str:
|
| 84 |
+
"""Fetches recent news headlines or articles about a specific technology topic. Useful for staying updated on AI, Linux, IT trends, etc.
|
| 85 |
+
Args:
|
| 86 |
+
topic: The specific area of technology to search for (e.g., 'Artificial Intelligence', 'Ubuntu LTS', 'Prompt Engineering breakthroughs').
|
| 87 |
+
max_results: The maximum number of news items to return (default is 5).
|
| 88 |
+
"""
|
| 89 |
+
search_query = f"{topic} site:https://www.futuretools.io/"
|
| 90 |
+
search_results = web_search({"query": search_query})
|
| 91 |
+
|
| 92 |
+
news_items = []
|
| 93 |
+
for result in search_results[:max_results]:
|
| 94 |
+
news_items.append(result['title'])
|
| 95 |
+
|
| 96 |
+
return "\n".join(news_items)
|
| 97 |
|
| 98 |
final_answer = FinalAnswerTool()
|
| 99 |
|
| 100 |
# If the agent does not answer, the model is overloaded, please use another model or the following Hugging Face Endpoint that also contains qwen2.5 coder:
|
| 101 |
+
# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
|
| 102 |
|
| 103 |
model = HfApiModel(
|
| 104 |
+
max_tokens=2096,
|
| 105 |
+
temperature=0.5,
|
| 106 |
+
model_id='Qwen/Qwen2.5-Coder-32B-Instruct', # it is possible that this model may be overloaded
|
| 107 |
+
custom_role_conversions=None,
|
| 108 |
)
|
| 109 |
|
|
|
|
| 110 |
# Import tool from Hub
|
| 111 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 112 |
|
| 113 |
with open("prompts.yaml", 'r') as stream:
|
| 114 |
prompt_templates = yaml.safe_load(stream)
|
| 115 |
+
|
| 116 |
agent = CodeAgent(
|
| 117 |
model=model,
|
| 118 |
+
tools=[
|
| 119 |
+
final_answer,
|
| 120 |
+
weather_forecaster,
|
| 121 |
+
language_translator,
|
| 122 |
+
stock_price_checker,
|
| 123 |
+
news_headlines,
|
| 124 |
+
recipe_finder,
|
| 125 |
+
get_current_time_in_timezone,
|
| 126 |
+
get_latest_tech_news
|
| 127 |
+
], # Add your tools here (don't remove final answer)
|
| 128 |
max_steps=6,
|
| 129 |
verbosity_level=1,
|
| 130 |
grammar=None,
|
|
|
|
| 134 |
prompt_templates=prompt_templates
|
| 135 |
)
|
| 136 |
|
|
|
|
| 137 |
GradioUI(agent).launch()
|