Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,21 +1,23 @@
|
|
|
|
|
|
|
|
| 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)
|
| 10 |
qa = pipeline(
|
| 11 |
"table-question-answering",
|
| 12 |
-
model="
|
| 13 |
-
tokenizer="
|
| 14 |
-
device=-1
|
| 15 |
)
|
| 16 |
|
| 17 |
-
# 3)
|
| 18 |
-
|
| 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
|
|
@@ -27,14 +29,10 @@ A: Filter Product=A & Region=EMEA & FiscalYear=2024 & FiscalQuarter=Q1, then sum
|
|
| 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 =
|
| 38 |
try:
|
| 39 |
out = qa(table=table, query=prompt)
|
| 40 |
return out.get("answer", "No answer found.")
|
|
@@ -44,13 +42,10 @@ def answer_question(question: str) -> str:
|
|
| 44 |
# 4) Gradio UI
|
| 45 |
iface = gr.Interface(
|
| 46 |
fn=answer_question,
|
| 47 |
-
inputs=gr.Textbox(lines=2, placeholder="e.g. What is the
|
| 48 |
-
outputs=gr.Textbox(lines=
|
| 49 |
-
title="SAP Profitability Q&A (
|
| 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 |
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
|
| 3 |
import gradio as gr
|
| 4 |
import pandas as pd
|
| 5 |
from transformers import pipeline
|
| 6 |
|
| 7 |
+
# 1) Load your synthetic SAP data
|
| 8 |
df = pd.read_csv("synthetic_profit.csv")
|
| 9 |
table = df.astype(str).to_dict(orient="records")
|
| 10 |
|
| 11 |
+
# 2) Use TAPEX fine-tuned on WikiTableQuestions
|
| 12 |
qa = pipeline(
|
| 13 |
"table-question-answering",
|
| 14 |
+
model="microsoft/tapex-base-finetuned-wtq",
|
| 15 |
+
tokenizer="microsoft/tapex-base-finetuned-wtq",
|
| 16 |
+
device=-1 # CPU; switch to 0 if you have GPU
|
| 17 |
)
|
| 18 |
|
| 19 |
+
# 3) Few-shot examples teaching “filter + sum”
|
| 20 |
+
EXAMPLES = """
|
| 21 |
Example 1:
|
| 22 |
Q: What is the total revenue for Product A in EMEA in Q1 2024?
|
| 23 |
A: Filter Product=A & Region=EMEA & FiscalYear=2024 & FiscalQuarter=Q1, then sum Revenue → 3075162.49
|
|
|
|
| 29 |
Example 3:
|
| 30 |
Q: What is the total margin for Product A in EMEA in Q1 2024?
|
| 31 |
A: Filter Product=A & Region=EMEA & FiscalYear=2024 & FiscalQuarter=Q1, then sum ProfitMargin → 0.18
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
"""
|
| 33 |
|
| 34 |
def answer_question(question: str) -> str:
|
| 35 |
+
prompt = EXAMPLES + f"\nQ: {question}\nA:"
|
| 36 |
try:
|
| 37 |
out = qa(table=table, query=prompt)
|
| 38 |
return out.get("answer", "No answer found.")
|
|
|
|
| 42 |
# 4) Gradio UI
|
| 43 |
iface = gr.Interface(
|
| 44 |
fn=answer_question,
|
| 45 |
+
inputs=gr.Textbox(lines=2, placeholder="e.g. What is the total revenue for Product A in EMEA in Q1 2024?", label="Your question"),
|
| 46 |
+
outputs=gr.Textbox(lines=3, label="Answer"),
|
| 47 |
+
title="SAP Profitability Q&A (TAPEX)",
|
| 48 |
+
description="Ask basic sum questions on SAP data, powered by TAPEX with few-shot examples.",
|
|
|
|
|
|
|
|
|
|
| 49 |
allow_flagging="never",
|
| 50 |
)
|
| 51 |
|