blazingbunny commited on
Commit
569a26f
·
1 Parent(s): c2b8ffb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -16
app.py CHANGED
@@ -1,19 +1,20 @@
1
  import json
 
2
  from google.oauth2 import service_account
3
  from google.cloud import language_v1
4
- import streamlit as st
5
 
6
  # Header and intro
 
7
  st.write("## Introduction to the Knowledge Graph API")
8
  st.write("---")
9
  st.write("""
10
  The Google Knowledge Graph API reveals entity information related to a keyword, that Google knows about.
11
- This information can be very useful for SEO – discovering related topics and what Google believes is relevant.
12
  It can also help when trying to claim/win a Knowledge Graph box on search results.
13
  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.
14
  """)
15
 
16
- def sample_analyze_entities(text_content):
17
  # Parse the JSON string to a dictionary
18
  service_account_info = json.loads(st.secrets["google_nlp"])
19
 
@@ -58,25 +59,34 @@ def sample_analyze_entities(text_content):
58
  # Append the dictionary to the list
59
  entities_list.append(entity_details)
60
 
61
- # Use Streamlit to display the results
62
- st.write("### Analyzed Entities")
63
- for entity in entities_list:
64
- st.write(f"**Name**: {entity['Name']}")
65
- st.write(f"**Type**: {entity['Type']}")
66
- st.write(f"**Salience Score**: {entity['Salience Score']}")
 
67
 
68
- if entity["Metadata"]:
69
- st.write("**Metadata**: ")
70
- st.json(entity["Metadata"])
 
71
 
72
- if entity["Mentions"]:
73
- st.write("**Mentions**: ")
74
- st.json(entity["Mentions"])
75
 
 
 
 
 
 
76
 
77
  st.write(f"### Language of the text: {response.language}")
78
 
79
  # User input for text analysis
80
  user_input = st.text_area("Enter text to analyze")
 
 
81
  if st.button("Analyze"):
82
- sample_analyze_entities(user_input)
 
1
  import json
2
+ import streamlit as st
3
  from google.oauth2 import service_account
4
  from google.cloud import language_v1
 
5
 
6
  # Header and intro
7
+ st.title("Google Cloud NLP Entity Analyzer")
8
  st.write("## Introduction to the Knowledge Graph API")
9
  st.write("---")
10
  st.write("""
11
  The Google Knowledge Graph API reveals entity information related to a keyword, that Google knows about.
12
+ This information can be very useful for SEO discovering related topics and what Google believes is relevant.
13
  It can also help when trying to claim/win a Knowledge Graph box on search results.
14
  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.
15
  """)
16
 
17
+ def sample_analyze_entities(text_content, your_query=""):
18
  # Parse the JSON string to a dictionary
19
  service_account_info = json.loads(st.secrets["google_nlp"])
20
 
 
59
  # Append the dictionary to the list
60
  entities_list.append(entity_details)
61
 
62
+ # Streamlit UI
63
+ if your_query:
64
+ st.write(f"### We found {len(entities_list)} results for your query of **{your_query}**")
65
+ else:
66
+ st.write("### We found results for your query")
67
+
68
+ st.write("----")
69
 
70
+ for i, entity in enumerate(entities_list):
71
+ st.write(f"**Relevance Score:** {entity.get('Salience Score', 'N/A')} \t {i+1} of {len(entities_list)}")
72
+ st.write(f"### {entity.get('Name', 'N/A')}")
73
+ st.write(f"**Entity Type:** {entity.get('Type', 'N/A')}")
74
 
75
+ if entity.get('Metadata'):
76
+ st.write("**Metadata:**")
77
+ st.json(entity['Metadata'])
78
 
79
+ if entity.get('Mentions'):
80
+ st.write("**Mentions:**")
81
+ st.json(entity['Mentions'])
82
+
83
+ st.write("----")
84
 
85
  st.write(f"### Language of the text: {response.language}")
86
 
87
  # User input for text analysis
88
  user_input = st.text_area("Enter text to analyze")
89
+ your_query = st.text_input("Enter your query (optional)")
90
+
91
  if st.button("Analyze"):
92
+ sample_analyze_entities(user_input, your_query)