LLM ChatBot with Gradio UI Tutorial - Part 2

1littlecoder · Beginner ·🧠 Large Language Models ·3y ago

Key Takeaways

This video tutorial demonstrates how to build a custom knowledge bot using LLama-Index, LangChain, and Gradio UI, leveraging Hugging Face models and GPT-Index for vector index building and querying.

Full Transcript

hey what's up codash welcome to one little coder in this tutorial we're going to learn how to build a chatbot UI using Radio application this is the second part of my first tutorial where we used Google's flan a large language model and Lang chain and GPT index or Lama index to use an open source language model to build a q a bot but we left that code at Google collab and we did not create a UI and a lot of people had been asking me that we should actually build a UI so this is basically the second part of the first video so I would strongly encourage you to watch the first video if you need to understand how this chatbot actually works but here we are going to build the front end and then we are going to build the front end in such a way that it looks like a chatbot and we are going to use one of our favorite tools radio for that so first I would like to show you a quick demo of how it is going to look like then I'm going to take you into the code which is also in Google collab and explain the chatbot part I'm not going to explain the large language part large language model part where we use flan to build the Q a board but this is only going to be the front end and the grade your pattern itself so let me show you how the chatbot looks like this is how it looks like you can go here and ask any question so I've already pasted a document about Ashton Kutcher so I'm going to ask a question so um what are the companies companies where Ashton has invested what are the companies where Ashton is invested okay let's see if it mentions one or two companies okay it says version Galactic okay what are the three companies let's say if it gives me more number of companies it again gives me Vision Galaxy so what are the startups let me ask a question different way so what are the startups where Astron has invested let's see if it says something it is research gate Okay cool so we have got an answer okay who who is Ashton Kutcher who's who is he by the way so he is an actor cool when was Ashton born and let's see if it gives the right answer empty response we didn't get any response okay let me ask it in a different way what is the birth date of Ashton Kutcher and then see what happens February 21st 1971 let me go to the Wikipedia and then see February 7 1978 that's wrong so it is probably picked up a different date but overall our objective is to say that this chatbot is actually working fine so now what we are going to do is we're going to see the code part on how to build this chat button itself first let me take you to the Google collab so I'm going to stop this so this is not going to work for now but okay let me stop it again so this is the part like if you have seen the first video you know we ended the code somewhere here so we ended with index.query and then we asked a question and it responded so now the rest of the part begins where we are going to build a UI user interface a chatbot user interface using radio now first thing that you need to do is PIP install radio once you have successfully installed radio then next you need to import graduate as gr now typically if you have been watching our Channel you know that for grade you we need three important things one as a function second is an input and third is an output so typically most of the radio applications that we have built on this channel had only one function but today we are going to build two functions and with two different tabs as you have seen in the interface you can see that we have two tabs one is the tab where you can go paste the knowledge the text in and itself and then ask it to build an index and then you can ask questions with the knowledge bot so for that we need to we need to have two core functions that grader will use to do whatever we are asking it to do the first function I'm calling it build the bot and the second function I'm calling it chart so what is the first function supposed to do so the first function is going to take the input text and then it is going to build a lamba index document and it is going to build a GPT simple Vector index which is the lamba index so this index we can use now to ask question about the chatbot now again the input text string goes here and from that we are going to build a llama document and from that we are going to build the GPT simple Vector index now the documents embed model llm predictor all these things are predefined so the document comes from this the llm predictor is something that we have already defined using the large language model in this particular case the flan model and also the embedding model in this particular case is a hugging phase embedding model so these things are already predefined so we are simply using gpt's simple Vector index to build an index you're not returning the index it's being it's just stored there as an object but you can also store save the index as an index.json or something and then you can load it it's up to you the code is already available here if you want to do that but for now we have built the index object and with that we are going to return a message to the user saying indexing is successful so you can see that we have returned a message to the user saying indexing is successful after we have done that now we are going to build the second you second function what is the second function the second function is a chat function where somebody can ask a question and then it is going to respond something but we want it to be a chat interface and also you know wouldn't it be fun if we have like the streaming response like how chat GPT does and this is exactly what we're going to do so the first object in this function is chat history so that is going to be always there so you're going to receive chat history and you are going to send chat history and then the next object is the user input so this is the actual question that the user is asking now um when user asks the question you are going to do index.query with the user input so index has been already built index.query will let you ask or query this index so you're going to take the question from the user and do index.query and then get this output and that is going to be the bot response now this bot response is good it's a llama object llama response object but what we want to do is we want to kind of simulate what charge GPS has been doing the streaming response and thankfully gradier has a very easy hack to do the streaming response and that is exactly what I've done so we are going to create a temporary string here called response and that response is basically what we are going to use to create the streaming response so the entire response the bot response that we got it is a response object from that we are going to extract response so at this point bot underscore response dot response is a string now we are going to split this ring by every character let's say I've got how are you h o w everything is going to be separate iterative element so that's what we are doing here now we are going to iterate through every single letter in that string which is the response that the bot has given us and we are going to add it to response and while we are doing that we are going to return we are going to yield like in this case not return we are going to yield it so that it is it can be displayed to the user along with the chart history now that is done now we are going to talk about the UI design the user interface design so as you can see the gradu application we've got a very simple title you have got two tabs inside the first tab we have a text box where the input is collected and then we have a text box where there's an output message is displayed and then we have a button in the knowledge board tab we have a simple chart interface at the top but there is no button all you have to do is type the question here send it type the questionnaire send it so that's exactly what we are going to design now we're going to use radio blocks radio blocks as demo we have a simple markdown here so if you want title you can do title whatever you want q a bot with hugging face models and we can say we want the first tab which is gr dot tab input text document here they have a you have a text input you have a text output and then you have a text button now when you click the button then the function this is called with this input and this output so basically you're telling this that when the first tab in the first tab you're going to have three elements on the screen an input text box an output text box which you cannot edit and then a button and when the button is clicked the function called build the bot is going to be called with the input text and whatever the output text is being returned by the function will be displayed in the text underscore output now the second tab is called knowledge tab now what do we do in the knowledge tab we're going to build a chatbot that's why gr dot chatbot and we are going to have a text box where you can send a message and instead of having a button to create a chat interface we are going to use the text box itself as a submit option so you can submit like directly from action from the text box in itself and when you do that we are going to call the function call chart and instead of sending only one object we are sending a list here which has the chat bot which is the chat history and also the message which is the user what they've typed and we are going to get the response as chatbot again so instead of sending a string getting a string we are going to send the chat bot and again get the chatbot as an output that's what this yielding is going to the chart is three plus user input response is going to help us do so now at this point all these are good and well the UI has been designed the way we wanted and you can see that we have got the demo we are queuing it and then launching it I'm doing it debug is equal to true just because I wanted to see if there is any error but if you are very sure that there is no error you can remove that I am going to run this and then I'm going to show you how the interface looks like and then I'm going to give you a quick recap of the code and also this time instead of Ashton Kutcher we can try something so let's see the code is run I'm going to click this link this is going to open in a new tab for me I wanted it to be new to q and a bot with hugging face models cool well and good so this is the place where we can input a text box so I'm going to go to Wikipedia and I'm going to look for somebody new so I'm going to say Elon Musk okay probably I don't know how much the bot is going to screw up but let's see Elon Musk I'm going to copy all these things it's quite a big page honestly it's a big page copy this go to the radio application in the text box paste this and then click build the bot once you click build the bot it's going to take a bit of time it took me about like 200 two point something seconds then it said indexing is successful so indexing is done I'm going to go to the knowledge what I'm going to ask a simple question click here send the document what is this document about it's it says Ashton Kutcher is this still about Ashton Kutcher let me go see if there is any indexing issue so the reason why the result showed Ashton Kutcher is because I made two rookie mistake one when I was reading this user input you would have seen that I was using text one which is wrong I should have been using input text and the second mistake is even though that we are building an index we are not actually making this index available globally so we had to make this index available globally and that's exactly why you can see that now I've made the index available globally the right way to do is to return it or store it as a Json object and then retrieve it but just because this is a demo I'm making this index available globally the disadvantage in this case is if you have got multiple objects with index then it might mess it up so Global is usually not advisable to do it but because I know for sure that I'm using only one index here this is going to work fine so I'm going to rerun this code like I said I've fixed two things one is the text list where I'm using input text second is making it index globally available so I'm going to rerun this again this this and this and let's see what is going to happen this time when I'm going to ask questions about Elon Musk so I'm going to go click the link now once again paste the article from Elon Musk and then click build the bot and then ideally it should say index F successfully go to the knowledge bot I'm going to ask what is this about it is going to tell me Elon Musk maybe no it's his Tesla that's fine and then let's ask another question saying when was Elon Musk born and let's see what does it say it's going to tell us that June 28 1971 so let's go see if it is June 28 1971 okay it's June 28 1971. so the next question that we are going to ask is we are going to ask what all the companies started by Elon Musk and then let's see if it answers one of the questions right like at least like one of the companies okay it says Tesla that's fine is there any other company Elon Musk started other than Tesla most likely it will say yes I think let's see okay it's a Tesla it just said Tesla anyways so we have successfully learned how to build a knowledge bot if you still dot the code I would like to quickly show you one more demo so instead of Elon Musk let's pick somebody else in this case I'm going to get Bill Gates so get Bill Gates get everything about Bill Gates probably this is fine yeah copy this go back to the pace paste it click build the bot it is going to now create the index save it go to the knowledge bot and then ask what is this document about and then let's see what it says it says it's about Bill Gates okay who is Bill Gates and it's going to say most likely Bill Gates it's just Microsoft okay fine so we have successfully learned to build a boat you can now optimize this board you can you can use different large language model and play with it but at this point we have successfully managed to build a chat bot on top of radio I did not edit out my mistake because I wanted you to know what kind of mistake can cause what kind of problems I have I've kept that if you are annoyed by that I'm really sorry that I kept my mistake in the video but I've heard from a lot of people who appreciate when I show my mistakes and the like the bugs and the errors that how do I resolve it and people have told me that helps that's why I've kept it so so far in this video we learned how to build a UI a radio friend and for a knowledge bot that we learned to build using flan open source model GPT index a llama index and also Lang chain if you have any question let me know in the comment section otherwise see in another video peace

