Session 3 – Building AI Applications with Hugging Face

Objective

In this session, we’ll learn to build an AI application using Hugging Face tools, focusing on creating a chatbot. You’ll also gain insights into the differences between local and cloud-based LLM (Large Language Model) solutions, helping you make informed decisions for your projects. ????️????


Part 1: Understanding Chatbots

What is a Chatbot?

A chatbot is a software application designed to simulate human-like conversations. It can:

  • Answer questions
  • Provide recommendations
  • Automate repetitive tasks

Real-World Examples

  1. Customer Support: Companies use chatbots to handle FAQs.
  2. Travel Assistants: Chatbots can suggest flights, hotels, or itineraries.
  3. Healthcare: Assist patients by answering common medical questions.

How Do Chatbots Work?

Chatbots use pre-trained AI models to:

  1. Understand Input: Interpret the user’s query.
  2. Generate Responses: Create meaningful replies based on context.

Let’s now build our own chatbot! ????️


Part 2: Coding the Chatbot

We’ll use Hugging Face’s transformers library, which offers pre-trained models that make building chatbots easy.

Step 1: Install Required Libraries

First, ensure you have the Hugging Face transformers library installed. Run the following command:

!pip install transformers
Requirement already satisfied: transformers in /usr/local/lib/python3.11/dist-packages (4.47.1)
Requirement already satisfied: filelock in /usr/local/lib/python3.11/dist-packages (from transformers) (3.17.0)
Requirement already satisfied: huggingface-hub<1.0,>=0.24.0 in /usr/local/lib/python3.11/dist-packages (from transformers) (0.27.1)
Requirement already satisfied: numpy>=1.17 in /usr/local/lib/python3.11/dist-packages (from transformers) (1.26.4)
Requirement already satisfied: packaging>=20.0 in /usr/local/lib/python3.11/dist-packages (from transformers) (24.2)
Requirement already satisfied: pyyaml>=5.1 in /usr/local/lib/python3.11/dist-packages (from transformers) (6.0.2)
Requirement already satisfied: regex!=2019.12.17 in /usr/local/lib/python3.11/dist-packages (from transformers) (2024.11.6)
Requirement already satisfied: requests in /usr/local/lib/python3.11/dist-packages (from transformers) (2.32.3)
Requirement already satisfied: tokenizers<0.22,>=0.21 in /usr/local/lib/python3.11/dist-packages (from transformers) (0.21.0)
Requirement already satisfied: safetensors>=0.4.1 in /usr/local/lib/python3.11/dist-packages (from transformers) (0.5.2)
Requirement already satisfied: tqdm>=4.27 in /usr/local/lib/python3.11/dist-packages (from transformers) (4.67.1)
Requirement already satisfied: fsspec>=2023.5.0 in /usr/local/lib/python3.11/dist-packages (from huggingface-hub<1.0,>=0.24.0->transformers) (2024.10.0)
Requirement already satisfied: typing-extensions>=3.7.4.3 in /usr/local/lib/python3.11/dist-packages (from huggingface-hub<1.0,>=0.24.0->transformers) (4.12.2)
Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.11/dist-packages (from requests->transformers) (3.4.1)
Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.11/dist-packages (from requests->transformers) (3.10)
Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/local/lib/python3.11/dist-packages (from requests->transformers) (2.3.0)
Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.11/dist-packages (from requests->transformers) (2024.12.14)

Step 2: Write the Chatbot Code

Below is the Python code for a simple chatbot.

Code: AI-Powered Chatbot

from transformers import pipeline

# Load a pre-trained conversational model
chatbot = pipeline("conversational", model="microsoft/DialoGPT-medium")

# Start a conversation loop
print("Chatbot: Hi! I’m your assistant. Ask me anything or type 'exit' to quit.")

while True:
    # Take user input
    user_input = input("You: ")

    # Check for exit condition
    if user_input.lower() in ["exit", "quit"]:
        print("Chatbot: Goodbye! Have a great day! ????")
        break

    # Generate a response
    conversation = chatbot(user_input)
    print("Chatbot:", conversation[0]['generated_text'])

config.json: 100%
 642/642 [00:00<00:00, 22.2kB/s]
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-191-3727cc9669e1> in <cell line: 0>()
      2 
      3 # Load a pre-trained conversational model
----> 4 chatbot = pipeline("conversational", model="microsoft/DialoGPT-medium")
      5 
      6 # Start a conversation loop

2 frames
/usr/local/lib/python3.11/dist-packages/transformers/pipelines/__init__.py in pipeline(task, model, config, tokenizer, feature_extractor, image_processor, processor, framework, revision, use_fast, token, device, device_map, torch_dtype, trust_remote_code, model_kwargs, pipeline_class, **kwargs)
    891             )
    892     else:
