aamanlamba commited on
Commit
b45622c
Β·
verified Β·
1 Parent(s): 7a75a93

Initial commit

Browse files
Files changed (3) hide show
  1. README.md +85 -6
  2. app.py +328 -0
  3. requirements.txt +2 -0
README.md CHANGED
@@ -1,14 +1,93 @@
1
  ---
2
- title: TastyBytesCortexAnalyst
3
- emoji: πŸ“Š
4
  colorFrom: blue
5
- colorTo: gray
6
  sdk: gradio
7
- sdk_version: 6.0.2
8
  app_file: app.py
9
  pinned: false
10
  license: mit
11
- short_description: Snowflake Cortex Analyst example on the TastyBytes dataset
12
  ---
13
 
14
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
+ title: Tasty Bytes Cortex Analyst Demo
3
+ emoji: πŸ”
4
  colorFrom: blue
5
+ colorTo: purple
6
  sdk: gradio
7
+ sdk_version: 4.44.0
8
  app_file: app.py
9
  pinned: false
10
  license: mit
 
11
  ---
12
 
13
+ # Tasty Bytes Customer Analytics - Cortex Analyst Demo
14
+
15
+ This interactive demo showcases a natural language interface for customer data analytics built with **Snowflake Cortex Analyst**.
16
+
17
+ ## 🎯 What This Demonstrates
18
+
19
+ Ask questions in plain English and get instant insights from customer loyalty data:
20
+
21
+ - "How many customers are in our loyalty program?"
22
+ - "Which countries have the most customers?"
23
+ - "Show me the top 10 customers by total sales"
24
+ - "What's the average sales per customer in each country?"
25
+
26
+ **No SQL knowledge required!** The AI understands your questions and generates the appropriate queries automatically.
27
+
28
+ ## πŸ—οΈ Architecture
29
+
30
+ This demo is built on:
31
+ - **Data Layer**: Snowflake TASTY_BYTES public dataset
32
+ - **Semantic Layer**: Native Snowflake Semantic View (business-friendly data definitions)
33
+ - **AI Layer**: Cortex Analyst (LLM-powered query understanding)
34
+ - **Interface**: Chat-based natural language interface
35
+
36
+ ## ⚠️ Important Note
37
+
38
+ This HuggingFace Space is a **demonstration and showcase** only. The actual Cortex Analyst agent runs on Snowflake infrastructure, not within this Space.
39
+
40
+ To try the real thing:
41
+ 1. Get a Snowflake account (free trial available)
42
+ 2. Follow the setup instructions in the [GitHub repository](https://github.com/yourusername/tasty-bytes-cortex-analyst)
43
+ 3. Complete setup in 20-30 minutes
44
+
45
+ ## πŸ“š What You'll Find Here
46
+
47
+ - **Demo Video**: See Cortex Analyst in action
48
+ - **Example Queries**: Sample questions you can ask
49
+ - **Sample Results**: Real query outputs
50
+ - **Architecture**: How the system works
51
+ - **Setup Guide**: Build it yourself step-by-step
52
+
53
+ ## πŸš€ Try It Yourself
54
+
55
+ **GitHub Repository**: [tasty-bytes-cortex-analyst](https://github.com/yourusername/tasty-bytes-cortex-analyst)
56
+
57
+ The repository includes:
58
+ - Complete setup scripts
59
+ - Semantic view YAML definition
60
+ - Detailed documentation
61
+ - 50+ example queries
62
+ - Troubleshooting guide
63
+
64
+ ## πŸ“Š Dataset
65
+
66
+ Uses Snowflake's **TASTY_BYTES** sample dataset:
67
+ - 11,420+ customer loyalty members
68
+ - Geographic data (30+ countries)
69
+ - Sales transactions
70
+ - Location visit history
71
+
72
+ This is publicly available sample data - no real customer information.
73
+
74
+ ## πŸ‘€ Author
75
+
76
+ **Aaman Lamba**
77
+ - Strategy Consultant & Author
78
+ - AI Governance & Data Economy Expert
79
+ - Former Senior Industry Principal, Infosys
80
+
81
+ **Connect:**
82
+ - [LinkedIn](https://linkedin.com/in/aamanlamba)
83
+ - [GitHub](https://github.com/aamanlamba)
84
+
85
+ ## πŸ“„ License
86
+
87
+ MIT License - Free to use and modify
88
+
89
+ ---
90
+
91
+ ⭐ **Like this demo?** Star the [GitHub repository](https://github.com/yourusername/tasty-bytes-cortex-analyst)!
92
+
93
+ πŸ› **Questions or issues?** [Open an issue](https://github.com/yourusername/tasty-bytes-cortex-analyst/issues)
app.py ADDED
@@ -0,0 +1,328 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Tasty Bytes Cortex Analyst - Demo Space
3
+ ========================================
4
+ An interactive showcase of the Tasty Bytes Customer Analytics Cortex Analyst agent.
5
+
6
+ This Gradio app demonstrates:
7
+ - Natural language querying capabilities
8
+ - Example queries and results
9
+ - Architecture and setup instructions
10
+ - Live demo video
11
+
12
+ Note: This is a demo/showcase space. The actual Cortex Analyst runs on Snowflake infrastructure.
13
+ To try it yourself, follow the setup instructions in the GitHub repository.
14
+ """
15
+
16
+ import gradio as gr
17
+ import pandas as pd
18
+
19
+ # Sample data for demonstration
20
+ sample_customer_counts = pd.DataFrame([
21
+ {"Rank": 1, "Country": "United States", "Customers": 5420, "Total Sales": "$1,250,890.75"},
22
+ {"Rank": 2, "Country": "India", "Customers": 3890, "Total Sales": "$980,230.50"},
23
+ {"Rank": 3, "Country": "Egypt", "Customers": 2110, "Total Sales": "$520,450.00"},
24
+ ])
25
+
26
+ sample_top_customers = pd.DataFrame([
27
+ {"Customer ID": 110913, "Name": "Anna Sanchez", "City": "Boston", "Total Sales": "$3,302.00"},
28
+ {"Customer ID": 130576, "Name": "Grace Kline", "City": "Mumbai", "Total Sales": "$2,809.50"},
29
+ {"Customer ID": 90298, "Name": "Dahlia Buchanan", "City": "Cairo", "Total Sales": "$1,745.75"},
30
+ ])
31
+
32
+ example_queries = [
33
+ ("How many customers are in our loyalty program?", "SELECT COUNT(DISTINCT customer_id) FROM customer_loyalty_metrics_v", "11,420 customers"),
34
+ ("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"),
35
+ ("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"),
36
+ ("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"),
37
+ ]
38
+
39
+ def create_demo():
40
+ with gr.Blocks(theme=gr.themes.Soft(), title="Tasty Bytes Cortex Analyst Demo") as demo:
41
+ gr.Markdown("""
42
+ # πŸ” Tasty Bytes Customer Analytics - Cortex Analyst Demo
43
+
44
+ **Natural Language Interface for Customer Loyalty Data**
45
+
46
+ This demo showcases a Snowflake Cortex Analyst agent built on the TASTY_BYTES public dataset.
47
+ Ask questions in plain English and get instant insights - no SQL knowledge required!
48
+
49
+ ⚠️ **Note**: This is a demonstration space. To try the actual agent, follow the setup instructions in the GitHub repository.
50
+ """)
51
+
52
+ # Main demo section
53
+ with gr.Tab("πŸŽ₯ Demo Video"):
54
+ gr.Markdown("""
55
+ ## Watch the Full Demo
56
+
57
+ See Cortex Analyst in action: natural language queries, automatic SQL generation, and instant results.
58
+ """)
59
+
60
+ # Placeholder for video (you'll upload your actual demo video)
61
+ gr.Markdown("""
62
+ πŸ“Ή **Demo video coming soon!**
63
+
64
+ The video will demonstrate:
65
+ - Asking questions in natural language
66
+ - Automatic SQL generation by Cortex Analyst
67
+ - Real-time results and insights
68
+ - Various query types (counts, aggregations, rankings)
69
+
70
+ **Until then, explore the other tabs to see example queries and results!**
71
+ """)
72
+
73
+ # Example queries tab
74
+ with gr.Tab("πŸ’‘ Example Queries"):
75
+ gr.Markdown("""
76
+ ## Try These Questions
77
+
78
+ Here are some examples of natural language queries you can ask the Cortex Analyst agent:
79
+ """)
80
+
81
+ for i, (question, sql, result) in enumerate(example_queries, 1):
82
+ with gr.Accordion(f"Example {i}: {question}", open=(i==1)):
83
+ gr.Markdown(f"**Natural Language Query:**")
84
+ gr.Code(question, language=None)
85
+
86
+ gr.Markdown(f"**Generated SQL:**")
87
+ gr.Code(sql, language="sql")
88
+
89
+ gr.Markdown(f"**Result:**")
90
+ gr.Markdown(f"`{result}`")
91
+
92
+ gr.Markdown("""
93
+ ### More Example Questions:
94
+
95
+ **Customer Analytics:**
96
+ - "How many customers do we have in total?"
97
+ - "Show me all customers from Boston"
98
+ - "Which customer has the highest total sales?"
99
+ - "List customers who have visited more than 50 locations"
100
+
101
+ **Geographic Analysis:**
102
+ - "What cities have the most customers?"
103
+ - "Compare customer counts across countries"
104
+ - "Show me the geographic distribution of our customers"
105
+
106
+ **Sales Insights:**
107
+ - "What's the average sales per customer?"
108
+ - "Which country generates the most revenue?"
109
+ - "Show me total sales by city"
110
+ - "Who are our top 10 customers by spend?"
111
+ """)
112
+
113
+ # Sample results tab
114
+ with gr.Tab("πŸ“Š Sample Results"):
115
+ gr.Markdown("""
116
+ ## Example Query Results
117
+
118
+ Here's what the actual results look like when you query the agent:
119
+ """)
120
+
121
+ gr.Markdown("### Query: 'Which countries have the highest number of customers?'")
122
+ gr.Dataframe(sample_customer_counts, label="Customer Count by Country")
123
+
124
+ gr.Markdown("---")
125
+
126
+ gr.Markdown("### Query: 'Show me the top 3 customers by total sales'")
127
+ gr.Dataframe(sample_top_customers, label="Top Customers")
128
+
129
+ gr.Markdown("""
130
+ ---
131
+ πŸ’‘ **Tip**: All numeric values are automatically rounded to 2 decimal places as specified
132
+ in the semantic view's custom instructions.
133
+ """)
134
+
135
+ # Architecture tab
136
+ with gr.Tab("πŸ—οΈ Architecture"):
137
+ gr.Markdown("""
138
+ ## System Architecture
139
+
140
+ The Cortex Analyst agent uses a layered architecture to transform natural language
141
+ into actionable insights:
142
+ """)
143
+
144
+ gr.Markdown("""
145
+ ```
146
+ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
147
+ β”‚ Business User β”‚
148
+ β”‚ (Natural Language Questions) β”‚
149
+ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
150
+ β”‚
151
+ β–Ό
152
+ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
153
+ β”‚ Cortex Analyst (AI Layer) β”‚
154
+ β”‚ β€’ Understands natural language β”‚
155
+ β”‚ β€’ Maps questions to business concepts β”‚
156
+ β”‚ β€’ Generates SQL queries β”‚
157
+ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
158
+ β”‚
159
+ β–Ό
160
+ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
161
+ β”‚ Semantic View (Business Logic) β”‚
162
+ β”‚ β€’ Business-friendly names β”‚
163
+ β”‚ β€’ Metrics & dimensions β”‚
164
+ β”‚ β€’ Pre-verified queries β”‚
165
+ β”‚ β€’ Custom instructions β”‚
166
+ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
167
+ β”‚
168
+ β–Ό
169
+ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
170
+ β”‚ TASTY_BYTES Dataset β”‚
171
+ β”‚ β€’ Customer loyalty data β”‚
172
+ β”‚ β€’ Order history β”‚
173
+ β”‚ β€’ Geographic information β”‚
174
+ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
175
+ ```
176
+ """)
177
+
178
+ gr.Markdown("""
179
+ ### Key Components:
180
+
181
+ **1. Data Layer** - Snowflake TASTY_BYTES public dataset
182
+ - Customer loyalty program data
183
+ - Order transactions
184
+ - Location and geographic information
185
+
186
+ **2. Semantic Layer** - Native Snowflake Semantic View
187
+ - Translates technical column names to business terms
188
+ - Defines metrics (e.g., "total sales") and dimensions (e.g., "country")
189
+ - Includes verified query examples
190
+ - Enforces data access controls
191
+
192
+ **3. AI Layer** - Cortex Analyst
193
+ - Powered by LLM (Large Language Model)
194
+ - Understands natural language intent
195
+ - Automatically generates SQL queries
196
+ - Returns results in business-friendly format
197
+
198
+ **4. Interface** - Snowflake Intelligence & Cortex Analyst UI
199
+ - Chat-based interaction
200
+ - Visual result presentation
201
+ - Query history and refinement
202
+ """)
203
+
204
+ # Setup guide tab
205
+ with gr.Tab("πŸš€ Setup Guide"):
206
+ gr.Markdown("""
207
+ ## How to Set This Up Yourself
208
+
209
+ Follow these steps to create your own Cortex Analyst agent with the TASTY_BYTES dataset:
210
+
211
+ ### Prerequisites
212
+ - Snowflake account (free trial available at [signup.snowflake.com](https://signup.snowflake.com))
213
+ - ACCOUNTADMIN role or equivalent permissions
214
+ - Cortex Analyst feature enabled (available in most regions)
215
+
216
+ ### Step 1: Clone the Repository
217
+ ```bash
218
+ git clone https://github.com/yourusername/tasty-bytes-cortex-analyst.git
219
+ cd tasty-bytes-cortex-analyst
220
+ ```
221
+
222
+ ### Step 2: Load the TASTY_BYTES Dataset
223
+ ```sql
224
+ -- Execute in Snowflake Snowsight or your SQL client
225
+ -- This creates the database, schemas, and loads sample data
226
+ @scripts/load_tasty_bytes_data.sql
227
+ ```
228
+ ⏱️ Takes approximately 5-10 minutes
229
+
230
+ ### Step 3: Create the Semantic View
231
+ ```sql
232
+ -- Execute the semantic view creation script
233
+ @scripts/create_semantic_view.sql
234
+ ```
235
+ ⏱️ Takes less than 1 minute
236
+
237
+ ### Step 4: Create the Cortex Analyst Agent
238
+
239
+ 1. Navigate to **AI & ML** β†’ **Cortex Analyst** in Snowsight
240
+ 2. Click **Create new agent**
241
+ 3. Configure:
242
+ - **Name**: Tasty Bytes Customer Analytics
243
+ - **Description**: Natural language interface for customer data
244
+ 4. Add Tool:
245
+ - **Type**: Semantic View
246
+ - **View**: `HARMONIZEDCUSTOMERMETRICSSEMANTICVIEW`
247
+ - **Description**:
248
+ ```
249
+ Use this tool to answer questions about Tasty Bytes customer
250
+ loyalty program metrics, including customer demographics,
251
+ total sales, and location visit patterns.
252
+ ```
253
+ 5. Click **Create agent**
254
+
255
+ ### Step 5: Test Your Agent
256
+
257
+ Try asking:
258
+ - "How many customers are in our loyalty program?"
259
+ - "Which countries have the most customers?"
260
+ - "Show me the top 10 customers by sales"
261
+
262
+ ### πŸ“š Full Documentation
263
+
264
+ For detailed instructions, troubleshooting, and advanced customization:
265
+ - [GitHub Repository](https://github.com/yourusername/tasty-bytes-cortex-analyst)
266
+ - [Snowflake Cortex Analyst Docs](https://docs.snowflake.com/en/user-guide/snowflake-cortex/cortex-analyst)
267
+ - [Semantic Views Guide](https://docs.snowflake.com/en/user-guide/views-semantic/overview)
268
+ """)
269
+
270
+ # About tab
271
+ with gr.Tab("ℹ️ About"):
272
+ gr.Markdown("""
273
+ ## About This Project
274
+
275
+ This demo showcases **Snowflake Cortex Analyst**, an AI-powered natural language
276
+ interface for querying structured data. The project uses the publicly available
277
+ TASTY_BYTES sample dataset from Snowflake.
278
+
279
+ ### Dataset
280
+ **TASTY_BYTES** represents a fictional global food truck franchise with:
281
+ - 11,420+ customer loyalty program members
282
+ - Order transaction history
283
+ - Multiple countries and cities
284
+ - Customer demographics and preferences
285
+
286
+ ### Features Demonstrated
287
+ - βœ… Natural language to SQL translation
288
+ - βœ… Automatic query generation
289
+ - βœ… Business-friendly semantic layer
290
+ - βœ… Pre-verified query examples
291
+ - βœ… Custom SQL generation instructions
292
+
293
+ ### Use Cases
294
+ This pattern applies to many industries:
295
+ - **Retail**: Customer segmentation, sales analysis
296
+ - **Finance**: Client analytics, transaction patterns
297
+ - **Healthcare**: Patient demographics, visit patterns
298
+ - **SaaS**: User engagement, feature adoption
299
+ - **E-commerce**: Customer lifetime value, purchase behavior
300
+
301
+ ### Author
302
+ **Aaman Lamba**
303
+ - Strategy Consultant & Author
304
+ - AI Governance & Data Economy Expert
305
+ - Former Senior Industry Principal, Infosys
306
+ - IAPP Certified AI Governance Professional (in progress)
307
+
308
+ ### Resources
309
+ - πŸ“¦ [GitHub Repository](https://github.com/yourusername/tasty-bytes-cortex-analyst)
310
+ - πŸ“– [Snowflake Documentation](https://docs.snowflake.com/en/user-guide/snowflake-cortex/cortex-analyst)
311
+ - πŸ’Ό [LinkedIn](https://linkedin.com/in/aamanlamba)
312
+
313
+ ### License
314
+ MIT License - Open source and free to use
315
+
316
+ ---
317
+
318
+ ⭐ **Like this project?** Star it on [GitHub](https://github.com/yourusername/tasty-bytes-cortex-analyst)!
319
+
320
+ οΏ½οΏ½ **Found an issue?** [Report it here](https://github.com/yourusername/tasty-bytes-cortex-analyst/issues)
321
+ """)
322
+
323
+ return demo
324
+
325
+ # Launch the app
326
+ if __name__ == "__main__":
327
+ demo = create_demo()
328
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ gradio==4.44.0
2
+ pandas==2.1.4