blazingbunny commited on
Commit
8deafd3
·
1 Parent(s): 0d36871

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -3
app.py CHANGED
@@ -2,6 +2,23 @@ import json
2
  import streamlit as st
3
  from google.oauth2 import service_account
4
  from google.cloud import language_v1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
  # Function to count entities with 'mid' that contains '/g/' or '/m/' in their metadata
7
  def count_entities(entities):
@@ -26,7 +43,7 @@ st.sidebar.markdown("""
26
  # Header and intro
27
  st.title("Google Cloud NLP Entity Analyzer")
28
  st.write("This tool analyzes text to identify entities such as people, locations, organizations, and events")
29
- st.write("Entity salience scores are always relative to the analysed text. In natural language processing, a salience score is always a prediction of what a human would consider to be the most important entities in the same text.")
30
 
31
  def sample_analyze_entities(text_content):
32
  service_account_info = json.loads(st.secrets["google_nlp"])
@@ -58,8 +75,7 @@ def sample_analyze_entities(text_content):
58
 
59
  if 'mid' in entity.metadata and ('/g/' in entity.metadata['mid'] or '/m/' in entity.metadata['mid']):
60
  entity_id = entity.metadata['mid']
61
- ahref_link = f"https://kgsearch.googleapis.com/v1/entities:search?ids={entity_id}&key=credentials&limit=1&indent=True"
62
- st.markdown(f'<a href="{ahref_link}" target="_blank">Google Entity Link</a>', unsafe_allow_html=True)
63
 
64
  if entity.mentions:
65
  st.write("Mentions:")
 
2
  import streamlit as st
3
  from google.oauth2 import service_account
4
  from google.cloud import language_v1
5
+ import urllib.parse
6
+ import urllib.request
7
+
8
+ # Function to query Google's Knowledge Graph API
9
+ def query_knowledge_graph(entity_id):
10
+ service_url = 'https://kgsearch.googleapis.com/v1/entities:search'
11
+ params = {
12
+ 'ids': entity_id,
13
+ 'limit': 1,
14
+ 'indent': True,
15
+ 'key': st.secrets["kg_api"],
16
+ }
17
+ url = service_url + '?' + urllib.parse.urlencode(params)
18
+ response = json.loads(urllib.request.urlopen(url).read())
19
+ for element in response['itemListElement']:
20
+ st.write(f"Knowledge Graph name: {element['result']['name']}")
21
+ st.write(f"Knowledge Graph score: {element['resultScore']}")
22
 
23
  # Function to count entities with 'mid' that contains '/g/' or '/m/' in their metadata
24
  def count_entities(entities):
 
43
  # Header and intro
44
  st.title("Google Cloud NLP Entity Analyzer")
45
  st.write("This tool analyzes text to identify entities such as people, locations, organizations, and events")
46
+ st.write("Entity salience scores are always relative to the analysed text.")
47
 
48
  def sample_analyze_entities(text_content):
49
  service_account_info = json.loads(st.secrets["google_nlp"])
 
75
 
76
  if 'mid' in entity.metadata and ('/g/' in entity.metadata['mid'] or '/m/' in entity.metadata['mid']):
77
  entity_id = entity.metadata['mid']
78
+ query_knowledge_graph(entity_id)
 
79
 
80
  if entity.mentions:
81
  st.write("Mentions:")