File size: 1,200 Bytes
a5ece8b
 
0f5aca7
8353d84
 
887b999
daedd19
a5ece8b
 
daedd19
0f5aca7
 
 
daedd19
887b999
 
daedd19
0f5aca7
daedd19
6a97111
0f5aca7
67fc297
0f5aca7
 
67fc297
0f5aca7
887b999
0f5aca7
887b999
 
0f5aca7
6a97111
0f5aca7
 
887b999
a5ece8b
8353d84
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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

# Load data
df = pd.read_csv("synthetic_profit.csv")

# Set up lightweight Hugging Face pipeline (Flan-T5 Base)
hf_pipeline = pipeline(
    task="text2text-generation",
    model="google/flan-t5-base",
    device=-1  # CPU
)

# LangChain agent setup (with explicit security opt-in)
llm = HuggingFacePipeline(pipeline=hf_pipeline)
agent = create_pandas_dataframe_agent(llm, df, verbose=True, allow_dangerous_code=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()