Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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) β
|
| 28 |
match = re.search(
|
| 29 |
-
r"\b(total|average)\s+(
|
| 30 |
q, re.IGNORECASE
|
| 31 |
)
|
| 32 |
if match:
|
| 33 |
-
agg_type,
|
|
|
|
| 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
|
| 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."
|