File size: 3,024 Bytes
f8342a5
2fa9719
f8342a5
 
 
 
 
 
 
 
c1285d9
f8342a5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
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()