Update app.py
#728
by carunreddy05 - opened
app.py
CHANGED
|
@@ -1,69 +1,293 @@
|
|
| 1 |
-
from smolagents import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return type
|
| 13 |
-
#Keep this format for the description / args / args description but feel free to modify the tool
|
| 14 |
-
"""A tool that does nothing yet
|
| 15 |
-
Args:
|
| 16 |
-
arg1: the first argument
|
| 17 |
-
arg2: the second argument
|
| 18 |
-
"""
|
| 19 |
-
return "What magic will you build ?"
|
| 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 |
-
|
| 31 |
-
local_time = datetime.datetime.now(tz).strftime(
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
except Exception as e:
|
| 34 |
-
return f"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
| 47 |
)
|
| 48 |
|
| 49 |
|
| 50 |
-
#
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
prompt_templates = yaml.safe_load(stream)
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
agent = CodeAgent(
|
| 57 |
model=model,
|
| 58 |
-
|
| 59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
verbosity_level=1,
|
| 61 |
grammar=None,
|
| 62 |
planning_interval=None,
|
| 63 |
-
name=
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
prompt_templates=prompt_templates
|
| 66 |
)
|
| 67 |
|
| 68 |
|
|
|
|
|
|
|
|
|
|
| 69 |
GradioUI(agent).launch()
|
|
|
|
| 1 |
+
from smolagents import (
|
| 2 |
+
CodeAgent,
|
| 3 |
+
DuckDuckGoSearchTool,
|
| 4 |
+
InferenceClientModel,
|
| 5 |
+
load_tool,
|
| 6 |
+
tool
|
| 7 |
+
)
|
| 8 |
+
|
| 9 |
import datetime
|
|
|
|
| 10 |
import pytz
|
| 11 |
import yaml
|
|
|
|
| 12 |
|
| 13 |
+
from tools.final_answer import FinalAnswerTool
|
| 14 |
from Gradio_UI import GradioUI
|
| 15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
+
# ---------------------------------------------------------
|
| 18 |
+
# CUSTOM TOOL 1: Current time
|
| 19 |
+
# ---------------------------------------------------------
|
| 20 |
@tool
|
| 21 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 22 |
+
"""
|
| 23 |
+
Gets the current local date and time for a timezone.
|
| 24 |
+
|
| 25 |
Args:
|
| 26 |
+
timezone: Valid timezone such as America/New_York,
|
| 27 |
+
Europe/London, or Asia/Kolkata.
|
| 28 |
"""
|
| 29 |
+
|
| 30 |
try:
|
|
|
|
| 31 |
tz = pytz.timezone(timezone)
|
| 32 |
+
|
| 33 |
+
local_time = datetime.datetime.now(tz).strftime(
|
| 34 |
+
"%Y-%m-%d %H:%M:%S"
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
+
return f"The current local time in {timezone} is {local_time}"
|
| 38 |
+
|
| 39 |
except Exception as e:
|
| 40 |
+
return f"Could not get time for {timezone}: {str(e)}"
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
# ---------------------------------------------------------
|
| 44 |
+
# CUSTOM TOOL 2: AI technology impact analyzer
|
| 45 |
+
# ---------------------------------------------------------
|
| 46 |
+
@tool
|
| 47 |
+
def analyze_ai_technology(topic: str) -> str:
|
| 48 |
+
"""
|
| 49 |
+
Gives a quick engineering impact analysis for common AI technologies.
|
| 50 |
+
|
| 51 |
+
Args:
|
| 52 |
+
topic: AI technology or concept such as MCP, RAG,
|
| 53 |
+
LangGraph, vector database, agents, or embeddings.
|
| 54 |
+
"""
|
| 55 |
+
|
| 56 |
+
technologies = {
|
| 57 |
+
"mcp": {
|
| 58 |
+
"name": "Model Context Protocol (MCP)",
|
| 59 |
+
"impact": 90,
|
| 60 |
+
"description":
|
| 61 |
+
"A standard protocol for connecting AI applications "
|
| 62 |
+
"to tools, APIs, services, and external data.",
|
| 63 |
+
"use_case":
|
| 64 |
+
"Let an AI agent access GitHub, databases, files, "
|
| 65 |
+
"Slack, APIs, and enterprise systems."
|
| 66 |
+
},
|
| 67 |
+
|
| 68 |
+
"rag": {
|
| 69 |
+
"name": "Retrieval-Augmented Generation (RAG)",
|
| 70 |
+
"impact": 88,
|
| 71 |
+
"description":
|
| 72 |
+
"Retrieves relevant information before asking an LLM "
|
| 73 |
+
"to generate an answer.",
|
| 74 |
+
"use_case":
|
| 75 |
+
"Enterprise search, documentation assistants, "
|
| 76 |
+
"knowledge bots, and support systems."
|
| 77 |
+
},
|
| 78 |
+
|
| 79 |
+
"langgraph": {
|
| 80 |
+
"name": "LangGraph",
|
| 81 |
+
"impact": 82,
|
| 82 |
+
"description":
|
| 83 |
+
"Framework for building stateful and multi-step "
|
| 84 |
+
"LLM workflows and agents.",
|
| 85 |
+
"use_case":
|
| 86 |
+
"Agent orchestration, approval workflows, "
|
| 87 |
+
"multi-agent systems, and long-running AI tasks."
|
| 88 |
+
},
|
| 89 |
+
|
| 90 |
+
"vector database": {
|
| 91 |
+
"name": "Vector Database",
|
| 92 |
+
"impact": 80,
|
| 93 |
+
"description":
|
| 94 |
+
"Stores embeddings and performs semantic similarity search.",
|
| 95 |
+
"use_case":
|
| 96 |
+
"RAG, recommendation systems, semantic search, "
|
| 97 |
+
"and AI memory."
|
| 98 |
+
},
|
| 99 |
+
|
| 100 |
+
"embeddings": {
|
| 101 |
+
"name": "Embeddings",
|
| 102 |
+
"impact": 78,
|
| 103 |
+
"description":
|
| 104 |
+
"Numeric representations of text, images, or other data "
|
| 105 |
+
"that capture semantic meaning.",
|
| 106 |
+
"use_case":
|
| 107 |
+
"Similarity search, clustering, recommendations, and RAG."
|
| 108 |
+
},
|
| 109 |
+
|
| 110 |
+
"agents": {
|
| 111 |
+
"name": "AI Agents",
|
| 112 |
+
"impact": 92,
|
| 113 |
+
"description":
|
| 114 |
+
"LLM-powered systems that can reason about a task "
|
| 115 |
+
"and decide which tools or actions to execute.",
|
| 116 |
+
"use_case":
|
| 117 |
+
"Coding assistants, research agents, workflow automation, "
|
| 118 |
+
"customer support, and enterprise automation."
|
| 119 |
+
}
|
| 120 |
+
}
|
| 121 |
+
|
| 122 |
+
key = topic.lower().strip()
|
| 123 |
+
|
| 124 |
+
# Handle a few common variations
|
| 125 |
+
aliases = {
|
| 126 |
+
"agent": "agents",
|
| 127 |
+
"ai agent": "agents",
|
| 128 |
+
"ai agents": "agents",
|
| 129 |
+
"model context protocol": "mcp",
|
| 130 |
+
"retrieval augmented generation": "rag",
|
| 131 |
+
"vector db": "vector database",
|
| 132 |
+
"vectors": "vector database",
|
| 133 |
+
"embedding": "embeddings"
|
| 134 |
+
}
|
| 135 |
+
|
| 136 |
+
key = aliases.get(key, key)
|
| 137 |
+
|
| 138 |
+
if key not in technologies:
|
| 139 |
+
return (
|
| 140 |
+
f"I don't have a predefined analysis for '{topic}'. "
|
| 141 |
+
"Use web search to research it instead."
|
| 142 |
+
)
|
| 143 |
|
| 144 |
+
item = technologies[key]
|
| 145 |
|
| 146 |
+
return f"""
|
| 147 |
+
Technology: {item['name']}
|
| 148 |
+
Estimated engineering impact: {item['impact']}%
|
| 149 |
+
|
| 150 |
+
What it is:
|
| 151 |
+
{item['description']}
|
| 152 |
+
|
| 153 |
+
Typical use case:
|
| 154 |
+
{item['use_case']}
|
| 155 |
+
"""
|
| 156 |
+
|
| 157 |
+
|
| 158 |
+
# ---------------------------------------------------------
|
| 159 |
+
# CUSTOM TOOL 3: Developer recommendation
|
| 160 |
+
# ---------------------------------------------------------
|
| 161 |
+
@tool
|
| 162 |
+
def recommend_ai_learning_topic(current_skill: str) -> str:
|
| 163 |
+
"""
|
| 164 |
+
Suggests an AI engineering topic to learn based on a developer's skill.
|
| 165 |
+
|
| 166 |
+
Args:
|
| 167 |
+
current_skill: Developer skill such as React, Node.js,
|
| 168 |
+
Python, backend, cloud, or fullstack.
|
| 169 |
+
"""
|
| 170 |
+
|
| 171 |
+
skill = current_skill.lower()
|
| 172 |
+
|
| 173 |
+
if "react" in skill or "frontend" in skill:
|
| 174 |
+
return (
|
| 175 |
+
"Recommended next topic: AI application development.\n"
|
| 176 |
+
"Learn LLM APIs, streaming responses, tool calling, "
|
| 177 |
+
"AI SDKs, and agent UIs."
|
| 178 |
+
)
|
| 179 |
+
|
| 180 |
+
if "node" in skill or "backend" in skill:
|
| 181 |
+
return (
|
| 182 |
+
"Recommended next topic: Agent orchestration.\n"
|
| 183 |
+
"Learn tool calling, MCP, RAG, LangGraph, queues, "
|
| 184 |
+
"and asynchronous AI workflows."
|
| 185 |
+
)
|
| 186 |
+
|
| 187 |
+
if "python" in skill:
|
| 188 |
+
return (
|
| 189 |
+
"Recommended next topic: AI agent frameworks.\n"
|
| 190 |
+
"Explore smolagents, LangGraph, PydanticAI, "
|
| 191 |
+
"RAG pipelines, and evaluation."
|
| 192 |
+
)
|
| 193 |
+
|
| 194 |
+
if "cloud" in skill or "aws" in skill:
|
| 195 |
+
return (
|
| 196 |
+
"Recommended next topic: AI platform engineering.\n"
|
| 197 |
+
"Learn model gateways, vector databases, observability, "
|
| 198 |
+
"guardrails, GPU inference, and MCP servers."
|
| 199 |
+
)
|
| 200 |
+
|
| 201 |
+
if "fullstack" in skill:
|
| 202 |
+
return (
|
| 203 |
+
"Recommended path:\n"
|
| 204 |
+
"1. LLM APIs\n"
|
| 205 |
+
"2. Structured outputs\n"
|
| 206 |
+
"3. Tool calling\n"
|
| 207 |
+
"4. RAG\n"
|
| 208 |
+
"5. MCP\n"
|
| 209 |
+
"6. LangGraph\n"
|
| 210 |
+
"7. Agent evaluation and observability"
|
| 211 |
+
)
|
| 212 |
+
|
| 213 |
+
return (
|
| 214 |
+
"Start with LLM APIs, prompt engineering, structured outputs, "
|
| 215 |
+
"tool calling, RAG, and then AI agents."
|
| 216 |
+
)
|
| 217 |
+
|
| 218 |
+
|
| 219 |
+
# ---------------------------------------------------------
|
| 220 |
+
# FINAL ANSWER TOOL
|
| 221 |
+
# ---------------------------------------------------------
|
| 222 |
final_answer = FinalAnswerTool()
|
| 223 |
|
|
|
|
|
|
|
| 224 |
|
| 225 |
+
# ---------------------------------------------------------
|
| 226 |
+
# MODEL
|
| 227 |
+
# ---------------------------------------------------------
|
| 228 |
+
model = InferenceClientModel(
|
| 229 |
+
max_tokens=2096,
|
| 230 |
+
temperature=0.5,
|
| 231 |
+
model_id="Qwen/Qwen2.5-Coder-32B-Instruct",
|
| 232 |
+
custom_role_conversions=None,
|
| 233 |
)
|
| 234 |
|
| 235 |
|
| 236 |
+
# ---------------------------------------------------------
|
| 237 |
+
# TOOL FROM HUGGING FACE HUB
|
| 238 |
+
# ---------------------------------------------------------
|
| 239 |
+
image_generation_tool = load_tool(
|
| 240 |
+
"agents-course/text-to-image",
|
| 241 |
+
trust_remote_code=True
|
| 242 |
+
)
|
| 243 |
|
| 244 |
+
|
| 245 |
+
# ---------------------------------------------------------
|
| 246 |
+
# LOAD SYSTEM PROMPTS
|
| 247 |
+
# ---------------------------------------------------------
|
| 248 |
+
with open("prompts.yaml", "r") as stream:
|
| 249 |
prompt_templates = yaml.safe_load(stream)
|
| 250 |
+
|
| 251 |
+
|
| 252 |
+
# ---------------------------------------------------------
|
| 253 |
+
# CREATE AGENT
|
| 254 |
+
# ---------------------------------------------------------
|
| 255 |
agent = CodeAgent(
|
| 256 |
model=model,
|
| 257 |
+
|
| 258 |
+
tools=[
|
| 259 |
+
# Search current information from the web
|
| 260 |
+
DuckDuckGoSearchTool(),
|
| 261 |
+
|
| 262 |
+
# Custom tools
|
| 263 |
+
get_current_time_in_timezone,
|
| 264 |
+
analyze_ai_technology,
|
| 265 |
+
recommend_ai_learning_topic,
|
| 266 |
+
|
| 267 |
+
# Hugging Face Hub tool
|
| 268 |
+
image_generation_tool,
|
| 269 |
+
|
| 270 |
+
# Required final-answer tool
|
| 271 |
+
final_answer,
|
| 272 |
+
],
|
| 273 |
+
|
| 274 |
+
max_steps=8,
|
| 275 |
verbosity_level=1,
|
| 276 |
grammar=None,
|
| 277 |
planning_interval=None,
|
| 278 |
+
name="AI Developer Assistant",
|
| 279 |
+
|
| 280 |
+
description=(
|
| 281 |
+
"An AI engineering assistant that can search the web, "
|
| 282 |
+
"analyze AI technologies, recommend learning topics, "
|
| 283 |
+
"check world times, and generate images."
|
| 284 |
+
),
|
| 285 |
+
|
| 286 |
prompt_templates=prompt_templates
|
| 287 |
)
|
| 288 |
|
| 289 |
|
| 290 |
+
# ---------------------------------------------------------
|
| 291 |
+
# START GRADIO UI
|
| 292 |
+
# ---------------------------------------------------------
|
| 293 |
GradioUI(agent).launch()
|