Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -3,24 +3,39 @@ import gradio as gr
|
|
3 |
from transformers import pipeline
|
4 |
from langchain_community.llms import HuggingFacePipeline
|
5 |
from langchain_experimental.agents import create_pandas_dataframe_agent
|
|
|
6 |
|
7 |
# Load data
|
8 |
df = pd.read_csv("synthetic_profit.csv")
|
9 |
|
10 |
-
#
|
11 |
hf_pipeline = pipeline(
|
12 |
task="text2text-generation",
|
13 |
model="google/flan-t5-base",
|
14 |
device=-1 # CPU
|
15 |
)
|
16 |
|
17 |
-
# LangChain
|
18 |
llm = HuggingFacePipeline(pipeline=hf_pipeline)
|
19 |
-
agent = create_pandas_dataframe_agent(llm, df, verbose=True, allow_dangerous_code=True)
|
20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
def answer(query: str) -> str:
|
22 |
try:
|
23 |
-
|
|
|
|
|
|
|
|
|
24 |
return f"π {response}"
|
25 |
except Exception as e:
|
26 |
return f"β Error: {str(e)}"
|
@@ -28,10 +43,13 @@ def answer(query: str) -> str:
|
|
28 |
# Gradio interface
|
29 |
demo = gr.Interface(
|
30 |
fn=answer,
|
31 |
-
inputs=gr.Textbox(
|
|
|
|
|
|
|
32 |
outputs="text",
|
33 |
title="π’ SAP Profitability Data Chat (Flan-T5 + Pandas)",
|
34 |
-
description="Ask questions about synthetic SAP profitability data.
|
35 |
)
|
36 |
|
37 |
demo.launch()
|
|
|
3 |
from transformers import pipeline
|
4 |
from langchain_community.llms import HuggingFacePipeline
|
5 |
from langchain_experimental.agents import create_pandas_dataframe_agent
|
6 |
+
from langchain.agents.agent_types import AgentType
|
7 |
|
8 |
# Load data
|
9 |
df = pd.read_csv("synthetic_profit.csv")
|
10 |
|
11 |
+
# Lightweight Hugging Face pipeline (Flan-T5-base)
|
12 |
hf_pipeline = pipeline(
|
13 |
task="text2text-generation",
|
14 |
model="google/flan-t5-base",
|
15 |
device=-1 # CPU
|
16 |
)
|
17 |
|
18 |
+
# LangChain LLM
|
19 |
llm = HuggingFacePipeline(pipeline=hf_pipeline)
|
|
|
20 |
|
21 |
+
# Create LangChain agent with explicit parsing-error handling
|
22 |
+
agent = create_pandas_dataframe_agent(
|
23 |
+
llm,
|
24 |
+
df,
|
25 |
+
verbose=True,
|
26 |
+
agent_type=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
|
27 |
+
handle_parsing_errors=True,
|
28 |
+
allow_dangerous_code=True
|
29 |
+
)
|
30 |
+
|
31 |
+
# Answer query function with clearer prompts
|
32 |
def answer(query: str) -> str:
|
33 |
try:
|
34 |
+
prompt = (
|
35 |
+
f"Answer this clearly and numerically without scientific notation: {query}. "
|
36 |
+
"If multiple numbers, provide their total sum clearly."
|
37 |
+
)
|
38 |
+
response = agent.run(prompt)
|
39 |
return f"π {response}"
|
40 |
except Exception as e:
|
41 |
return f"β Error: {str(e)}"
|
|
|
43 |
# Gradio interface
|
44 |
demo = gr.Interface(
|
45 |
fn=answer,
|
46 |
+
inputs=gr.Textbox(
|
47 |
+
lines=2,
|
48 |
+
placeholder="E.g., 'Total revenue for Product B in EMEA during Q2 2024'"
|
49 |
+
),
|
50 |
outputs="text",
|
51 |
title="π’ SAP Profitability Data Chat (Flan-T5 + Pandas)",
|
52 |
+
description="Ask clearly numeric questions about synthetic SAP profitability data. Results are precise and human-readable."
|
53 |
)
|
54 |
|
55 |
demo.launch()
|