Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,51 +1,94 @@
|
|
1 |
-
|
2 |
-
|
3 |
import gradio as gr
|
4 |
import pandas as pd
|
5 |
from transformers import pipeline
|
6 |
|
7 |
-
# 1) Load your
|
8 |
df = pd.read_csv("synthetic_profit.csv")
|
9 |
-
table = df.astype(str).to_dict(orient="records")
|
10 |
|
11 |
-
# 2)
|
12 |
-
|
13 |
"table-question-answering",
|
14 |
-
model="
|
15 |
-
tokenizer="
|
16 |
-
device=-1
|
17 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
|
25 |
-
|
26 |
-
Q: What is the total cost for Product A in EMEA in Q1 2024?
|
27 |
-
A: Filter Product=A & Region=EMEA & FiscalYear=2024 & FiscalQuarter=Q1, then sum Cost → 2894321.75
|
28 |
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
|
|
33 |
|
34 |
-
|
35 |
-
prompt = EXAMPLES + f"\nQ: {question}\nA:"
|
36 |
try:
|
37 |
-
out =
|
38 |
return out.get("answer", "No answer found.")
|
39 |
except Exception as e:
|
40 |
return f"❌ Error: {e}"
|
41 |
|
42 |
-
#
|
43 |
iface = gr.Interface(
|
44 |
-
fn=
|
45 |
-
inputs=gr.Textbox(lines=2, placeholder="e.g. What is the total revenue for Product A in EMEA in Q1 2024?"
|
46 |
-
outputs=gr.Textbox(lines=3
|
47 |
-
title="SAP Profitability Q&A
|
48 |
-
description=
|
|
|
|
|
|
|
49 |
allow_flagging="never",
|
50 |
)
|
51 |
|
|
|
1 |
+
import re
|
|
|
2 |
import gradio as gr
|
3 |
import pandas as pd
|
4 |
from transformers import pipeline
|
5 |
|
6 |
+
# 1) Load your data
|
7 |
df = pd.read_csv("synthetic_profit.csv")
|
|
|
8 |
|
9 |
+
# 2) Prepare the TAPAS fallback
|
10 |
+
tapas = pipeline(
|
11 |
"table-question-answering",
|
12 |
+
model="google/tapas-base-finetuned-wtq",
|
13 |
+
tokenizer="google/tapas-base-finetuned-wtq",
|
14 |
+
device=-1
|
15 |
)
|
16 |
+
table = df.astype(str).to_dict(orient="records")
|
17 |
+
|
18 |
+
# 3) Helpers for parsing
|
19 |
+
OPERATIONS = {
|
20 |
+
"total": "sum",
|
21 |
+
"sum": "sum",
|
22 |
+
"average": "mean",
|
23 |
+
"mean": "mean"
|
24 |
+
}
|
25 |
+
COLUMNS = {
|
26 |
+
"revenue": "Revenue",
|
27 |
+
"cost": "Cost",
|
28 |
+
"profit": "Profit",
|
29 |
+
"margin": "ProfitMargin",
|
30 |
+
"profit margin":"ProfitMargin"
|
31 |
+
}
|
32 |
|
33 |
+
def parse_and_compute(question: str):
|
34 |
+
q = question.lower()
|
35 |
+
# 1) detect op
|
36 |
+
op = next((OPERATIONS[k] for k in OPERATIONS if k in q), None)
|
37 |
+
# 2) detect column
|
38 |
+
col = next((COLUMNS[k] for k in COLUMNS if k in q), None)
|
39 |
+
# 3) detect product (assumes "Product X")
|
40 |
+
m = re.search(r"product\s*([A-Za-z0-9]+)", q)
|
41 |
+
prod = f"Product {m.group(1)}" if m else None
|
42 |
+
# 4) detect region from known values
|
43 |
+
region = next((r for r in df["Region"].unique() if r.lower() in q), None)
|
44 |
+
# 5) detect year
|
45 |
+
y = re.search(r"\b(20\d{2})\b", q)
|
46 |
+
year = int(y.group(1)) if y else None
|
47 |
+
# 6) detect quarter
|
48 |
+
qtr = next((fq for fq in df["FiscalQuarter"].unique() if fq.lower() in q), None)
|
49 |
+
|
50 |
+
if None in (op, col, prod, region, year, qtr):
|
51 |
+
return None # fallback
|
52 |
+
|
53 |
+
# filter
|
54 |
+
sub = df[
|
55 |
+
(df["Product"] == prod) &
|
56 |
+
(df["Region"] == region) &
|
57 |
+
(df["FiscalYear"] == year) &
|
58 |
+
(df["FiscalQuarter"] == qtr)
|
59 |
+
]
|
60 |
+
|
61 |
+
# compute
|
62 |
+
try:
|
63 |
+
val = getattr(sub[col], op)()
|
64 |
+
except Exception:
|
65 |
+
return None
|
66 |
|
67 |
+
return f"{op.capitalize()} {col} for {prod} in {region}, {qtr} {year}: {val:.2f}"
|
|
|
|
|
68 |
|
69 |
+
# 4) Main answer fn
|
70 |
+
def answer(question: str) -> str:
|
71 |
+
res = parse_and_compute(question)
|
72 |
+
if res is not None:
|
73 |
+
return res
|
74 |
|
75 |
+
# fallback to TAPAS
|
|
|
76 |
try:
|
77 |
+
out = tapas(table=table, query=question)
|
78 |
return out.get("answer", "No answer found.")
|
79 |
except Exception as e:
|
80 |
return f"❌ Error: {e}"
|
81 |
|
82 |
+
# 5) Gradio UI
|
83 |
iface = gr.Interface(
|
84 |
+
fn=answer,
|
85 |
+
inputs=gr.Textbox(lines=2, placeholder="e.g. What is the total revenue for Product A in EMEA in Q1 2024?"),
|
86 |
+
outputs=gr.Textbox(lines=3),
|
87 |
+
title="SAP Profitability Q&A",
|
88 |
+
description=(
|
89 |
+
"Supports any basic “total”/“average” question by parsing and computing via Pandas. \n"
|
90 |
+
"Falls back to TAPAS for anything else."
|
91 |
+
),
|
92 |
allow_flagging="never",
|
93 |
)
|
94 |
|