blazingbunny commited on
Commit
676e72f
·
verified ·
1 Parent(s): ad1dcf0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -13
app.py CHANGED
@@ -2,6 +2,8 @@ import json
2
  import streamlit as st
3
  from google.oauth2 import service_account
4
  from google.cloud import language_v1
 
 
5
  import pandas as pd
6
 
7
  # Function to query Google's Knowledge Graph API
@@ -12,9 +14,14 @@ def query_knowledge_graph(entity_id):
12
  except Exception as e:
13
  st.write(f"An error occurred: {e}")
14
 
15
- # Function to filter entities with "mid" in their metadata
16
- def filter_entities_with_mid(entities):
17
- return [entity for entity in entities if 'mid' in entity.metadata]
 
 
 
 
 
18
 
19
  # Function to serialize entity metadata
20
  def serialize_entity_metadata(metadata):
@@ -73,14 +80,22 @@ def sample_analyze_entities(text_content):
73
 
74
  response = client.analyze_entities(request={"document": document, "encoding_type": encoding_type})
75
 
76
- # Filter entities that have a "mid" in their metadata
77
- entities_with_mid = filter_entities_with_mid(response.entities)
78
-
79
- st.markdown(f"# We found {len(entities_with_mid)} entities with 'mid' in their metadata")
80
- st.write("---")
81
-
82
- for i, entity in enumerate(entities_with_mid):
83
- st.write(f"Entity {i+1} of {len(entities_with_mid)}")
 
 
 
 
 
 
 
 
84
  st.write(f"Name: {entity.name}")
85
  st.write(f"Type: {language_v1.Entity.Type(entity.type_).name}")
86
  st.write(f"Salience Score: {entity.salience}")
@@ -89,7 +104,7 @@ def sample_analyze_entities(text_content):
89
  st.write("Metadata:")
90
  st.write(entity.metadata)
91
 
92
- if 'mid' in entity.metadata:
93
  entity_id = entity.metadata['mid']
94
  query_knowledge_graph(entity_id)
95
 
@@ -103,7 +118,7 @@ def sample_analyze_entities(text_content):
103
  st.write("---")
104
 
105
  # Add the export functionality
106
- export_entities(entities_with_mid)
107
 
108
  # User input for text analysis
109
  user_input = st.text_area("Enter text to analyze")
 
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
  import pandas as pd
8
 
9
  # Function to query Google's Knowledge Graph API
 
14
  except Exception as e:
15
  st.write(f"An error occurred: {e}")
16
 
17
+ # Function to count entities with 'mid' that contains '/g/' or '/m/' in their metadata
18
+ def count_entities(entities):
19
+ count = 0
20
+ for entity in entities:
21
+ metadata = entity.metadata
22
+ if 'mid' in metadata and ('/g/' in metadata['mid'] or '/m/' in metadata['mid']):
23
+ count += 1
24
+ return count
25
 
26
  # Function to serialize entity metadata
27
  def serialize_entity_metadata(metadata):
 
80
 
81
  response = client.analyze_entities(request={"document": document, "encoding_type": encoding_type})
82
 
83
+ # Count the entities with 'mid' and either '/g/' or '/m/' in their metadata
84
+ entity_count = count_entities(response.entities)
85
+
86
+ if entity_count == 0:
87
+ st.markdown(f"# We found {len(response.entities)} entities - but found no Google Entities")
88
+ st.write("---")
89
+ elif entity_count == 1:
90
+ st.markdown(f"# We found {len(response.entities)} entities - and found 1 Google Entity")
91
+ st.write("---")
92
+ else:
93
+ st.markdown(f"# We found {len(response.entities)} entities - and found {entity_count} Google Entities")
94
+ st.write("---")
95
+
96
+
97
+ for i, entity in enumerate(response.entities):
98
+ st.write(f"Entity {i+1} of {len(response.entities)}")
99
  st.write(f"Name: {entity.name}")
100
  st.write(f"Type: {language_v1.Entity.Type(entity.type_).name}")
101
  st.write(f"Salience Score: {entity.salience}")
 
104
  st.write("Metadata:")
105
  st.write(entity.metadata)
106
 
107
+ if 'mid' in entity.metadata and ('/g/' in entity.metadata['mid'] or '/m/' in entity.metadata['mid']):
108
  entity_id = entity.metadata['mid']
109
  query_knowledge_graph(entity_id)
110
 
 
118
  st.write("---")
119
 
120
  # Add the export functionality
121
+ export_entities(response.entities)
122
 
123
  # User input for text analysis
124
  user_input = st.text_area("Enter text to analyze")