The AI Book
    Facebook Twitter Instagram
    The AI BookThe AI Book
    • Home
    • Categories
      • AI Media Processing
      • AI Language processing (NLP)
      • AI Marketing
      • AI Business Applications
    • Guides
    • Contact
    Subscribe
    Facebook Twitter Instagram
    The AI Book
    AI Business Applications

    Create an AI chatbot in 5 minutes with a huggable face and Gradio

    1 July 2023No Comments4 Mins Read

    [ad_1]

    Create an AI chatbot in 5 minutes with a huggable face and Gradio
    Image by author

    This short tutorial will build a simple chatbot using the Microsoft DialoGPT model, Hugging Face Space and Gradio intervention. Using similar techniques, you will be able to create and customize your own application in 5 minutes.

    1. Go hf.co and create a free account. After that press your Display image in the upper right corner and select “New Space”.
    2. Fill in the form with the app name, license, space hardware and visibility.

    Create an AI chatbot in 5 minutes with a huggable face and Gradio
    A picture from space

    1. Click the “Create space” button to initialize the application.
    2. You can clone the repository and install files from your local system or create and edit files on Hugging Face using a browser.

    Create an AI chatbot in 5 minutes with a huggable face and Gradio
    Image from AI ChatBot

    Click on the “Files” tab. > + Add file > Create a new file.

    Create an AI chatbot in 5 minutes with a huggable face and Gradio
    Image from kingabzpro/AI-ChatBot

    Create a Gradio interface. You can copy my code.

    Create an AI chatbot in 5 minutes with a huggable face and Gradio
    Image from app.py

    I loaded the “microsoft/DialoGPT-large” tokenizer and model and created a `prediction’ function to get the response and generate the history.

    from transformers import AutoModelForCausalLM, AutoTokenizer
    import gradio as gr
    import torch
    
    
    title = "🤖AI ChatBot"
    description = "A State-of-the-Art Large-scale Pretrained Response generation model (DialoGPT)"
    examples = [["How are you?"]]
    
    
    tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-large")
    model = AutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-large")
    
    
    def predict(input, history=[]):
        # tokenize the new input sentence
        new_user_input_ids = tokenizer.encode(
            input + tokenizer.eos_token, return_tensors="pt"
        )
    
        # append the new user input tokens to the chat history
        bot_input_ids = torch.cat([torch.LongTensor(history), new_user_input_ids], dim=-1)
    
        # generate a response
        history = model.generate(
            bot_input_ids, max_length=4000, pad_token_id=tokenizer.eos_token_id
        ).tolist()
    
        # convert the tokens to text, and then split the responses into lines
        response = tokenizer.decode(history[0]).split("<|endoftext|>")
        # print('decoded_response-->>'+str(response))
        response = [
            (response[i], response[i + 1]) for i in range(0, len(response) - 1, 2)
        ]  # convert to tuples of list
        # print('response-->>'+str(response))
        return response, history
    
    
    gr.Interface(
        fn=predict,
        title=title,
        description=description,
        examples=examples,
        inputs=["text", "state"],
        outputs=["chatbot", "state"],
        theme="finlaymacklon/boxy_violet",
    ).launch()

    Moreover, I have provided my application with a custom theme: boxy_violet. You can browse Gradio Theme Gallery to choose a theme according to your taste.

    Now we need to create a `requirement.txt` file and add the required Python packages.

    Create an AI chatbot in 5 minutes with a huggable face and Gradio
    Image from requests.txt

    After that, your application will start building and in a few minutes it will download the model and load the model conclusion.

    Create an AI chatbot in 5 minutes with a huggable face and Gradio

    The Gradio app looks amazing. We just need to create a `prediction’ function for all the different model architects to get the answers and keep the history.

    Now you can chat and interact with the app on kingabzpro/AI-ChatBot or embed your app on your website using https://kingabzpro-ai-chatbot.hf.space.

    Create an AI Chatbot in 5 Minutes with Hug Face and Gradio
    Image from kingabzpro/AI-ChatBot

    Are you still confused? Search hundreds of chatbot apps on Spaces to get inspired and learn how to model.

    For example, if you have a mode that is perfectly set to “LLaMA-7B”. Search for model and scroll down to see different implementations of the model.

    Create an AI chatbot in 5 minutes with a huggable face and Gradio
    Image from decapoda-research/llama-7b-hf

    In conclusion, this blog provides a quick and easy tutorial on how to create an AI chatbot using Hugging Face and Gradio in just 5 minutes. With step-by-step instructions and customizable options, anyone can easily create their own chatbot.

    It was fun and I hope you learned something. Please share your Gradio demo in the comments section. If you’re looking for an even simpler solution, check out OpenChat: a free and easy platform to build custom chatbots in minutes.

    Abid Ali Awan (@1abidaliawan) is a certified data scientist professional who enjoys building machine learning models. Currently, he focuses on content creation and writes technical blogs on machine learning and data science technologies. Abid holds a Master’s degree in Technology Management and a Bachelor’s degree in Telecommunications Engineering. His vision is to create an AI product using a graph neural network for students struggling with mental illness.



    [ad_2]

    Source link

    Previous Article‘AI Will Save the World’ Says Marc Andreessen
    Next Article Generating 3D Molecular Conformers Using Equivariant Coarse-Grained and Aggregate Attention – Berkeley Artificial Intelligence Research Blog
    The AI Book

    Related Posts

    AI Business Applications

    An in-depth guide to the Meta LLaMa language model and LlaMa 2

    25 July 2023
    AI Business Applications

    Large language models for multilingual AI-driven virtual assistants

    23 July 2023
    AI Business Applications

    How SAS can help catapult practitioners’ careers

    22 July 2023
    Add A Comment

    Leave A Reply Cancel Reply

    • Privacy Policy
    • Terms and Conditions
    • About Us
    • Contact Form
    © 2025 The AI Book.

    Type above and press Enter to search. Press Esc to cancel.