Original Description

In this Applied NLP LLM Tutorial, We will build our Custom KnowledgeBot using LLama-Index and LangChain. LangChain for accessing Hugging Face Model Hub and GPT-Index for Vector Index Building and Index Querying - Information Retrieval. Gradio for ChatBOT User Interface Part 1 - https://youtu.be/9TxEQQyv9cE Code with UI - https://github.com/amrrs/LLM-QA-Bot/blob/main/LLM_Q%26A_with_Open_Source_Hugging_Face_Models.ipynb https://github.com/jerryjliu/gpt_index https://github.com/hwchase17/langchain ❤️ If you want to support what we are doing ❤️ Support here: Patreon - https://www.patreon.com/1littlecoder/ Ko-Fi - https://ko-fi.com/1littlecoder
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from 1littlecoder · 1littlecoder · 0 of 60

← Previous Next →
1 How to create your Free Data Science Blog on Github with Fastpages from Fastai
How to create your Free Data Science Blog on Github with Fastpages from Fastai
1littlecoder
2 Making Interactive Matplotlib Plots for Data Science Visualizations on Jupyter (Python)
Making Interactive Matplotlib Plots for Data Science Visualizations on Jupyter (Python)
1littlecoder
3 Create your first Data Science Web App using R Shiny
Create your first Data Science Web App using R Shiny
1littlecoder
4 How to create a Reproducible Example in R using reprex
How to create a Reproducible Example in R using reprex
1littlecoder
5 No Code Visualization using esquisse with Tableau-like Drag and Drop GUI in R
No Code Visualization using esquisse with Tableau-like Drag and Drop GUI in R
1littlecoder
6 Scrape HTML Table using rvest and Process them for insights using tidyverse in R
Scrape HTML Table using rvest and Process them for insights using tidyverse in R
1littlecoder
7 Google Teachable Machine Learning Build No Code AI solution
Google Teachable Machine Learning Build No Code AI solution
1littlecoder
8 Create meaningful fake tidy datasets in R using fakir [#rstats Package]
Create meaningful fake tidy datasets in R using fakir [#rstats Package]
1littlecoder
9 How to enable using R Programming with Visual Studio VS Code
How to enable using R Programming with Visual Studio VS Code
1littlecoder
10 Python, Community, Books - with Abhiram R - Bangpypers Co-organizers | 1littlecoder podcast
Python, Community, Books - with Abhiram R - Bangpypers Co-organizers | 1littlecoder podcast
1littlecoder
11 Growing a Tech Community across India - Anubha Maneshwar, Founder Girlscript | 1littlecoder Podcast
Growing a Tech Community across India - Anubha Maneshwar, Founder Girlscript | 1littlecoder Podcast
1littlecoder
12 Intro to Google Colab - How to use Colab
Intro to Google Colab - How to use Colab
1littlecoder
13 Intro to Plotly Express - Complex Interactive Charts with One-Line of Python Code
Intro to Plotly Express - Complex Interactive Charts with One-Line of Python Code
1littlecoder
14 Indic NLP Python Toolkit Open Source Development - iNLTK Creator Gaurav Arora | 1littlecoder Podcast
Indic NLP Python Toolkit Open Source Development - iNLTK Creator Gaurav Arora | 1littlecoder Podcast
1littlecoder
15 Do you want a career in Data Science - Tamil Webinar
Do you want a career in Data Science - Tamil Webinar
1littlecoder
16 Android Smartphone Analysis in R [Live Coding Screencast]
Android Smartphone Analysis in R [Live Coding Screencast]
1littlecoder
17 Programmatically create Images, Memes, Watermarks using Python with imgmaker
Programmatically create Images, Memes, Watermarks using Python with imgmaker
1littlecoder
18 Kaggle Walkthrough to get you started with Data Science - Webinar
Kaggle Walkthrough to get you started with Data Science - Webinar
1littlecoder
19 Community, Corporate Job, Coding - Gnana Lakshmi T C aka Gyan, WomenWhoCode Leadership Fellow
Community, Corporate Job, Coding - Gnana Lakshmi T C aka Gyan, WomenWhoCode Leadership Fellow
1littlecoder
20 Easy ggplot2 Theme Customization with {ggeasy} | Data Visualization in R
Easy ggplot2 Theme Customization with {ggeasy} | Data Visualization in R
1littlecoder
21 Excel to R - Pivot + Bar Chart in Excel  & R using tidyverse [Live Coding]
Excel to R - Pivot + Bar Chart in Excel & R using tidyverse [Live Coding]
1littlecoder
22 Excel to R #2 - VLOOKUP in Excel to LEFT_JOIN, MERGE in R
Excel to R #2 - VLOOKUP in Excel to LEFT_JOIN, MERGE in R
1littlecoder
23 5 websites to get Free Real-World Datasets for Data Science/ML Projects
5 websites to get Free Real-World Datasets for Data Science/ML Projects
1littlecoder
24 Excel to R #3 - APPROXIMATE VLOOKUP in Excel to FUZZY LEFT_JOIN in R
Excel to R #3 - APPROXIMATE VLOOKUP in Excel to FUZZY LEFT_JOIN in R
1littlecoder
25 Correlation-alternative PPS (Predictive Power Score) Python Package Demo
Correlation-alternative PPS (Predictive Power Score) Python Package Demo
1littlecoder
26 Automated Website Screenshots in R using {webshot}
Automated Website Screenshots in R using {webshot}
1littlecoder
27 Installing Custom RStudio Theme (Synthwave85)
Installing Custom RStudio Theme (Synthwave85)
1littlecoder
28 Analyse Google Trends Search Data in R using {gtrendsR}
Analyse Google Trends Search Data in R using {gtrendsR}
1littlecoder
29 3 Tips to ask question on Stack Overflow the right way to get answers
3 Tips to ask question on Stack Overflow the right way to get answers
1littlecoder
30 Learn Data Science with R - Mini Projects - Web Scraping Zomato
Learn Data Science with R - Mini Projects - Web Scraping Zomato
1littlecoder
31 Easily make Dumbbell Chart using {ggcharts} | Data Visualization in R
Easily make Dumbbell Chart using {ggcharts} | Data Visualization in R
1littlecoder
32 GET Hackernews Front Page Results using REST API in R
GET Hackernews Front Page Results using REST API in R
1littlecoder
33 Quickly deploy ML WebApps from Google Colab using ngrok
Quickly deploy ML WebApps from Google Colab using ngrok
1littlecoder
34 Use Jupyter Notebooks within VSCode (Visual Studio Code) in 2020
Use Jupyter Notebooks within VSCode (Visual Studio Code) in 2020
1littlecoder
35 Plotly Interactive Plots as Pandas Plotting Backend df.plot()
Plotly Interactive Plots as Pandas Plotting Backend df.plot()
1littlecoder
36 Stack Overflow Developer Survey 2020 Highlights for New Programmers
Stack Overflow Developer Survey 2020 Highlights for New Programmers
1littlecoder
37 Matplotlib Animation Charts in Python using Celluloid
Matplotlib Animation Charts in Python using Celluloid
1littlecoder
38 Coding, Postwoman, Passion Project Book - Liyas Thomas Open Source Developer - 1littlecoder podcast
Coding, Postwoman, Passion Project Book - Liyas Thomas Open Source Developer - 1littlecoder podcast
1littlecoder
39 Aspiring Data Scientist, Tips on How to learn Business Domain Knowledge
Aspiring Data Scientist, Tips on How to learn Business Domain Knowledge
1littlecoder
40 Bokeh Interactive Charts as Pandas Plotting Backend df.plot_bokeh()
Bokeh Interactive Charts as Pandas Plotting Backend df.plot_bokeh()
1littlecoder
41 Easy Fast Python Pandas Summary with Sidetable | Pandas Tips & Tricks
Easy Fast Python Pandas Summary with Sidetable | Pandas Tips & Tricks
1littlecoder
42 Inception, Content Ideas, Consistency - Srivatsan Srinivasan AIEngineering YouTube Content Creator
Inception, Content Ideas, Consistency - Srivatsan Srinivasan AIEngineering YouTube Content Creator
1littlecoder
43 ggplot2 Text Customization with ggtext | Data Visualization in R
ggplot2 Text Customization with ggtext | Data Visualization in R
1littlecoder
44 Penguins Dataset Overview - iris alternative | EDA Data Visualization in R
Penguins Dataset Overview - iris alternative | EDA Data Visualization in R
1littlecoder
45 YouTube Growth Tips, Content Creation - Bhavesh Bhatt, YouTuber (Data Science & Machine Learning) #7
YouTube Growth Tips, Content Creation - Bhavesh Bhatt, YouTuber (Data Science & Machine Learning) #7
1littlecoder
46 Matplotlib Animated Bar Chart Race in Python | Data Visualization
Matplotlib Animated Bar Chart Race in Python | Data Visualization
1littlecoder
47 Simple Python GUI Development using {guietta}
Simple Python GUI Development using {guietta}
1littlecoder
48 #8 Niche, Growth, Monetization - David Langer - YouTuber Dave on Data
#8 Niche, Growth, Monetization - David Langer - YouTuber Dave on Data
1littlecoder
49 Simple Fast 3-step Python OCR using Deep Learning 40+ Languages
Simple Fast 3-step Python OCR using Deep Learning 40+ Languages
1littlecoder
50 Github New Feature Profile Summary/Mini-Resume - Profile Views
Github New Feature Profile Summary/Mini-Resume - Profile Views
1littlecoder
51 Otto ML Assistant, GPT-3 on Philosophers, Nvidia-ARM - 3 ML Tech News
Otto ML Assistant, GPT-3 on Philosophers, Nvidia-ARM - 3 ML Tech News
1littlecoder
52 What is OpenAI GPT-3 - Hype, Examples, Worries
What is OpenAI GPT-3 - Hype, Examples, Worries
1littlecoder
53 Julia 1.5, Datamuse API, Live HDR+ Pixel 4a - Machine Learning Tech News
Julia 1.5, Datamuse API, Live HDR+ Pixel 4a - Machine Learning Tech News
1littlecoder
54 Self-driving Car Engineer sentenced, arXiv Dataset, AI/ML Startup Idea - Machine Learning Tech News
Self-driving Car Engineer sentenced, arXiv Dataset, AI/ML Startup Idea - Machine Learning Tech News
1littlecoder
55 GPT-3 Explorer, Ciphey (Automated Decryption), Py-Sudoku - ML Tech News
GPT-3 Explorer, Ciphey (Automated Decryption), Py-Sudoku - ML Tech News
1littlecoder
56 How to use Advanced Google Search to extract Email Ids from Linkedin
How to use Advanced Google Search to extract Email Ids from Linkedin
1littlecoder
57 Cartoonizer Toon-IT (AI Web App), GPT-3 Advice, Android Earthquake Detection - ML Tech News
Cartoonizer Toon-IT (AI Web App), GPT-3 Advice, Android Earthquake Detection - ML Tech News
1littlecoder
58 Flow - R Package to visualize code logic, functions as a Flow Diagram
Flow - R Package to visualize code logic, functions as a Flow Diagram
1littlecoder
59 Build GPT-3-like Language Model on Google Colab with minGPT [PyTorch]
Build GPT-3-like Language Model on Google Colab with minGPT [PyTorch]
1littlecoder
60 Create a Pencil Sketch Portrait with Python OpenCV
Create a Pencil Sketch Portrait with Python OpenCV
1littlecoder

This video tutorial teaches viewers how to build a custom knowledge bot using LLama-Index, LangChain, and Gradio UI, covering topics such as retrieval augmented generation, fine-tuning, and chatbot interface design. By following the tutorial, viewers can create their own chatbot with a user-friendly Gradio UI and leverage Hugging Face models for LLM functionality.

Key Takeaways
  1. Install Gradio using pip
  2. Import Gradio as 'gr'
  3. Create two functions in the Gradio app: 'build the bot' and 'chat'
  4. Build the bot function with input text and output text
  5. Use text box as submit option in knowledge tab
  6. Yield chat history and user input for response
  7. Index Wikipedia page with 200+ seconds
  8. Paste user input into the text box
  9. Click Build the bot
  10. Make the index available globally
💡 The tutorial demonstrates how to leverage pre-trained LLMs and fine-tuning to create a custom knowledge bot with a user-friendly Gradio UI, highlighting the importance of effective prompt design and optimization for chatbot performance.

Related Reads

📰
Will Developers Need LLM Integration Skills in 2026 for Success?
Developers will need LLM integration skills in 2026 to stay competitive, learn how to integrate AI into your workflow
Dev.to AI
📰
I Trained a 471M-Parameter Language Model From Scratch on One RTX 4090 in 100 Hours.
Train a large language model from scratch on a single GPU in under 100 hours, leveraging recent advances in AI hardware and software
Medium · LLM
📰
Masking PII Without Losing It
Learn to mask personally identifiable information (PII) without losing its value using LLMs, ensuring user privacy and data security
Medium · LLM
📰
Build a Career in Artificial Intelligence : AI Mastery Course in Telugu
Learn how to build a career in Artificial Intelligence with a comprehensive AI mastery course in Telugu
Dev.to AI
Up next
5 Levels of AI Agents - From Simple LLM Calls to Multi-Agent Systems
Dave Ebbelaar (LLM Eng)
Watch →