Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,12 +8,13 @@ from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline
|
|
| 8 |
# 1) Load your synthetic profitability dataset
|
| 9 |
df = pd.read_csv('synthetic_profit.csv')
|
| 10 |
|
| 11 |
-
# 2) Ensure numeric columns for true aggregation
|
| 12 |
for col in ["Revenue", "Profit", "ProfitMargin"]:
|
| 13 |
df[col] = pd.to_numeric(df[col], errors='coerce')
|
| 14 |
|
| 15 |
# 3) Build the schema description text
|
| 16 |
-
|
|
|
|
| 17 |
schema_text = "Table schema:\n" + "\n".join(schema_lines)
|
| 18 |
|
| 19 |
# 4) Few-shot examples teaching SUM and AVERAGE patterns
|
|
@@ -44,10 +45,10 @@ table_qa = pipeline(
|
|
| 44 |
|
| 45 |
# 6) QA function with schema-aware prompting
|
| 46 |
def answer_profitability(question: str) -> str:
|
| 47 |
-
#
|
| 48 |
table = df.astype(str).to_dict(orient="records")
|
| 49 |
|
| 50 |
-
#
|
| 51 |
prompt = f"""{schema_text}
|
| 52 |
|
| 53 |
{example_block}
|
|
@@ -55,7 +56,6 @@ def answer_profitability(question: str) -> str:
|
|
| 55 |
Q: {question}
|
| 56 |
A:"""
|
| 57 |
|
| 58 |
-
# 6c) call TAPEX
|
| 59 |
try:
|
| 60 |
out = table_qa(table=table, query=prompt)
|
| 61 |
return out.get("answer", "No answer found.")
|
|
@@ -74,5 +74,6 @@ iface = gr.Interface(
|
|
| 74 |
)
|
| 75 |
)
|
| 76 |
|
|
|
|
| 77 |
if __name__ == "__main__":
|
| 78 |
iface.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
| 8 |
# 1) Load your synthetic profitability dataset
|
| 9 |
df = pd.read_csv('synthetic_profit.csv')
|
| 10 |
|
| 11 |
+
# 2) Ensure numeric columns for true aggregation
|
| 12 |
for col in ["Revenue", "Profit", "ProfitMargin"]:
|
| 13 |
df[col] = pd.to_numeric(df[col], errors='coerce')
|
| 14 |
|
| 15 |
# 3) Build the schema description text
|
| 16 |
+
# ← replaced .iteritems() with .items() here
|
| 17 |
+
schema_lines = [f"- {col}: {dtype.name}" for col, dtype in df.dtypes.items()]
|
| 18 |
schema_text = "Table schema:\n" + "\n".join(schema_lines)
|
| 19 |
|
| 20 |
# 4) Few-shot examples teaching SUM and AVERAGE patterns
|
|
|
|
| 45 |
|
| 46 |
# 6) QA function with schema-aware prompting
|
| 47 |
def answer_profitability(question: str) -> str:
|
| 48 |
+
# cast all cells to string for safety
|
| 49 |
table = df.astype(str).to_dict(orient="records")
|
| 50 |
|
| 51 |
+
# assemble the full prompt
|
| 52 |
prompt = f"""{schema_text}
|
| 53 |
|
| 54 |
{example_block}
|
|
|
|
| 56 |
Q: {question}
|
| 57 |
A:"""
|
| 58 |
|
|
|
|
| 59 |
try:
|
| 60 |
out = table_qa(table=table, query=prompt)
|
| 61 |
return out.get("answer", "No answer found.")
|
|
|
|
| 74 |
)
|
| 75 |
)
|
| 76 |
|
| 77 |
+
# 8) Launch the app
|
| 78 |
if __name__ == "__main__":
|
| 79 |
iface.launch(server_name="0.0.0.0", server_port=7860)
|