[ad_1]
![Create an AI chatbot in 5 minutes with a huggable face and Gradio](https://www.kdnuggets.com/wp-content/uploads/awan_build_ai_chatbot_5_minutes_hugging_face_gradio_7.png)
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.
- Go hf.co and create a free account. After that press your Display image in the upper right corner and select “New Space”.
- 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](https://www.kdnuggets.com/wp-content/uploads/awan_build_ai_chatbot_5_minutes_hugging_face_gradio_6.png)
A picture from space
- Click the “Create space” button to initialize the application.
- 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](https://www.kdnuggets.com/wp-content/uploads/awan_build_ai_chatbot_5_minutes_hugging_face_gradio_8.png)
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](https://www.kdnuggets.com/wp-content/uploads/awan_build_ai_chatbot_5_minutes_hugging_face_gradio_1.png)
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](https://www.kdnuggets.com/wp-content/uploads/awan_build_ai_chatbot_5_minutes_hugging_face_gradio_2.png)
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](https://www.kdnuggets.com/wp-content/uploads/awan_build_ai_chatbot_5_minutes_hugging_face_gradio_9.png)
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](https://www.kdnuggets.com/wp-content/uploads/awan_build_ai_chatbot_5_minutes_hugging_face_gradio_3.png)
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](https://www.kdnuggets.com/wp-content/uploads/awan_build_ai_chatbot_5_minutes_hugging_face_gradio_5.png)
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](https://www.kdnuggets.com/wp-content/uploads/awan_build_ai_chatbot_5_minutes_hugging_face_gradio_4.png)
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