Spaces:
Sleeping
Sleeping
import pandas as pd | |
import gradio as gr | |
from transformers import pipeline | |
from langchain_community.llms import HuggingFacePipeline | |
from langchain_experimental.agents import create_pandas_dataframe_agent | |
from langchain.agents.agent_types import AgentType | |
# Load data | |
df = pd.read_csv("synthetic_profit.csv") | |
# Lightweight Hugging Face pipeline (Flan-T5-base) | |
hf_pipeline = pipeline( | |
task="text2text-generation", | |
model="google/flan-t5-base", | |
device=-1 # CPU | |
) | |
# LangChain LLM | |
llm = HuggingFacePipeline(pipeline=hf_pipeline) | |
# Create LangChain agent with explicit parsing-error handling | |
agent = create_pandas_dataframe_agent( | |
llm, | |
df, | |
verbose=True, | |
agent_type=AgentType.ZERO_SHOT_REACT_DESCRIPTION, | |
handle_parsing_errors=True, | |
allow_dangerous_code=True | |
) | |
# Answer query function with clearer prompts | |
def answer(query: str) -> str: | |
try: | |
prompt = ( | |
f"Answer this clearly and numerically without scientific notation: {query}. " | |
"If multiple numbers, provide their total sum clearly." | |
) | |
response = agent.run(prompt) | |
return f"π {response}" | |
except Exception as e: | |
return f"β Error: {str(e)}" | |
# Gradio interface | |
demo = gr.Interface( | |
fn=answer, | |
inputs=gr.Textbox( | |
lines=2, | |
placeholder="E.g., 'Total revenue for Product B in EMEA during Q2 2024'" | |
), | |
outputs="text", | |
title="π’ SAP Profitability Data Chat (Flan-T5 + Pandas)", | |
description="Ask clearly numeric questions about synthetic SAP profitability data. Results are precise and human-readable." | |
) | |
demo.launch() | |