Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,53 +1,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)
|
| 10 |
qa = pipeline(
|
| 11 |
"table-question-answering",
|
| 12 |
-
model="
|
| 13 |
-
tokenizer="
|
| 14 |
-
device=-1
|
| 15 |
)
|
| 16 |
|
| 17 |
-
# 3)
|
| 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 |
-
|
| 32 |
def answer_question(question: str) -> str:
|
| 33 |
-
full_query = EXAMPLE_PROMPT + f"\nQ: {question}\nA:"
|
| 34 |
try:
|
| 35 |
-
|
| 36 |
-
return
|
| 37 |
except Exception as e:
|
| 38 |
-
|
| 39 |
-
return f"❌ Pipeline error:\n{e}"
|
| 40 |
|
| 41 |
# 4) Gradio UI
|
| 42 |
iface = gr.Interface(
|
| 43 |
fn=answer_question,
|
| 44 |
-
inputs=gr.Textbox(lines=2, placeholder="
|
| 45 |
-
outputs=gr.Textbox(lines=
|
| 46 |
-
title="SAP Profitability Q&A",
|
| 47 |
-
description=
|
| 48 |
-
"Ask simple revenue/cost/margin questions on the synthetic SAP data. "
|
| 49 |
-
"Powered by microsoft/tapex-base-finetuned-wtq with three few‐shot examples."
|
| 50 |
-
),
|
| 51 |
allow_flagging="never",
|
| 52 |
)
|
| 53 |
|
|
|
|
| 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 (all as strings so TAPAS never sees floats)
|
| 8 |
df = pd.read_csv("synthetic_profit.csv")
|
| 9 |
table = df.astype(str).to_dict(orient="records")
|
| 10 |
|
| 11 |
+
# 2) Instantiate TAPAS fine-tuned on WikiTableQuestions
|
| 12 |
qa = pipeline(
|
| 13 |
"table-question-answering",
|
| 14 |
+
model="google/tapas-base-finetuned-wtq",
|
| 15 |
+
tokenizer="google/tapas-base-finetuned-wtq",
|
| 16 |
+
device=-1 # CPU; change to 0 if you enable GPU
|
| 17 |
)
|
| 18 |
|
| 19 |
+
# 3) Simple QA function
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
def answer_question(question: str) -> str:
|
|
|
|
| 21 |
try:
|
| 22 |
+
out = qa(table=table, query=question)
|
| 23 |
+
return out.get("answer", "No answer found.")
|
| 24 |
except Exception as e:
|
| 25 |
+
return f"❌ Error: {e}"
|
|
|
|
| 26 |
|
| 27 |
# 4) Gradio UI
|
| 28 |
iface = gr.Interface(
|
| 29 |
fn=answer_question,
|
| 30 |
+
inputs=gr.Textbox(lines=2, placeholder="e.g. What is the total revenue for Product A in EMEA in Q1 2024?"),
|
| 31 |
+
outputs=gr.Textbox(lines=4),
|
| 32 |
+
title="SAP Profitability Q&A (TAPAS)",
|
| 33 |
+
description="Ask simple sum/avg questions on your SAP data. Powered by google/tapas-base-finetuned-wtq.",
|
|
|
|
|
|
|
|
|
|
| 34 |
allow_flagging="never",
|
| 35 |
)
|
| 36 |
|