import json import streamlit as st from google.oauth2 import service_account from google.cloud import language_v1 # About This Tool Section st.sidebar.title("About This Tool") # Descriptive Introduction st.sidebar.markdown("#### Descriptive Introduction") st.sidebar.markdown("The Google Cloud NLP Text Classifier is designed to analyze and classify a given text into predefined categories. It leverages Google's Natural Language Processing technology to achieve this. The tool is particularly useful for understanding the context or subject matter of the text.") # Step-by-Step Guide st.sidebar.markdown("#### Step-by-Step Guide") st.sidebar.markdown(""" 1. **Open the Tool**: Navigate to the URL where the Google Cloud NLP Text Classifier is hosted. 2. **User Input**: Upon arriving at the homepage, you will find a text area labeled 'Enter text to classify.' Here, you can paste or type the text that you want to classify. 3. **Analyze**: After entering the text, click the button labeled 'Analyze'. The tool will then process the text and classify it into various categories. 4. **View Results**: Once the analysis is complete, the tool will display the classified categories and their corresponding confidence scores. """) # Google Cloud NLP Text Classifier Description st.title("Google Cloud NLP Text Classifier") st.write("This tool is designed to classify text into predefined categories using Google's Natural Language Processing (NLP) technology. By using this tool, you can gain insights into the themes or subjects that the text covers, which can be especially useful for tasks like content analysis, market research, and SEO (Search Engine Optimization).") def sample_classify_text(text_content): try: # Assuming service_account_info is set in your Streamlit secrets service_account_info = json.loads(st.secrets["google_nlp"]) except json.JSONDecodeError: st.error("Invalid or empty JSON in 'google_nlp' secret.") return credentials = service_account.Credentials.from_service_account_info( service_account_info, scopes=["https://www.googleapis.com/auth/cloud-platform"] ) client = language_v1.LanguageServiceClient(credentials=credentials) document = {"content": text_content, "type_": language_v1.Document.Type.PLAIN_TEXT, "language": "en"} content_categories_version = ( language_v1.ClassificationModelOptions.V2Model.ContentCategoriesVersion.V2 ) response = client.classify_text( request={ "document": document, "classification_model_options": { "v2_model": {"content_categories_version": content_categories_version} # choose between v1_model or v2_model, https://cloud.google.com/natural-language/docs/reference/rest/v1/ClassificationModelOptions }, } ) st.write(f"### We found {len(response.categories)} categories") st.write("---") for category in response.categories: st.write(f"Category Name: {category.name}") st.write(f"Confidence Score: {category.confidence}") st.write("---") # User input for text analysis user_input = st.text_area("Enter text to classify") if st.button("Analyze"): if user_input: sample_classify_text(user_input)