From the course: Advanced Python Projects: Build AI Applications
Building a chat interface with Streamlit - Python Tutorial
From the course: Advanced Python Projects: Build AI Applications
Building a chat interface with Streamlit
- [Instructor] In this lesson, we're going to explore how to handle the user input, update the chat history, display user and assistant messages in the Streamlit app. We're also going to explore how to simulate a streaming effect for the assistant's response. The assistant's response are going to be added to the chat history for future references. So let's dive in and see how to do that. First we use st.chat input to accept the user input in the Streamlit app. The provided message, "You can ask any question," serves as a placeholder. If the user provides input, it is assigned to the variable prompt. Next, we append the user's message, the chat history stored in the session state. Then we display the user's message in the Streamlit app using st.chat_message with the role user and the st.markdown to get the content. Next, we're going to display the assistant's response in the chat message container. If there's no existing session ID, we start a new session by calling the chat function with the user's prompt and the data from the uploaded file. If there's an existing session ID, we continue the session. An empty Streamlit element message_placeholder is created to be used for dynamic updating of the assistant's response. After that, we're going to stimulate the display of the assistant's response with the streaming effect. The response is split into chunks and each chunk is added to the full response string with a small delay of 50 milliseconds. You can increase this delay if you'd like or decrease this. This creates a typing like effect. And over here a blinking cursor is added to simulate the appearance of typing. Last but not least, we append the assistant's response to the chat history stored in the session state. And that's it. Our front end coding is done. We were trying to create an application that takes in a large document and chats with the user about that document. Now let's see this application in action.
Contents
-
-
-
-
-
-
Introduction to API communication with Python3m 51s
-
File uploads and advanced API interactions1m 41s
-
Streamlit app basics and file handling2m 29s
-
Uploading files to API endpoints with Streamlit1m 21s
-
Building a chat interface with Streamlit2m 4s
-
How to run the front end of the chatbot3m 12s
-
Challenge: Create a front-end UI for file upload1m 42s
-
Solution: Create a front-end UI for file upload7m 42s
-
-