blazingbunny's picture
Update app.py
f0b9975
raw
history blame
1.88 kB
import json
import streamlit as st
from google.oauth2 import service_account
from google.cloud import language_v1
# ... (sidebar and headers are the same)
def sample_analyze_entities(text_content, your_query=""):
# ... (NLP setup is the same)
entities_list = []
for entity in response.entities:
entity_details = {
"Name": entity.name,
"Type": language_v1.Entity.Type(entity.type_).name,
"Salience Score": entity.salience,
"Metadata": entity.metadata,
"Mentions": [mention.text.content for mention in entity.mentions]
}
entities_list.append(entity_details)
if your_query:
st.write(f"### We found {len(entities_list)} results for your query of **{your_query}**")
else:
st.write("### We found results for your query")
st.write("----")
st.write("----")
for i, entity in enumerate(entities_list):
st.write(f"Entity {i+1} of {len(entities_list)}")
st.write(f"Relevance Score: {round(entity.get('Salience Score', 0) * 100)}%")
st.write(f"Name: {entity.get('Name', 'N/A')}")
st.write(f"Type: {entity.get('Type', 'N/A')}")
st.write(f"Salience Score: {entity.get('Salience Score', 'N/A')}")
metadata = entity.get('Metadata', {})
if metadata:
st.write("Metadata:")
st.write(metadata)
mentions = entity.get('Mentions', [])
if mentions:
st.write("Mentions:")
st.write(', '.join(mentions))
st.write("----")
st.write(f"### Language of the text: {response.language}")
# User input for text analysis
user_input = st.text_area("Enter text to analyze")
your_query = st.text_input("Enter your query (optional)")
if st.button("Analyze"):
sample_analyze_entities(user_input, your_query)