vjain's picture
Update app.py
366420f
import gradio as gr
import openai
from langchain.agents import create_pandas_dataframe_agent
from langchain.llms import OpenAI
import pandas as pd
from langchain import OpenAI
from langchain.chat_models import ChatOpenAI
import os
from langchain.agents import ZeroShotAgent, Tool, AgentExecutor
from langchain import OpenAI,LLMChain
OPENAI_API_KEY= os.environ.get("OPENAI_API_KEY")
prefix = """
Hi! I'm a data analysis bot. I can help you perform basic exploratory data analysis (EDA) on your pandas dataframe.
To get started, please make sure you have pandas and matplotlib installed and provide me with a valid pandas dataframe by assigning it to the variable 'df'.
if the user ask anything other than questions related to the data analysis answer with "I cannot help you with that"
When you're ready to begin, type 'Start' and I'll perform the following tasks:
- List all the column names.
- Count the number of rows and columns.
- Count the number of missing values.
- Compute descriptive statistics for numerical columns (count, mean, standard deviation, minimum, maximum, and quartiles).
- Get value counts for categorical variables.
- Identify any outliers, anomalies, or unusual patterns in the data.
"""
suffix = """
Please make sure to perform all the tasks you can and answer them one by one.\
Make sure you perform all the taks you can and answer them one by one
answer in this format
"list column names:
"Index","Class"
Input:
{input}
Agent Scratchpad:
{agent_scratchpad}
"""
global df
df = pd.read_csv('titanic.csv')
pandas = create_pandas_dataframe_agent(OpenAI(temperature=0), df, verbose=True)
llm = ChatOpenAI(temperature=0)
tools = [
Tool(
name = "Pandas",
func= pandas.run,
description="Useful to seach dataframe and perform data analysis"
)
]
prompt = ZeroShotAgent.create_prompt(
tools,
prefix=prefix,
suffix=suffix,
input_variables=["input","agent_scratchpad"]
)
llm_chain = LLMChain(llm=OpenAI(temperature=0), prompt=prompt)
tool_names = [tool.name for tool in tools]
agent = ZeroShotAgent(llm_chain=llm_chain, allowed_tools=tool_names)
def reply(input):
try:
agent_executor = AgentExecutor.from_agent_and_tools(agent=agent, tools=tools, verbose=True)
message = agent_executor.run(input)
return message
except Exception as e:
message = f"An error occurred: {str(e)}"
return message
input_text = gr.inputs.Textbox(
label="Enter your questions here",
placeholder="Enter 'Start' for basic EDA",
lines=3
)
text_output = gr.outputs.Textbox(label="Answer")
description = ""
iface = gr.Interface(
fn=reply,
inputs=input_text,
outputs=text_output,
title="Auto-Data Analysis",
description=description,
theme="light",
layout="vertical",
allow_flagging=False,
).launch()