PD03 commited on
Commit
67fc297
Β·
verified Β·
1 Parent(s): a5ece8b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -15
app.py CHANGED
@@ -6,8 +6,6 @@ import gradio as gr
6
 
7
  # Load numeric data
8
  df = pd.read_csv("synthetic_profit.csv")
9
-
10
- # String DataFrame for TAPAS
11
  df_str = df.astype(str)
12
 
13
  # Initialize TAPAS
@@ -26,32 +24,47 @@ def answer(q: str) -> str:
26
  results = negative_profits[['Product', 'Region', 'FiscalQuarter', 'FiscalYear', 'Profit']]
27
  return results.to_string(index=False)
28
 
29
- # 2. Numeric summaries (total/average)
30
- match = re.search(r"\b(total|average)\s+(ProfitMargin|Profit|Revenue|Cost)\b.*\bProduct\s*([A-D])\b.*\b(Q[1-4])\s*(\d{4})", q, re.IGNORECASE)
 
 
 
31
  if match:
32
- agg, metric, product, quarter, year = match.groups()
 
33
  subset = df[
34
- (df["Product"] == f"Product {product.upper()}") &
35
  (df["FiscalQuarter"] == quarter) &
36
  (df["FiscalYear"] == int(year))
37
  ]
38
  if subset.empty:
39
- return "⚠️ No matching data."
40
- value = subset[metric].sum() if agg.lower() == "total" else subset[metric].mean()
41
- formatted_val = f"{value:.3f}" if metric == "ProfitMargin" else f"{value:,.2f}"
42
- return f"πŸ“Š {agg.title()} {metric} for Product {product.upper()} in {quarter} {year}: {formatted_val}"
 
 
 
 
 
 
 
 
43
 
44
- # 3. TAPAS fallback for everything else
45
- res = qa(table=df_str, query=q)
46
- return f"πŸ” {res['answer']} (agg: {res.get('aggregator','NONE')})"
 
 
 
47
 
48
  # Launch Gradio
49
  demo = gr.Interface(
50
  fn=answer,
51
- inputs=gr.Textbox(lines=2, placeholder="e.g. 'total Profit for Product A in Q1 2024?' or 'List products with negative profit.'"),
52
  outputs="text",
53
  title="🟒 SAP S/4HANA Profitability Chat",
54
  description="Ask questions on profitability data (synthetic demo). Supports total, average, and conditional queries."
55
  )
56
 
57
- demo.launch()
 
6
 
7
  # Load numeric data
8
  df = pd.read_csv("synthetic_profit.csv")
 
 
9
  df_str = df.astype(str)
10
 
11
  # Initialize TAPAS
 
24
  results = negative_profits[['Product', 'Region', 'FiscalQuarter', 'FiscalYear', 'Profit']]
25
  return results.to_string(index=False)
26
 
27
+ # 2. Numeric summaries (total/average) – improved regex parsing
28
+ match = re.search(
29
+ r"\b(total|average)\s+(ProfitMargin|Profit|Revenue|Cost)\b.*\bProduct\s*([A-D])\b.*\b(Q[1-4])\s+(\d{4})",
30
+ q, re.IGNORECASE
31
+ )
32
  if match:
33
+ agg_type, metric, product_letter, quarter, year = match.groups()
34
+ product = f"Product {product_letter.upper()}"
35
  subset = df[
36
+ (df["Product"] == product) &
37
  (df["FiscalQuarter"] == quarter) &
38
  (df["FiscalYear"] == int(year))
39
  ]
40
  if subset.empty:
41
+ return "⚠️ No matching data found."
42
+ if agg_type.lower() == "total":
43
+ val = subset[metric].sum()
44
+ else: # average
45
+ val = subset[metric].mean()
46
+
47
+ if metric == "ProfitMargin":
48
+ formatted_val = f"{val:.3f}"
49
+ else:
50
+ formatted_val = f"{val:,.2f}"
51
+
52
+ return f"πŸ“Š {agg_type.title()} {metric} for {product} in {quarter} {year}: {formatted_val}"
53
 
54
+ # 3. TAPAS fallback
55
+ try:
56
+ res = qa(table=df_str, query=q)
57
+ return f"πŸ” {res['answer']} (agg: {res.get('aggregator','NONE')})"
58
+ except Exception as e:
59
+ return f"❗ Error: {str(e)}"
60
 
61
  # Launch Gradio
62
  demo = gr.Interface(
63
  fn=answer,
64
+ inputs=gr.Textbox(lines=2, placeholder="e.g. 'total Cost for Product B in Q1 2024?' or 'List products with negative profit.'"),
65
  outputs="text",
66
  title="🟒 SAP S/4HANA Profitability Chat",
67
  description="Ask questions on profitability data (synthetic demo). Supports total, average, and conditional queries."
68
  )
69
 
70
+ demo.launch()