Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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(
|
|
|
|
|
|
|
31 |
if match:
|
32 |
-
|
|
|
33 |
subset = df[
|
34 |
-
(df["Product"] ==
|
35 |
(df["FiscalQuarter"] == quarter) &
|
36 |
(df["FiscalYear"] == int(year))
|
37 |
]
|
38 |
if subset.empty:
|
39 |
-
return "β οΈ No matching data."
|
40 |
-
|
41 |
-
|
42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
|
44 |
-
# 3. TAPAS fallback
|
45 |
-
|
46 |
-
|
|
|
|
|
|
|
47 |
|
48 |
# Launch Gradio
|
49 |
demo = gr.Interface(
|
50 |
fn=answer,
|
51 |
-
inputs=gr.Textbox(lines=2, placeholder="e.g. 'total
|
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()
|