blazingbunny commited on
Commit
9502681
·
1 Parent(s): 4194e16

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -4
app.py CHANGED
@@ -1,13 +1,63 @@
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
- pass # Remove this line when you add your code back
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
 
10
- # Streamlit app
11
  st.title('Google Cloud NLP Entity Analyzer')
12
  user_input = st.text_area("Enter text to analyze", "Your text goes here")
13
 
 
 
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
+ service_account_info = json.loads(st.secrets["google_nlp"])
8
+ credentials = service_account.Credentials.from_service_account_info(
9
+ service_account_info, scopes=["https://www.googleapis.com/auth/cloud-platform"]
10
+ )
11
+ client = language_v1.LanguageServiceClient(credentials=credentials)
12
+
13
+ type_ = language_v1.Document.Type.PLAIN_TEXT
14
+ language = "en"
15
+ document = {"content": text_content, "type_": type_, "language": language}
16
+ encoding_type = language_v1.EncodingType.UTF8
17
+
18
+ response = client.analyze_entities(request={"document": document, "encoding_type": encoding_type})
19
+
20
+ # Create an empty list to hold the results
21
+ entities_list = []
22
+
23
+ for entity in response.entities:
24
+ # Create a dictionary to hold individual entity details
25
+ entity_details = {
26
+ "Name": entity.name,
27
+ "Type": language_v1.Entity.Type(entity.type_).name,
28
+ "Salience Score": entity.salience,
29
+ "Metadata": [],
30
+ "Mentions": []
31
+ }
32
+
33
+ for metadata_name, metadata_value in entity.metadata.items():
34
+ entity_details["Metadata"].append({metadata_name: metadata_value})
35
+
36
+ for mention in entity.mentions:
37
+ entity_details["Mentions"].append({
38
+ "Text": mention.text.content,
39
+ "Type": language_v1.EntityMention.Type(mention.type_).name
40
+ })
41
+
42
+ # Append the dictionary to the list
43
+ entities_list.append(entity_details)
44
+
45
+ # Use Streamlit to display the results
46
+ st.write("### Analyzed Entities")
47
+ for entity in entities_list:
48
+ st.write(f"**Name**: {entity['Name']}")
49
+ st.write(f"**Type**: {entity['Type']}")
50
+ st.write(f"**Salience Score**: {entity['Salience Score']}")
51
+
52
+ if entity["Metadata"]:
53
+ st.write("**Metadata**: ")
54
+ st.json(entity["Metadata"])
55
+
56
+ if entity["Mentions"]:
57
+ st.write("**Mentions**: ")
58
+ st.json(entity["Mentions"])
59
+
60
 
 
61
  st.title('Google Cloud NLP Entity Analyzer')
62
  user_input = st.text_area("Enter text to analyze", "Your text goes here")
63