File size: 15,399 Bytes
e135aef
 
 
 
9231df6
e135aef
 
 
 
 
 
 
 
 
 
9231df6
e135aef
 
 
 
 
 
 
9231df6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e135aef
 
9231df6
31e4035
e135aef
 
9231df6
e135aef
 
9231df6
e135aef
4b4dd57
9231df6
e135aef
 
9231df6
 
 
e135aef
 
9231df6
 
 
 
 
e135aef
 
9231df6
e135aef
 
 
 
 
 
9231df6
 
 
e135aef
9231df6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e135aef
9231df6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e135aef
 
 
 
 
 
9231df6
e135aef
4b4dd57
9231df6
31e4035
 
4b4dd57
9ef8ded
9231df6
31e4035
9231df6
4b4dd57
31e4035
9231df6
4b4dd57
 
 
e135aef
 
 
 
 
 
 
 
 
 
 
9231df6
 
 
e135aef
9231df6
e135aef
9231df6
 
 
 
 
e135aef
 
9231df6
 
e135aef
 
 
 
 
 
 
 
9231df6
4b4dd57
e135aef
 
 
 
 
 
 
9231df6
e135aef
 
 
 
 
 
 
 
9231df6
9ef8ded
31e4035
 
e135aef
 
9231df6
e135aef
 
 
 
 
31e4035
e135aef
 
 
 
 
 
 
 
 
 
 
9231df6
 
 
 
e135aef
 
 
 
 
 
 
31e4035
e135aef
9231df6
e135aef
9231df6
e135aef
9231df6
 
 
e135aef
 
 
 
 
 
 
 
9231df6
e135aef
 
 
 
 
 
9231df6
e135aef
31e4035
e135aef
 
 
 
 
 
 
31e4035
e135aef
 
 
 
 
 
 
 
 
 
 
 
 
 
9231df6
e135aef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9231df6
 
 
 
 
 
 
e135aef
9231df6
 
 
 
 
e135aef
9231df6
 
 
 
 
e135aef
9231df6
e135aef
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
import gradio as gr
import pandas as pd
import duckdb
from datasets import load_dataset
from huggingface_hub import login
import openai
import os
from typing import Dict, List, Any

