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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -12
app.py CHANGED
@@ -7,18 +7,22 @@ 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):
 
7
 
8
  # Function to query Google's Knowledge Graph API
9
  def query_knowledge_graph(entity_id):
10
+ try:
11
+ service_url = 'https://kgsearch.googleapis.com/v1/entities:search'
12
+ params = {
13
+ 'ids': entity_id,
14
+ 'limit': 1,
15
+ 'indent': True,
16
+ 'key': st.secrets["kg_api"],
17
+ }
18
+ url = service_url + '?' + urllib.parse.urlencode(params)
19
+ response = json.loads(urllib.request.urlopen(url).read())
20
+ for element in response['itemListElement']:
21
+ st.write(f"Knowledge Graph name: {element['result']['name']}")
22
+ st.write(f"Knowledge Graph score: {element['resultScore']}")
23
+ except Exception as e:
24
+ st.write(f"An error occurred while querying the Knowledge Graph: {e}")
25
+
26
 
27
  # Function to count entities with 'mid' that contains '/g/' or '/m/' in their metadata
28
  def count_entities(entities):