Update app.py
Browse files
app.py
CHANGED
@@ -4,8 +4,8 @@ from google.oauth2 import service_account
|
|
4 |
from google.cloud import language_v1
|
5 |
import urllib.parse
|
6 |
import urllib.request
|
|
|
7 |
|
8 |
-
# Function to query Google's Knowledge Graph API
|
9 |
# Function to query Google's Knowledge Graph API
|
10 |
def query_knowledge_graph(entity_id):
|
11 |
try:
|
@@ -14,7 +14,6 @@ def query_knowledge_graph(entity_id):
|
|
14 |
except Exception as e:
|
15 |
st.write(f"An error occurred: {e}")
|
16 |
|
17 |
-
|
18 |
# Function to count entities with 'mid' that contains '/g/' or '/m/' in their metadata
|
19 |
def count_entities(entities):
|
20 |
count = 0
|
@@ -24,6 +23,30 @@ def count_entities(entities):
|
|
24 |
count += 1
|
25 |
return count
|
26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
# Sidebar content
|
28 |
st.sidebar.title("About This Tool")
|
29 |
st.sidebar.markdown("This tool leverages Google's NLP technology for entity analysis.")
|
@@ -33,12 +56,13 @@ st.sidebar.markdown("""
|
|
33 |
2. **User Input**: Enter the text you want to analyze.
|
34 |
3. **Analyze**: Click the 'Analyze' button.
|
35 |
4. **View Results**: See the identified entities and their details.
|
|
|
36 |
""")
|
37 |
|
38 |
# Header and intro
|
39 |
st.title("Google Cloud NLP Entity Analyzer")
|
40 |
-
st.write("This tool analyzes text to identify entities such as people, locations, organizations, and events")
|
41 |
-
st.write("Entity salience scores are always relative to the
|
42 |
|
43 |
def sample_analyze_entities(text_content):
|
44 |
service_account_info = json.loads(st.secrets["google_nlp"])
|
@@ -86,14 +110,14 @@ def sample_analyze_entities(text_content):
|
|
86 |
st.write(f"Mentions: {mention_count} mention{plural}")
|
87 |
st.write("Raw Array:")
|
88 |
st.write(entity.mentions)
|
89 |
-
# st.write(', '.join([mention.text.content for mention in entity.mentions]))
|
90 |
-
|
91 |
|
92 |
st.write("---")
|
93 |
|
|
|
|
|
|
|
94 |
# User input for text analysis
|
95 |
user_input = st.text_area("Enter text to analyze")
|
96 |
-
#user_input = st.text_area("Enter text to analyze", max_chars=5000)
|
97 |
|
98 |
if st.button("Analyze"):
|
99 |
if user_input:
|
|
|
4 |
from google.cloud import language_v1
|
5 |
import urllib.parse
|
6 |
import urllib.request
|
7 |
+
import pandas as pd
|
8 |
|
|
|
9 |
# Function to query Google's Knowledge Graph API
|
10 |
def query_knowledge_graph(entity_id):
|
11 |
try:
|
|
|
14 |
except Exception as e:
|
15 |
st.write(f"An error occurred: {e}")
|
16 |
|
|
|
17 |
# Function to count entities with 'mid' that contains '/g/' or '/m/' in their metadata
|
18 |
def count_entities(entities):
|
19 |
count = 0
|
|
|
23 |
count += 1
|
24 |
return count
|
25 |
|
26 |
+
# Function to export entities as a JSON or CSV file
|
27 |
+
def export_entities(entities):
|
28 |
+
entity_list = []
|
29 |
+
for entity in entities:
|
30 |
+
entity_info = {
|
31 |
+
"Name": entity.name,
|
32 |
+
"Type": language_v1.Entity.Type(entity.type_).name,
|
33 |
+
"Salience Score": entity.salience,
|
34 |
+
"Metadata": entity.metadata,
|
35 |
+
"Mentions": [mention.text.content for mention in entity.mentions]
|
36 |
+
}
|
37 |
+
entity_list.append(entity_info)
|
38 |
+
|
39 |
+
# Convert to DataFrame for easier export as CSV
|
40 |
+
df = pd.DataFrame(entity_list)
|
41 |
+
|
42 |
+
# Export as CSV
|
43 |
+
csv = df.to_csv(index=False)
|
44 |
+
st.download_button(label="Export Entities as CSV", data=csv, file_name="entities.csv", mime="text/csv")
|
45 |
+
|
46 |
+
# Export as JSON
|
47 |
+
json_data = json.dumps(entity_list, indent=2)
|
48 |
+
st.download_button(label="Export Entities as JSON", data=json_data, file_name="entities.json", mime="application/json")
|
49 |
+
|
50 |
# Sidebar content
|
51 |
st.sidebar.title("About This Tool")
|
52 |
st.sidebar.markdown("This tool leverages Google's NLP technology for entity analysis.")
|
|
|
56 |
2. **User Input**: Enter the text you want to analyze.
|
57 |
3. **Analyze**: Click the 'Analyze' button.
|
58 |
4. **View Results**: See the identified entities and their details.
|
59 |
+
5. **Export Entities**: Export the entities as JSON or CSV.
|
60 |
""")
|
61 |
|
62 |
# Header and intro
|
63 |
st.title("Google Cloud NLP Entity Analyzer")
|
64 |
+
st.write("This tool analyzes text to identify entities such as people, locations, organizations, and events.")
|
65 |
+
st.write("Entity salience scores are always relative to the analyzed text.")
|
66 |
|
67 |
def sample_analyze_entities(text_content):
|
68 |
service_account_info = json.loads(st.secrets["google_nlp"])
|
|
|
110 |
st.write(f"Mentions: {mention_count} mention{plural}")
|
111 |
st.write("Raw Array:")
|
112 |
st.write(entity.mentions)
|
|
|
|
|
113 |
|
114 |
st.write("---")
|
115 |
|
116 |
+
# Add the export functionality
|
117 |
+
export_entities(response.entities)
|
118 |
+
|
119 |
# User input for text analysis
|
120 |
user_input = st.text_area("Enter text to analyze")
|
|
|
121 |
|
122 |
if st.button("Analyze"):
|
123 |
if user_input:
|