HomeArtificial IntelligenceConstructing a Versatile Multi‑Device AI Agent Utilizing Light-weight Hugging Face Fashions

Constructing a Versatile Multi‑Device AI Agent Utilizing Light-weight Hugging Face Fashions


On this tutorial, we start by establishing a compact but succesful AI agent that runs easily, leveraging Hugging Face transformers. We combine dialog era, query‑answering, sentiment evaluation, net search stubs, climate look‑ups, and a secure calculator right into a single Python class. As we progress, we set up solely the important libraries, load light-weight fashions that respect Colab’s reminiscence limits, and wrap every functionality inside tidy, reusable strategies. Collectively, we discover how each element, from intent detection to device-aware mannequin loading, suits right into a coherent workflow, empowering us to prototype subtle, multi-tool brokers.

!pip set up transformers torch speed up 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 start by putting in the important thing Python libraries, Transformers, Torch, Speed up, Datasets, Requests, and BeautifulSoup, so our Colab surroundings has every part it wants for mannequin loading, inference, and net scraping. Subsequent, we import PyTorch, JSON utilities, HTTP and date helpers, Hugging Face courses for era, classification, and QA, in addition to BeautifulSoup for HTML parsing, whereas silencing pointless warnings to maintain the pocket book output clear.

class AdvancedAIAgent:
   def __init__(self):
       """Initialize the AI Agent with a number of fashions and capabilities"""
       self.machine = "cuda" if torch.cuda.is_available() else "cpu"
       print(f"🚀 Initializing AI Agent on {self.machine}")
      
       self._load_models()
      
       self.instruments = {
           "web_search": self.web_search,
           "calculator": self.calculator,
           "climate": self.get_weather,
           "sentiment": self.analyze_sentiment
       }
      
       print("✅ AI Agent initialized efficiently!")


   def _load_models(self):
       """Load all required fashions"""
       print("📥 Loading fashions...")
      
       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",
           mannequin="cardiffnlp/twitter-roberta-base-sentiment-latest",
           machine=0 if self.machine == "cuda" else -1
       )
      
       self.qa_pipeline = pipeline(
           "question-answering",
           mannequin="distilbert-base-cased-distilled-squad",
           machine=0 if self.machine == "cuda" else -1
       )
      
       print("✅ All fashions loaded!")


   def generate_response(self, immediate, max_length=100, temperature=0.7):
       """Generate textual content response utilizing the language mannequin"""
       inputs = self.gen_tokenizer.encode(immediate + 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, textual content):
       """Analyze sentiment of given textual content"""
       outcome = self.sentiment_pipeline(textual content)[0]
       return {
           "sentiment": outcome['label'],
           "confidence": spherical(outcome['score'], 4),
           "textual content": textual content
       }


   def answer_question(self, query, context):
       """Reply questions based mostly on given context"""
       outcome = self.qa_pipeline(query=query, context=context)
       return {
           "reply": outcome['answer'],
           "confidence": spherical(outcome['score'], 4),
           "query": query
       }


   def web_search(self, question):
       """Simulate net search (change with precise API if wanted)"""
       strive:
           return {
               "question": question,
               "outcomes": f"Search outcomes for '{question}': Newest info retrieved efficiently.",
               "timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S")
           }
       besides Exception as e:
           return {"error": f"Search failed: {str(e)}"}


   def calculator(self, expression):
       """Secure calculator perform"""
       strive:
           allowed_chars = set('0123456789+-*/.() ')
           if not all(c in allowed_chars for c in expression):
               return {"error": "Invalid characters in expression"}
          
           outcome = eval(expression)
           return {
               "expression": expression,
               "outcome": outcome,
               "kind": kind(outcome).__name__
           }
       besides Exception as e:
           return {"error": f"Calculation failed: {str(e)}"}


   def get_weather(self, location):
       """Mock climate perform (change with precise climate API)"""
       return {
           "location": location,
           "temperature": "22°C",
           "situation": "Partly cloudy",
           "humidity": "65%",
           "notice": "That is mock knowledge. Combine with an actual climate API for precise knowledge."
       }


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


   def process_request(self, user_input, context=""):
       """Primary technique to course of person requests"""
       print(f"🤖 Processing: {user_input}")
      
       intent = self.detect_intent(user_input)
       response = {"intent": intent, "enter": user_input}
      
       strive:
           if intent == 'calculator':
               import re
               expr = re.findall(r'[0-9+-*/.() ]+', user_input)
               if expr:
                   outcome = self.calculator(expr[0].strip())
                   response.replace(outcome)
               else:
                   response["error"] = "No legitimate mathematical expression discovered"
          
           elif intent == 'climate':
               phrases = user_input.break up()
               location = "your location" 
               for i, phrase in enumerate(phrases):
                   if phrase.decrease() in ['in', 'at', 'for']:
                       if i + 1 

We encapsulate our complete toolkit inside an AdvancedAIAgent class that boots on GPU when obtainable, hundreds dialogue, sentiment, and QA fashions, and registers helper instruments for search, climate, and arithmetic. With light-weight keyword-based intent detection, we dynamically route every person message to the suitable pipeline or fall again to free-form era, offering a unified, multi-skill agent pushed by only a few clear strategies.

if __name__ == "__main__":
   agent = AdvancedAIAgent()
  
   print("n" + "="*50)
   print("🎯 DEMO: Superior 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 take a look at in test_cases:
       print(f"n👤 Consumer: {take a look at}")
       outcome = agent.process_request(take a look at)
       print(f"🤖 Agent: {json.dumps(outcome, indent=2)}")
  
   """
   print("n🎮 Interactive Mode - Sort 'stop' to exit")
   whereas True:
       user_input = enter("n👤 You: ")
       if user_input.decrease() == 'stop':
           break
      
       outcome = agent.process_request(user_input)
       print(f"🤖 Agent: {json.dumps(outcome, indent=2)}")
   """

We conclude by spawning the AdvancedAIAgent, asserting a fast demo part, and firing 5 consultant prompts that take a look at calculation, climate, search, sentiment, and open‑ended chat in a single sweep. After reviewing the neatly formatted JSON replies, we hold an non-compulsory interactive loop on standby, prepared for dwell experimentation each time we determine to un‑remark it.

In conclusion, we take a look at a wide range of real-world prompts and observe the way it handles arithmetic, fetches mock climate knowledge, gauges sentiment, and engages in pure dialog, all via a single unified interface utilizing Hugging Face fashions. This train demonstrates how we will sew a number of NLP duties into an extensible framework that is still pleasant to Colab assets.


Try the Codes. All credit score for this analysis goes to the researchers of this undertaking.


Asif Razzaq is the CEO of Marktechpost Media Inc.. As a visionary entrepreneur and engineer, Asif is dedicated to harnessing the potential of Synthetic Intelligence for social good. His most up-to-date endeavor is the launch of an Synthetic Intelligence Media Platform, Marktechpost, which stands out for its in-depth protection of machine studying and deep studying information that’s each technically sound and simply comprehensible by a large viewers. The platform boasts of over 2 million month-to-month views, illustrating its recognition amongst audiences.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments