Streamlit for ML #4 - Adding Bootstrap Components

James Briggs · Beginner ·🧠 Large Language Models ·4y ago

Key Takeaways

Streamlit for ML is used to create a web app for open domain question answering, and Bootstrap components are added to improve the app's UI. The video demonstrates how to create cards using Bootstrap and integrate them into the Streamlit app.

Full Transcript

okay in the last few videos we've put together this streamlit app uh for open domain question answering and what it does at the moment is we can we can ask you a question like who are the normans or we can ask something i don't know where are the normans from um i'm not sure if we have i'm not sure what topics we have in there in the in the vector database so i'm just gonna ask like norman questions for now maybe i can ask something like um where is italy that must come up with something surely yeah okay so we see uh different things in there uh okay cool so we have this and we can we can ask it questions and it all works but it's not very nice to look at at the moment we just get these like big uh you know chunks of text back and i mean that's pretty much all we can do with the the current back end uh but we can at least format it a little nicer so what we're going to do is convert these big chunks of text into what are called cards now to create these cards we are going to use bootstrap so if i just type in bootstrap cards you can see it's in my history but i'm going to just put bootstrap cards for now so you can follow and go on here and let's let's make this a little bit bigger zoom in a little bit and scroll down so we have some cool examples here so this is one card uh we can go down a little bit more or this one i think this is probably the one i'll use okay so basically bootstrap is like a framework which is pretty lightweight and you have all these really nice examples of how you can build something that was super nice so what we can do is actually just copy this and we'll go back over to our code and what i'm going to do is i'm going to create the definition for one of these cards i'm going to do that in a function so we'll make our lives a little bit easier made us a lot easier to read anyway okay so i'm going to what we're going to do is return an f string let me clean up a little bit so uninvent that and then that's our card so now um well actually no this won't work yet but if i come down here i'm going to write okay for context instead of taking this um sc right metadata i'm going to just write st dot right card okay actually not i see uh we actually want to include sc.markdown and so markdown is another useful component in instrument let me open it up here so it will be a text element and we have st markdown so let's have a look at that and it displays string formatted as markdown now what it also allows you to do is if we take this put it in here and say it's true we can also use it to display html so let's save that let's go to our app and get okay um always rerun at the top there okay and now we have these cards and they're not very nice looking and we'll figure out why in a moment but before we figure out why uh let's actually make this a little bit nicer so i'll make it relevant because at the moment we don't have anything relevant in here so i'm going to include the title or maybe the i yeah let's go id value title and context okay we'll make this an f formatted string and what i'm going to do is remove these links at the bottom because i don't really have anywhere to link to with these and what i'm going to do is replace this default text with the context so let me minimize that here close up context uh the title will make title and the subtitle i want it to be the id value okay so if i do that okay it's not going to work straight away because we need to actually pass our id value title and context uh so let's do that so the well we know the text is here okay and so we have the the context which is the last parameter we also have in the context or in the metadata we also have the title and then the the id is in a different part of the json response and so that is in if i remove this that is in id okay let's run this see if it works okay cool so now we have like the the title so the wikipedia topic that came from the id unique id for each context we have and then we also have this you know this chunk of text the context now you know there's a few things that are kind of going a little bit weird here first the formatting of the card isn't what we saw in the in the bootstrap example come over here uh it looks really nice okay so something weird going on there and so what we actually need to do is if we let's search css see if it comes up okay so we have this uh compiled css and js and i come down to here what we need to do is actually copy the style sheet for bootstrap so that basically how it's been styled and we need to put that into our into our app um so all we need to do is copy that first line which is a style sheet the second one is for if you have javascript uh components we don't for the cards so we don't need to worry about that and what we're going to do is actually create another markdown um component so st.markdown because as before all we need to do here is add some html to our app so we're just going to paste that in okay but as before with html we also need to use this allow or unsafe allow html okay now if i down here and rerun the app our card should start looking nicer okay so you can see now we have that nice little border and we have um just looks better the only thing is now that it's actually like really tight it's not taking the full width of the the parent container that is within so to change that we don't actually need to really do anything we just need to remove this style component okay so let's remove that and see see what happens okay so now we will see this card sticks the same width as our search component so we're now getting something a little nicer the only issue is that now if we look between our cards they they're like attached and we also don't really want that so what we can do is add a margin to our cards so this is all just like html and css it's nothing like too complicated so you can look this up if you want to i'm going to use one ram which is a more flexible measure rather than using pixels i'm going to save that so look at what we get so now we get a little bit of a gap between one of the edges and you can remove that if you want by using the margin top and origin bottom uh but i think it looks nice so i'm going to keep it and we have an actual gap between each of our cards okay so yeah that's that's pretty cool um if we go back to you know something that we know works so who are the normans run that and we now get this cool response okay so i think that is pretty much like the core of our app that's everything we need and the app is there it's not you know it's definitely not perfect there's a lot we can do to it to make it much more interesting and we will look into those things but for now that's i think pretty cool it's the core of our app and with that we can actually do something useful with it we can actually you know help people find information uh in a quicker more natural way but that's it for for that video i really hope it's been useful and yeah thank you very much for watching i'll see you in the next one bye

