|
|
""" |
|
|
Tasty Bytes Cortex Analyst - Demo Space |
|
|
======================================== |
|
|
An interactive showcase of the Tasty Bytes Customer Analytics Cortex Analyst agent. |
|
|
|
|
|
This Gradio app demonstrates: |
|
|
- Natural language querying capabilities |
|
|
- Example queries and results |
|
|
- Architecture and setup instructions |
|
|
- Live demo video |
|
|
|
|
|
Note: This is a demo/showcase space. The actual Cortex Analyst runs on Snowflake infrastructure. |
|
|
To try it yourself, follow the setup instructions in the GitHub repository. |
|
|
""" |
|
|
|
|
|
import gradio as gr |
|
|
import pandas as pd |
|
|
|
|
|
|
|
|
sample_customer_counts = pd.DataFrame([ |
|
|
{"Rank": 1, "Country": "United States", "Customers": 5420, "Total Sales": "$1,250,890.75"}, |
|
|
{"Rank": 2, "Country": "India", "Customers": 3890, "Total Sales": "$980,230.50"}, |
|
|
{"Rank": 3, "Country": "Egypt", "Customers": 2110, "Total Sales": "$520,450.00"}, |
|
|
]) |
|
|
|
|
|
sample_top_customers = pd.DataFrame([ |
|
|
{"Customer ID": 110913, "Name": "Anna Sanchez", "City": "Boston", "Total Sales": "$3,302.00"}, |
|
|
{"Customer ID": 130576, "Name": "Grace Kline", "City": "Mumbai", "Total Sales": "$2,809.50"}, |
|
|
{"Customer ID": 90298, "Name": "Dahlia Buchanan", "City": "Cairo", "Total Sales": "$1,745.75"}, |
|
|
]) |
|
|
|
|
|
example_queries = [ |
|
|
("How many customers are in our loyalty program?", "SELECT COUNT(DISTINCT customer_id) FROM customer_loyalty_metrics_v", "11,420 customers"), |
|
|
("Which countries have the most customers?", "SELECT country, COUNT(DISTINCT customer_id) FROM customer_loyalty_metrics_v GROUP BY country ORDER BY COUNT(DISTINCT customer_id) DESC", "See table below"), |
|
|
("Show me the top 5 customers by total sales", "SELECT customer_id, first_name, last_name, city, total_sales FROM customer_loyalty_metrics_v ORDER BY total_sales DESC LIMIT 5", "See table below"), |
|
|
("What's the total sales by country?", "SELECT country, SUM(total_sales) FROM customer_loyalty_metrics_v GROUP BY country ORDER BY SUM(total_sales)", "See results in table"), |
|
|
] |
|
|
|
|
|
def create_demo(): |
|
|
with gr.Blocks(theme=gr.themes.Soft(), title="Tasty Bytes Cortex Analyst Demo") as demo: |
|
|
gr.Markdown(""" |
|
|
# π Tasty Bytes Customer Analytics - Cortex Analyst Demo |
|
|
|
|
|
**Natural Language Interface for Customer Loyalty Data** |
|
|
|
|
|
This demo showcases a Snowflake Cortex Analyst agent built on the TASTY_BYTES public dataset. |
|
|
Ask questions in plain English and get instant insights - no SQL knowledge required! |
|
|
|
|
|
β οΈ **Note**: This is a demonstration space. To try the actual agent, follow the setup instructions in the GitHub repository. |
|
|
""") |
|
|
|
|
|
|
|
|
with gr.Tab("π₯ Demo Video"): |
|
|
gr.Markdown(""" |
|
|
## Watch the Full Demo |
|
|
|
|
|
See Cortex Analyst in action: natural language queries, automatic SQL generation, and instant results. |
|
|
""") |
|
|
|
|
|
|
|
|
gr.Markdown(""" |
|
|
πΉ **Demo video coming soon!** |
|
|
|
|
|
The video will demonstrate: |
|
|
- Asking questions in natural language |
|
|
- Automatic SQL generation by Cortex Analyst |
|
|
- Real-time results and insights |
|
|
- Various query types (counts, aggregations, rankings) |
|
|
|
|
|
**Until then, explore the other tabs to see example queries and results!** |
|
|
""") |
|
|
|
|
|
|
|
|
with gr.Tab("π‘ Example Queries"): |
|
|
gr.Markdown(""" |
|
|
## Try These Questions |
|
|
|
|
|
Here are some examples of natural language queries you can ask the Cortex Analyst agent: |
|
|
""") |
|
|
|
|
|
for i, (question, sql, result) in enumerate(example_queries, 1): |
|
|
with gr.Accordion(f"Example {i}: {question}", open=(i==1)): |
|
|
gr.Markdown(f"**Natural Language Query:**") |
|
|
gr.Code(question, language=None) |
|
|
|
|
|
gr.Markdown(f"**Generated SQL:**") |
|
|
gr.Code(sql, language="sql") |
|
|
|
|
|
gr.Markdown(f"**Result:**") |
|
|
gr.Markdown(f"`{result}`") |
|
|
|
|
|
gr.Markdown(""" |
|
|
### More Example Questions: |
|
|
|
|
|
**Customer Analytics:** |
|
|
- "How many customers do we have in total?" |
|
|
- "Show me all customers from Boston" |
|
|
- "Which customer has the highest total sales?" |
|
|
- "List customers who have visited more than 50 locations" |
|
|
|
|
|
**Geographic Analysis:** |
|
|
- "What cities have the most customers?" |
|
|
- "Compare customer counts across countries" |
|
|
- "Show me the geographic distribution of our customers" |
|
|
|
|
|
**Sales Insights:** |
|
|
- "What's the average sales per customer?" |
|
|
- "Which country generates the most revenue?" |
|
|
- "Show me total sales by city" |
|
|
- "Who are our top 10 customers by spend?" |
|
|
""") |
|
|
|
|
|
|
|
|
with gr.Tab("π Sample Results"): |
|
|
gr.Markdown(""" |
|
|
## Example Query Results |
|
|
|
|
|
Here's what the actual results look like when you query the agent: |
|
|
""") |
|
|
|
|
|
gr.Markdown("### Query: 'Which countries have the highest number of customers?'") |
|
|
gr.Dataframe(sample_customer_counts, label="Customer Count by Country") |
|
|
|
|
|
gr.Markdown("---") |
|
|
|
|
|
gr.Markdown("### Query: 'Show me the top 3 customers by total sales'") |
|
|
gr.Dataframe(sample_top_customers, label="Top Customers") |
|
|
|
|
|
gr.Markdown(""" |
|
|
--- |
|
|
π‘ **Tip**: All numeric values are automatically rounded to 2 decimal places as specified |
|
|
in the semantic view's custom instructions. |
|
|
""") |
|
|
|
|
|
|
|
|
with gr.Tab("ποΈ Architecture"): |
|
|
gr.Markdown(""" |
|
|
## System Architecture |
|
|
|
|
|
The Cortex Analyst agent uses a layered architecture to transform natural language |
|
|
into actionable insights: |
|
|
""") |
|
|
|
|
|
gr.Markdown(""" |
|
|
``` |
|
|
βββββββββββββββββββββββββββββββββββββββββββ |
|
|
β Business User β |
|
|
β (Natural Language Questions) β |
|
|
ββββββββββββββββ¬βββββββββββββββββββββββββββ |
|
|
β |
|
|
βΌ |
|
|
βββββββββββββββββββββββββββββββββββββββββββ |
|
|
β Cortex Analyst (AI Layer) β |
|
|
β β’ Understands natural language β |
|
|
β β’ Maps questions to business concepts β |
|
|
β β’ Generates SQL queries β |
|
|
ββββββββββββββββ¬βββββββββββββββββββββββββββ |
|
|
β |
|
|
βΌ |
|
|
βββββββββββββββββββββββββββββββββββββββββββ |
|
|
β Semantic View (Business Logic) β |
|
|
β β’ Business-friendly names β |
|
|
β β’ Metrics & dimensions β |
|
|
β β’ Pre-verified queries β |
|
|
β β’ Custom instructions β |
|
|
ββββββββββββββββ¬βββββββββββββββββββββββββββ |
|
|
β |
|
|
βΌ |
|
|
βββββββββββββββββββββββββββββββββββββββββββ |
|
|
β TASTY_BYTES Dataset β |
|
|
β β’ Customer loyalty data β |
|
|
β β’ Order history β |
|
|
β β’ Geographic information β |
|
|
βββββββββββββββββββββββββββββββββββββββββββ |
|
|
``` |
|
|
""") |
|
|
|
|
|
gr.Markdown(""" |
|
|
### Key Components: |
|
|
|
|
|
**1. Data Layer** - Snowflake TASTY_BYTES public dataset |
|
|
- Customer loyalty program data |
|
|
- Order transactions |
|
|
- Location and geographic information |
|
|
|
|
|
**2. Semantic Layer** - Native Snowflake Semantic View |
|
|
- Translates technical column names to business terms |
|
|
- Defines metrics (e.g., "total sales") and dimensions (e.g., "country") |
|
|
- Includes verified query examples |
|
|
- Enforces data access controls |
|
|
|
|
|
**3. AI Layer** - Cortex Analyst |
|
|
- Powered by LLM (Large Language Model) |
|
|
- Understands natural language intent |
|
|
- Automatically generates SQL queries |
|
|
- Returns results in business-friendly format |
|
|
|
|
|
**4. Interface** - Snowflake Intelligence & Cortex Analyst UI |
|
|
- Chat-based interaction |
|
|
- Visual result presentation |
|
|
- Query history and refinement |
|
|
""") |
|
|
|
|
|
|
|
|
with gr.Tab("π Setup Guide"): |
|
|
gr.Markdown(""" |
|
|
## How to Set This Up Yourself |
|
|
|
|
|
Follow these steps to create your own Cortex Analyst agent with the TASTY_BYTES dataset: |
|
|
|
|
|
### Prerequisites |
|
|
- Snowflake account (free trial available at [signup.snowflake.com](https://signup.snowflake.com)) |
|
|
- ACCOUNTADMIN role or equivalent permissions |
|
|
- Cortex Analyst feature enabled (available in most regions) |
|
|
|
|
|
### Step 1: Clone the Repository |
|
|
```bash |
|
|
git clone https://github.com/aamanlamba/tasty-bytes-cortex-analyst.git |
|
|
cd tasty-bytes-cortex-analyst |
|
|
``` |
|
|
|
|
|
### Step 2: Load the TASTY_BYTES Dataset |
|
|
```sql |
|
|
-- Execute in Snowflake Snowsight or your SQL client |
|
|
-- This creates the database, schemas, and loads sample data |
|
|
@scripts/load_tasty_bytes_data.sql |
|
|
``` |
|
|
β±οΈ Takes approximately 5-10 minutes |
|
|
|
|
|
### Step 3: Create the Semantic View |
|
|
```sql |
|
|
-- Execute the semantic view creation script |
|
|
@scripts/create_semantic_view.sql |
|
|
``` |
|
|
β±οΈ Takes less than 1 minute |
|
|
|
|
|
### Step 4: Create the Cortex Analyst Agent |
|
|
|
|
|
1. Navigate to **AI & ML** β **Cortex Analyst** in Snowsight |
|
|
2. Click **Create new agent** |
|
|
3. Configure: |
|
|
- **Name**: Tasty Bytes Customer Analytics |
|
|
- **Description**: Natural language interface for customer data |
|
|
4. Add Tool: |
|
|
- **Type**: Semantic View |
|
|
- **View**: `HARMONIZEDCUSTOMERMETRICSSEMANTICVIEW` |
|
|
- **Description**: |
|
|
``` |
|
|
Use this tool to answer questions about Tasty Bytes customer |
|
|
loyalty program metrics, including customer demographics, |
|
|
total sales, and location visit patterns. |
|
|
``` |
|
|
5. Click **Create agent** |
|
|
|
|
|
### Step 5: Test Your Agent |
|
|
|
|
|
Try asking: |
|
|
- "How many customers are in our loyalty program?" |
|
|
- "Which countries have the most customers?" |
|
|
- "Show me the top 10 customers by sales" |
|
|
|
|
|
### π Full Documentation |
|
|
|
|
|
For detailed instructions, troubleshooting, and advanced customization: |
|
|
- [GitHub Repository](https://github.com/aamanlamba/tasty-bytes-cortex-analyst) |
|
|
- [Snowflake Cortex Analyst Docs](https://docs.snowflake.com/en/user-guide/snowflake-cortex/cortex-analyst) |
|
|
- [Semantic Views Guide](https://docs.snowflake.com/en/user-guide/views-semantic/overview) |
|
|
""") |
|
|
|
|
|
|
|
|
with gr.Tab("βΉοΈ About"): |
|
|
gr.Markdown(""" |
|
|
## About This Project |
|
|
|
|
|
This demo showcases **Snowflake Cortex Analyst**, an AI-powered natural language |
|
|
interface for querying structured data. The project uses the publicly available |
|
|
TASTY_BYTES sample dataset from Snowflake. |
|
|
|
|
|
### Dataset |
|
|
**TASTY_BYTES** represents a fictional global food truck franchise with: |
|
|
- 11,420+ customer loyalty program members |
|
|
- Order transaction history |
|
|
- Multiple countries and cities |
|
|
- Customer demographics and preferences |
|
|
|
|
|
### Features Demonstrated |
|
|
- β
Natural language to SQL translation |
|
|
- β
Automatic query generation |
|
|
- β
Business-friendly semantic layer |
|
|
- β
Pre-verified query examples |
|
|
- β
Custom SQL generation instructions |
|
|
|
|
|
### Use Cases |
|
|
This pattern applies to many industries: |
|
|
- **Retail**: Customer segmentation, sales analysis |
|
|
- **Finance**: Client analytics, transaction patterns |
|
|
- **Healthcare**: Patient demographics, visit patterns |
|
|
- **SaaS**: User engagement, feature adoption |
|
|
- **E-commerce**: Customer lifetime value, purchase behavior |
|
|
|
|
|
### Author |
|
|
**Aaman Lamba** |
|
|
- Strategy Consultant & Author |
|
|
- AI Governance & Data Economy Expert |
|
|
- Former Senior Industry Principal, Infosys |
|
|
- IAPP Certified AI Governance Professional (in progress) |
|
|
|
|
|
### Resources |
|
|
- π¦ [GitHub Repository](https://github.com/aamanlamba/tasty-bytes-cortex-analyst) |
|
|
- π [Snowflake Documentation](https://docs.snowflake.com/en/user-guide/snowflake-cortex/cortex-analyst) |
|
|
- πΌ [LinkedIn](https://linkedin.com/in/aamanlamba) |
|
|
|
|
|
### License |
|
|
MIT License - Open source and free to use |
|
|
|
|
|
--- |
|
|
|
|
|
β **Like this project?** Star it on [GitHub](https://github.com/aamanlamba/tasty-bytes-cortex-analyst)! |
|
|
|
|
|
π **Found an issue?** [Report it here](https://github.com/aamanlamba/tasty-bytes-cortex-analyst/issues) |
|
|
""") |
|
|
|
|
|
return demo |
|
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
|
demo = create_demo() |
|
|
demo.launch() |
|
|
|