Spaces:
Runtime error
Runtime error
Upload 2 files
Browse files- app.py +90 -0
- requirements.txt +69 -0
app.py
ADDED
@@ -0,0 +1,90 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import time
|
2 |
+
from io import BytesIO
|
3 |
+
from langchain.llms import OpenAI
|
4 |
+
from dotenv import load_dotenv
|
5 |
+
import os
|
6 |
+
import streamlit as st
|
7 |
+
import pandas as pd
|
8 |
+
|
9 |
+
# Set the page configuration here
|
10 |
+
st.set_page_config(page_title="Insightly")
|
11 |
+
|
12 |
+
def main():
|
13 |
+
load_dotenv()
|
14 |
+
|
15 |
+
# Load the OpenAI API key from the environment variable
|
16 |
+
api_key = os.getenv("OPENAI_API_KEY")
|
17 |
+
if api_key is None or api_key == "":
|
18 |
+
st.error("OPENAI_API_KEY is not set")
|
19 |
+
return
|
20 |
+
|
21 |
+
st.sidebar.image("https://i.ibb.co/bX6GdqG/insightly-wbg.png", use_column_width=True)
|
22 |
+
st.title("Chat with Columns 💬")
|
23 |
+
|
24 |
+
csv_files = st.file_uploader("Upload CSV files", type="csv", accept_multiple_files=True)
|
25 |
+
if csv_files:
|
26 |
+
llm = OpenAI(temperature=0)
|
27 |
+
user_input = st.text_input("Question here:")
|
28 |
+
|
29 |
+
# Read the CSV file and get the column names
|
30 |
+
for csv_file in csv_files:
|
31 |
+
try:
|
32 |
+
df = pd.read_csv(csv_file)
|
33 |
+
except pd.errors.EmptyDataError:
|
34 |
+
st.error(f"Empty CSV file uploaded: {csv_file.name}")
|
35 |
+
continue
|
36 |
+
|
37 |
+
if df.empty:
|
38 |
+
st.error(f"Empty CSV file uploaded: {csv_file.name}")
|
39 |
+
continue
|
40 |
+
|
41 |
+
column_names = df.columns.tolist()
|
42 |
+
|
43 |
+
# Dropdown to select the column for prompts
|
44 |
+
column_for_prompt = st.selectbox("Select the column for prompts:", [None] + column_names)
|
45 |
+
|
46 |
+
if column_for_prompt is not None:
|
47 |
+
# Create a list to store the responses and original rows for each CSV file
|
48 |
+
responses_list = []
|
49 |
+
original_rows_list = []
|
50 |
+
|
51 |
+
# Check if the specified column for prompts exists in the DataFrame
|
52 |
+
if column_for_prompt not in df.columns:
|
53 |
+
st.error(f"The column '{column_for_prompt}' does not exist in the CSV file: {csv_file.name}")
|
54 |
+
continue
|
55 |
+
|
56 |
+
# Perform any necessary data preprocessing or feature engineering here
|
57 |
+
# You can modify the code based on your specific requirements
|
58 |
+
|
59 |
+
# Example: Accessing columns from the DataFrame
|
60 |
+
column_data = df[column_for_prompt]
|
61 |
+
|
62 |
+
# Loop through each row in the specified column and pass the user input as prompt
|
63 |
+
for row_value in column_data:
|
64 |
+
original_rows_list.append(row_value)
|
65 |
+
# Example: Using the preprocessed data with the OpenAI API
|
66 |
+
llm_response = llm.predict(row_value + " " + user_input)
|
67 |
+
responses_list.append(llm_response)
|
68 |
+
|
69 |
+
# Introduce a delay of 1 second between API calls to reduce the rate of requests
|
70 |
+
time.sleep(1)
|
71 |
+
|
72 |
+
# Create a new DataFrame containing the original rows and responses
|
73 |
+
response_df = pd.DataFrame({
|
74 |
+
"Original Rows": original_rows_list,
|
75 |
+
"Responses": responses_list
|
76 |
+
})
|
77 |
+
|
78 |
+
# Offer the option to download the responses as a CSV file
|
79 |
+
if st.button("Download Responses as CSV"):
|
80 |
+
with BytesIO() as output_file:
|
81 |
+
response_df.to_csv(output_file, index=False)
|
82 |
+
st.download_button(
|
83 |
+
label="Download CSV",
|
84 |
+
data=output_file.getvalue(),
|
85 |
+
file_name="responses.csv",
|
86 |
+
mime="text/csv",
|
87 |
+
)
|
88 |
+
|
89 |
+
if __name__ == "__main__":
|
90 |
+
main()
|
requirements.txt
ADDED
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
aiohttp==3.8.5
|
2 |
+
aiosignal==1.3.1
|
3 |
+
altair==5.0.1
|
4 |
+
async-timeout==4.0.2
|
5 |
+
attrs==23.1.0
|
6 |
+
blinker==1.6.2
|
7 |
+
cachetools==5.3.1
|
8 |
+
certifi==2023.7.22
|
9 |
+
charset-normalizer==3.2.0
|
10 |
+
click==8.1.6
|
11 |
+
dataclasses-json==0.5.13
|
12 |
+
decorator==5.1.1
|
13 |
+
frozenlist==1.4.0
|
14 |
+
gitdb==4.0.10
|
15 |
+
GitPython==3.1.32
|
16 |
+
greenlet==2.0.2
|
17 |
+
idna==3.4
|
18 |
+
importlib-metadata==6.8.0
|
19 |
+
Jinja2==3.1.2
|
20 |
+
jsonschema==4.18.4
|
21 |
+
jsonschema-specifications==2023.7.1
|
22 |
+
langchain==0.0.240
|
23 |
+
langsmith==0.0.14
|
24 |
+
markdown-it-py==3.0.0
|
25 |
+
MarkupSafe==2.1.3
|
26 |
+
marshmallow==3.20.1
|
27 |
+
mdurl==0.1.2
|
28 |
+
multidict==6.0.4
|
29 |
+
mypy-extensions==1.0.0
|
30 |
+
numexpr==2.8.4
|
31 |
+
numpy==1.25.1
|
32 |
+
openai==0.27.8
|
33 |
+
openapi-schema-pydantic==1.2.4
|
34 |
+
packaging==23.1
|
35 |
+
pandas==2.0.3
|
36 |
+
Pillow==9.5.0
|
37 |
+
protobuf==4.23.4
|
38 |
+
pyarrow==12.0.1
|
39 |
+
pydantic==1.10.11
|
40 |
+
pydeck==0.8.0
|
41 |
+
Pygments==2.15.1
|
42 |
+
Pympler==1.0.1
|
43 |
+
python-dateutil==2.8.2
|
44 |
+
python-dotenv==1.0.0
|
45 |
+
pytz==2023.3
|
46 |
+
pytz-deprecation-shim==0.1.0.post0
|
47 |
+
PyYAML==6.0.1
|
48 |
+
referencing==0.30.0
|
49 |
+
requests==2.31.0
|
50 |
+
rich==13.4.2
|
51 |
+
rpds-py==0.9.2
|
52 |
+
six==1.16.0
|
53 |
+
smmap==5.0.0
|
54 |
+
SQLAlchemy==2.0.19
|
55 |
+
streamlit==1.25.0
|
56 |
+
tenacity==8.2.2
|
57 |
+
toml==0.10.2
|
58 |
+
toolz==0.12.0
|
59 |
+
tornado==6.3.2
|
60 |
+
tqdm==4.65.0
|
61 |
+
typing-inspect==0.9.0
|
62 |
+
typing_extensions==4.7.1
|
63 |
+
tzdata==2023.3
|
64 |
+
tzlocal==4.3.1
|
65 |
+
urllib3==2.0.4
|
66 |
+
validators==0.20.0
|
67 |
+
watchdog==3.0.0
|
68 |
+
yarl==1.9.2
|
69 |
+
zipp==3.16.2
|