Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,19 +2,19 @@ import gradio as gr
|
|
2 |
import pandas as pd
|
3 |
from transformers import pipeline
|
4 |
|
5 |
-
#
|
6 |
df = pd.read_csv("synthetic_profit.csv")
|
7 |
table = df.astype(str).to_dict(orient="records")
|
8 |
|
9 |
-
#
|
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 |
-
#
|
18 |
EXAMPLES = """
|
19 |
Example 1:
|
20 |
Q: What is the total revenue for Product A in EMEA in Q1 2024?
|
@@ -31,28 +31,25 @@ A: Filter Product=A & Region=EMEA & FiscalYear=2024 & FiscalQuarter=Q1, then sum
|
|
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 |
-
|
39 |
-
|
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="
|
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 |
)
|
56 |
|
57 |
if __name__ == "__main__":
|
58 |
-
iface.launch(
|
|
|
2 |
import pandas as pd
|
3 |
from transformers import pipeline
|
4 |
|
5 |
+
# Load & stringify your CSV
|
6 |
df = pd.read_csv("synthetic_profit.csv")
|
7 |
table = df.astype(str).to_dict(orient="records")
|
8 |
|
9 |
+
# Instantiate 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 |
+
# Four + one few-shot examples
|
18 |
EXAMPLES = """
|
19 |
Example 1:
|
20 |
Q: What is the total revenue for Product A in EMEA in Q1 2024?
|
|
|
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 |
+
Example 5:
|
36 |
+
Q: What is the total revenue for Product A in Q1 2024?
|
37 |
+
A: Filter Product=A & FiscalYear=2024 & FiscalQuarter=Q1, then sum Revenue → YOUR_SUM_HERE
|
38 |
"""
|
39 |
|
40 |
def answer_question(question: str) -> str:
|
41 |
prompt = EXAMPLES + f"\nQ: {question}\nA:"
|
42 |
+
out = qa(table=table, query=prompt)
|
43 |
+
return out.get("answer", "No answer found.")
|
|
|
|
|
|
|
44 |
|
|
|
45 |
iface = gr.Interface(
|
46 |
fn=answer_question,
|
47 |
+
inputs=gr.Textbox(lines=2, placeholder="Ask a question…"),
|
48 |
outputs=gr.Textbox(lines=3),
|
49 |
title="SAP Profitability Q&A",
|
50 |
+
description="TAPAS few-shot sum/mean demo",
|
|
|
|
|
|
|
51 |
allow_flagging="never",
|
52 |
)
|
53 |
|
54 |
if __name__ == "__main__":
|
55 |
+
iface.launch()
|