File size: 3,950 Bytes
564ce0c 569a26f 564ce0c c077e58 127ae0e c077e58 e054f20 127ae0e c2b8ffb 569a26f c077e58 9502681 015a0a7 e67172f 9502681 9ddc9bf bc4e0d2 9ddc9bf bc4e0d2 d57d7e1 c077e58 d57d7e1 c077e58 d57d7e1 13f5ed0 f0b9975 d57d7e1 f0b9975 f66f708 c2b8ffb ed60501 569a26f c2b8ffb ed60501 |
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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
import json
import streamlit as st
from google.oauth2 import service_account
from google.cloud import language_v1
# Sidebar content
st.sidebar.title("About This Tool")
st.sidebar.markdown("### Descriptive Introduction")
st.sidebar.markdown
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**:
- **Text to Analyze**: In the text area labeled "Enter text to analyze", paste or type the text you want to analyze.
- **Query**: Optionally, you can also enter a specific query in the text input field labeled "Enter your query (optional)". This is for your own reference.
- **Analyze**: Click the button labeled "Analyze". The tool will then process the text and perform entity analysis on it.
3. **View Results**:
- After the analysis is complete, you'll see a section that says, "We found X results for your query of your_query" (or just "We found results for your query" if no query was entered).
- Below this, you'll find a line-by-line breakdown of each entity identified in the text.
""")
# Header and intro
st.title("Google Cloud NLP Entity Analyzer")
st.write("""The "Google Cloud NLP Entity Analyzer" is a powerful tool designed to analyze text and identify various types of entities such as people, locations, organizations, and events. Leveraging Google's Natural Language Processing (NLP) technology, this tool provides insights into how Google understands text, which can be particularly useful for Search Engine Optimization (SEO) efforts. It also serves as an interface to the Google Knowledge Graph API, providing additional contextual information about the identified entities.""")
def sample_analyze_entities(text_content, your_query=""):
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)
type_ = language_v1.Document.Type.PLAIN_TEXT
language = "en"
document = {"content": text_content, "type_": type_, "language": language}
encoding_type = language_v1.EncodingType.UTF8
response = client.analyze_entities(request={"document": document, "encoding_type": encoding_type})
entities_list = []
for entity in response.entities:
entity_details = {
"Name": entity.name,
"Type": language_v1.Entity.Type(entity.type_).name,
"Salience Score": entity.salience,
"Metadata": entity.metadata,
"Mentions": [mention.text.content for mention in entity.mentions]
}
entities_list.append(entity_details)
if your_query:
st.write(f"### We found {len(entities_list)} results for your query of **{your_query}**")
else:
st.write("### We found results for your query")
st.write("----")
for i, entity in enumerate(entities_list):
st.write(f"Entity {i+1} of {len(entities_list)}")
st.write(f"Relevance Score: {round(entity.get('Salience Score', 0) * 100)}%")
st.write(f"Name: {entity.get('Name', 'N/A')}")
st.write(f"Type: {entity.get('Type', 'N/A')}")
st.write(f"Salience Score: {entity.get('Salience Score', 'N/A')}")
metadata = entity.get('Metadata', {})
if metadata:
st.write("Metadata:")
st.write(metadata)
mentions = entity.get('Mentions', [])
if mentions:
st.write("Mentions:")
st.write(json.dumps(mentions))
st.write("----")
st.write(f"### Language of the text: {response.language}")
# User input for text analysis
user_input = st.text_area("Enter text to analyze")
#your_query = st.text_input("Enter your query (optional)")
if st.button("Analyze"):
sample_analyze_entities(user_input)
|