Spaces:
Build error
Build error
Commit
·
6038ff0
1
Parent(s):
8c77761
Update app.py
Browse files
app.py
CHANGED
@@ -4,50 +4,43 @@ from google.oauth2 import service_account
|
|
4 |
from google.cloud import language_v1
|
5 |
import requests
|
6 |
|
7 |
-
# Adding checkbox options for entity types
|
8 |
-
entity_types_to_show = ["PERSON", "ORGANIZATION", "EVENT"]
|
9 |
-
selected_types = st.multiselect('Select entity types to show:', entity_types_to_show)
|
10 |
-
|
11 |
# Function for querying Google Knowledge Graph API
|
12 |
-
def query_google_knowledge_graph(entity_name):
|
13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
|
15 |
# Header and intro
|
16 |
st.title("Google Cloud NLP Entity Analyzer")
|
17 |
st.write("## Introduction to the Knowledge Graph API")
|
18 |
st.write("---")
|
19 |
-
|
20 |
-
The Google Knowledge Graph API reveals entity information related to a keyword, that Google knows about.
|
21 |
-
This information can be very useful for SEO – discovering related topics and what Google believes is relevant.
|
22 |
-
It can also help when trying to claim/win a Knowledge Graph box on search results.
|
23 |
-
The API requires a high level of technical understanding, so this tool creates a simple public interface, with the ability to export data into spreadsheets.
|
24 |
-
""")
|
25 |
|
26 |
def sample_analyze_entities(text_content, your_query=""):
|
27 |
-
|
28 |
credentials = service_account.Credentials.from_service_account_info(
|
29 |
-
|
30 |
)
|
31 |
client = language_v1.LanguageServiceClient(credentials=credentials)
|
32 |
-
type_ = language_v1.Document.Type.PLAIN_TEXT
|
33 |
-
language = "en"
|
34 |
-
document = {"content": text_content, "type_": type_, "language": language}
|
35 |
-
encoding_type = language_v1.EncodingType.UTF8
|
36 |
-
|
37 |
-
response = client.analyze_entities(request={"document": document, "encoding_type": encoding_type})
|
38 |
|
|
|
39 |
entities_list = []
|
40 |
for entity in response.entities:
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
}
|
50 |
-
entities_list.append(entity_details)
|
51 |
|
52 |
if your_query:
|
53 |
st.write(f"### We found {len(entities_list)} results for your query of **{your_query}**")
|
@@ -56,21 +49,15 @@ def sample_analyze_entities(text_content, your_query=""):
|
|
56 |
|
57 |
st.write("----")
|
58 |
for i, entity in enumerate(entities_list):
|
59 |
-
|
60 |
-
|
61 |
-
if value:
|
62 |
-
st.write(f"**{key}:**")
|
63 |
-
st.write(value)
|
64 |
-
|
65 |
# Query Google Knowledge Graph API for each entity
|
66 |
-
kg_info = query_google_knowledge_graph(entity['Name'])
|
67 |
st.write("### Google Knowledge Graph Information")
|
68 |
-
st.
|
69 |
|
70 |
st.write("----")
|
71 |
|
72 |
-
st.write(f"### Language of the text: {response.language}")
|
73 |
-
|
74 |
# User input for text analysis
|
75 |
user_input = st.text_area("Enter text to analyze")
|
76 |
your_query = st.text_input("Enter your query (optional)")
|
|
|
4 |
from google.cloud import language_v1
|
5 |
import requests
|
6 |
|
|
|
|
|
|
|
|
|
7 |
# Function for querying Google Knowledge Graph API
|
8 |
+
def query_google_knowledge_graph(api_key, entity_name):
|
9 |
+
query = entity_name
|
10 |
+
service_url = "https://kgsearch.googleapis.com/v1/entities:search"
|
11 |
+
params = {
|
12 |
+
'query': query,
|
13 |
+
'limit': 1,
|
14 |
+
'indent': True,
|
15 |
+
'key': api_key,
|
16 |
+
}
|
17 |
+
response = requests.get(service_url, params=params)
|
18 |
+
return response.json()
|
19 |
|
20 |
# Header and intro
|
21 |
st.title("Google Cloud NLP Entity Analyzer")
|
22 |
st.write("## Introduction to the Knowledge Graph API")
|
23 |
st.write("---")
|
24 |
+
# ... (your intro text here)
|
|
|
|
|
|
|
|
|
|
|
25 |
|
26 |
def sample_analyze_entities(text_content, your_query=""):
|
27 |
+
api_key = json.loads(st.secrets["google_nlp"]) # The key is the same for both APIs
|
28 |
credentials = service_account.Credentials.from_service_account_info(
|
29 |
+
api_key, scopes=["https://www.googleapis.com/auth/cloud-platform"]
|
30 |
)
|
31 |
client = language_v1.LanguageServiceClient(credentials=credentials)
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
|
33 |
+
# ... (rest of your NLP code)
|
34 |
entities_list = []
|
35 |
for entity in response.entities:
|
36 |
+
entity_details = {
|
37 |
+
"Name": entity.name,
|
38 |
+
"Type": language_v1.Entity.Type(entity.type_).name,
|
39 |
+
"Salience Score": entity.salience,
|
40 |
+
"Metadata": entity.metadata,
|
41 |
+
"Mentions": [mention.text.content for mention in entity.mentions]
|
42 |
+
}
|
43 |
+
entities_list.append(entity_details)
|
|
|
|
|
44 |
|
45 |
if your_query:
|
46 |
st.write(f"### We found {len(entities_list)} results for your query of **{your_query}**")
|
|
|
49 |
|
50 |
st.write("----")
|
51 |
for i, entity in enumerate(entities_list):
|
52 |
+
# ... (your existing entity display code)
|
53 |
+
|
|
|
|
|
|
|
|
|
54 |
# Query Google Knowledge Graph API for each entity
|
55 |
+
kg_info = query_google_knowledge_graph(api_key, entity['Name'])
|
56 |
st.write("### Google Knowledge Graph Information")
|
57 |
+
st.json(kg_info) # Display the JSON response
|
58 |
|
59 |
st.write("----")
|
60 |
|
|
|
|
|
61 |
# User input for text analysis
|
62 |
user_input = st.text_area("Enter text to analyze")
|
63 |
your_query = st.text_input("Enter your query (optional)")
|