Original Description

▶️ Streamlit for ML Part 5.1: https://www.youtube.com/watch?v=SGazDb8o-to&list=PLIUOU7oqGTLg5ssYxPGWaci6695wtosGw&index=5 Streamlit has proven itself as an incredibly popular tool for quickly putting together high-quality ML-oriented web apps. More recently, it has seen wider adoption in production environments by ever-larger organizations. All of this means that there is no better time to pick up some experience with Streamlit. Fortunately, the basics of Streamlit are incredibly easy to learn, and for most tools, this will be more than you need! In this series, we will introduce Streamlit by building a general knowledge Q&A interface. We will learn about key Streamlit components like write, text_input, container. How to use external libraries like Bootstrap to quickly create new app components. And use caching to speed up our app. ▶️ Streamlit for ML Playlist: https://www.youtube.com/watch?v=JLKUV-LiXjk&list=PLIUOU7oqGTLg5ssYxPGWaci6695wtosGw&index=1 📕 Article: https://towardsdatascience.com/getting-started-with-streamlit-for-nlp-75fe463821ec 🤖 70% Discount on the NLP With Transformers in Python course: https://bit.ly/3DFvvY5 🎉 Subscribe for Article and Video Updates! https://jamescalam.medium.com/subscribe https://medium.com/@jamescalam/membership 📖 Friend link to article: https://towardsdatascience.com/getting-started-with-streamlit-for-nlp-75fe463821ec?sk=ac5e0b7c39938f52162862411a66a58b 👾 Discord: https://discord.gg/c5QtDB9RAP 00:00 Intro 02:35 Streamlit Caching 06:56 Experimental Caching Primitives
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from James Briggs · James Briggs · 0 of 60

