Commit
·
726abaa
1
Parent(s):
7c393ac
Update app.py
Browse files
app.py
CHANGED
@@ -1,66 +1,17 @@
|
|
|
|
1 |
import json
|
2 |
from google.oauth2 import service_account
|
3 |
from google.cloud import language_v1
|
4 |
-
import streamlit as st
|
5 |
|
6 |
def sample_analyze_entities(text_content):
|
7 |
-
#
|
8 |
-
service_account_info = json.loads(st.secrets["google_nlp"])
|
9 |
-
|
10 |
-
# Create credentials
|
11 |
-
credentials = service_account.Credentials.from_service_account_info(
|
12 |
-
service_account_info, scopes=["https://www.googleapis.com/auth/cloud-platform"]
|
13 |
-
)
|
14 |
-
|
15 |
-
# Initialize the LanguageServiceClient with the credentials
|
16 |
-
client = language_v1.LanguageServiceClient(credentials=credentials)
|
17 |
-
|
18 |
-
type_ = language_v1.Document.Type.PLAIN_TEXT
|
19 |
-
language = "en"
|
20 |
-
document = {"content": text_content, "type_": type_, "language": language}
|
21 |
-
encoding_type = language_v1.EncodingType.UTF8
|
22 |
-
|
23 |
-
response = client.analyze_entities(request={"document": document, "encoding_type": encoding_type})
|
24 |
-
|
25 |
-
# Create an empty list to hold the results
|
26 |
-
entities_list = []
|
27 |
-
|
28 |
-
for entity in response.entities:
|
29 |
-
# Create a dictionary to hold individual entity details
|
30 |
-
entity_details = {
|
31 |
-
"Name": entity.name,
|
32 |
-
"Type": language_v1.Entity.Type(entity.type_).name,
|
33 |
-
"Salience Score": entity.salience,
|
34 |
-
"Metadata": [],
|
35 |
-
"Mentions": []
|
36 |
-
}
|
37 |
-
|
38 |
-
for metadata_name, metadata_value in entity.metadata.items():
|
39 |
-
entity_details["Metadata"].append({metadata_name: metadata_value})
|
40 |
-
|
41 |
-
for mention in entity.mentions:
|
42 |
-
entity_details["Mentions"].append({
|
43 |
-
"Text": mention.text.content,
|
44 |
-
"Type": language_v1.EntityMention.Type(mention.type_).name
|
45 |
-
})
|
46 |
-
|
47 |
-
# Append the dictionary to the list
|
48 |
-
entities_list.append(entity_details)
|
49 |
-
|
50 |
-
# Use Streamlit to display the results
|
51 |
-
st.write("### Analyzed Entities")
|
52 |
-
for entity in entities_list:
|
53 |
-
st.write(f"**Name**: {entity['Name']}")
|
54 |
-
st.write(f"**Type**: {entity['Type']}")
|
55 |
-
st.write(f"**Salience Score**: {entity['Salience Score']}")
|
56 |
-
|
57 |
-
if entity["Metadata"]:
|
58 |
-
st.write("**Metadata**: ")
|
59 |
-
st.json(entity["Metadata"])
|
60 |
-
|
61 |
-
if entity["Mentions"]:
|
62 |
-
st.write("**Mentions**: ")
|
63 |
-
st.json(entity["Mentions"])
|
64 |
|
|
|
|
|
|
|
65 |
|
66 |
-
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
import json
|
3 |
from google.oauth2 import service_account
|
4 |
from google.cloud import language_v1
|
|
|
5 |
|
6 |
def sample_analyze_entities(text_content):
|
7 |
+
# Your existing code here
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
|
9 |
+
# Streamlit app
|
10 |
+
st.title('Google Cloud NLP Entity Analyzer')
|
11 |
+
user_input = st.text_area("Enter text to analyze", "Your text goes here")
|
12 |
|
13 |
+
if st.button('Analyze'):
|
14 |
+
if user_input:
|
15 |
+
sample_analyze_entities(user_input)
|
16 |
+
else:
|
17 |
+
st.write("Please enter some text to analyze.")
|