Spaces:
Sleeping
Sleeping
testing new tools
Browse files
app.py
CHANGED
|
@@ -3,20 +3,93 @@ 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 |
-
"""A tool that does nothing yet
|
| 15 |
Args:
|
| 16 |
-
|
| 17 |
-
arg2: the second argument
|
| 18 |
"""
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
@tool
|
| 22 |
def get_current_time_in_timezone(timezone: str) -> str:
|
|
@@ -35,6 +108,8 @@ def get_current_time_in_timezone(timezone: str) -> str:
|
|
| 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'
|
|
@@ -55,7 +130,16 @@ with open("prompts.yaml", 'r') as stream:
|
|
| 55 |
|
| 56 |
agent = CodeAgent(
|
| 57 |
model=model,
|
| 58 |
-
tools=[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
max_steps=6,
|
| 60 |
verbosity_level=1,
|
| 61 |
grammar=None,
|
|
|
|
| 3 |
import requests
|
| 4 |
import pytz
|
| 5 |
import yaml
|
| 6 |
+
import json
|
| 7 |
from tools.final_answer import FinalAnswerTool
|
| 8 |
+
from tools.visit_webpage import VisitWebpageTool
|
| 9 |
+
from tools.web_search import DuckDuckGoSearchTool as WebSearchTool
|
| 10 |
|
| 11 |
from Gradio_UI import GradioUI
|
| 12 |
|
|
|
|
| 13 |
@tool
|
| 14 |
+
def get_wikipedia_summary(topic: str) -> str:
|
| 15 |
+
"""Fetches a short summary from Wikipedia for a given topic.
|
|
|
|
| 16 |
Args:
|
| 17 |
+
topic: The topic to search for on Wikipedia (e.g., 'Python programming language').
|
|
|
|
| 18 |
"""
|
| 19 |
+
try:
|
| 20 |
+
url = "https://en.wikipedia.org/api/rest_v1/page/summary/" + topic.replace(" ", "_")
|
| 21 |
+
response = requests.get(url, timeout=15)
|
| 22 |
+
if response.status_code == 200:
|
| 23 |
+
data = response.json()
|
| 24 |
+
title = data.get("title", topic)
|
| 25 |
+
extract = data.get("extract", "No summary available.")
|
| 26 |
+
page_url = data.get("content_urls", {}).get("desktop", {}).get("page", "")
|
| 27 |
+
return f"**{title}**\n\n{extract}\n\nRead more: {page_url}"
|
| 28 |
+
elif response.status_code == 404:
|
| 29 |
+
return f"No Wikipedia article found for '{topic}'. Try a different search term."
|
| 30 |
+
else:
|
| 31 |
+
return f"Error fetching Wikipedia summary (HTTP {response.status_code})."
|
| 32 |
+
except Exception as e:
|
| 33 |
+
return f"Error fetching Wikipedia summary for '{topic}': {str(e)}"
|
| 34 |
+
|
| 35 |
+
@tool
|
| 36 |
+
def get_random_fun_fact() -> str:
|
| 37 |
+
"""Fetches a random fun/useless fact from the uselessfacts API. No arguments needed.
|
| 38 |
+
"""
|
| 39 |
+
try:
|
| 40 |
+
response = requests.get("https://uselessfacts.jsph.pl/api/v2/facts/random", timeout=10)
|
| 41 |
+
if response.status_code == 200:
|
| 42 |
+
data = response.json()
|
| 43 |
+
return f"🎲 Fun fact: {data.get('text', 'No fact available.')}"
|
| 44 |
+
return "Could not fetch a fun fact right now. Try again later."
|
| 45 |
+
except Exception as e:
|
| 46 |
+
return f"Error fetching fun fact: {str(e)}"
|
| 47 |
+
|
| 48 |
+
@tool
|
| 49 |
+
def convert_units(value: float, from_unit: str, to_unit: str) -> str:
|
| 50 |
+
"""Converts a numeric value between common units of measurement.
|
| 51 |
+
Supports length (km, miles, m, ft, in, cm), weight (kg, lbs, g, oz),
|
| 52 |
+
and temperature (celsius, fahrenheit, kelvin).
|
| 53 |
+
Args:
|
| 54 |
+
value: The numeric value to convert.
|
| 55 |
+
from_unit: The unit to convert from (e.g., 'km', 'lbs', 'celsius').
|
| 56 |
+
to_unit: The unit to convert to (e.g., 'miles', 'kg', 'fahrenheit').
|
| 57 |
+
"""
|
| 58 |
+
conversions_to_base = {
|
| 59 |
+
# Length → meters
|
| 60 |
+
"km": ("length", lambda v: v * 1000),
|
| 61 |
+
"m": ("length", lambda v: v),
|
| 62 |
+
"cm": ("length", lambda v: v / 100),
|
| 63 |
+
"miles": ("length", lambda v: v * 1609.344),
|
| 64 |
+
"ft": ("length", lambda v: v * 0.3048),
|
| 65 |
+
"in": ("length", lambda v: v * 0.0254),
|
| 66 |
+
# Weight → grams
|
| 67 |
+
"kg": ("weight", lambda v: v * 1000),
|
| 68 |
+
"g": ("weight", lambda v: v),
|
| 69 |
+
"lbs": ("weight", lambda v: v * 453.592),
|
| 70 |
+
"oz": ("weight", lambda v: v * 28.3495),
|
| 71 |
+
# Temperature → celsius
|
| 72 |
+
"celsius": ("temp", lambda v: v),
|
| 73 |
+
"fahrenheit": ("temp", lambda v: (v - 32) * 5 / 9),
|
| 74 |
+
"kelvin": ("temp", lambda v: v - 273.15),
|
| 75 |
+
}
|
| 76 |
+
conversions_from_base = {
|
| 77 |
+
"km": lambda v: v / 1000, "m": lambda v: v, "cm": lambda v: v * 100,
|
| 78 |
+
"miles": lambda v: v / 1609.344, "ft": lambda v: v / 0.3048, "in": lambda v: v / 0.0254,
|
| 79 |
+
"kg": lambda v: v / 1000, "g": lambda v: v, "lbs": lambda v: v / 453.592, "oz": lambda v: v / 28.3495,
|
| 80 |
+
"celsius": lambda v: v, "fahrenheit": lambda v: v * 9 / 5 + 32, "kelvin": lambda v: v + 273.15,
|
| 81 |
+
}
|
| 82 |
+
fu = from_unit.lower().strip()
|
| 83 |
+
tu = to_unit.lower().strip()
|
| 84 |
+
if fu not in conversions_to_base or tu not in conversions_from_base:
|
| 85 |
+
return f"Unsupported unit(s): '{from_unit}' or '{to_unit}'. Supported: {list(conversions_to_base.keys())}"
|
| 86 |
+
cat_from, to_base = conversions_to_base[fu]
|
| 87 |
+
cat_to, _ = conversions_to_base[tu]
|
| 88 |
+
if cat_from != cat_to:
|
| 89 |
+
return f"Cannot convert between '{from_unit}' ({cat_from}) and '{to_unit}' ({cat_to}). Units must be in the same category."
|
| 90 |
+
base_value = to_base(value)
|
| 91 |
+
result = conversions_from_base[tu](base_value)
|
| 92 |
+
return f"{value} {from_unit} = {result:.4f} {to_unit}"
|
| 93 |
|
| 94 |
@tool
|
| 95 |
def get_current_time_in_timezone(timezone: str) -> str:
|
|
|
|
| 108 |
|
| 109 |
|
| 110 |
final_answer = FinalAnswerTool()
|
| 111 |
+
web_search = WebSearchTool()
|
| 112 |
+
visit_webpage = VisitWebpageTool()
|
| 113 |
|
| 114 |
# 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:
|
| 115 |
# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
|
|
|
|
| 130 |
|
| 131 |
agent = CodeAgent(
|
| 132 |
model=model,
|
| 133 |
+
tools=[
|
| 134 |
+
final_answer,
|
| 135 |
+
web_search,
|
| 136 |
+
visit_webpage,
|
| 137 |
+
get_current_time_in_timezone,
|
| 138 |
+
get_wikipedia_summary,
|
| 139 |
+
get_random_fun_fact,
|
| 140 |
+
convert_units,
|
| 141 |
+
image_generation_tool,
|
| 142 |
+
],
|
| 143 |
max_steps=6,
|
| 144 |
verbosity_level=1,
|
| 145 |
grammar=None,
|