blazingbunny commited on
Commit
c077e58
·
1 Parent(s): f0b9975

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -8
app.py CHANGED
@@ -3,11 +3,45 @@ import streamlit as st
3
  from google.oauth2 import service_account
4
  from google.cloud import language_v1
5
 
6
- # ... (sidebar and headers are the same)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
 
8
  def sample_analyze_entities(text_content, your_query=""):
9
- # ... (NLP setup is the same)
10
-
 
 
 
 
 
 
 
 
 
 
11
  entities_list = []
12
  for entity in response.entities:
13
  entity_details = {
@@ -25,20 +59,18 @@ def sample_analyze_entities(text_content, your_query=""):
25
  st.write("### We found results for your query")
26
 
27
  st.write("----")
28
-
29
- st.write("----")
30
  for i, entity in enumerate(entities_list):
31
  st.write(f"Entity {i+1} of {len(entities_list)}")
32
  st.write(f"Relevance Score: {round(entity.get('Salience Score', 0) * 100)}%")
33
  st.write(f"Name: {entity.get('Name', 'N/A')}")
34
  st.write(f"Type: {entity.get('Type', 'N/A')}")
35
  st.write(f"Salience Score: {entity.get('Salience Score', 'N/A')}")
36
-
37
  metadata = entity.get('Metadata', {})
38
  if metadata:
39
  st.write("Metadata:")
40
  st.write(metadata)
41
-
42
  mentions = entity.get('Mentions', [])
43
  if mentions:
44
  st.write("Mentions:")
@@ -46,7 +78,6 @@ def sample_analyze_entities(text_content, your_query=""):
46
 
47
  st.write("----")
48
 
49
-
50
  st.write(f"### Language of the text: {response.language}")
51
 
52
  # User input for text analysis
 
3
  from google.oauth2 import service_account
4
  from google.cloud import language_v1
5
 
6
+ # Sidebar content
7
+ st.sidebar.title("About This Tool")
8
+ st.sidebar.markdown("### Descriptive Introduction")
9
+ st.sidebar.markdown("""
10
+ 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.
11
+ """)
12
+ st.sidebar.markdown("### Step-by-Step Guide")
13
+ st.sidebar.markdown("""
14
+ 1. **Open the Tool**: Navigate to the URL where the tool is hosted.
15
+ 2. **User Input**:
16
+ - **Text to Analyze**: In the text area labeled "Enter text to analyze", paste or type the text you want to analyze.
17
+ - **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.
18
+ - **Analyze**: Click the button labeled "Analyze". The tool will then process the text and perform entity analysis on it.
19
+ 3. **View Results**:
20
+ - 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).
21
+ - Below this, you'll find a line-by-line breakdown of each entity identified in the text.
22
+ """)
23
+
24
+ # Header and intro
25
+ st.title("Google Cloud NLP Entity Analyzer")
26
+ st.write("## Introduction to the Knowledge Graph API")
27
+ st.write("---")
28
+ st.write("""
29
+ The Google Knowledge Graph API reveals entity information related to a keyword, that Google knows about. This information can be very useful for SEO – discovering related topics and what Google believes is relevant. It can also help when trying to claim/win a Knowledge Graph box on search results. The API requires a high level of technical understanding, so this tool creates a simple public interface, with the ability to export data into spreadsheets.
30
+ """)
31
 
32
  def sample_analyze_entities(text_content, your_query=""):
33
+ service_account_info = json.loads(st.secrets["google_nlp"])
34
+ credentials = service_account.Credentials.from_service_account_info(
35
+ service_account_info, scopes=["https://www.googleapis.com/auth/cloud-platform"]
36
+ )
37
+ client = language_v1.LanguageServiceClient(credentials=credentials)
38
+ type_ = language_v1.Document.Type.PLAIN_TEXT
39
+ language = "en"
40
+ document = {"content": text_content, "type_": type_, "language": language}
41
+ encoding_type = language_v1.EncodingType.UTF8
42
+
43
+ response = client.analyze_entities(request={"document": document, "encoding_type": encoding_type})
44
+
45
  entities_list = []
46
  for entity in response.entities:
47
  entity_details = {
 
59
  st.write("### We found results for your query")
60
 
61
  st.write("----")
 
 
62
  for i, entity in enumerate(entities_list):
63
  st.write(f"Entity {i+1} of {len(entities_list)}")
64
  st.write(f"Relevance Score: {round(entity.get('Salience Score', 0) * 100)}%")
65
  st.write(f"Name: {entity.get('Name', 'N/A')}")
66
  st.write(f"Type: {entity.get('Type', 'N/A')}")
67
  st.write(f"Salience Score: {entity.get('Salience Score', 'N/A')}")
68
+
69
  metadata = entity.get('Metadata', {})
70
  if metadata:
71
  st.write("Metadata:")
72
  st.write(metadata)
73
+
74
  mentions = entity.get('Mentions', [])
75
  if mentions:
76
  st.write("Mentions:")
 
78
 
79
  st.write("----")
80
 
 
81
  st.write(f"### Language of the text: {response.language}")
82
 
83
  # User input for text analysis