Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -28,18 +28,25 @@ def count_entities(entities):
|
|
28 |
def serialize_entity_metadata(metadata):
|
29 |
return {k: str(v) for k, v in metadata.items()}
|
30 |
|
31 |
-
# Function to export entities as a JSON or CSV file
|
32 |
def export_entities(entities):
|
33 |
entity_list = []
|
34 |
for entity in entities:
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
|
44 |
# Convert to DataFrame for easier export as CSV
|
45 |
df = pd.DataFrame(entity_list)
|
@@ -64,8 +71,6 @@ st.sidebar.markdown("""
|
|
64 |
5. **Export Entities**: Export the entities as JSON or CSV.
|
65 |
""")
|
66 |
|
67 |
-
|
68 |
-
|
69 |
# Header and intro
|
70 |
st.title("Google Cloud NLP Entity Analyzer")
|
71 |
st.write("This tool analyzes text to identify entities such as people, locations, organizations, and events.")
|
|
|
28 |
def serialize_entity_metadata(metadata):
|
29 |
return {k: str(v) for k, v in metadata.items()}
|
30 |
|
31 |
+
# Function to export entities as a JSON or CSV file, only exporting entities with 'mid' in their metadata
|
32 |
def export_entities(entities):
|
33 |
entity_list = []
|
34 |
for entity in entities:
|
35 |
+
# Check if entity has 'mid' in its metadata and if it contains '/g/' or '/m/' in 'mid'
|
36 |
+
if 'mid' in entity.metadata and ('/g/' in entity.metadata['mid'] or '/m/' in entity.metadata['mid']):
|
37 |
+
entity_info = {
|
38 |
+
"Name": entity.name,
|
39 |
+
"Type": language_v1.Entity.Type(entity.type_).name,
|
40 |
+
"Salience Score": entity.salience,
|
41 |
+
"Metadata": serialize_entity_metadata(entity.metadata),
|
42 |
+
"Mentions": [mention.text.content for mention in entity.mentions]
|
43 |
+
}
|
44 |
+
entity_list.append(entity_info)
|
45 |
+
|
46 |
+
# If there are no entities to export, notify the user
|
47 |
+
if not entity_list:
|
48 |
+
st.write("No entities with a valid 'mid' found to export.")
|
49 |
+
return
|
50 |
|
51 |
# Convert to DataFrame for easier export as CSV
|
52 |
df = pd.DataFrame(entity_list)
|
|
|
71 |
5. **Export Entities**: Export the entities as JSON or CSV.
|
72 |
""")
|
73 |
|
|
|
|
|
74 |
# Header and intro
|
75 |
st.title("Google Cloud NLP Entity Analyzer")
|
76 |
st.write("This tool analyzes text to identify entities such as people, locations, organizations, and events.")
|