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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -11
app.py CHANGED
@@ -15,6 +15,10 @@ qa = pipeline(
15
  tokenizer="google/tapas-base-finetuned-sqa"
16
  )
17
 
 
 
 
 
18
  def answer(q: str) -> str:
19
  # 1. Conditional query: negative profit
20
  if re.search(r"products.*negative.*profit", q, re.IGNORECASE):
@@ -24,13 +28,14 @@ def answer(q: str) -> str:
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) &
@@ -44,24 +49,20 @@ def answer(q: str) -> str:
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."
 
15
  tokenizer="google/tapas-base-finetuned-sqa"
16
  )
17
 
18
+ # Ensure column names are properly capitalized
19
+ metric_columns = {"profitmargin": "ProfitMargin", "profit": "Profit",
20
+ "revenue": "Revenue", "cost": "Cost"}
21
+
22
  def answer(q: str) -> str:
23
  # 1. Conditional query: negative profit
24
  if re.search(r"products.*negative.*profit", q, re.IGNORECASE):
 
28
  results = negative_profits[['Product', 'Region', 'FiscalQuarter', 'FiscalYear', 'Profit']]
29
  return results.to_string(index=False)
30
 
31
+ # 2. Numeric summaries (total/average) – Case insensitive metric matching
32
  match = re.search(
33
+ r"\b(total|average)\s+(profitmargin|profit|revenue|cost)\b.*\bProduct\s*([A-D])\b.*\b(Q[1-4])\s+(\d{4})",
34
  q, re.IGNORECASE
35
  )
36
  if match:
37
+ agg_type, metric_key, product_letter, quarter, year = match.groups()
38
+ metric = metric_columns[metric_key.lower()]
39
  product = f"Product {product_letter.upper()}"
40
  subset = df[
41
  (df["Product"] == product) &
 
49
  else: # average
50
  val = subset[metric].mean()
51
 
52
+ formatted_val = f"{val:.3f}" if metric == "ProfitMargin" else f"{val:,.2f}"
 
 
 
 
53
  return f"πŸ“Š {agg_type.title()} {metric} for {product} in {quarter} {year}: {formatted_val}"
54
 
55
+ # 3. TAPAS fallback (with error handling)
56
  try:
57
  res = qa(table=df_str, query=q)
58
  return f"πŸ” {res['answer']} (agg: {res.get('aggregator','NONE')})"
59
  except Exception as e:
60
+ return f"❗ Error from TAPAS: {str(e)}"
61
 
62
  # Launch Gradio
63
  demo = gr.Interface(
64
  fn=answer,
65
+ inputs=gr.Textbox(lines=2, placeholder="e.g. 'total Revenue for Product B in Q1 2024?' or 'List products with negative profit.'"),
66
  outputs="text",
67
  title="🟒 SAP S/4HANA Profitability Chat",
68
  description="Ask questions on profitability data (synthetic demo). Supports total, average, and conditional queries."