|
import json |
|
import streamlit as st |
|
from google.oauth2 import service_account |
|
from google.cloud import language_v1 |
|
|
|
|
|
st.sidebar.title("About This Tool") |
|
st.sidebar.markdown("### Descriptive Introduction") |
|
st.sidebar.markdown("This tool leverages Google's NLP technology for text classification.") |
|
st.sidebar.markdown("### Step-by-Step Guide") |
|
st.sidebar.markdown(""" |
|
1. **Open the Tool**: Navigate to the URL where the tool is hosted. |
|
2. **User Input**: Enter the text you want to classify. |
|
3. **Analyze**: Click the 'Analyze' button. |
|
4. **View Results**: See the classified categories and their confidence scores. |
|
""") |
|
|
|
|
|
st.title("Google Cloud NLP Text Classifier") |
|
st.write("This tool classifies text into predefined categories.") |
|
|
|
def sample_classify_text(text_content): |
|
|
|
service_account_info = json.loads(st.secrets["google_nlp"]) |
|
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} |
|
}, |
|
} |
|
) |
|
|
|
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 = st.text_area("Enter text to classify", max_chars=2500) |
|
|
|
if st.button("Analyze"): |
|
if user_input: |
|
sample_classify_text(user_input) |
|
|