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