Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,67 +1,53 @@
|
|
| 1 |
-
from smolagents import CodeAgent,
|
| 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 |
-
# -------------------------------
|
| 10 |
-
# 1️⃣ Initialize models
|
| 11 |
-
# -------------------------------
|
| 12 |
|
| 13 |
-
#
|
| 14 |
search_model = HfApiModel(
|
| 15 |
"Intelligent-Internet/II-Search-4B",
|
| 16 |
return_text=True,
|
| 17 |
-
streaming=False #
|
| 18 |
)
|
| 19 |
|
| 20 |
-
# Main agent model
|
| 21 |
-
model = HfApiModel(
|
| 22 |
-
max_tokens=2096,
|
| 23 |
-
temperature=0.5,
|
| 24 |
-
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
|
| 25 |
-
custom_role_conversions=None
|
| 26 |
-
)
|
| 27 |
-
|
| 28 |
-
# -------------------------------
|
| 29 |
-
# 2️⃣ Define tools
|
| 30 |
-
# -------------------------------
|
| 31 |
-
|
| 32 |
@tool
|
| 33 |
-
def my_custom_tool(
|
| 34 |
-
|
| 35 |
-
|
|
|
|
| 36 |
Args:
|
| 37 |
-
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
| 39 |
"""
|
| 40 |
-
# Query the search model
|
| 41 |
-
|
| 42 |
-
|
|
|
|
|
|
|
| 43 |
|
| 44 |
-
#
|
| 45 |
-
|
| 46 |
-
|
|
|
|
| 47 |
|
| 48 |
-
# Format the output
|
| 49 |
-
output = f"Top 5 sightseeing locations in {city1}:\n"
|
| 50 |
-
output += "\n".join(results_city1[:5]) + "\n\n"
|
| 51 |
-
output += f"Top 5 sightseeing locations in {city2}:\n"
|
| 52 |
-
output += "\n".join(results_city2[:5])
|
| 53 |
return output
|
| 54 |
|
| 55 |
|
| 56 |
@tool
|
| 57 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 58 |
-
|
| 59 |
"""A tool that fetches the current local time in a specified timezone.
|
| 60 |
Args:
|
| 61 |
timezone: A string representing a valid timezone (e.g., 'America/New_York').
|
| 62 |
"""
|
| 63 |
try:
|
|
|
|
| 64 |
tz = pytz.timezone(timezone)
|
|
|
|
| 65 |
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
| 66 |
return f"The current local time in {timezone} is: {local_time}"
|
| 67 |
except Exception as e:
|
|
@@ -69,22 +55,20 @@ def get_current_time_in_timezone(timezone: str) -> str:
|
|
| 69 |
|
| 70 |
|
| 71 |
final_answer = FinalAnswerTool()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
-
# Image generation tool from Hugging Face Hub
|
| 74 |
-
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 75 |
-
|
| 76 |
-
# -------------------------------
|
| 77 |
-
# 3️⃣ Load prompt templates
|
| 78 |
-
# -------------------------------
|
| 79 |
with open("prompts.yaml", 'r') as stream:
|
| 80 |
prompt_templates = yaml.safe_load(stream)
|
| 81 |
-
|
| 82 |
-
#
|
| 83 |
-
# 4️⃣ Create agent
|
| 84 |
-
# -------------------------------
|
| 85 |
agent = CodeAgent(
|
| 86 |
model=model,
|
| 87 |
-
tools=[final_answer,
|
| 88 |
max_steps=6,
|
| 89 |
verbosity_level=1,
|
| 90 |
grammar=None,
|
|
@@ -94,12 +78,5 @@ agent = CodeAgent(
|
|
| 94 |
prompt_templates=prompt_templates
|
| 95 |
)
|
| 96 |
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
# -------------------------------
|
| 100 |
-
if __name__ == "__main__":
|
| 101 |
-
result = my_custom_tool("Berlin", "Bremen")
|
| 102 |
-
print(result)
|
| 103 |
-
|
| 104 |
-
# Launch Gradio UI
|
| 105 |
-
GradioUI(agent).launch()
|
|
|
|
| 1 |
+
from smolagents import CodeAgent, DuckDuckGoSearchTool, FinalAnswerTool, InferenceClientModel, load_tool, tool
|
| 2 |
import datetime
|
| 3 |
import requests
|
| 4 |
import pytz
|
| 5 |
import yaml
|
|
|
|
|
|
|
| 6 |
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
+
# Initialize the Hugging Face web search model
|
| 9 |
search_model = HfApiModel(
|
| 10 |
"Intelligent-Internet/II-Search-4B",
|
| 11 |
return_text=True,
|
| 12 |
+
streaming=False # ensures the output is plain text
|
| 13 |
)
|
| 14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
@tool
|
| 16 |
+
def my_custom_tool(arg1:str, arg2:int)-> str: # it's important to specify the return type
|
| 17 |
+
"""
|
| 18 |
+
Fetches the top 5 sightseeing events in a city for a given season using Hugging Face II-Search.
|
| 19 |
+
|
| 20 |
Args:
|
| 21 |
+
city: Name of the city (e.g., "Paris").
|
| 22 |
+
season: Season or time of year (e.g., "summer").
|
| 23 |
+
|
| 24 |
+
Returns:
|
| 25 |
+
A formatted string listing the top 5 sightseeing events.
|
| 26 |
"""
|
| 27 |
+
# Query the search model
|
| 28 |
+
response_text = search_model(f"Top 5 sightseeing events in {city} during {season}")
|
| 29 |
+
|
| 30 |
+
# Split by lines to extract individual events
|
| 31 |
+
events = response_text.split("\n")
|
| 32 |
|
| 33 |
+
# Format output as a readable string
|
| 34 |
+
output = f"Top 5 sightseeing events in {city} during {season}:\n"
|
| 35 |
+
for i, event in enumerate(events[:5], start=1):
|
| 36 |
+
output += f"{i}. {event.strip()}\n"
|
| 37 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
return output
|
| 39 |
|
| 40 |
|
| 41 |
@tool
|
| 42 |
def get_current_time_in_timezone(timezone: str) -> str:
|
|
|
|
| 43 |
"""A tool that fetches the current local time in a specified timezone.
|
| 44 |
Args:
|
| 45 |
timezone: A string representing a valid timezone (e.g., 'America/New_York').
|
| 46 |
"""
|
| 47 |
try:
|
| 48 |
+
# Create timezone object
|
| 49 |
tz = pytz.timezone(timezone)
|
| 50 |
+
# Get current time in that timezone
|
| 51 |
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
| 52 |
return f"The current local time in {timezone} is: {local_time}"
|
| 53 |
except Exception as e:
|
|
|
|
| 55 |
|
| 56 |
|
| 57 |
final_answer = FinalAnswerTool()
|
| 58 |
+
model = InferenceClientModel(
|
| 59 |
+
max_tokens=2096,
|
| 60 |
+
temperature=0.5,
|
| 61 |
+
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
|
| 62 |
+
custom_role_conversions=None,
|
| 63 |
+
)
|
| 64 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
with open("prompts.yaml", 'r') as stream:
|
| 66 |
prompt_templates = yaml.safe_load(stream)
|
| 67 |
+
|
| 68 |
+
# We're creating our CodeAgent
|
|
|
|
|
|
|
| 69 |
agent = CodeAgent(
|
| 70 |
model=model,
|
| 71 |
+
tools=[final_answer], # add your tools here (don't remove final_answer)
|
| 72 |
max_steps=6,
|
| 73 |
verbosity_level=1,
|
| 74 |
grammar=None,
|
|
|
|
| 78 |
prompt_templates=prompt_templates
|
| 79 |
)
|
| 80 |
|
| 81 |
+
GradioUI(agent).launch()
|
| 82 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|