← Previous Next →
1 Stoic Philosophy Text Generation with TensorFlow
Stoic Philosophy Text Generation with TensorFlow
James Briggs
2 How to Build TensorFlow Pipelines with tf.data.Dataset
How to Build TensorFlow Pipelines with tf.data.Dataset
James Briggs
3 Every New Feature in Python 3.10.0a2
Every New Feature in Python 3.10.0a2
James Briggs
4 How-to Build a Transformer for Language Classification in TensorFlow
How-to Build a Transformer for Language Classification in TensorFlow
James Briggs
5 How-to use the Kaggle API in Python
How-to use the Kaggle API in Python
James Briggs
6 Language Generation with OpenAI's GPT-2 in Python
Language Generation with OpenAI's GPT-2 in Python
James Briggs
7 Text Summarization with Google AI's T5 in Python
Text Summarization with Google AI's T5 in Python
James Briggs
8 How-to do Sentiment Analysis with Flair in Python
How-to do Sentiment Analysis with Flair in Python
James Briggs
9 Python Environment Setup for Machine Learning
Python Environment Setup for Machine Learning
James Briggs
10 Sequential Model - TensorFlow Essentials #1
Sequential Model - TensorFlow Essentials #1
James Briggs
11 Functional API - TensorFlow Essentials #2
Functional API - TensorFlow Essentials #2
James Briggs
12 Training Parameters - TensorFlow Essentials #3
Training Parameters - TensorFlow Essentials #3
James Briggs
13 Input Data Pipelines - TensorFlow Essentials #4
Input Data Pipelines - TensorFlow Essentials #4
James Briggs
14 6 of Python's Newest and Best Features (3.7-3.9)
6 of Python's Newest and Best Features (3.7-3.9)
James Briggs
15 Novice to Advanced RegEx in Less-than 30 Minutes + Python
Novice to Advanced RegEx in Less-than 30 Minutes + Python
James Briggs
16 Building a PlotLy $GME Chart in Python
Building a PlotLy $GME Chart in Python
James Briggs
17 How-to Use The Reddit API in Python
How-to Use The Reddit API in Python
James Briggs
18 How to Build Custom Q&A Transformer Models in Python
How to Build Custom Q&A Transformer Models in Python
James Briggs
19 How to Build Q&A Models in Python (Transformers)
How to Build Q&A Models in Python (Transformers)
James Briggs
20 How-to Decode Outputs From NLP Models (Python)
How-to Decode Outputs From NLP Models (Python)
James Briggs
21 Identify Stocks on Reddit with SpaCy (NER in Python)
Identify Stocks on Reddit with SpaCy (NER in Python)
James Briggs
22 Sentiment Analysis on ANY Length of Text With Transformers (Python)
Sentiment Analysis on ANY Length of Text With Transformers (Python)
James Briggs
23 Unicode Normalization for NLP in Python
Unicode Normalization for NLP in Python
James Briggs
24 The NEW Match-Case Statement in Python 3.10
The NEW Match-Case Statement in Python 3.10
James Briggs
25 Multi-Class Language Classification With BERT in TensorFlow
Multi-Class Language Classification With BERT in TensorFlow
James Briggs
26 How to Build Python Packages for Pip
How to Build Python Packages for Pip
James Briggs
27 How-to Structure a Q&A ML App
How-to Structure a Q&A ML App
James Briggs
28 How to Index Q&A Data With Haystack and Elasticsearch
How to Index Q&A Data With Haystack and Elasticsearch
James Briggs
29 Q&A Document Retrieval With DPR
Q&A Document Retrieval With DPR
James Briggs
30 How to Use Type Annotations in Python
How to Use Type Annotations in Python
James Briggs
31 Extractive Q&A With Haystack and FastAPI in Python
Extractive Q&A With Haystack and FastAPI in Python
James Briggs
32 Sentence Similarity With Sentence-Transformers in Python
Sentence Similarity With Sentence-Transformers in Python
James Briggs
33 Sentence Similarity With Transformers and PyTorch (Python)
Sentence Similarity With Transformers and PyTorch (Python)
James Briggs
34 NER With Transformers and spaCy (Python)
NER With Transformers and spaCy (Python)
James Briggs
35 Training BERT #1 - Masked-Language Modeling (MLM)
Training BERT #1 - Masked-Language Modeling (MLM)
James Briggs
36 Training BERT #2 - Train With Masked-Language Modeling (MLM)
Training BERT #2 - Train With Masked-Language Modeling (MLM)
James Briggs
37 Training BERT #3 - Next Sentence Prediction (NSP)
Training BERT #3 - Next Sentence Prediction (NSP)
James Briggs
38 Training BERT #4 - Train With Next Sentence Prediction (NSP)
Training BERT #4 - Train With Next Sentence Prediction (NSP)
James Briggs
39 FREE 11 Hour NLP Transformers Course (Next 3 Days Only)
FREE 11 Hour NLP Transformers Course (Next 3 Days Only)
James Briggs
40 New Features in Python 3.10
New Features in Python 3.10
James Briggs
41 Training BERT #5 - Training With BertForPretraining
Training BERT #5 - Training With BertForPretraining
James Briggs
42 How-to Use HuggingFace's Datasets - Transformers From Scratch #1
How-to Use HuggingFace's Datasets - Transformers From Scratch #1
James Briggs
43 Build a Custom Transformer Tokenizer - Transformers From Scratch #2
Build a Custom Transformer Tokenizer - Transformers From Scratch #2
James Briggs
44 3 Traditional Methods for Similarity Search (Jaccard, w-shingling, Levenshtein)
3 Traditional Methods for Similarity Search (Jaccard, w-shingling, Levenshtein)
James Briggs
45 3 Vector-based Methods for Similarity Search (TF-IDF, BM25, SBERT)
3 Vector-based Methods for Similarity Search (TF-IDF, BM25, SBERT)
James Briggs
46 Building MLM Training Input Pipeline - Transformers From Scratch #3
Building MLM Training Input Pipeline - Transformers From Scratch #3
James Briggs
47 Training and Testing an Italian BERT - Transformers From Scratch #4
Training and Testing an Italian BERT - Transformers From Scratch #4
James Briggs
48 Faiss - Introduction to Similarity Search
Faiss - Introduction to Similarity Search
James Briggs
49 Angular App Setup With Material - Stoic Q&A #5
Angular App Setup With Material - Stoic Q&A #5
James Briggs
50 Why are there so many Tokenization methods in HF Transformers?
Why are there so many Tokenization methods in HF Transformers?
James Briggs
51 Choosing Indexes for Similarity Search (Faiss in Python)
Choosing Indexes for Similarity Search (Faiss in Python)
James Briggs
52 Locality Sensitive Hashing (LSH) for Search with Shingling + MinHashing (Python)
Locality Sensitive Hashing (LSH) for Search with Shingling + MinHashing (Python)
James Briggs
53 How LSH Random Projection works in search (+Python)
How LSH Random Projection works in search (+Python)
James Briggs
54 IndexLSH for Fast Similarity Search in Faiss
IndexLSH for Fast Similarity Search in Faiss
James Briggs
55 Faiss - Vector Compression with PQ and IVFPQ (in Python)
Faiss - Vector Compression with PQ and IVFPQ (in Python)
James Briggs
56 Product Quantization for Vector Similarity Search (+ Python)
Product Quantization for Vector Similarity Search (+ Python)
James Briggs
57 How to Build a Bert WordPiece Tokenizer in Python and HuggingFace
How to Build a Bert WordPiece Tokenizer in Python and HuggingFace
James Briggs
58 Metadata Filtering for Vector Search + Latest Filter Tech
Metadata Filtering for Vector Search + Latest Filter Tech
James Briggs
59 Build NLP Pipelines with HuggingFace Datasets
Build NLP Pipelines with HuggingFace Datasets
James Briggs
60 Composite Indexes and the Faiss Index Factory
Composite Indexes and the Faiss Index Factory
James Briggs