class SALTAnalytics:
    def __init__(self):
        """Initialize SALT Analytics"""
        self.con = duckdb.connect(':memory:')
        self.data_loaded = False
        self.schema_info = ""
        self.available_columns = []
        
    def load_salt_dataset(self):
        """Load SAP SALT dataset from Hugging Face into DuckDB"""
        if self.data_loaded:
            return "Dataset already loaded!"
            
        try:
            # Try loading with authentication
            hf_token = os.getenv('HF_TOKEN')
            
            if hf_token:
                dataset = load_dataset(
                    "SAP/SALT", 
                    "joined_table", 
                    split="train", 
                    token=hf_token,
                    streaming=False
                )
            else:
                dataset = load_dataset(
                    "SAP/SALT", 
                    "joined_table", 
                    split="train", 
                    use_auth_token=True,
                    streaming=False
                )
            
            df = dataset.to_pandas()
            
            # Sample data for demo if too large
            if len(df) > 100000:
                df = df.sample(n=50000, random_state=42)
            
            # Load into DuckDB
            self.con.execute("CREATE TABLE salt_data AS SELECT * FROM df")
            
            # Get schema information and available columns
            schema_result = self.con.execute("DESCRIBE salt_data").fetchall()
            self.schema_info = "\n".join([f"{col[0]}: {col[1]}" for col in schema_result])
            self.available_columns = [col[0] for col in schema_result]
            
            self.data_loaded = True
            
            # Return success message with column info
            return f"βœ… Successfully loaded {len(df)} records into DuckDB\n\nπŸ“‹ Available columns:\n" + "\n".join(f"β€’ {col}" for col in self.available_columns[:20]) + ("\n... and more" if len(self.available_columns) > 20 else "")
            
        except Exception as e:
            error_msg = str(e)
            if "gated dataset" in error_msg or "authentication" in error_msg.lower():
                return f"❌ Authentication Error: {error_msg}\n\nTo fix this:\n1. Go to https://huggingface.co/datasets/SAP/SALT\n2. Request access to the dataset\n3. Wait for approval\n4. Set HF_TOKEN in your Space secrets"
            else:
                return f"❌ Error loading dataset: {error_msg}"
    
    def get_predefined_insights(self):
        """Generate predefined analytical insights using correct column names"""
        if not self.data_loaded:
            return "Please load the dataset first"
            
        try:
            insights = {}
            
            # Find the right customer and sales office columns
            customer_col = None
            sales_office_col = None
            
            # Look for customer-related columns
            for col in self.available_columns:
                if 'CUSTOMER' in col.upper() and ('ID' in col.upper() or 'NUM' in col.upper()):
                    customer_col = col
                    break
                elif 'SHIP' in col.upper() and 'PARTY' in col.upper():
                    customer_col = col  # ShipToParty is often used as customer identifier
                    break
            
            # Look for sales office column
            for col in self.available_columns:
                if 'SALES' in col.upper() and 'OFFICE' in col.upper():
                    sales_office_col = col
                    break
            
            # Sales Office Performance (adjusted for available columns)
            if sales_office_col:
                if customer_col:
                    insights['Sales Office Performance'] = self.con.execute(f"""
                        SELECT {sales_office_col}, 
                               COUNT(*) as total_orders,
                               COUNT(DISTINCT {customer_col}) as unique_customers
                        FROM salt_data 
                        WHERE {sales_office_col} IS NOT NULL
                        GROUP BY {sales_office_col}
                        ORDER BY total_orders DESC
                        LIMIT 10
                    """).fetchdf()
                else:
                    insights['Sales Office Performance'] = self.con.execute(f"""
                        SELECT {sales_office_col}, 
                               COUNT(*) as total_orders
                        FROM salt_data 
                        WHERE {sales_office_col} IS NOT NULL
                        GROUP BY {sales_office_col}
                        ORDER BY total_orders DESC
                        LIMIT 10
                    """).fetchdf()
            
            # Payment Terms Distribution (if available)
            if 'CUSTOMERPAYMENTTERMS' in self.available_columns:
                insights['Payment Terms Distribution'] = self.con.execute("""
                    SELECT CUSTOMERPAYMENTTERMS,
                           COUNT(*) as frequency,
                           ROUND(COUNT(*) * 100.0 / SUM(COUNT(*)) OVER(), 2) as percentage
                    FROM salt_data 
                    WHERE CUSTOMERPAYMENTTERMS IS NOT NULL
                    GROUP BY CUSTOMERPAYMENTTERMS
                    ORDER BY frequency DESC
                    LIMIT 10
                """).fetchdf()
            
            # Shipping Conditions Analysis (look for shipping-related columns)
            shipping_col = None
            plant_col = None
            
            for col in self.available_columns:
                if 'SHIPPING' in col.upper() and 'CONDITION' in col.upper():
                    shipping_col = col
                elif 'PLANT' in col.upper():
                    plant_col = col
            
            if shipping_col:
                if plant_col:
                    insights['Shipping Conditions'] = self.con.execute(f"""
                        SELECT {shipping_col},
                               COUNT(*) as order_count,
                               COUNT(DISTINCT {plant_col}) as plants_served
                        FROM salt_data
                        WHERE {shipping_col} IS NOT NULL
                        GROUP BY {shipping_col}
                        ORDER BY order_count DESC
                        LIMIT 10
                    """).fetchdf()
                else:
                    insights['Shipping Conditions'] = self.con.execute(f"""
                        SELECT {shipping_col},
                               COUNT(*) as order_count
                        FROM salt_data
                        WHERE {shipping_col} IS NOT NULL
                        GROUP BY {shipping_col}
                        ORDER BY order_count DESC
                        LIMIT 10
                    """).fetchdf()
            
            # General Data Overview
            insights['Dataset Overview'] = self.con.execute("""
                SELECT 
                    COUNT(*) as total_records,
                    COUNT(DISTINCT CREATIONDATE) as unique_dates,
                    MIN(CREATIONDATE) as earliest_date,
                    MAX(CREATIONDATE) as latest_date
                FROM salt_data
            """).fetchdf()
            
            return insights
            
        except Exception as e:
            return f"Error generating insights: {str(e)}\n\nAvailable columns: {', '.join(self.available_columns[:10])}..."
    
    def clean_sql_response(self, sql_query: str) -> str:
        """Clean SQL response - avoiding string literal errors"""
        backticks = "`" + "`" + "`"
        sql_marker = backticks + "sql"
        
        if sql_query.startswith(sql_marker):
            sql_query = sql_query[6:]
        elif sql_query.startswith(backticks):
            sql_query = sql_query[3:]
        
        if sql_query.endswith(backticks):
            sql_query = sql_query[:-3]
            
        return sql_query.strip()
    
    def natural_language_query(self, question: str, api_key: str):
        """Convert natural language to SQL and execute"""
        if not self.data_loaded:
            return "Please load the dataset first"
            
        if not api_key:
            return "Please provide OpenAI API key"
            
        try:
            client = openai.OpenAI(api_key=api_key)
            
            # Enhanced prompt with actual available columns
            columns_list = ", ".join(self.available_columns[:30])  # Include first 30 columns
            
            prompt = f"""
            You are a SQL expert analyzing SAP SALT dataset. The database has a table called 'salt_data' with these available columns:
            
            {columns_list}
            
            The SALT dataset contains SAP ERP sales order data where each row represents a sales document item.
            
            IMPORTANT: Use only the column names I provided above. Do not assume column names like 'CUSTOMERID' exist.
            
            Convert this question to a DuckDB SQL query: "{question}"
            
            Return ONLY the SQL query, no explanation. Limit results to 20 rows and use WHERE clauses to filter out NULL values.
            """
            
            response = client.chat.completions.create(
                model="gpt-4",
                messages=[{"role": "user", "content": prompt}],
                temperature=0.1
            )
            
            sql_query = response.choices[0].message.content.strip()
            sql_query = self.clean_sql_response(sql_query)
            
            result_df = self.con.execute(sql_query).fetchdf()
            
            explanation_prompt = f"""
            Question: {question}
            Results: {result_df.head(10).to_string()}
            
            Provide a clear business explanation of these SAP ERP results in 2-3 sentences, focusing on actionable insights for sales operations.
            """
            
            explanation_response = client.chat.completions.create(
                model="gpt-4",
                messages=[{"role": "user", "content": explanation_prompt}],
                temperature=0.3
            )
            
            explanation = explanation_response.choices[0].message.content
            
            code_block = "`" + "`" + "`"
            return f"**SQL Query:**\n{code_block}sql\n{sql_query}\n{code_block}\n\n**Results:**\n{result_df.to_string(index=False)}\n\n**Explanation:**\n{explanation}"
            
        except Exception as e:
            return f"Error: {str(e)}\n\nTry rephrasing your question. Available columns: {', '.join(self.available_columns[:10])}..."

