Spaces:
Sleeping
Sleeping
Commit
·
e5f77a9
1
Parent(s):
8deafd3
Update app.py
Browse files
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 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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):
|