Spaces:
Build error
Build error
Upload prompt.py
Browse files
prompt.py
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from langchain.agents import create_pandas_dataframe_agent
|
| 3 |
+
from langchain.llms import OpenAI
|
| 4 |
+
import pandas as pd
|
| 5 |
+
from langchain import OpenAI
|
| 6 |
+
from langchain.chat_models import ChatOpenAI
|
| 7 |
+
import os
|
| 8 |
+
from langchain.agents import ZeroShotAgent, Tool, AgentExecutor
|
| 9 |
+
from langchain import OpenAI,LLMChain
|
| 10 |
+
os.environ['OPENAI_API_KEY'] = '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'
|
| 11 |
+
os.environ["WOLFRAM_ALPHA_APPID"] = "5829GX-JVY5Y6LR3Q"
|
| 12 |
+
|
| 13 |
+
prefix = """
|
| 14 |
+
Hi! I'm a data analysis bot. I can help you perform basic exploratory data analysis (EDA) on your pandas dataframe.
|
| 15 |
+
|
| 16 |
+
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'.
|
| 17 |
+
|
| 18 |
+
if the user ask anything other than questions related to the data analysis answer with "I cannot help you with that"
|
| 19 |
+
When you're ready to begin, type 'Start' and I'll perform the following tasks:
|
| 20 |
+
- List all the column names.
|
| 21 |
+
- Count the number of rows and columns.
|
| 22 |
+
- Count the number of missing values.
|
| 23 |
+
- Compute descriptive statistics for numerical columns (count, mean, standard deviation, minimum, maximum, and quartiles).
|
| 24 |
+
- Get value counts for categorical variables.
|
| 25 |
+
- Identify any outliers, anomalies, or unusual patterns in the data.
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
"""
|
| 29 |
+
|
| 30 |
+
suffix = """
|
| 31 |
+
Please make sure to perform all the tasks you can and answer them one by one.\
|
| 32 |
+
Make sure you perform all the taks you can and answer them one by one
|
| 33 |
+
answer in this format
|
| 34 |
+
"list column names:
|
| 35 |
+
"Index","Class"
|
| 36 |
+
|
| 37 |
+
Input:
|
| 38 |
+
{input}
|
| 39 |
+
|
| 40 |
+
Agent Scratchpad:
|
| 41 |
+
{agent_scratchpad}
|
| 42 |
+
"""
|
| 43 |
+
global df
|
| 44 |
+
df = pd.read_csv('titanic.csv')
|
| 45 |
+
pandas = create_pandas_dataframe_agent(OpenAI(temperature=0), df, verbose=True)
|
| 46 |
+
llm = ChatOpenAI(temperature=0)
|
| 47 |
+
tools = [
|
| 48 |
+
Tool(
|
| 49 |
+
name = "Pandas",
|
| 50 |
+
func= pandas.run,
|
| 51 |
+
description="Useful to seach dataframe and perform data analysis"
|
| 52 |
+
)
|
| 53 |
+
]
|
| 54 |
+
|
| 55 |
+
prompt = ZeroShotAgent.create_prompt(
|
| 56 |
+
tools,
|
| 57 |
+
prefix=prefix,
|
| 58 |
+
suffix=suffix,
|
| 59 |
+
input_variables=["input","agent_scratchpad"]
|
| 60 |
+
)
|
| 61 |
+
llm_chain = LLMChain(llm=OpenAI(temperature=0), prompt=prompt)
|
| 62 |
+
|
| 63 |
+
tool_names = [tool.name for tool in tools]
|
| 64 |
+
agent = ZeroShotAgent(llm_chain=llm_chain, allowed_tools=tool_names)
|
| 65 |
+
|
| 66 |
+
def reply(input):
|
| 67 |
+
try:
|
| 68 |
+
agent_executor = AgentExecutor.from_agent_and_tools(agent=agent, tools=tools, verbose=True)
|
| 69 |
+
message = agent_executor.run(input)
|
| 70 |
+
|
| 71 |
+
return message
|
| 72 |
+
except Exception as e:
|
| 73 |
+
message = f"An error occurred: {str(e)}"
|
| 74 |
+
return message
|
| 75 |
+
input_text = gr.inputs.Textbox(
|
| 76 |
+
label="Enter your questions here",
|
| 77 |
+
placeholder="Enter 'Start' for basic EDA",
|
| 78 |
+
lines=3
|
| 79 |
+
|
| 80 |
+
)
|
| 81 |
+
text_output = gr.outputs.Textbox(label="Answer")
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
description = ""
|
| 85 |
+
|
| 86 |
+
iface = gr.Interface(
|
| 87 |
+
fn=reply,
|
| 88 |
+
inputs=input_text,
|
| 89 |
+
outputs=text_output,
|
| 90 |
+
title="Auto-Data Analysis",
|
| 91 |
+
description=description,
|
| 92 |
+
theme="light",
|
| 93 |
+
layout="vertical",
|
| 94 |
+
allow_flagging=False,
|
| 95 |
+
|
| 96 |
+
).launch()
|