# Initialize analytics
analytics = SALTAnalytics()

def load_dataset_interface():
    return analytics.load_salt_dataset()

def show_insights_interface():
    insights = analytics.get_predefined_insights()
    
    if isinstance(insights, str):
        return insights
    
    output = "# πŸ“Š SAP SALT Dataset Insights\n\n"
    
    for title, df in insights.items():
        output += f"## {title}\n\n"
        if len(df) > 0:
            output += df.to_markdown(index=False)
        else:
            output += "*No data available for this analysis*"
        output += "\n\n---\n\n"
    
    return output

def qa_interface(question: str, api_key: str):
    if not question.strip():
        return "Please enter a question"
    return analytics.natural_language_query(question, api_key)

# Updated sample questions based on likely available columns
sample_questions = [
    "Which sales offices process the most orders?",
    "What are the most common payment terms?",
    "Show me the distribution of shipping conditions",
    "What is the date range of orders in the dataset?",
    "Which plants are most frequently used?"
]

with gr.Blocks(title="SAP SALT Analytics Demo", theme=gr.themes.Soft()) as demo:
    
    gr.Markdown("""
    # πŸš€ SAP SALT Dataset Analytics Demo
    ## Open Source Analytics + AI for SAP ERP
    
    This demo uses the **authentic SAP SALT dataset** - real ERP data from sales orders, items, customers, and addresses.
    """)
    
    with gr.Tab("πŸ“₯ Load Dataset"):
        gr.Markdown("### Load SAP SALT Dataset from Hugging Face")
        
        load_btn = gr.Button("Load SALT Dataset", variant="primary")
        load_output = gr.Textbox(label="Status", lines=8)
        
        load_btn.click(fn=load_dataset_interface, outputs=load_output)
    
    with gr.Tab("πŸ“ˆ Insights"):
        gr.Markdown("### Pre-built Analytics Insights")
        
        insights_btn = gr.Button("Generate Insights", variant="primary")
        insights_output = gr.Markdown()
        
        insights_btn.click(fn=show_insights_interface, outputs=insights_output)
    
    with gr.Tab("πŸ€– AI Q&A"):
        gr.Markdown("### Ask Questions in Natural Language")
        
        with gr.Row():
            with gr.Column(scale=3):
                api_key_input = gr.Textbox(
                    label="OpenAI API Key",
                    type="password",
                    placeholder="Enter your OpenAI API key"
                )
                
                question_input = gr.Textbox(
                    label="Your Question",
                    placeholder="e.g., Which sales offices process the most orders?",
                    lines=2
                )
                
                sample_dropdown = gr.Dropdown(
                    choices=sample_questions,
                    label="Or choose a sample question",
                    value=None
                )
                
                ask_btn = gr.Button("Get Answer", variant="primary")
            
            with gr.Column(scale=4):
                qa_output = gr.Markdown()
        
        sample_dropdown.change(
            fn=lambda x: x if x else "",
            inputs=sample_dropdown,
            outputs=question_input
        )
        
        ask_btn.click(
            fn=qa_interface,
            inputs=[question_input, api_key_input],
            outputs=qa_output
        )
    
    with gr.Tab("ℹ️ About"):
        gr.Markdown("""
        ### About the SALT Dataset
        
        **SAP SALT** (Sales Autocompletion Linked Business Tables) contains:
        - **500,908 sales orders** from real SAP S/4HANA system
        - **2.3M sales order line items** 
        - **139,611 unique customers**
        - **Data from 2018-2020** with full business context
        
        **Key Use Cases:**
        - Sales process automation (70-80% accuracy)
        - Customer behavior analysis
        - Shipping and logistics optimization
        - Payment terms prediction
        
        **Technology Stack:**
        - **DuckDB**: High-performance analytics
        - **OpenAI GPT-4**: Natural language to SQL
        - **Gradio**: Interactive interface
        - **Real ERP Data**: Authentic business scenarios
        
        This demonstrates how **open source tools** can unlock massive value from enterprise SAP systems at zero licensing cost.
        """)

if __name__ == "__main__":
    demo.launch()