Commit
·
f9ed673
1
Parent(s):
3d73574
Upload 7 files
Browse files- .env +1 -0
- .env.example +2 -0
- app.py +19 -0
- employees.csv +51 -0
- newapp.py +102 -0
- requirements.txt +7 -0
- utils.py +19 -0
.env
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
OPENAI_API_KEY="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"
|
.env.example
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
OPENAI_API_KEY=""
|
| 2 |
+
HUGGINGFACEHUB_API_TOKEN=""
|
app.py
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from dotenv import load_dotenv
|
| 3 |
+
load_dotenv()
|
| 4 |
+
from utils import query_agent
|
| 5 |
+
|
| 6 |
+
st.title("Let's do some analysis on your file")
|
| 7 |
+
st.header("Please upload the document here:")
|
| 8 |
+
|
| 9 |
+
# Capture the file
|
| 10 |
+
|
| 11 |
+
data = st.file_uploader("Upload CSV file", type="csv")
|
| 12 |
+
|
| 13 |
+
query = st.text_area("Enter your query")
|
| 14 |
+
button = st.button("Generate Response")
|
| 15 |
+
|
| 16 |
+
if button:
|
| 17 |
+
# Get Response
|
| 18 |
+
answer = query_agent(data,query)
|
| 19 |
+
st.write(answer)
|
employees.csv
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL,PHONE_NUMBER,HIRE_DATE,JOB_ID,SALARY,COMMISSION_PCT,MANAGER_ID,DEPARTMENT_ID
|
| 2 |
+
198,Donald,OConnell,DOCONNEL,650.507.9833,21-JUN-07,SH_CLERK,2600, - ,124,50
|
| 3 |
+
199,Douglas,Grant,DGRANT,650.507.9844,13-JAN-08,SH_CLERK,2600, - ,124,50
|
| 4 |
+
200,Jennifer,Whalen,JWHALEN,515.123.4444,17-SEP-03,AD_ASST,4400, - ,101,10
|
| 5 |
+
201,Michael,Hartstein,MHARTSTE,515.123.5555,17-FEB-04,MK_MAN,13000, - ,100,20
|
| 6 |
+
202,Pat,Fay,PFAY,603.123.6666,17-AUG-05,MK_REP,6000, - ,201,20
|
| 7 |
+
203,Susan,Mavris,SMAVRIS,515.123.7777,07-JUN-02,HR_REP,6500, - ,101,40
|
| 8 |
+
204,Hermann,Baer,HBAER,515.123.8888,07-JUN-02,PR_REP,10000, - ,101,70
|
| 9 |
+
205,Shelley,Higgins,SHIGGINS,515.123.8080,07-JUN-02,AC_MGR,12008, - ,101,110
|
| 10 |
+
206,William,Gietz,WGIETZ,515.123.8181,07-JUN-02,AC_ACCOUNT,8300, - ,205,110
|
| 11 |
+
100,Steven,King,SKING,515.123.4567,17-JUN-03,AD_PRES,24000, - , - ,90
|
| 12 |
+
101,Neena,Kochhar,NKOCHHAR,515.123.4568,21-SEP-05,AD_VP,17000, - ,100,90
|
| 13 |
+
102,Lex,De Haan,LDEHAAN,515.123.4569,13-JAN-01,AD_VP,17000, - ,100,90
|
| 14 |
+
103,Alexander,Hunold,AHUNOLD,590.423.4567,03-JAN-06,IT_PROG,9000, - ,102,60
|
| 15 |
+
104,Bruce,Ernst,BERNST,590.423.4568,21-MAY-07,IT_PROG,6000, - ,103,60
|
| 16 |
+
105,David,Austin,DAUSTIN,590.423.4569,25-JUN-05,IT_PROG,4800, - ,103,60
|
| 17 |
+
106,Valli,Pataballa,VPATABAL,590.423.4560,05-FEB-06,IT_PROG,4800, - ,103,60
|
| 18 |
+
107,Diana,Lorentz,DLORENTZ,590.423.5567,07-FEB-07,IT_PROG,4200, - ,103,60
|
| 19 |
+
108,Nancy,Greenberg,NGREENBE,515.124.4569,17-AUG-02,FI_MGR,12008, - ,101,100
|
| 20 |
+
109,Daniel,Faviet,DFAVIET,515.124.4169,16-AUG-02,FI_ACCOUNT,9000, - ,108,100
|
| 21 |
+
110,John,Chen,JCHEN,515.124.4269,28-SEP-05,FI_ACCOUNT,8200, - ,108,100
|
| 22 |
+
111,Ismael,Sciarra,ISCIARRA,515.124.4369,30-SEP-05,FI_ACCOUNT,7700, - ,108,100
|
| 23 |
+
112,Jose Manuel,Urman,JMURMAN,515.124.4469,07-MAR-06,FI_ACCOUNT,7800, - ,108,100
|
| 24 |
+
113,Luis,Popp,LPOPP,515.124.4567,07-DEC-07,FI_ACCOUNT,6900, - ,108,100
|
| 25 |
+
114,Den,Raphaely,DRAPHEAL,515.127.4561,07-DEC-02,PU_MAN,11000, - ,100,30
|
| 26 |
+
115,Alexander,Khoo,AKHOO,515.127.4562,18-MAY-03,PU_CLERK,3100, - ,114,30
|
| 27 |
+
116,Shelli,Baida,SBAIDA,515.127.4563,24-DEC-05,PU_CLERK,2900, - ,114,30
|
| 28 |
+
117,Sigal,Tobias,STOBIAS,515.127.4564,24-JUL-05,PU_CLERK,2800, - ,114,30
|
| 29 |
+
118,Guy,Himuro,GHIMURO,515.127.4565,15-NOV-06,PU_CLERK,2600, - ,114,30
|
| 30 |
+
119,Karen,Colmenares,KCOLMENA,515.127.4566,10-AUG-07,PU_CLERK,2500, - ,114,30
|
| 31 |
+
120,Matthew,Weiss,MWEISS,650.123.1234,18-JUL-04,ST_MAN,8000, - ,100,50
|
| 32 |
+
121,Adam,Fripp,AFRIPP,650.123.2234,10-APR-05,ST_MAN,8200, - ,100,50
|
| 33 |
+
122,Payam,Kaufling,PKAUFLIN,650.123.3234,01-MAY-03,ST_MAN,7900, - ,100,50
|
| 34 |
+
123,Shanta,Vollman,SVOLLMAN,650.123.4234,10-OCT-05,ST_MAN,6500, - ,100,50
|
| 35 |
+
124,Kevin,Mourgos,KMOURGOS,650.123.5234,16-NOV-07,ST_MAN,5800, - ,100,50
|
| 36 |
+
125,Julia,Nayer,JNAYER,650.124.1214,16-JUL-05,ST_CLERK,3200, - ,120,50
|
| 37 |
+
126,Irene,Mikkilineni,IMIKKILI,650.124.1224,28-SEP-06,ST_CLERK,2700, - ,120,50
|
| 38 |
+
127,James,Landry,JLANDRY,650.124.1334,14-JAN-07,ST_CLERK,2400, - ,120,50
|
| 39 |
+
128,Steven,Markle,SMARKLE,650.124.1434,08-MAR-08,ST_CLERK,2200, - ,120,50
|
| 40 |
+
129,Laura,Bissot,LBISSOT,650.124.5234,20-AUG-05,ST_CLERK,3300, - ,121,50
|
| 41 |
+
130,Mozhe,Atkinson,MATKINSO,650.124.6234,30-OCT-05,ST_CLERK,2800, - ,121,50
|
| 42 |
+
131,James,Marlow,JAMRLOW,650.124.7234,16-FEB-05,ST_CLERK,2500, - ,121,50
|
| 43 |
+
132,TJ,Olson,TJOLSON,650.124.8234,10-APR-07,ST_CLERK,2100, - ,121,50
|
| 44 |
+
133,Jason,Mallin,JMALLIN,650.127.1934,14-JUN-04,ST_CLERK,3300, - ,122,50
|
| 45 |
+
134,Michael,Rogers,MROGERS,650.127.1834,26-AUG-06,ST_CLERK,2900, - ,122,50
|
| 46 |
+
135,Ki,Gee,KGEE,650.127.1734,12-DEC-07,ST_CLERK,2400, - ,122,50
|
| 47 |
+
136,Hazel,Philtanker,HPHILTAN,650.127.1634,06-FEB-08,ST_CLERK,2200, - ,122,50
|
| 48 |
+
137,Renske,Ladwig,RLADWIG,650.121.1234,14-JUL-03,ST_CLERK,3600, - ,123,50
|
| 49 |
+
138,Stephen,Stiles,SSTILES,650.121.2034,26-OCT-05,ST_CLERK,3200, - ,123,50
|
| 50 |
+
139,John,Seo,JSEO,650.121.2019,12-FEB-06,ST_CLERK,2700, - ,123,50
|
| 51 |
+
140,Joshua,Patel,JPATEL,650.121.1834,06-APR-06,ST_CLERK,2500, - ,123,50
|
newapp.py
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import matplotlib.pyplot as plt
|
| 4 |
+
import seaborn as sns
|
| 5 |
+
import os
|
| 6 |
+
from langchain.agents import create_pandas_dataframe_agent
|
| 7 |
+
from langchain.llms import OpenAI
|
| 8 |
+
from dotenv import load_dotenv
|
| 9 |
+
load_dotenv()
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
def query_agent(df, query):
|
| 13 |
+
# Use the environment variable for OpenAI API key
|
| 14 |
+
openai_api_key = os.getenv("OPENAI_API_KEY")
|
| 15 |
+
|
| 16 |
+
llm = OpenAI(openai_api_key=openai_api_key)
|
| 17 |
+
|
| 18 |
+
# Create a Pandas DataFrame Agent.
|
| 19 |
+
agent = create_pandas_dataframe_agent(llm, df, verbose=True)
|
| 20 |
+
|
| 21 |
+
# Python REPL: A python shell used to evaluating and executing Python commands
|
| 22 |
+
# It takes python code as an input and outputs the results. The input python code can be generated from another tool in the langchain
|
| 23 |
+
return agent.run(query)
|
| 24 |
+
|
| 25 |
+
def plot_bar_chart(df, x_column, y_column):
|
| 26 |
+
fig, ax = plt.subplots()
|
| 27 |
+
ax.bar(df[x_column], df[y_column])
|
| 28 |
+
ax.set_xlabel(x_column)
|
| 29 |
+
ax.set_ylabel(y_column)
|
| 30 |
+
ax.set_title(f"Bar Chart: {x_column} vs {y_column}")
|
| 31 |
+
st.pyplot(fig)
|
| 32 |
+
|
| 33 |
+
def plot_line_chart(df, x_column, y_column):
|
| 34 |
+
fig, ax = plt.subplots()
|
| 35 |
+
ax.plot(df[x_column], df[y_column])
|
| 36 |
+
ax.set_xlabel(x_column)
|
| 37 |
+
ax.set_ylabel(y_column)
|
| 38 |
+
ax.set_title(f"Line Chart: {x_column} vs {y_column}")
|
| 39 |
+
st.pyplot(fig)
|
| 40 |
+
|
| 41 |
+
def plot_scatter_plot(df, x_column, y_column):
|
| 42 |
+
fig, ax = plt.subplots()
|
| 43 |
+
ax.scatter(df[x_column], df[y_column])
|
| 44 |
+
ax.set_xlabel(x_column)
|
| 45 |
+
ax.set_ylabel(y_column)
|
| 46 |
+
ax.set_title(f"Scatter Plot: {x_column} vs {y_column}")
|
| 47 |
+
st.pyplot(fig)
|
| 48 |
+
|
| 49 |
+
def plot_heatmap(df):
|
| 50 |
+
fig, ax = plt.subplots()
|
| 51 |
+
sns.heatmap(df.corr(), annot=True, cmap="coolwarm")
|
| 52 |
+
ax.set_title("Heatmap")
|
| 53 |
+
st.pyplot(fig)
|
| 54 |
+
|
| 55 |
+
def main():
|
| 56 |
+
st.title("Data Analysis with Streamlit")
|
| 57 |
+
st.header("Please upload the document here:")
|
| 58 |
+
|
| 59 |
+
# Initialize df to None
|
| 60 |
+
df = None
|
| 61 |
+
|
| 62 |
+
# Capture the file
|
| 63 |
+
data = st.file_uploader("Upload CSV file", type="csv")
|
| 64 |
+
|
| 65 |
+
query = st.text_area("Enter your query")
|
| 66 |
+
button = st.button("Generate Response")
|
| 67 |
+
|
| 68 |
+
if data is not None:
|
| 69 |
+
# Read the uploaded CSV file into a DataFrame
|
| 70 |
+
df = pd.read_csv(data)
|
| 71 |
+
|
| 72 |
+
if button:
|
| 73 |
+
if df is not None:
|
| 74 |
+
# Get Response using query_agent function
|
| 75 |
+
answer = query_agent(df, query)
|
| 76 |
+
st.write("Response: ", answer)
|
| 77 |
+
|
| 78 |
+
# Data Visualization: Chart Options
|
| 79 |
+
st.subheader("Data Visualization: Chart Options")
|
| 80 |
+
chart_types = ["Bar Chart", "Line Chart", "Scatter Plot", "Heatmap"]
|
| 81 |
+
selected_chart = st.selectbox("Select Chart Type", chart_types)
|
| 82 |
+
|
| 83 |
+
if df is not None:
|
| 84 |
+
x_column = st.selectbox("Select X-axis column", df.columns)
|
| 85 |
+
y_column = st.selectbox("Select Y-axis column", df.columns)
|
| 86 |
+
|
| 87 |
+
if x_column != y_column:
|
| 88 |
+
if selected_chart == "Bar Chart":
|
| 89 |
+
plot_bar_chart(df, x_column, y_column)
|
| 90 |
+
elif selected_chart == "Line Chart":
|
| 91 |
+
plot_line_chart(df, x_column, y_column)
|
| 92 |
+
elif selected_chart == "Scatter Plot":
|
| 93 |
+
plot_scatter_plot(df, x_column, y_column)
|
| 94 |
+
elif selected_chart == "Heatmap":
|
| 95 |
+
plot_heatmap(df)
|
| 96 |
+
else:
|
| 97 |
+
st.write("Please select different columns for X-axis and Y-axis.")
|
| 98 |
+
else:
|
| 99 |
+
st.warning("Please upload a CSV file.")
|
| 100 |
+
|
| 101 |
+
if __name__ == "__main__":
|
| 102 |
+
main()
|
requirements.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
langchain
|
| 2 |
+
streamlit
|
| 3 |
+
openai
|
| 4 |
+
tiktoken
|
| 5 |
+
python-dotenv
|
| 6 |
+
pinecone-client
|
| 7 |
+
plotly
|
utils.py
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from langchain.agents import create_pandas_dataframe_agent
|
| 2 |
+
import pandas as pd
|
| 3 |
+
from langchain.llms import OpenAI
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
def query_agent(data, query):
|
| 7 |
+
|
| 8 |
+
#Parse the CSV file and create a Pandas DataFrame from its contents.
|
| 9 |
+
df = pd.read_csv(data)
|
| 10 |
+
|
| 11 |
+
llm = OpenAI()
|
| 12 |
+
|
| 13 |
+
# Create a Pandas DataFrame Agent.
|
| 14 |
+
agent = create_pandas_dataframe_agent(llm,df,verbose=True)
|
| 15 |
+
|
| 16 |
+
#Python REPL: A python shell used to evaluating and excuting Python commands
|
| 17 |
+
#It takes python code as an input and outputs the results. The input python code can be generated from another tool in the langchain
|
| 18 |
+
return agent.run(query)
|
| 19 |
+
|