Spaces:
Build error
Build error
Commit
·
015a0a7
1
Parent(s):
e7ccb62
Update app.py
Browse files
app.py
CHANGED
@@ -21,18 +21,11 @@ The API requires a high level of technical understanding, so this tool creates a
|
|
21 |
""")
|
22 |
|
23 |
def sample_analyze_entities(text_content, your_query=""):
|
24 |
-
# Parse the JSON string to a dictionary
|
25 |
service_account_info = json.loads(st.secrets["google_nlp"])
|
26 |
-
|
27 |
-
# Create credentials
|
28 |
credentials = service_account.Credentials.from_service_account_info(
|
29 |
service_account_info, scopes=["https://www.googleapis.com/auth/cloud-platform"]
|
30 |
)
|
31 |
-
|
32 |
-
# Initialize the LanguageServiceClient with the credentials
|
33 |
client = language_v1.LanguageServiceClient(credentials=credentials)
|
34 |
-
|
35 |
-
# NLP analysis
|
36 |
type_ = language_v1.Document.Type.PLAIN_TEXT
|
37 |
language = "en"
|
38 |
document = {"content": text_content, "type_": type_, "language": language}
|
@@ -40,57 +33,35 @@ def sample_analyze_entities(text_content, your_query=""):
|
|
40 |
|
41 |
response = client.analyze_entities(request={"document": document, "encoding_type": encoding_type})
|
42 |
|
43 |
-
# Create an empty list to hold the results
|
44 |
entities_list = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
|
46 |
-
for entity in response.entities:
|
47 |
-
entity_type_name = language_v1.Entity.Type(entity.type_).name
|
48 |
-
if not selected_types or entity_type_name in selected_types:
|
49 |
-
entity_details = {
|
50 |
-
"Name": entity.name,
|
51 |
-
"Type": entity_type_name,
|
52 |
-
"Salience Score": entity.salience,
|
53 |
-
"Metadata": [],
|
54 |
-
"Mentions": []
|
55 |
-
}
|
56 |
-
|
57 |
-
for metadata_name, metadata_value in entity.metadata.items():
|
58 |
-
entity_details["Metadata"].append({metadata_name: metadata_value})
|
59 |
-
|
60 |
-
for mention in entity.mentions:
|
61 |
-
entity_details["Mentions"].append({
|
62 |
-
"Text": mention.text.content,
|
63 |
-
"Type": language_v1.EntityMention.Type(mention.type_).name
|
64 |
-
})
|
65 |
-
|
66 |
-
entities_list.append(entity_details) # Append the dictionary to the list
|
67 |
-
|
68 |
-
|
69 |
-
# Streamlit UI
|
70 |
if your_query:
|
71 |
st.write(f"### We found {len(entities_list)} results for your query of **{your_query}**")
|
72 |
else:
|
73 |
st.write("### We found results for your query")
|
74 |
|
75 |
st.write("----")
|
76 |
-
|
77 |
for i, entity in enumerate(entities_list):
|
78 |
st.write(f"Relevance Score: {entity.get('Salience Score', 'N/A')} \t {i+1} of {len(entities_list)}")
|
79 |
-
|
80 |
-
# Display all key-value pairs in the entity dictionary
|
81 |
for key, value in entity.items():
|
82 |
if value:
|
83 |
st.write(f"**{key}:**")
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
else:
|
90 |
-
st.write(value)
|
91 |
-
|
92 |
-
st.write("----") # Line separator
|
93 |
-
st.write(" ") # Extra line
|
94 |
|
95 |
st.write(f"### Language of the text: {response.language}")
|
96 |
|
|
|
21 |
""")
|
22 |
|
23 |
def sample_analyze_entities(text_content, your_query=""):
|
|
|
24 |
service_account_info = json.loads(st.secrets["google_nlp"])
|
|
|
|
|
25 |
credentials = service_account.Credentials.from_service_account_info(
|
26 |
service_account_info, scopes=["https://www.googleapis.com/auth/cloud-platform"]
|
27 |
)
|
|
|
|
|
28 |
client = language_v1.LanguageServiceClient(credentials=credentials)
|
|
|
|
|
29 |
type_ = language_v1.Document.Type.PLAIN_TEXT
|
30 |
language = "en"
|
31 |
document = {"content": text_content, "type_": type_, "language": language}
|
|
|
33 |
|
34 |
response = client.analyze_entities(request={"document": document, "encoding_type": encoding_type})
|
35 |
|
|
|
36 |
entities_list = []
|
37 |
+
for entity in response.entities:
|
38 |
+
entity_type_name = language_v1.Entity.Type(entity.type_).name
|
39 |
+
if not selected_types or entity_type_name in selected_types:
|
40 |
+
entity_details = {
|
41 |
+
"Name": entity.name,
|
42 |
+
"Type": entity_type_name,
|
43 |
+
"Salience Score": entity.salience,
|
44 |
+
"Metadata": entity.metadata,
|
45 |
+
"Mentions": [mention.text.content for mention in entity.mentions]
|
46 |
+
}
|
47 |
+
entities_list.append(entity_details)
|
48 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
if your_query:
|
50 |
st.write(f"### We found {len(entities_list)} results for your query of **{your_query}**")
|
51 |
else:
|
52 |
st.write("### We found results for your query")
|
53 |
|
54 |
st.write("----")
|
|
|
55 |
for i, entity in enumerate(entities_list):
|
56 |
st.write(f"Relevance Score: {entity.get('Salience Score', 'N/A')} \t {i+1} of {len(entities_list)}")
|
|
|
|
|
57 |
for key, value in entity.items():
|
58 |
if value:
|
59 |
st.write(f"**{key}:**")
|
60 |
+
try:
|
61 |
+
st.json(value)
|
62 |
+
except Exception as e:
|
63 |
+
st.write(f"Error while displaying JSON: {e}")
|
64 |
+
st.write("----")
|
|
|
|
|
|
|
|
|
|
|
65 |
|
66 |
st.write(f"### Language of the text: {response.language}")
|
67 |
|