Update app.py
Browse filesVersion-safe model import (handles smolagents < 1.9 and >= 1.14)
1234566 unmodified lines13141516171819202122232425262728293031101 unmodified lines1331341351361371381398 unmodified lines14814915015115215315418 unmodified lines1731741751761771781794 unmodified lines1841851861871881891901914 unmodified lines19619719819920020120237 unmodified lines2402412422432442452462472482492505 unmodified lines256257258259260261262263from smolagents import (CodeAgent,HfApiModel,load_tool,tool,)6 unmodified linesfrom Gradio_UI import GradioUI# ---------------------------------------------------------------------------# Search tool: try Bing first (via WebSearchTool), fallback to DuckDuckGo# ---------------------------------------------------------------------------try:from smolagents import WebSearchToolsearch_tool = WebSearchTool(engine="bing")print("Using Bing search engine via WebSearchTool")except ImportError:# smolagents < 1.16.0 does not have WebSearchToolfrom smolagents import DuckDuckGoSearchToolsearch_tool = DuckDuckGoSearchTool()print("Using DuckDuckGo search engine (upgrade smolagents for Bing)")# ---------------------------------------------------------------------------# Custom tool 1: Real-time weather (Open-Meteo — free, no API key required)101 unmodified lines# ---------------------------------------------------------------------------# Custom tool 4: Browse a webpage (Chrome‑mimicking headers)# ---------------------------------------------------------------------------@tooldef browse_webpage(url: str) -> str:8 unmodified lines"""MAX_CHARS = 5000# Full Chrome‑like header set — this is what actually gets past many bot checksheaders = {"User-Agent": ("Mozilla/5.0 (Windows NT 10.0; Win64; x64) "18 unmodified linesif not url.startswith(("http://", "https://")):url = "https://" + url# --- Network request ---------------------------------------------------try:resp = requests.get(url, headers=headers, timeout=20, allow_redirects=True)except requests.exceptions.Timeout:4 unmodified linesif resp.status_code != 200:return (f"Error: '{url}' returned HTTP {resp.status_code}. "f"The site may be blocking automated access — try a different URL "f"or use a search result that does not require a login.")content_type = resp.headers.get("Content-Type", "").lower()4 unmodified linestext = text[:MAX_CHARS] + "\n\n[...truncated...]"return f"Source: {url}\n\n{text}"# --- Parse HTML --------------------------------------------------------try:soup = BeautifulSoup(resp.text, "html.parser")except Exception as e:37 unmodified linesfinal_answer = FinalAnswerTool()model = HfApiModel(max_tokens=2096,temperature=0.5,model_id='Qwen/Qwen2.5-Coder-32B-Instruct',custom_role_conversions=None,)image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)5 unmodified linesmodel=model,tools=[final_answer,search_tool, # 🔍 Bing (or DuckDuckGo fallback)browse_webpage, # 🌐 read a result URLimage_generation_tool,get_current_time_in_timezone,get_weather,12345678910111213146 unmodified lines212223242526101 unmodified lines1281291301311321331348 unmodified lines14314414514614714818 unmodified lines1671681691701711724 unmodified lines1771781791801811821834 unmodified lines18818919019119219337 unmodified lines2312322332342352362372382392402412422435 unmodified lines249250251252253254255256# ---------------------------------------------------------------------------# Version-safe model import (handles smolagents < 1.9 and >= 1.14)# ---------------------------------------------------------------------------try:from smolagents import InferenceClientModel as HFModelexcept ImportError:from smolagents import HfApiModel as HFModelfrom smolagents import (CodeAgent,DuckDuckGoSearchTool,load_tool,tool,)6 unmodified linesfrom Gradio_UI import GradioUI# ---------------------------------------------------------------------------# Custom tool 1: Real-time weather (Open-Meteo — free, no API key required)101 unmodified lines# ---------------------------------------------------------------------------# Custom tool 4: Browse a webpage (Chrome-mimicking headers)# ---------------------------------------------------------------------------@tooldef browse_webpage(url: str) -> str:8 unmodified lines"""MAX_CHARS = 5000headers = {"User-Agent": ("Mozilla/5.0 (Windows NT 10.0; Win64; x64) "18 unmodified linesif not url.startswith(("http://", "https://")):url = "https://" + urltry:resp = requests.get(url, headers=headers, timeout=20, allow_redirects=True)except requests.exceptions.Timeout:4 unmodified linesif resp.status_code != 200:return (f"Error: '{url}' returned HTTP {resp.status_code}. "f"The site may be blocking automated access — try a different URL.")content_type = resp.headers.get("Content-Type", "").lower()4 unmodified linestext = text[:MAX_CHARS] + "\n\n[...truncated...]"return f"Source: {url}\n\n{text}"try:soup = BeautifulSoup(resp.text, "html.parser")except Exception as e:37 unmodified linesfinal_answer = FinalAnswerTool()# ---------------------------------------------------------------------------# Model — uses InferenceClientModel on new smolagents, HfApiModel on old# ---------------------------------------------------------------------------model = HFModel(model_id="Qwen/Qwen2.5-Coder-32B-Instruct",max_tokens=2096,temperature=0.5,)image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)5 unmodified linesmodel=model,tools=[final_answer,DuckDuckGoSearchTool(),browse_webpage,image_generation_tool,get_current_time_in_timezone,get_weather,
app.py+18-25