• bitcoinBitcoin(BTC)$84,581.00-1.87%
  • ethereumEthereum(ETH)$2,692.13-2.34%
  • tetherTether(USDT)$1.00-0.01%
  • binancecoinBNB(BNB)$768.18-2.40%
  • rippleXRP(XRP)$1.50-4.57%
  • usd-coinUSDC(USDC)$1.000.00%
  • solanaSolana(SOL)$115.17-2.87%
  • tronTRON(TRX)$0.341269-0.17%
  • zcashZcash(ZEC)$1,501.45-2.76%
  • Figure HelocFigure Heloc(FIGR_HELOC)$1.03-0.88%
  • HyperliquidHyperliquid(HYPE)$93.98-2.99%
  • dogecoinDogecoin(DOGE)$0.092796-7.80%
  • moneroMonero(XMR)$551.03-2.80%
  • whitebitWhiteBIT Coin(WBT)$84.97-1.98%
  • USDSUSDS(USDS)$1.00-0.01%
  • chainlinkChainlink(LINK)$12.37-5.13%
  • cardanoCardano(ADA)$0.238987-5.91%
  • RainRain(RAIN)$0.012280-6.28%
  • leo-tokenLEO Token(LEO)$9.010.37%
  • stellarStellar(XLM)$0.202399-6.74%
  • bitcoin-cashBitcoin Cash(BCH)$338.750.07%
  • uniswapUniswap(UNI)$9.26-9.02%
  • nearNEAR Protocol(NEAR)$4.34-1.06%
  • Ethena USDeEthena USDe(USDE)$1.00-0.01%
  • litecoinLitecoin(LTC)$61.51-2.77%
  • daiDai(DAI)$1.00-0.01%
  • avalanche-2Avalanche(AVAX)$10.32-8.07%
  • USD1USD1(USD1)$1.000.00%
  • CantonCanton(CC)$0.109802-4.24%
  • the-open-networkGram (prev. Toncoin)(GRAM)$1.42-2.98%
  • hedera-hashgraphHedera(HBAR)$0.090353-9.04%
  • suiSui(SUI)$0.96-6.00%
  • shiba-inuShiba Inu(SHIB)$0.000006-7.61%
  • BittensorBittensor(TAO)$287.22-8.32%
  • Global DollarGlobal Dollar(USDG)$1.00-0.01%
  • crypto-com-chainCronos(CRO)$0.061283-8.31%
  • BitwayBitway(BTW)$1.0313.19%
  • MemeCoreMemeCore(M)$1.22-7.09%
  • paypal-usdPayPal USD(PYUSD)$1.00-0.01%
  • tether-goldTether Gold(XAUT)$4,290.73-1.58%
  • okbOKB(OKB)$118.63-3.44%
  • Circle USYCCircle USYC(USYC)$1.140.01%
  • Ripple USDRipple USD(RLUSD)$1.000.00%
  • BlackRock USD Institutional Digital Liquidity FundBlackRock USD Institutional Digital Liquidity Fund(BUIDL)$1.000.00%
  • Ondo US Dollar YieldOndo US Dollar Yield(USDY)$1.15-0.03%
  • mantleMantle(MNT)$0.65-2.16%
  • aaveAave(AAVE)$138.95-5.44%
  • EthenaEthena(ENA)$0.205625-4.93%
  • OndoOndo(ONDO)$0.414499-5.53%
  • AsterAster(ASTER)$0.69-5.32%
TradePoint.io
  • Main
  • AI & Technology
  • Stock Charts
  • Market & News
  • Business
  • Finance Tips
  • Trade Tube
  • Blog
  • Shop
No Result
View All Result
TradePoint.io
No Result
View All Result

Building a Versatile Multi‑Tool AI Agent Using Lightweight Hugging Face Models

July 22, 2025
in AI & Technology
Reading Time: 7 mins read
A A
Building a Versatile Multi‑Tool AI Agent Using Lightweight Hugging Face Models
ShareShareShareShareShare

In this tutorial, we begin by setting up a compact yet capable AI agent that runs smoothly, leveraging Hugging Face transformers. We integrate dialog generation, question‑answering, sentiment analysis, web search stubs, weather look‑ups, and a safe calculator into a single Python class. As we progress, we install only the essential libraries, load lightweight models that respect Colab’s memory limits, and wrap each capability inside tidy, reusable methods. Together, we explore how every component, from intent detection to device-aware model loading, fits into a coherent workflow, empowering us to prototype sophisticated, multi-tool agents.

