Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,89 +1,55 @@
|
|
1 |
import gradio as gr
|
2 |
import pandas as pd
|
3 |
-
|
4 |
-
|
5 |
-
from tapas.protos import interaction_pb2
|
6 |
-
from tapas.utils import number_annotation_utils, tf_example_utils, prediction_utils
|
7 |
-
from tapas.scripts.run_task_main import get_classifier_model, get_task_config
|
8 |
|
9 |
# 1) Load & stringify your CSV
|
10 |
df = pd.read_csv("synthetic_profit.csv")
|
11 |
-
|
12 |
-
|
13 |
-
# 2)
|
14 |
-
|
15 |
-
table
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
vocab_file="tapas_sqa_base/vocab.txt",
|
20 |
-
max_seq_length=512,
|
21 |
-
max_column_id=512,
|
22 |
-
max_row_id=512,
|
23 |
-
strip_column_names=False,
|
24 |
-
add_aggregation_candidates=True,
|
25 |
)
|
26 |
-
converter = tf_example_utils.ToClassifierTensorflowExample(config)
|
27 |
|
28 |
-
#
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
# question
|
42 |
-
q = interaction.questions.add()
|
43 |
-
q.original_text = query
|
44 |
-
# columns
|
45 |
-
for col in table[0]:
|
46 |
-
interaction.table.columns.add().text = col
|
47 |
-
# rows
|
48 |
-
for row_vals in table[1:]:
|
49 |
-
row = interaction.table.rows.add()
|
50 |
-
for cell in row_vals:
|
51 |
-
row.cells.add().text = cell
|
52 |
-
# numeric annotation for SUM/AVG
|
53 |
-
number_annotation_utils.add_numeric_values(interaction)
|
54 |
-
# convert to serialized Example
|
55 |
-
return converter.convert(interaction)
|
56 |
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
[example],
|
62 |
-
is_training=False,
|
63 |
-
drop_remainder=False,
|
64 |
-
batch_size=1,
|
65 |
-
seq_length=config.max_seq_length,
|
66 |
-
)
|
67 |
-
preds = model.predict(input_fn)
|
68 |
-
coords = prediction_utils.parse_coordinates(preds[0]["answer_coordinates"])
|
69 |
-
answers = [ table[r+1][c] for (r, c) in coords ] # r+1 because row 0 is header
|
70 |
-
return ", ".join(answers) if answers else "No answer found."
|
71 |
|
72 |
-
|
73 |
-
|
74 |
try:
|
75 |
-
|
|
|
76 |
except Exception as e:
|
77 |
-
return f"❌
|
78 |
|
|
|
79 |
iface = gr.Interface(
|
80 |
-
fn=
|
81 |
-
inputs=gr.Textbox(lines=2,
|
82 |
-
outputs=gr.Textbox(
|
83 |
-
title="SAP Profitability Q&A
|
84 |
description=(
|
85 |
-
"
|
86 |
-
"
|
87 |
),
|
88 |
allow_flagging="never",
|
89 |
)
|
|
|
1 |
import gradio as gr
|
2 |
import pandas as pd
|
3 |
+
from transformers import pipeline
|
|
|
|
|
|
|
|
|
4 |
|
5 |
# 1) Load & stringify your CSV
|
6 |
df = pd.read_csv("synthetic_profit.csv")
|
7 |
+
table = df.astype(str).to_dict(orient="records")
|
8 |
+
|
9 |
+
# 2) Instantiate the TAPAS pipeline from Transformers
|
10 |
+
qa = pipeline(
|
11 |
+
"table-question-answering",
|
12 |
+
model="google/tapas-base-finetuned-wtq",
|
13 |
+
tokenizer="google/tapas-base-finetuned-wtq",
|
14 |
+
device=-1, # CPU; change to 0 if you have a GPU
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
)
|
|
|
16 |
|
17 |
+
# 3) Few-shot examples teach “filter + sum” vs. “filter + mean”
|
18 |
+
EXAMPLES = """
|
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 = EXAMPLES + f"\nQ: {question}\nA:"
|
38 |
try:
|
39 |
+
result = qa(table=table, query=prompt)
|
40 |
+
return result.get("answer", "No answer found.")
|
41 |
except Exception as e:
|
42 |
+
return f"❌ Pipeline error:\n{e}"
|
43 |
|
44 |
+
# 4) Gradio UI
|
45 |
iface = gr.Interface(
|
46 |
+
fn=answer_question,
|
47 |
+
inputs=gr.Textbox(lines=2, placeholder="e.g. What is the total revenue for Product A in Q1 2024?"),
|
48 |
+
outputs=gr.Textbox(lines=3),
|
49 |
+
title="SAP Profitability Q&A",
|
50 |
description=(
|
51 |
+
"Ask simple sum/mean questions on the synthetic SAP data. \n"
|
52 |
+
"Powered by google/tapas-base-finetuned-wtq with four few-shot examples."
|
53 |
),
|
54 |
allow_flagging="never",
|
55 |
)
|