Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,67 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
#
|
| 4 |
-
|
|
|
|
|
|
|
| 5 |
qa = pipeline(
|
| 6 |
"table-question-answering",
|
| 7 |
model="google/tapas-base-finetuned-sqa",
|
| 8 |
tokenizer="google/tapas-base-finetuned-sqa"
|
| 9 |
)
|
| 10 |
|
| 11 |
-
# 4) cast to strings to avoid the regex bug
|
| 12 |
-
df_str = df.astype(str)
|
| 13 |
-
|
| 14 |
-
# 5) sanity check
|
| 15 |
-
print( qa(table=df_str, query="What was the ProfitMargin for Product B in EMEA Q2 2024?") )
|
| 16 |
-
|
| 17 |
-
# 6) launch Gradio
|
| 18 |
-
import gradio as gr
|
| 19 |
-
|
| 20 |
-
import re
|
| 21 |
-
|
| 22 |
def answer(q: str) -> str:
|
| 23 |
-
#
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
# filter the *numeric* DataFrame
|
| 36 |
subset = df[
|
| 37 |
-
(df["Product"]
|
| 38 |
(df["FiscalQuarter"] == quarter) &
|
| 39 |
-
(df["FiscalYear"]
|
| 40 |
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
-
|
| 43 |
-
if agg_type == "total":
|
| 44 |
-
val = subset[metric].sum()
|
| 45 |
-
return f"Total {metric} for {product} in {quarter} {year}: {val:,.2f}"
|
| 46 |
-
else: # average
|
| 47 |
-
val = subset[metric].mean()
|
| 48 |
-
# show 3 decimal places for margins, 2 for currency
|
| 49 |
-
fmt = "{:,.3f}" if metric=="ProfitMargin" else "{:,.2f}"
|
| 50 |
-
return f"Average {metric} for {product} in {quarter} {year}: " + fmt.format(val)
|
| 51 |
-
|
| 52 |
-
# --- 2. fallback to TAPAS for everything else ---
|
| 53 |
res = qa(table=df_str, query=q)
|
| 54 |
-
|
| 55 |
-
if agg and agg != "NONE":
|
| 56 |
-
return f"Answer: {res['answer']} (agg: {agg})"
|
| 57 |
-
# last-resort: raw answer
|
| 58 |
-
return f"Answer: {res['answer']}"
|
| 59 |
-
|
| 60 |
|
|
|
|
| 61 |
demo = gr.Interface(
|
| 62 |
fn=answer,
|
| 63 |
-
inputs=gr.Textbox(lines=2, placeholder="e.g. Profit for Product A in Q1
|
| 64 |
outputs="text",
|
| 65 |
-
title="S/4HANA Profitability Chat",
|
|
|
|
| 66 |
)
|
| 67 |
-
|
|
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
+
import numpy as np
|
| 3 |
+
import re
|
| 4 |
+
from transformers import pipeline
|
| 5 |
+
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
|
| 14 |
qa = pipeline(
|
| 15 |
"table-question-answering",
|
| 16 |
model="google/tapas-base-finetuned-sqa",
|
| 17 |
tokenizer="google/tapas-base-finetuned-sqa"
|
| 18 |
)
|
| 19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
def answer(q: str) -> str:
|
| 21 |
+
# 1. Conditional query: negative profit
|
| 22 |
+
if re.search(r"products.*negative.*profit", q, re.IGNORECASE):
|
| 23 |
+
negative_profits = df[df["Profit"] < 0]
|
| 24 |
+
if negative_profits.empty:
|
| 25 |
+
return "✅ No products with negative profit found."
|
| 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()
|