!pip install transformers torch accelerate datasets requests beautifulsoup4


import torch
import json
import requests
from datetime import datetime
from transformers import (
   AutoTokenizer, AutoModelForCausalLM, AutoModelForSequenceClassification,
   AutoModelForQuestionAnswering, pipeline
)
from bs4 import BeautifulSoup
import warnings
warnings.filterwarnings('ignore')

We begin by installing the key Python libraries, Transformers, Torch, Accelerate, Datasets, Requests, and BeautifulSoup, so our Colab environment has everything it needs for model loading, inference, and web scraping. Next, we import PyTorch, JSON utilities, HTTP and date helpers, Hugging Face classes for generation, classification, and QA, as well as BeautifulSoup for HTML parsing, while silencing unnecessary warnings to keep the notebook output clean.

YOU MAY ALSO LIKE

Microsoft’s New Surface Pro 12 And Surface Laptop 13 Feature Snapdragon X2 Plus Chips

Google Releases Gemini 3.8 Flash TTS and Flash-Lite TTS With Prompt-Based Voice Design

class AdvancedAIAgent:
   def __init__(self):
       """Initialize the AI Agent with multiple models and capabilities"""
       self.device = "cuda" if torch.cuda.is_available() else "cpu"
       print(f"🚀 Initializing AI Agent on {self.device}")
      
       self._load_models()
      
       self.tools = {
           "web_search": self.web_search,
           "calculator": self.calculator,
           "weather": self.get_weather,
           "sentiment": self.analyze_sentiment
       }
      
       print("✅ AI Agent initialized successfully!")


   def _load_models(self):
       """Load all required models"""
       print("📥 Loading models...")
      
       self.gen_tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-medium")
       self.gen_model = AutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-medium")
       self.gen_tokenizer.pad_token = self.gen_tokenizer.eos_token
      
       self.sentiment_pipeline = pipeline(
           "sentiment-analysis",
           model="cardiffnlp/twitter-roberta-base-sentiment-latest",
           device=0 if self.device == "cuda" else -1
       )
      
       self.qa_pipeline = pipeline(
           "question-answering",
           model="distilbert-base-cased-distilled-squad",
           device=0 if self.device == "cuda" else -1
       )
      
       print("✅ All models loaded!")


   def generate_response(self, prompt, max_length=100, temperature=0.7):
       """Generate text response using the language model"""
       inputs = self.gen_tokenizer.encode(prompt + self.gen_tokenizer.eos_token,
                                        return_tensors="pt")
      
       with torch.no_grad():
           outputs = self.gen_model.generate(
               inputs,
               max_length=max_length,
               temperature=temperature,
               do_sample=True,
               pad_token_id=self.gen_tokenizer.eos_token_id,
               attention_mask=torch.ones_like(inputs)
           )
      
       response = self.gen_tokenizer.decode(outputs[0][len(inputs[0]):],
                                          skip_special_tokens=True)
       return response.strip()


   def analyze_sentiment(self, text):
       """Analyze sentiment of given text"""
       result = self.sentiment_pipeline(text)[0]
       return {
           "sentiment": result['label'],
           "confidence": round(result['score'], 4),
           "text": text
       }


   def answer_question(self, question, context):
       """Answer questions based on given context"""
       result = self.qa_pipeline(question=question, context=context)
       return {
           "answer": result['answer'],
           "confidence": round(result['score'], 4),
           "question": question
       }


   def web_search(self, query):
       """Simulate web search (replace with actual API if needed)"""
       try:
           return {
               "query": query,
               "results": f"Search results for '{query}': Latest information retrieved successfully.",
               "timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S")
           }
       except Exception as e:
           return {"error": f"Search failed: {str(e)}"}


   def calculator(self, expression):
       """Safe calculator function"""
       try:
           allowed_chars = set('0123456789+-*/.() ')
           if not all(c in allowed_chars for c in expression):
               return {"error": "Invalid characters in expression"}
          
           result = eval(expression)
           return {
               "expression": expression,
               "result": result,
               "type": type(result).__name__
           }
       except Exception as e:
           return {"error": f"Calculation failed: {str(e)}"}


   def get_weather(self, location):
       """Mock weather function (replace with actual weather API)"""
       return {
           "location": location,
           "temperature": "22°C",
           "condition": "Partly cloudy",
           "humidity": "65%",
           "note": "This is mock data. Integrate with a real weather API for actual data."
       }


   def detect_intent(self, user_input):
       """Simple intent detection based on keywords"""
       user_input = user_input.lower()
      
       if any(word in user_input for word in ['calculate', 'math', '+', '-', '*', '/']):
           return 'calculator'
       elif any(word in user_input for word in ['weather', 'temperature', 'forecast']):
           return 'weather'
       elif any(word in user_input for word in ['search', 'find', 'look up']):
           return 'web_search'
       elif any(word in user_input for word in ['sentiment', 'emotion', 'feeling']):
           return 'sentiment'
       elif '?' in user_input:
           return 'question_answering'
       else:
           return 'chat'


   def process_request(self, user_input, context=""):
       """Main method to process user requests"""
       print(f"🤖 Processing: {user_input}")
      
       intent = self.detect_intent(user_input)
       response = {"intent": intent, "input": user_input}
      
       try:
           if intent == 'calculator':
               import re
               expr = re.findall(r'[0-9+\-*/.() ]+', user_input)
               if expr:
                   result = self.calculator(expr[0].strip())
                   response.update(result)
               else:
                   response["error"] = "No valid mathematical expression found"
          
           elif intent == 'weather':
               words = user_input.split()
               location = "your location" 
               for i, word in enumerate(words):
                   if word.lower() in ['in', 'at', 'for']:
                       if i + 1 < len(words):
                           location = words[i + 1]
                           break
               result = self.get_weather(location)
               response.update(result)
          
           elif intent == 'web_search':
               query = user_input.replace('search', '').replace('find', '').strip()
               result = self.web_search(query)
               response.update(result)
          
           elif intent == 'sentiment':
               text_to_analyze = user_input.replace('sentiment', '').strip()
               if not text_to_analyze:
                   text_to_analyze = "I'm feeling great today!"
               result = self.analyze_sentiment(text_to_analyze)
               response.update(result)
          
           elif intent == 'question_answering' and context:
               result = self.answer_question(user_input, context)
               response.update(result)
          
           else:
               generated_response = self.generate_response(user_input)
               response["response"] = generated_response
               response["type"] = "generated_text"
      
       except Exception as e:
           response["error"] = f"Error processing request: {str(e)}"
      
       return response

