Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,10 +3,10 @@ import gradio as gr
|
|
| 3 |
import pandas as pd
|
| 4 |
from transformers import pipeline
|
| 5 |
|
| 6 |
-
#
|
| 7 |
df = pd.read_csv("synthetic_profit.csv")
|
| 8 |
|
| 9 |
-
#
|
| 10 |
tapas = pipeline(
|
| 11 |
"table-question-answering",
|
| 12 |
model="google/tapas-base-finetuned-wtq",
|
|
@@ -15,82 +15,66 @@ tapas = pipeline(
|
|
| 15 |
)
|
| 16 |
table = df.astype(str).to_dict(orient="records")
|
| 17 |
|
| 18 |
-
#
|
| 19 |
-
OPERATIONS = {
|
| 20 |
-
"
|
| 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 |
-
|
|
|
|
| 36 |
op = next((OPERATIONS[k] for k in OPERATIONS if k in q), None)
|
| 37 |
# 2) detect column
|
| 38 |
-
col = next((COLUMNS[k]
|
| 39 |
-
# 3) detect product
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 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
|
| 52 |
|
| 53 |
-
# filter
|
| 54 |
sub = df[
|
| 55 |
(df["Product"] == prod) &
|
| 56 |
(df["Region"] == region) &
|
| 57 |
(df["FiscalYear"] == year) &
|
| 58 |
-
(df["FiscalQuarter"]
|
| 59 |
]
|
| 60 |
-
|
| 61 |
-
# compute
|
| 62 |
try:
|
| 63 |
val = getattr(sub[col], op)()
|
| 64 |
-
except
|
| 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 |
-
|
| 72 |
-
if
|
| 73 |
-
return
|
| 74 |
|
| 75 |
-
# fallback
|
| 76 |
try:
|
| 77 |
-
|
| 78 |
-
return
|
| 79 |
except Exception as e:
|
| 80 |
return f"❌ Error: {e}"
|
| 81 |
|
| 82 |
-
#
|
| 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 |
|
| 95 |
-
if __name__
|
| 96 |
iface.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
| 3 |
import pandas as pd
|
| 4 |
from transformers import pipeline
|
| 5 |
|
| 6 |
+
# Load data
|
| 7 |
df = pd.read_csv("synthetic_profit.csv")
|
| 8 |
|
| 9 |
+
# Prepare TAPAS fallback
|
| 10 |
tapas = pipeline(
|
| 11 |
"table-question-answering",
|
| 12 |
model="google/tapas-base-finetuned-wtq",
|
|
|
|
| 15 |
)
|
| 16 |
table = df.astype(str).to_dict(orient="records")
|
| 17 |
|
| 18 |
+
# Helpers
|
| 19 |
+
OPERATIONS = {"total": "sum", "sum": "sum", "average": "mean", "mean": "mean"}
|
| 20 |
+
COLUMNS = {"revenue": "Revenue", "cost": "Cost", "profit": "Profit", "margin":"ProfitMargin","profit margin":"ProfitMargin"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
def parse_and_compute(question: str):
|
| 23 |
q = question.lower()
|
| 24 |
+
|
| 25 |
+
# 1) detect operation
|
| 26 |
op = next((OPERATIONS[k] for k in OPERATIONS if k in q), None)
|
| 27 |
# 2) detect column
|
| 28 |
+
col = next((COLUMNS[k] for k in COLUMNS if k in q), None)
|
| 29 |
+
# 3) detect product by scanning your actual values
|
| 30 |
+
prod = next((p for p in df["Product"].unique() if p.lower() in q), None)
|
| 31 |
+
# 4) region
|
| 32 |
+
region = next((r for r in df["Region"].unique() if r.lower() in q), None)
|
| 33 |
+
# 5) year
|
| 34 |
+
yr_match = re.search(r"\b(20\d{2})\b", q)
|
| 35 |
+
year = int(yr_match.group(1)) if yr_match else None
|
| 36 |
+
# 6) quarter
|
| 37 |
+
qtr = next((x for x in df["FiscalQuarter"].unique() if x.lower() in q), None)
|
|
|
|
| 38 |
|
| 39 |
+
# if any piece missing, we fallback
|
| 40 |
if None in (op, col, prod, region, year, qtr):
|
| 41 |
+
return None
|
| 42 |
|
| 43 |
+
# filter & compute
|
| 44 |
sub = df[
|
| 45 |
(df["Product"] == prod) &
|
| 46 |
(df["Region"] == region) &
|
| 47 |
(df["FiscalYear"] == year) &
|
| 48 |
+
(df["FiscalQuarter"]== qtr)
|
| 49 |
]
|
|
|
|
|
|
|
| 50 |
try:
|
| 51 |
val = getattr(sub[col], op)()
|
| 52 |
+
except:
|
| 53 |
return None
|
| 54 |
|
| 55 |
return f"{op.capitalize()} {col} for {prod} in {region}, {qtr} {year}: {val:.2f}"
|
| 56 |
|
|
|
|
| 57 |
def answer(question: str) -> str:
|
| 58 |
+
out = parse_and_compute(question)
|
| 59 |
+
if out is not None:
|
| 60 |
+
return out
|
| 61 |
|
| 62 |
+
# fallback
|
| 63 |
try:
|
| 64 |
+
res = tapas(table=table, query=question)
|
| 65 |
+
return res.get("answer", "No answer found.")
|
| 66 |
except Exception as e:
|
| 67 |
return f"❌ Error: {e}"
|
| 68 |
|
| 69 |
+
# Gradio...
|
| 70 |
iface = gr.Interface(
|
| 71 |
fn=answer,
|
| 72 |
inputs=gr.Textbox(lines=2, placeholder="e.g. What is the total revenue for Product A in EMEA in Q1 2024?"),
|
| 73 |
outputs=gr.Textbox(lines=3),
|
| 74 |
title="SAP Profitability Q&A",
|
| 75 |
+
description="Basic total/average queries via Pandas+fallback to TAPAS",
|
|
|
|
|
|
|
|
|
|
| 76 |
allow_flagging="never",
|
| 77 |
)
|
| 78 |
|
| 79 |
+
if __name__=="__main__":
|
| 80 |
iface.launch(server_name="0.0.0.0", server_port=7860)
|