Spaces:
Sleeping
Sleeping
import gradio as gr | |
import pandas as pd | |
from transformers import pipeline | |
# 1) Load your data | |
df = pd.read_csv("synthetic_profit.csv") | |
table = df.astype(str).to_dict(orient="records") | |
# 2) TAPAS pipeline | |
qa = pipeline( | |
"table-question-answering", | |
model="google/tapas-base-finetuned-wtq", | |
tokenizer="google/tapas-base-finetuned-wtq", | |
device=-1 | |
) | |
# 3) Four few-shot examples: sum revenue, sum cost, sum margin, **mean margin** | |
EXAMPLE_PROMPT = """ | |
Example 1: | |
Q: What is the total revenue for Product A in EMEA in Q1 2024? | |
A: Filter Product=A & Region=EMEA & FiscalYear=2024 & FiscalQuarter=Q1, then sum Revenue β 3075162.49 | |
Example 2: | |
Q: What is the total cost for Product A in EMEA in Q1 2024? | |
A: Filter Product=A & Region=EMEA & FiscalYear=2024 & FiscalQuarter=Q1, then sum Cost β 2894321.75 | |
Example 3: | |
Q: What is the total margin for Product A in EMEA in Q1 2024? | |
A: Filter Product=A & Region=EMEA & FiscalYear=2024 & FiscalQuarter=Q1, then sum ProfitMargin β 0.18 | |
Example 4: | |
Q: What is the average profit margin for Product A in EMEA in Q1 2024? | |
A: Filter Product=A & Region=EMEA & FiscalYear=2024 & FiscalQuarter=Q1, then mean ProfitMargin β 0.18 | |
""" | |
def answer_question(question: str) -> str: | |
prompt = EXAMPLE_PROMPT + f"\nQ: {question}\nA:" | |
try: | |
out = qa(table=table, query=prompt) | |
return out.get("answer", "No answer found.") | |
except Exception as e: | |
return f"β Error: {e}" | |
# 4) Gradio UI | |
iface = gr.Interface( | |
fn=answer_question, | |
inputs=gr.Textbox(lines=2, placeholder="e.g. What is the average profit margin by region?", label="Your question"), | |
outputs=gr.Textbox(lines=4, label="Answer"), | |
title="SAP Profitability Q&A (TAPAS)", | |
description=( | |
"Ask simple sum/mean questions on your SAP data. \n" | |
"Powered by google/tapas-base-finetuned-wtq with four few-shot examples." | |
), | |
allow_flagging="never", | |
) | |
if __name__ == "__main__": | |
iface.launch(server_name="0.0.0.0", server_port=7860) |