This video teaches how to create a Streamlit app for open domain question answering and improve its UI using Bootstrap components. The app can be used to help people find information in a quicker and more natural way.

Key Takeaways
  1. Create a Streamlit app for open domain question answering
  2. Add Bootstrap components to the app to improve its UI
  3. Use css and html to style the app
  4. Integrate markdown into the app
  5. Add margins to the cards to improve the app's layout
💡 Using Bootstrap components can significantly improve the UI of a Streamlit app, making it more user-friendly and engaging.

Related AI Lessons

How We Translate 300-Page Books Using Claude Without Hitting Token Limits
Learn how to translate long documents using Claude without hitting token limits by breaking them into overlapping chunks
Dev.to · 龚旭东
Building HITL Feedback RAG: Embeddings, Retrieval, and Reranking
Learn to build a Human-in-the-Loop (HITL) Feedback RAG system using embeddings, retrieval, and reranking to improve model performance
Medium · AI
Building HITL Feedback RAG: Embeddings, Retrieval, and Reranking
Learn to build a Human-in-the-Loop (HITL) Feedback RAG system using embeddings, retrieval, and reranking to improve LLM performance
Medium · LLM
A simple way to test model fallbacks with RouterBase
Learn to test model fallbacks with RouterBase using a simple fallback wrapper and OpenAI-compatible API surface
Dev.to · routerbasecom

Chapters (3)

Intro
2:35 Streamlit Caching
6:56 Experimental Caching Primitives
Up next
5 Levels of AI Agents - From Simple LLM Calls to Multi-Agent Systems
Dave Ebbelaar (LLM Eng)
Watch →