Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,71 +1,37 @@
|
|
1 |
import pandas as pd
|
2 |
-
import numpy as np
|
3 |
-
import re
|
4 |
-
from transformers import pipeline
|
5 |
import gradio as gr
|
|
|
|
|
|
|
6 |
|
7 |
-
# Load
|
8 |
df = pd.read_csv("synthetic_profit.csv")
|
9 |
-
df_str = df.astype(str)
|
10 |
|
11 |
-
#
|
12 |
-
|
13 |
-
"
|
14 |
-
model="google/
|
15 |
-
|
16 |
)
|
17 |
|
18 |
-
#
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
def answer(q: str) -> str:
|
23 |
-
# 1. Conditional query: negative profit
|
24 |
-
if re.search(r"products.*negative.*profit", q, re.IGNORECASE):
|
25 |
-
negative_profits = df[df["Profit"] < 0]
|
26 |
-
if negative_profits.empty:
|
27 |
-
return "β
No products with negative profit found."
|
28 |
-
results = negative_profits[['Product', 'Region', 'FiscalQuarter', 'FiscalYear', 'Profit']]
|
29 |
-
return results.to_string(index=False)
|
30 |
-
|
31 |
-
# 2. Numeric summaries (total/average) β Case insensitive metric matching
|
32 |
-
match = re.search(
|
33 |
-
r"\b(total|average)\s+(profitmargin|profit|revenue|cost)\b.*\bProduct\s*([A-D])\b.*\b(Q[1-4])\s+(\d{4})",
|
34 |
-
q, re.IGNORECASE
|
35 |
-
)
|
36 |
-
if match:
|
37 |
-
agg_type, metric_key, product_letter, quarter, year = match.groups()
|
38 |
-
metric = metric_columns[metric_key.lower()]
|
39 |
-
product = f"Product {product_letter.upper()}"
|
40 |
-
subset = df[
|
41 |
-
(df["Product"] == product) &
|
42 |
-
(df["FiscalQuarter"] == quarter) &
|
43 |
-
(df["FiscalYear"] == int(year))
|
44 |
-
]
|
45 |
-
if subset.empty:
|
46 |
-
return "β οΈ No matching data found."
|
47 |
-
if agg_type.lower() == "total":
|
48 |
-
val = subset[metric].sum()
|
49 |
-
else: # average
|
50 |
-
val = subset[metric].mean()
|
51 |
-
|
52 |
-
formatted_val = f"{val:.3f}" if metric == "ProfitMargin" else f"{val:,.2f}"
|
53 |
-
return f"π {agg_type.title()} {metric} for {product} in {quarter} {year}: {formatted_val}"
|
54 |
|
55 |
-
|
56 |
try:
|
57 |
-
|
58 |
-
return f"
|
59 |
except Exception as e:
|
60 |
-
return f"β Error
|
61 |
|
62 |
-
#
|
63 |
demo = gr.Interface(
|
64 |
fn=answer,
|
65 |
-
inputs=gr.Textbox(lines=2, placeholder="
|
66 |
outputs="text",
|
67 |
-
title="π’ SAP
|
68 |
-
description="Ask questions
|
69 |
)
|
70 |
|
71 |
demo.launch()
|
|
|
1 |
import pandas as pd
|
|
|
|
|
|
|
2 |
import gradio as gr
|
3 |
+
from transformers import pipeline
|
4 |
+
from langchain.llms import HuggingFacePipeline
|
5 |
+
from langchain.agents import create_pandas_dataframe_agent
|
6 |
|
7 |
+
# Load your data
|
8 |
df = pd.read_csv("synthetic_profit.csv")
|
|
|
9 |
|
10 |
+
# Set up lightweight Hugging Face pipeline with Flan-T5 base
|
11 |
+
hf_pipeline = pipeline(
|
12 |
+
task="text2text-generation",
|
13 |
+
model="google/flan-t5-base",
|
14 |
+
device=-1 # ensures CPU
|
15 |
)
|
16 |
|
17 |
+
# LangChain agent setup
|
18 |
+
llm = HuggingFacePipeline(pipeline=hf_pipeline)
|
19 |
+
agent = create_pandas_dataframe_agent(llm, df, verbose=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
+
def answer(query: str) -> str:
|
22 |
try:
|
23 |
+
response = agent.run(query)
|
24 |
+
return f"π {response}"
|
25 |
except Exception as e:
|
26 |
+
return f"β Error: {str(e)}"
|
27 |
|
28 |
+
# Gradio interface
|
29 |
demo = gr.Interface(
|
30 |
fn=answer,
|
31 |
+
inputs=gr.Textbox(lines=2, placeholder="E.g., 'Total revenue for Product B in EMEA', 'List products with negative profit.'"),
|
32 |
outputs="text",
|
33 |
+
title="π’ SAP Profitability Data Chat (Flan-T5 + Pandas)",
|
34 |
+
description="Ask questions about synthetic SAP profitability data. Powered by Flan-T5-base via Hugging Face."
|
35 |
)
|
36 |
|
37 |
demo.launch()
|