Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,11 +2,11 @@ import gradio as gr
|
|
2 |
import pandas as pd
|
3 |
from transformers import pipeline
|
4 |
|
5 |
-
# 1) Load your
|
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="microsoft/tapex-base-finetuned-wtq",
|
@@ -14,7 +14,7 @@ qa = pipeline(
|
|
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?
|
@@ -30,16 +30,19 @@ A: Filter Product=A & Region=EMEA & FiscalYear=2024 & FiscalQuarter=Q1, then sum
|
|
30 |
"""
|
31 |
|
32 |
def answer_question(question: str) -> str:
|
33 |
-
# Prepend the examples to teach the model your pattern
|
34 |
full_query = EXAMPLE_PROMPT + f"\nQ: {question}\nA:"
|
35 |
-
|
36 |
-
|
|
|
|
|
|
|
|
|
37 |
|
38 |
# 4) Gradio UI
|
39 |
iface = gr.Interface(
|
40 |
fn=answer_question,
|
41 |
inputs=gr.Textbox(lines=2, placeholder="Ask a basic question…", label="Your question"),
|
42 |
-
outputs=gr.Textbox(lines=
|
43 |
title="SAP Profitability Q&A",
|
44 |
description=(
|
45 |
"Ask simple revenue/cost/margin questions on the synthetic SAP data. "
|
|
|
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) TAPEX table‐QA pipeline
|
10 |
qa = pipeline(
|
11 |
"table-question-answering",
|
12 |
model="microsoft/tapex-base-finetuned-wtq",
|
|
|
14 |
device=-1
|
15 |
)
|
16 |
|
17 |
+
# 3) Few‐shot examples
|
18 |
EXAMPLE_PROMPT = """
|
19 |
Example 1:
|
20 |
Q: What is the total revenue for Product A in EMEA in Q1 2024?
|
|
|
30 |
"""
|
31 |
|
32 |
def answer_question(question: str) -> str:
|
|
|
33 |
full_query = EXAMPLE_PROMPT + f"\nQ: {question}\nA:"
|
34 |
+
try:
|
35 |
+
result = qa(table=table, query=full_query)
|
36 |
+
return result.get("answer", "No answer found.")
|
37 |
+
except Exception as e:
|
38 |
+
# Return the actual exception message so you can debug
|
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="Ask a basic question…", label="Your question"),
|
45 |
+
outputs=gr.Textbox(lines=6, label="Answer"),
|
46 |
title="SAP Profitability Q&A",
|
47 |
description=(
|
48 |
"Ask simple revenue/cost/margin questions on the synthetic SAP data. "
|