Join the fastest growing AI Dev Newsletter read by Devs and Researchers from NVIDIA, OpenAI, DeepMind, Meta, Microsoft, JP Morgan Chase, Amgen, Aflac, Wells Fargo and 100s more…….

We encapsulate our entire toolkit inside an AdvancedAIAgent class that boots on GPU when available, loads dialogue, sentiment, and QA models, and registers helper tools for search, weather, and arithmetic. With lightweight keyword-based intent detection, we dynamically route each user message to the right pipeline or fall back to free-form generation, providing a unified, multi-skill agent driven by just a few clean methods.

if __name__ == "__main__":
   agent = AdvancedAIAgent()
  
   print("\n" + "="*50)
   print("🎯 DEMO: Advanced AI Agent Capabilities")
   print("="*50)
  
   test_cases = [
       "Calculate 25 * 4 + 10",
       "What's the weather in Tokyo?",
       "Search for latest AI developments",
       "Analyze sentiment of: I love working with AI!",
       "Hello, how are you today?"
   ]
  
   for test in test_cases:
       print(f"\n👤 User: {test}")
       result = agent.process_request(test)
       print(f"🤖 Agent: {json.dumps(result, indent=2)}")
  
   """
   print("\n🎮 Interactive Mode - Type 'quit' to exit")
   while True:
       user_input = input("\n👤 You: ")
       if user_input.lower() == 'quit':
           break
      
       result = agent.process_request(user_input)
       print(f"🤖 Agent: {json.dumps(result, indent=2)}")
   """

