Streamlit application to auto-generate custom emails and text messages
1. Streamlit application to auto-generate custom emails and text messages
Welcome back. We are almost at the end of the course. In this video, we are going to put all our learnings together to build an AI application using Streamlit that generates custom responses for the support tickets based on the customer issues. If you're not logged into your Snowflake account, please pause the video now to log into your account. Navigate to the Projects tab in the left panel and select Notebooks. Click on the Fine-tuning Mistral 7B notebook and select the Start button at the top right to initiate the notebook session. We'll already run the notebook until the Inference Using Fine-tuned Model section. Let's take a look at the last cell in the notebook. In about 30 lines of code, we have prototyped a UI for the AI app using Streamlit. Hit Run on the cell and the Streamlit application comes to life within the Snowflake notebook. As you see here, Snowflake notebooks support creating Streamlit apps in line as well. This application takes three inputs. First is the customer request. Feel free to play around with this input. Next up is the customer's preferred mode of contact. You could pick email or text message from the drop-down list. I am going to pick text message. Lastly, you can select the LLM model to generate custom response for the support ticket. I will select Mistral 7B here. Once you're done inputting these parameters, select Generate Messages button to see the output. The Classify Text function has correctly identified the ticket category as Closing Account. That is great. For the custom response to the support ticket, the general purpose Mistral 7B did not produce an ideal response. It has generated both email and text messages, although we input text as the customer's preferred mode of communication. Next, let's see how the fine-tuned Mistral 7B performs for the same set of inputs. This is fantastic. The fine-tuned model is able to generate the correct response. It has produced the text message with appropriate response for the customer request. Well done. If you want to play with this, you can change the customer request, the contact preference, and the LLM to see how the responses change. Creating the app in line within a Snowflake notebook is one way to build a streamlined application. What if you want to expose this app to your marketing team? Surely you don't want to share the entire notebook with all the code just to share the application. That would be cumbersome for our business users. There is another way you could create streamlined apps in Snowflake. Before we do that, pause the video and navigate to the Git Companion repo for this course. It is highlighted in the reading prior to this video. Click on the repo URL and navigate to Module 3 and download the support ticket response app Python file that has all the source code needed to build the AI app. Now you've downloaded the source code needed to build the streamlined app. Next up, log into your Snowflake account and navigate to Projects tab in the left panel and select Streamlit. Select the blue button on the upper right side of the screen which reads Streamlit app to create your app. On the pop-up, name your app. I'm calling it support ticket response app. Choose your app location by selecting the pull-down. Choose the database as telco support DB and the schema as support data and warehouse as compute warehouse and select create. Streamlit starts us with some sample code on the left pane. If you do not see this, select the edit button on the upper right to get back to it. Copy the Python code we downloaded earlier and paste it into the left pane of the Snowsight UI. Let us not worry about what each line of code does for the time being. Let's go ahead and create our app. Let us not worry about what each line of code does for the time being. We will come back to them in a few minutes. Once you've pasted the code from the Python file, click run on the top right of the screen. You can see the app UI getting updated on the right. When working with Streamlit apps in Snowsight, you can use the side-by-side editor and app preview screen to quickly add, adjust, or remove components. In this way, you can modify your code and see changes in the app right away. The app takes three inputs, exactly the same as the app we built in line within the Snowsight Notebook. The text box takes customer request as input. The dropdown list is used to select preferred mode of contact and another dropdown to select the LLM. You can experiment with different models and see how the custom messages are generated for different combinations of inputs. You can experiment with different models and see how the custom messages are generated for different combinations of inputs. How cool, right? The best part about this Streamlit app is that you can share this with your business users without having to share the underlying code along with it. Streamlit apps are snowflake objects and you can use rule-based access control to manage who can access these Streamlit apps. Click on the Share button on the top right of the screen. Type the name of the role you want to share your Streamlit app with. You, the app owner, can choose which roles have permission to use the app. To copy the URL to your Streamlit app, select Copy to Clipboard. You can then send this URL through email or text. Now you have your app. Let's take a look at the code that created this app. If the source code is not visible, click on Edit at the top right of the app UI. We have the Python code in front of us. In only 30 lines of Python, we were able to hack together a simple, clean user interface for our app. On the top left, just when the source code starts, you see a Packages drop-down list. You can use that to install the packages you need for the app. I have Python 3.8, Snowflake, Snowpark Python, and Streamlit packages installed. You don't need to install additional packages for this project, so we're all set. We start the code off with a bunch of import statements. Imported the Snowflake session to run the app, and Streamlit, of course, to create the app. I've imported AST module to process a string into a dictionary later in the app. With this, we get a Snowpark active session and are good to go. Next up, we have defined the prompt for LLMs. We also defined the five ticket categories to bucket our customer requests into. In the first container, we created a text box for customer requests. We split the container into two columns and created select box to select contact preference and LLM. We add another container for the generate messages button. With these, our simple, clean UI is now complete. It's definitely not the most sophisticated UI for an app, but serves the purpose for rapidly prototyping AI applications and sharing it. Let's look at the last 10 lines of code where we invoke the Snowflake Cortex classified text function that takes a customer request and tickets categories as input. It then classifies the request into one of five categories defined by our business teams. Lastly, we invoke the Cortex complete function with four inputs, the LLM prompt we defined earlier, along with the customer request, contact preference, and selected LLM from the UI. Once the user enters inputs and clicks on the generate messages button on the app UI, the if statement becomes true. It invokes the classified text LLM function and Cortex complete function to generate the custom response based on the inputs entered. Here you go. An AI application in Python with only 30 lines of code. Well done. We have covered a lot in this video. We learned how to build a clean, simple UI to the AI application we built using Streamlit. We also learned how to build the Streamlit app in line within a Snowflake notebook. In addition, we reviewed how Streamlit apps are also Snowflake objects and are bound by role-based access control. We reviewed how to install packages needed for your Streamlit app and how to view the source code and UI side-by-side while you're prototyping. Lastly, we saw how the Streamlit app can be shared with other Snowflake users that have access permissions granted by the app owner. That is it. Congratulations, and I will see you in the next video.2. Let's practice!
Create Your Free Account
or
By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.