Spaces:
Sleeping
Sleeping
Commit
·
f3178b1
1
Parent(s):
8c32010
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,16 +9,6 @@ entity_types_to_show = [
|
|
| 9 |
]
|
| 10 |
selected_types = st.multiselect('Select entity types to show:', entity_types_to_show)
|
| 11 |
|
| 12 |
-
def sample_analyze_entities(text_content, selected_types):
|
| 13 |
-
# Existing code for setting up credentials and client...
|
| 14 |
-
|
| 15 |
-
# Create an empty list to hold the results
|
| 16 |
-
entities_list = []
|
| 17 |
-
|
| 18 |
-
for entity in response.entities:
|
| 19 |
-
entity_type_name = language_v1.Entity.Type(entity.type_).name
|
| 20 |
-
if entity_type_name in selected_types:
|
| 21 |
-
|
| 22 |
# Header and intro
|
| 23 |
st.title("Google Cloud NLP Entity Analyzer")
|
| 24 |
st.write("## Introduction to the Knowledge Graph API")
|
|
@@ -54,51 +44,10 @@ def sample_analyze_entities(text_content, your_query=""):
|
|
| 54 |
entities_list = []
|
| 55 |
|
| 56 |
for entity in response.entities:
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
"Salience Score": entity.salience,
|
| 62 |
-
"Metadata": [],
|
| 63 |
-
"Mentions": []
|
| 64 |
-
}
|
| 65 |
-
|
| 66 |
-
for metadata_name, metadata_value in entity.metadata.items():
|
| 67 |
-
entity_details["Metadata"].append({metadata_name: metadata_value})
|
| 68 |
-
|
| 69 |
-
for mention in entity.mentions:
|
| 70 |
-
entity_details["Mentions"].append({
|
| 71 |
-
"Text": mention.text.content,
|
| 72 |
-
"Type": language_v1.EntityMention.Type(mention.type_).name
|
| 73 |
-
})
|
| 74 |
-
|
| 75 |
-
# Append the dictionary to the list
|
| 76 |
-
entities_list.append(entity_details)
|
| 77 |
-
|
| 78 |
-
# Streamlit UI
|
| 79 |
-
if your_query:
|
| 80 |
-
st.write(f"### We found {len(entities_list)} results for your query of **{your_query}**")
|
| 81 |
-
else:
|
| 82 |
-
st.write("### We found results for your query")
|
| 83 |
-
|
| 84 |
-
st.write("----")
|
| 85 |
-
|
| 86 |
-
for i, entity in enumerate(entities_list):
|
| 87 |
-
st.write(f"Relevance Score: {entity.get('Salience Score', 'N/A')} \t {i+1} of {len(entities_list)}")
|
| 88 |
-
|
| 89 |
-
# Display all key-value pairs in the entity dictionary
|
| 90 |
-
for key, value in entity.items():
|
| 91 |
-
if value:
|
| 92 |
-
st.write(f"**{key}:**")
|
| 93 |
-
if isinstance(value, (list, dict)):
|
| 94 |
-
st.json(value)
|
| 95 |
-
else:
|
| 96 |
-
st.write(value)
|
| 97 |
-
|
| 98 |
-
st.write("----")
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
st.write(f"### Language of the text: {response.language}")
|
| 102 |
|
| 103 |
# User input for text analysis
|
| 104 |
user_input = st.text_area("Enter text to analyze")
|
|
|
|
| 9 |
]
|
| 10 |
selected_types = st.multiselect('Select entity types to show:', entity_types_to_show)
|
| 11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
# Header and intro
|
| 13 |
st.title("Google Cloud NLP Entity Analyzer")
|
| 14 |
st.write("## Introduction to the Knowledge Graph API")
|
|
|
|
| 44 |
entities_list = []
|
| 45 |
|
| 46 |
for entity in response.entities:
|
| 47 |
+
entity_type_name = language_v1.Entity.Type(entity.type_).name
|
| 48 |
+
if entity_type_name in selected_types:
|
| 49 |
+
# Rest of your code to handle each entity
|
| 50 |
+
# ...
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
# User input for text analysis
|
| 53 |
user_input = st.text_area("Enter text to analyze")
|