--> 893         normalized_task, targeted_task, task_options = check_task(task)
    894         if pipeline_class is None:
    895             pipeline_class = targeted_task["impl"]

/usr/local/lib/python3.11/dist-packages/transformers/pipelines/__init__.py in check_task(task)
    546 
    547     """
--> 548     return PIPELINE_REGISTRY.check_task(task)
    549 
    550 

/usr/local/lib/python3.11/dist-packages/transformers/pipelines/base.py in check_task(self, task)
   1381             raise KeyError(f"Invalid translation task {task}, use 'translation_XX_to_YY' format")
   1382 
-> 1383         raise KeyError(
   1384             f"Unknown task {task}, available tasks are {self.get_supported_tasks() + ['translation_XX_to_YY']}"
   1385         )

KeyError: "Unknown task conversational, available tasks are ['audio-classification', 'automatic-speech-recognition', 'depth-estimation', 'document-question-answering', 'feature-extraction', 'fill-mask', 'image-classification', 'image-feature-extraction', 'image-segmentation', 'image-text-to-text', 'image-to-image', 'image-to-text', 'mask-generation', 'ner', 'object-detection', 'question-answering', 'sentiment-analysis', 'summarization', 'table-question-answering', 'text-classification', 'text-generation', 'text-to-audio', 'text-to-speech', 'text2text-generation', 'token-classification', 'translation', 'video-classification', 'visual-question-answering', 'vqa', 'zero-shot-audio-classification', 'zero-shot-classification', 'zero-shot-image-classification', 'zero-shot-object-detection', 'translation_XX_to_YY']"

Step 3: Run the Code

  1. Copy and paste the code into your Python IDE.
  2. Start the script and interact with your chatbot.
  3. Try prompts like:
    • “Tell me about the weather.”
    • “Suggest a good book for beginners in AI.”

Step 4: Enhance the Chatbot

Add Context Awareness

To maintain context, use Hugging Face’s Conversational class.

from transformers import pipeline, ConversationalPipeline

# Load model
chatbot = pipeline("conversational", model="microsoft/DialoGPT-medium")

# Create a conversation object
from transformers import Conversation

conversation = Conversation("Hello! I need a travel suggestion.")
chatbot([conversation])

# Continue the conversation
conversation.add_user_input("What are some destinations in Europe?")
chatbot([conversation])

print(conversation.generated_responses[-1])

Testing the Enhanced Chatbot

Ask multiple questions to see how the chatbot maintains context:

  • “What’s a good place to visit in Europe?”
  • “What’s the best time to visit there?”

Part 3: Local vs. Cloud-Based LLM Solutions

Understanding Deployment Options

When implementing chatbots, you can either:

  1. Run the model locally on your machine.
  2. Use a cloud service like AWS, Azure, or Google Cloud.

Local LLM Solutions

Pros:

  1. Data Privacy: Your data stays on your hardware.
  2. Cost Efficiency: No recurring cloud fees.
  3. Customization: Full control over model fine-tuning.

Cons:

  1. Hardware Requirements: Requires a powerful setup (e.g., GPUs).
  2. Setup Complexity: May need configuration and maintenance.

Cloud-Based LLM Solutions

Pros:

  1. Ease of Use: Simple APIs, no setup required.
  2. Scalability: Handles large-scale traffic efficiently.
  3. Access to Latest Models: Always up-to-date with the latest advancements.

Cons:

  1. Cost: Pay-per-use can become expensive for high traffic.
  2. Data Privacy: Requires sending data to third-party servers.

When to Choose Which?

FeatureLocalCloud-Based
Setup TimeModerate to high effortMinimal effort
CostUpfront (hardware)Ongoing (usage-based)
ScalabilityLimited by hardwareVirtually unlimited
CustomizationHighModerate
Data PrivacyStrongRelies on provider policies

Part 4: Discussion

  1. What are your chatbot’s primary requirements?
    • If privacy is critical, choose a local solution.
    • If quick scalability is important, opt for a cloud-based solution.
  2. Can a Hybrid Approach Work?
    • Use local models for sensitive tasks and cloud solutions for large-scale operations.

Part 5: Wrap-Up & Reflection

Key Takeaways:

  1. Chatbot Development: Built a functional chatbot using Hugging Face.
  2. Local vs. Cloud Deployment: Compared benefits and challenges of both approaches.
  3. Hands-On Practice: Enhanced chatbot with context awareness.

Activity: Build Your Chatbot

  1. Add a feature where the chatbot remembers user preferences (e.g., travel destinations).
  2. Discuss in groups:
    • What improvements would you make to your chatbot?
    • Which deployment approach would you use for your application?