talk_to_data / app.py
PD03's picture
Update app.py
0f5aca7 verified
raw
history blame
1.13 kB
import pandas as pd
import gradio as gr
from transformers import pipeline
from langchain.llms import HuggingFacePipeline
from langchain.agents import create_pandas_dataframe_agent
# Load your data
df = pd.read_csv("synthetic_profit.csv")
# Set up lightweight Hugging Face pipeline with Flan-T5 base
hf_pipeline = pipeline(
task="text2text-generation",
model="google/flan-t5-base",
device=-1 # ensures CPU
)
# LangChain agent setup
llm = HuggingFacePipeline(pipeline=hf_pipeline)
agent = create_pandas_dataframe_agent(llm, df, verbose=True)
def answer(query: str) -> str:
try:
response = agent.run(query)
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', 'List products with negative profit.'"),
outputs="text",
title="🟒 SAP Profitability Data Chat (Flan-T5 + Pandas)",
description="Ask questions about synthetic SAP profitability data. Powered by Flan-T5-base via Hugging Face."
)
demo.launch()