Update app.py
#675
by KaranSood15 - opened
app.py
CHANGED
|
@@ -1,69 +1,97 @@
|
|
| 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 |
-
|
|
|
|
| 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:
|
| 23 |
-
"""
|
| 24 |
Args:
|
| 25 |
-
timezone:
|
| 26 |
"""
|
| 27 |
try:
|
| 28 |
-
# Create timezone object
|
| 29 |
tz = pytz.timezone(timezone)
|
| 30 |
-
# Get current time in that timezone
|
| 31 |
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
| 32 |
-
return f"
|
| 33 |
except Exception as e:
|
| 34 |
-
return f"Error
|
| 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-
|
| 46 |
-
custom_role_conversions=None,
|
| 47 |
)
|
| 48 |
|
| 49 |
|
| 50 |
-
#
|
| 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=
|
| 61 |
grammar=None,
|
| 62 |
planning_interval=None,
|
| 63 |
-
name=
|
| 64 |
-
description=
|
| 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 |
+
|
| 10 |
+
# 🔧 Improved Custom Tool (Calculator)
|
| 11 |
@tool
|
| 12 |
+
def smart_calculator(expression: str) -> str:
|
| 13 |
+
"""Evaluates a mathematical expression safely.
|
|
|
|
| 14 |
Args:
|
| 15 |
+
expression: A math expression like '2+2*10'
|
|
|
|
| 16 |
"""
|
| 17 |
+
try:
|
| 18 |
+
result = eval(expression)
|
| 19 |
+
return f"Result: {result}"
|
| 20 |
+
except Exception as e:
|
| 21 |
+
return f"Error: {str(e)}"
|
| 22 |
|
| 23 |
+
|
| 24 |
+
# 🕒 Time Tool (unchanged but useful)
|
| 25 |
@tool
|
| 26 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 27 |
+
"""Fetches current local time in a timezone.
|
| 28 |
Args:
|
| 29 |
+
timezone: e.g. 'Asia/Kolkata'
|
| 30 |
"""
|
| 31 |
try:
|
|
|
|
| 32 |
tz = pytz.timezone(timezone)
|
|
|
|
| 33 |
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
| 34 |
+
return f"Time in {timezone}: {local_time}"
|
| 35 |
except Exception as e:
|
| 36 |
+
return f"Error: {str(e)}"
|
| 37 |
|
| 38 |
|
| 39 |
+
# 🌐 News Tool (NEW)
|
| 40 |
+
@tool
|
| 41 |
+
def get_latest_news(topic: str) -> str:
|
| 42 |
+
"""Fetch latest news on a topic.
|
| 43 |
+
Args:
|
| 44 |
+
topic: e.g. 'AI', 'India'
|
| 45 |
+
"""
|
| 46 |
+
try:
|
| 47 |
+
search = DuckDuckGoSearchTool()
|
| 48 |
+
return search.run(f"{topic} latest news")
|
| 49 |
+
except Exception as e:
|
| 50 |
+
return f"Error fetching news: {str(e)}"
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
# 🎯 Final Answer Tool (required)
|
| 54 |
final_answer = FinalAnswerTool()
|
| 55 |
|
|
|
|
|
|
|
| 56 |
|
| 57 |
+
# 🤖 Better Model (more general-purpose)
|
| 58 |
model = HfApiModel(
|
| 59 |
+
max_tokens=2096,
|
| 60 |
+
temperature=0.5,
|
| 61 |
+
model_id='Qwen/Qwen2.5-72B-Instruct', # upgraded model
|
| 62 |
+
custom_role_conversions=None,
|
| 63 |
)
|
| 64 |
|
| 65 |
|
| 66 |
+
# 🎨 Image Generation Tool
|
| 67 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 68 |
|
| 69 |
+
|
| 70 |
+
# 📄 Load prompt templates
|
| 71 |
with open("prompts.yaml", 'r') as stream:
|
| 72 |
prompt_templates = yaml.safe_load(stream)
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
# 🚀 Enhanced Agent
|
| 76 |
agent = CodeAgent(
|
| 77 |
model=model,
|
| 78 |
+
tools=[
|
| 79 |
+
final_answer,
|
| 80 |
+
smart_calculator,
|
| 81 |
+
get_current_time_in_timezone,
|
| 82 |
+
get_latest_news,
|
| 83 |
+
DuckDuckGoSearchTool(),
|
| 84 |
+
image_generation_tool
|
| 85 |
+
],
|
| 86 |
max_steps=6,
|
| 87 |
+
verbosity_level=2,
|
| 88 |
grammar=None,
|
| 89 |
planning_interval=None,
|
| 90 |
+
name="Smart AI Agent",
|
| 91 |
+
description="An AI agent that can search the web, calculate, fetch news, tell time, and generate images.",
|
| 92 |
prompt_templates=prompt_templates
|
| 93 |
)
|
| 94 |
|
| 95 |
|
| 96 |
+
# 🖥️ Launch UI
|
| 97 |
GradioUI(agent).launch()
|