Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,25 +1,42 @@
|
|
1 |
-
# app.py
|
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 |
qa = pipeline(
|
13 |
"table-question-answering",
|
14 |
model="google/tapas-base-finetuned-wtq",
|
15 |
tokenizer="google/tapas-base-finetuned-wtq",
|
16 |
-
device=-1
|
17 |
)
|
18 |
|
19 |
-
# 3)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
def answer_question(question: str) -> str:
|
|
|
21 |
try:
|
22 |
-
out = qa(table=table, query=
|
23 |
return out.get("answer", "No answer found.")
|
24 |
except Exception as e:
|
25 |
return f"β Error: {e}"
|
@@ -27,10 +44,13 @@ def answer_question(question: str) -> str:
|
|
27 |
# 4) Gradio UI
|
28 |
iface = gr.Interface(
|
29 |
fn=answer_question,
|
30 |
-
inputs=gr.Textbox(lines=2, placeholder="e.g. What is the
|
31 |
-
outputs=gr.Textbox(lines=4),
|
32 |
title="SAP Profitability Q&A (TAPAS)",
|
33 |
-
description=
|
|
|
|
|
|
|
34 |
allow_flagging="never",
|
35 |
)
|
36 |
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
import pandas as pd
|
3 |
from transformers import pipeline
|
4 |
|
5 |
+
# 1) Load your data
|
6 |
df = pd.read_csv("synthetic_profit.csv")
|
7 |
table = df.astype(str).to_dict(orient="records")
|
8 |
|
9 |
+
# 2) TAPAS pipeline
|
10 |
qa = pipeline(
|
11 |
"table-question-answering",
|
12 |
model="google/tapas-base-finetuned-wtq",
|
13 |
tokenizer="google/tapas-base-finetuned-wtq",
|
14 |
+
device=-1
|
15 |
)
|
16 |
|
17 |
+
# 3) Four few-shot examples: sum revenue, sum cost, sum margin, **mean margin**
|
18 |
+
EXAMPLE_PROMPT = """
|
19 |
+
Example 1:
|
20 |
+
Q: What is the total revenue for Product A in EMEA in Q1 2024?
|
21 |
+
A: Filter Product=A & Region=EMEA & FiscalYear=2024 & FiscalQuarter=Q1, then sum Revenue β 3075162.49
|
22 |
+
|
23 |
+
Example 2:
|
24 |
+
Q: What is the total cost for Product A in EMEA in Q1 2024?
|
25 |
+
A: Filter Product=A & Region=EMEA & FiscalYear=2024 & FiscalQuarter=Q1, then sum Cost β 2894321.75
|
26 |
+
|
27 |
+
Example 3:
|
28 |
+
Q: What is the total margin for Product A in EMEA in Q1 2024?
|
29 |
+
A: Filter Product=A & Region=EMEA & FiscalYear=2024 & FiscalQuarter=Q1, then sum ProfitMargin β 0.18
|
30 |
+
|
31 |
+
Example 4:
|
32 |
+
Q: What is the average profit margin for Product A in EMEA in Q1 2024?
|
33 |
+
A: Filter Product=A & Region=EMEA & FiscalYear=2024 & FiscalQuarter=Q1, then mean ProfitMargin β 0.18
|
34 |
+
"""
|
35 |
+
|
36 |
def answer_question(question: str) -> str:
|
37 |
+
prompt = EXAMPLE_PROMPT + f"\nQ: {question}\nA:"
|
38 |
try:
|
39 |
+
out = qa(table=table, query=prompt)
|
40 |
return out.get("answer", "No answer found.")
|
41 |
except Exception as e:
|
42 |
return f"β Error: {e}"
|
|
|
44 |
# 4) Gradio UI
|
45 |
iface = gr.Interface(
|
46 |
fn=answer_question,
|
47 |
+
inputs=gr.Textbox(lines=2, placeholder="e.g. What is the average profit margin by region?", label="Your question"),
|
48 |
+
outputs=gr.Textbox(lines=4, label="Answer"),
|
49 |
title="SAP Profitability Q&A (TAPAS)",
|
50 |
+
description=(
|
51 |
+
"Ask simple sum/mean questions on your SAP data. \n"
|
52 |
+
"Powered by google/tapas-base-finetuned-wtq with four few-shot examples."
|
53 |
+
),
|
54 |
allow_flagging="never",
|
55 |
)
|
56 |
|