0

Build a multi-function multi-tool AI agent with a lightweight hug facial model

In this tutorial, we first set up a compact and capable AI agent that runs smoothly, leveraging the hugging face-shaped transformer. We integrate dialog generation, problem analysis, web search stubs, weather appearance and security calculator into a single Python class. As we progress, we only install the base library, load lightweight models that respect Colab memory limitations, and wrap each feature in a collation of reusable methods. Together, we explore how each component loaded from intent detection to device-aware model is suitable for a coherent workflow, allowing us to use prototype-complicated 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 first install the key python library, transformers, torches, acceleration, datasets, requests and beautiful suites, so our COLAB environment has everything you need for model loading, inference and network scratching. Next, we import Pytorch, JSON utilities, HTTP and date helpers, embrace facial courses for generation, classification and quality checks, and a beautiful suite for HTML parsing while silencing unnecessary warnings to keep laptop output clean.

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 

We encapsulate the entire toolkit in a high-level category that starts on the GPU when available, loads conversations, sentiment and quality check models, and registers helper hand tools for search, weather and arithmetic. With lightweight keyword-based intent detection, we dynamically route each user message to the right pipe or lag behind freeform generation, providing a unified multi-skill proxy driven by only a few clean approaches.

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)}")
   """

Finally, we announced the quick demo section by spawning, and sent out five representative tips to calculate, weather, search, emotion and open chat in a scan test. After reviewing the neat JSON reply, we kept the optional interactive loop in standby state, and we could perform real-time experiments at any time whenever we decided not to comply.

In short, we tested various real-world tips and observed how they handled arithmetic, obtained simulated weather data, instrumented emotions and natural dialogue, and conducted natural dialogue using embracing facial models through a single unified interface. This exercise shows how we can stitch multiple NLP tasks into a scalable framework for Colab resources friendly.


Check Code. All credits for this study are to the researchers on the project.


Asif Razzaq is CEO of Marktechpost Media Inc. As a visionary entrepreneur and engineer, ASIF is committed to harnessing the potential of artificial intelligence to achieve social benefits. His recent effort is to launch Marktechpost, an artificial intelligence media platform that has an in-depth coverage of machine learning and deep learning news that can sound both technically, both through technical voices and be understood by a wide audience. The platform has over 2 million views per month, demonstrating its popularity among its audience.