We conclude by spawning the AdvancedAIAgent, announcing a quick demo section, and firing five representative prompts that test calculation, weather, search, sentiment, and open‑ended chat in one sweep. After reviewing the neatly formatted JSON replies, we keep an optional interactive loop on standby, ready for live experimentation whenever we decide to un‑comment it.

In conclusion, we test a variety of real-world prompts and observe how it handles arithmetic, fetches mock weather data, gauges sentiment, and engages in natural conversation, all through a single unified interface using Hugging Face models. This exercise demonstrates how we can stitch multiple NLP tasks into an extensible framework that remains friendly to Colab resources.


Check out the Codes. All credit for this research goes to the researchers of this project.

Join the fastest growing AI Dev Newsletter read by Devs and Researchers from NVIDIA, OpenAI, DeepMind, Meta, Microsoft, JP Morgan Chase, Amgen, Aflac, Wells Fargo and 100s more…….


Asif Razzaq is the CEO of Marktechpost Media Inc.. As a visionary entrepreneur and engineer, Asif is committed to harnessing the potential of Artificial Intelligence for social good. His most recent endeavor is the launch of an Artificial Intelligence Media Platform, Marktechpost, which stands out for its in-depth coverage of machine learning and deep learning news that is both technically sound and easily understandable by a wide audience. The platform boasts of over 2 million monthly views, illustrating its popularity among audiences.

Credit: Source link

ShareTweetSendSharePin

Related Posts

Microsoft’s New Surface Pro 12 And Surface Laptop 13 Feature Snapdragon X2 Plus Chips
AI & Technology

Microsoft’s New Surface Pro 12 And Surface Laptop 13 Feature Snapdragon X2 Plus Chips

September 23, 2026
Google Releases Gemini 3.8 Flash TTS and Flash-Lite TTS With Prompt-Based Voice Design
AI & Technology

Google Releases Gemini 3.8 Flash TTS and Flash-Lite TTS With Prompt-Based Voice Design

September 23, 2026
NVIDIA Releases Nemotron 3 Diarization: A 100M-Parameter Open-Weight Model That Tracks 8 Speakers in Real Time
AI & Technology

NVIDIA Releases Nemotron 3 Diarization: A 100M-Parameter Open-Weight Model That Tracks 8 Speakers in Real Time

September 23, 2026
Disney+ And Hulu Are Getting Even More Expensive (Again)
AI & Technology

Disney+ And Hulu Are Getting Even More Expensive (Again)

September 23, 2026
Next Post
Stock Market News Today: Dow, S&P 500 Edge Higher; Bessent Sees No Reason for Powell to Leave — Live Updates – The Wall Street Journal

Stock Market News Today: Dow, S&P 500 Edge Higher; Bessent Sees No Reason for Powell to Leave — Live Updates - The Wall Street Journal

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Search

No Result
View All Result
Steve Kornacki: How Democratic incumbents survived primary challenges in Massachusetts

Steve Kornacki: How Democratic incumbents survived primary challenges in Massachusetts

September 19, 2026
Flood waters destroy bridge in Nepal

Flood waters destroy bridge in Nepal

September 23, 2026
Meet the Press NOW — August 26

Meet the Press NOW — August 26

September 23, 2026

About

Learn more

Our Services

Legal

Privacy Policy

Terms of Use

Bloggers

Learn more

Article Links

Contact

Advertise

Ask us anything

©2020- TradePoint.io - All rights reserved!

Tradepoint.io, being just a publishing and technology platform, is not a registered broker-dealer or investment adviser. So we do not provide investment advice. Rather, brokerage services are provided to clients of Tradepoint.io by independent SEC-registered broker-dealers and members of FINRA/SIPC. Every form of investing carries some risk and past performance is not a guarantee of future results. “Tradepoint.io“, “Instant Investing” and “My Trading Tools” are registered trademarks of Apperbuild, LLC.

This website is operated by Apperbuild, LLC. We have no link to any brokerage firm and we do not provide investment advice. Every information and resource we provide is solely for the education of our readers. © 2020 Apperbuild, LLC. All rights reserved.

No Result
View All Result
  • Main
  • AI & Technology
  • Stock Charts
  • Market & News
  • Business
  • Finance Tips
  • Trade Tube
  • Blog
  • Shop

© 2023 - TradePoint.io - All Rights Reserved!