Skip to content

Commit

Permalink
Clean Audiences code
Browse files Browse the repository at this point in the history
  • Loading branch information
egonrian committed Sep 15, 2023
1 parent e40c980 commit 910cce2
Showing 1 changed file with 21 additions and 15 deletions.
36 changes: 21 additions & 15 deletions app/pages/3_‎ ‎ ‎ ‎ ● Audiences.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@

"""
Audience and Insight finder:
- Create a conversational interface with data by translating from natural language to SQL queries.
- Create a conversational interface with data
by translating from natural language to SQL queries.
"""

import streamlit as st
Expand All @@ -41,7 +42,9 @@
TAG_NAME = data["pages"]["3_audiences"]["tag_name"]

TAG_TEMPLATE_NAME = f'projects/{PROJECT_ID}/locations/{LOCATION}/tagTemplates/{TAG_NAME}'
QUERY = f'SELECT * FROM `{PROJECT_ID}.{DATASET_ID}.INFORMATION_SCHEMA.TABLES` WHERE table_name NOT LIKE "%metadata%"'
QUERY = (
f'SELECT * FROM `{PROJECT_ID}.{DATASET_ID}.INFORMATION_SCHEMA.TABLES`'
' WHERE table_name NOT LIKE "%metadata%"')

bqclient = bigquery.Client(project=PROJECT_ID)
vertexai.init(project=PROJECT_ID, location=LOCATION)
Expand Down Expand Up @@ -73,12 +76,12 @@
st.title(data["pages"]["3_audiences"]["page_title"])

st.write(
"""
This page provides instructions on how to extract data from BigQuery using natural language and the PaLM API.
PaLM is a large language model from Google AI that can understand and respond to natural language queries.
By using PaLM, you can ask questions about your data in plain English, and PaLM will generate the
SQL queries necessary to retrieve the data.
"""
"This page provides instructions on how to extract data from BigQuery"
"using natural language and the PaLM API. "
"PaLM is a large language model from Google AI that can understand "
"and respond to natural language queries. "
"By using PaLM, you can ask questions about your data in plain English, "
"and PaLM will generate the SQL queries necessary to retrieve the data."
)

# =========================== Data preview =====================================
Expand All @@ -91,19 +94,22 @@
if PREVIEW_TABLES_KEY in st.session_state:
del st.session_state[PREVIEW_TABLES_KEY]
else:
st.session_state[PREVIEW_TABLES_KEY] = [
{'query': f'SELECT * FROM `{PROJECT_ID}.{DATASET_ID}.customers` LIMIT 3', 'name': 'customers'},
{'query': f'SELECT * FROM `{PROJECT_ID}.{DATASET_ID}.events` LIMIT 3', 'name': 'events'},
{'query': f'SELECT * FROM `{PROJECT_ID}.{DATASET_ID}.transactions` LIMIT 3', 'name': 'transactions'}]
dataset_path = f"{PROJECT_ID}.{DATASET_ID}"
query_template = f"SELECT * FROM `{dataset_path}.""{table}` LIMIT 3"
table_names = ["customers", "events", "transactions"]
st.session_state[PREVIEW_TABLES_KEY] = [{
'query': query_template.format(table=table_name),
'name': table_name
} for table_name in table_names]

if PREVIEW_TABLES_KEY in st.session_state:
if RESULT_PREVIEW_QUERY_KEY not in st.session_state:
result_query = []
with st.spinner('Querying BigQuery...'):
for preview_table in st.session_state[PREVIEW_TABLES_KEY]:
for table in st.session_state[PREVIEW_TABLES_KEY]:
result_query.append({
"name": preview_table['name'],
"dataframe": bqclient.query(preview_table['query']).to_dataframe()
"name": table['name'],
"dataframe": bqclient.query(table['query']).to_dataframe()
})
st.session_state[RESULT_PREVIEW_QUERY_KEY] = result_query

Expand Down

0 comments on commit 910cce2

Please sign in to comment.