Spaces:
Sleeping
Sleeping
Commit
·
33476c5
1
Parent(s):
ae116c2
updated pom file
Browse files
app.py
CHANGED
@@ -20,160 +20,88 @@ import pandas as pd
|
|
20 |
import numpy as np
|
21 |
import pprint
|
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 |
-
pdfCSVURLText = "
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
embedding=GooglePalmEmbeddings(),
|
106 |
-
text_splitter=CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)).from_loaders(loader)
|
107 |
-
|
108 |
-
chain = RetrievalQA.from_chain_type(llm=llm,
|
109 |
-
chain_type="stuff",
|
110 |
-
retriever=index.vectorstore.as_retriever(),
|
111 |
-
input_key="question")
|
112 |
-
elif genre==radioButtonList[4]:
|
113 |
-
pdfCSVURLText = "URL"
|
114 |
-
exampleQuestion = "Can you summarize the contents?"
|
115 |
-
|
116 |
-
isCustomURL = genre==radioButtonList[4]
|
117 |
-
urlInput = st.text_input('Enter your own URL', '', placeholder="Type your URL here (e.g. https://abc.xyz/investor/)", disabled=not isCustomURL)
|
118 |
-
|
119 |
-
isCustomPDF = genre==radioButtonList[1] or genre==radioButtonList[2]
|
120 |
-
uploaded_file = st.file_uploader(f"Upload your own {pdfCSVURLText} here", type=pdfCSVURLText.lower(), disabled=not isCustomPDF)
|
121 |
-
uploadedFilename = ""
|
122 |
-
if uploaded_file is not None:
|
123 |
-
with NamedTemporaryFile(dir='.', suffix=f'.{pdfCSVURLText.lower()}') as f:
|
124 |
-
f.write(uploaded_file.getbuffer())
|
125 |
-
uploadedFilename = f.name
|
126 |
-
if genre==radioButtonList[1]: # Custom CSV Upload
|
127 |
-
dataDF = pd.read_csv(uploadedFilename, encoding= 'unicode_escape')
|
128 |
-
elif genre==radioButtonList[2]: # Custom PDF Upload
|
129 |
-
pdf_loaders = [UnstructuredPDFLoader(uploadedFilename)]
|
130 |
-
pdf_index = VectorstoreIndexCreator(
|
131 |
-
embedding=GooglePalmEmbeddings(),
|
132 |
-
text_splitter=CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)).from_loaders(pdf_loaders)
|
133 |
-
pdf_chain = RetrievalQA.from_chain_type(llm=llm,
|
134 |
-
chain_type="stuff",
|
135 |
-
retriever=pdf_index.vectorstore.as_retriever(),
|
136 |
-
input_key="question")
|
137 |
-
|
138 |
-
enableChatBox = False
|
139 |
-
if genre==radioButtonList[0]: # E-commerce CSV
|
140 |
-
enableChatBox = True
|
141 |
-
elif genre==radioButtonList[1]: # Custom CSV Upload
|
142 |
-
enableChatBox = uploadedFilename[-4:]==".csv"
|
143 |
-
elif genre==radioButtonList[2]: # Custom PDF Upload
|
144 |
-
enableChatBox = uploadedFilename[-4:]==".pdf"
|
145 |
-
elif genre==radioButtonList[3]: # Google Alphabet URL Earnings Report
|
146 |
-
enableChatBox = True
|
147 |
-
elif genre==radioButtonList[4]: # Custom URL
|
148 |
-
enableChatBox = True
|
149 |
-
|
150 |
-
chatTextStr = st.text_input(f'Ask me anything about this {pdfCSVURLText}', '', placeholder=f"Type here (e.g. {exampleQuestion})", disabled=not enableChatBox)
|
151 |
-
chatWithPDFButton = "CLICK HERE TO START CHATTING"
|
152 |
-
if st.button(chatWithPDFButton, disabled=not enableChatBox and not chatTextStr): # Button Cliked
|
153 |
-
|
154 |
-
|
155 |
-
if genre==radioButtonList[0]: # E-commerce CSV
|
156 |
-
# Initializing the agent
|
157 |
-
agent = create_pandas_dataframe_agent(llm, dataDF, verbose=False)
|
158 |
-
answer = agent.run(chatTextStr)
|
159 |
-
st.write(answer)
|
160 |
-
|
161 |
-
elif genre==radioButtonList[1]: # Custom CSV Upload
|
162 |
-
# Initializing the agent
|
163 |
-
agent = create_pandas_dataframe_agent(llm, dataDF, verbose=False)
|
164 |
-
answer = agent.run(chatTextStr)
|
165 |
-
st.write(answer)
|
166 |
-
|
167 |
-
elif genre==radioButtonList[2]: # Custom PDF Upload
|
168 |
-
pdf_answer = pdf_chain.run(chatTextStr)
|
169 |
-
st.write(pdf_answer)
|
170 |
-
|
171 |
-
elif genre==radioButtonList[3]: # Google Alphabet URL Earnings Report
|
172 |
-
answer = chain.run(chatTextStr)
|
173 |
-
st.write(answer)
|
174 |
-
|
175 |
-
elif genre==radioButtonList[4]: # Custom URL
|
176 |
-
urls = [urlInput]
|
177 |
loader = [UnstructuredURLLoader(urls=urls)]
|
178 |
index = VectorstoreIndexCreator(
|
179 |
embedding=GooglePalmEmbeddings(),
|
@@ -183,5 +111,79 @@ if st.button(chatWithPDFButton, disabled=not enableChatBox and not chatTextStr):
|
|
183 |
chain_type="stuff",
|
184 |
retriever=index.vectorstore.as_retriever(),
|
185 |
input_key="question")
|
186 |
-
|
187 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
import numpy as np
|
21 |
import pprint
|
22 |
|
23 |
+
isPswdValid = False
|
24 |
+
try:
|
25 |
+
pswdVal = st.experimental_get_query_params()['pwd'][0]
|
26 |
+
if pswdVal==st.secrets["PSWD"]:
|
27 |
+
isPswdValid = True
|
28 |
+
except:
|
29 |
+
pass
|
30 |
+
|
31 |
+
if not isPswdValid:
|
32 |
+
st.write("Invalid Password")
|
33 |
+
else:
|
34 |
+
radioButtonList = ["E-commerce CSV (https://www.kaggle.com/datasets/mervemenekse/ecommerce-dataset)",
|
35 |
+
"Upload my own CSV",
|
36 |
+
"Upload my own PDF",
|
37 |
+
"URL Chat with Google's Latest Earnings (https://abc.xyz/investor/)",
|
38 |
+
"Enter my own URL"]
|
39 |
+
|
40 |
+
# Add some designs to the radio buttons
|
41 |
+
st.markdown("""
|
42 |
+
<style>
|
43 |
+
.stRadio {
|
44 |
+
padding: 10px;
|
45 |
+
border-radius: 5px;
|
46 |
+
background-color: #f5f5f5;
|
47 |
+
}
|
48 |
+
|
49 |
+
.stRadio input[type="radio"] {
|
50 |
+
position: absolute;
|
51 |
+
opacity: 0;
|
52 |
+
cursor: pointer;
|
53 |
+
}
|
54 |
+
|
55 |
+
.stRadio label {
|
56 |
+
display: flex;
|
57 |
+
justify-content: center;
|
58 |
+
align-items: center;
|
59 |
+
cursor: pointer;
|
60 |
+
font-size: 16px;
|
61 |
+
color: #333;
|
62 |
+
}
|
63 |
+
|
64 |
+
.stRadio label:hover {
|
65 |
+
color: #000;
|
66 |
+
}
|
67 |
+
|
68 |
+
.stRadio.st-selected input[type="radio"] ~ label {
|
69 |
+
color: #000;
|
70 |
+
background-color: #d9d9d9;
|
71 |
+
}
|
72 |
+
</style>
|
73 |
+
""", unsafe_allow_html=True)
|
74 |
+
|
75 |
+
genre = st.radio(
|
76 |
+
"Choose dataset to finetune", radioButtonList, index=0
|
77 |
+
)
|
78 |
+
|
79 |
+
# Initialize language model
|
80 |
+
load_dotenv(find_dotenv()) # read local .env file
|
81 |
+
api_key = st.secrets["PALM_API_KEY"] # put your API key here
|
82 |
+
os.environ["GOOGLE_API_KEY"] = st.secrets["PALM_API_KEY"]
|
83 |
+
palm.configure(api_key=api_key)
|
84 |
+
llm = GooglePalm()
|
85 |
+
llm.temperature = 0.1
|
86 |
+
|
87 |
+
pdfCSVURLText = ""
|
88 |
+
if genre==radioButtonList[0]:
|
89 |
+
pdfCSVURLText = "CSV"
|
90 |
+
exampleQuestion = "Question1: What was the most sold item? Question2: What was the most common payment?"
|
91 |
+
dataDF = pd.read_csv('EcommerceDataset.csv', encoding= 'unicode_escape')
|
92 |
+
# st.write('You selected comedy.')
|
93 |
+
# else:
|
94 |
+
# st.write(f'''Password streamlit app: {st.secrets["PSWD"]}''')
|
95 |
+
elif genre==radioButtonList[1]:
|
96 |
+
pdfCSVURLText = "CSV"
|
97 |
+
exampleQuestion = "What are the data columns?"
|
98 |
+
elif genre==radioButtonList[2]:
|
99 |
+
pdfCSVURLText = "PDF"
|
100 |
+
exampleQuestion = "Can you summarize the contents?"
|
101 |
+
elif genre==radioButtonList[3]:
|
102 |
+
pdfCSVURLText = "URL"
|
103 |
+
exampleQuestion = "What is Google's latest earnings?"
|
104 |
+
urls = ['https://abc.xyz/investor/']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
105 |
loader = [UnstructuredURLLoader(urls=urls)]
|
106 |
index = VectorstoreIndexCreator(
|
107 |
embedding=GooglePalmEmbeddings(),
|
|
|
111 |
chain_type="stuff",
|
112 |
retriever=index.vectorstore.as_retriever(),
|
113 |
input_key="question")
|
114 |
+
elif genre==radioButtonList[4]:
|
115 |
+
pdfCSVURLText = "URL"
|
116 |
+
exampleQuestion = "Can you summarize the contents?"
|
117 |
+
|
118 |
+
isCustomURL = genre==radioButtonList[4]
|
119 |
+
urlInput = st.text_input('Enter your own URL', '', placeholder="Type your URL here (e.g. https://abc.xyz/investor/)", disabled=not isCustomURL)
|
120 |
+
|
121 |
+
isCustomPDF = genre==radioButtonList[1] or genre==radioButtonList[2]
|
122 |
+
uploaded_file = st.file_uploader(f"Upload your own {pdfCSVURLText} here", type=pdfCSVURLText.lower(), disabled=not isCustomPDF)
|
123 |
+
uploadedFilename = ""
|
124 |
+
if uploaded_file is not None:
|
125 |
+
with NamedTemporaryFile(dir='.', suffix=f'.{pdfCSVURLText.lower()}') as f:
|
126 |
+
f.write(uploaded_file.getbuffer())
|
127 |
+
uploadedFilename = f.name
|
128 |
+
if genre==radioButtonList[1]: # Custom CSV Upload
|
129 |
+
dataDF = pd.read_csv(uploadedFilename, encoding= 'unicode_escape')
|
130 |
+
elif genre==radioButtonList[2]: # Custom PDF Upload
|
131 |
+
pdf_loaders = [UnstructuredPDFLoader(uploadedFilename)]
|
132 |
+
pdf_index = VectorstoreIndexCreator(
|
133 |
+
embedding=GooglePalmEmbeddings(),
|
134 |
+
text_splitter=CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)).from_loaders(pdf_loaders)
|
135 |
+
pdf_chain = RetrievalQA.from_chain_type(llm=llm,
|
136 |
+
chain_type="stuff",
|
137 |
+
retriever=pdf_index.vectorstore.as_retriever(),
|
138 |
+
input_key="question")
|
139 |
+
|
140 |
+
enableChatBox = False
|
141 |
+
if genre==radioButtonList[0]: # E-commerce CSV
|
142 |
+
enableChatBox = True
|
143 |
+
elif genre==radioButtonList[1]: # Custom CSV Upload
|
144 |
+
enableChatBox = uploadedFilename[-4:]==".csv"
|
145 |
+
elif genre==radioButtonList[2]: # Custom PDF Upload
|
146 |
+
enableChatBox = uploadedFilename[-4:]==".pdf"
|
147 |
+
elif genre==radioButtonList[3]: # Google Alphabet URL Earnings Report
|
148 |
+
enableChatBox = True
|
149 |
+
elif genre==radioButtonList[4]: # Custom URL
|
150 |
+
enableChatBox = True
|
151 |
+
|
152 |
+
chatTextStr = st.text_input(f'Ask me anything about this {pdfCSVURLText}', '', placeholder=f"Type here (e.g. {exampleQuestion})", disabled=not enableChatBox)
|
153 |
+
chatWithPDFButton = "CLICK HERE TO START CHATTING"
|
154 |
+
if st.button(chatWithPDFButton, disabled=not enableChatBox and not chatTextStr): # Button Cliked
|
155 |
+
|
156 |
+
|
157 |
+
if genre==radioButtonList[0]: # E-commerce CSV
|
158 |
+
# Initializing the agent
|
159 |
+
agent = create_pandas_dataframe_agent(llm, dataDF, verbose=False)
|
160 |
+
answer = agent.run(chatTextStr)
|
161 |
+
st.write(answer)
|
162 |
+
|
163 |
+
elif genre==radioButtonList[1]: # Custom CSV Upload
|
164 |
+
# Initializing the agent
|
165 |
+
agent = create_pandas_dataframe_agent(llm, dataDF, verbose=False)
|
166 |
+
answer = agent.run(chatTextStr)
|
167 |
+
st.write(answer)
|
168 |
+
|
169 |
+
elif genre==radioButtonList[2]: # Custom PDF Upload
|
170 |
+
pdf_answer = pdf_chain.run(chatTextStr)
|
171 |
+
st.write(pdf_answer)
|
172 |
+
|
173 |
+
elif genre==radioButtonList[3]: # Google Alphabet URL Earnings Report
|
174 |
+
answer = chain.run(chatTextStr)
|
175 |
+
st.write(answer)
|
176 |
+
|
177 |
+
elif genre==radioButtonList[4]: # Custom URL
|
178 |
+
urls = [urlInput]
|
179 |
+
loader = [UnstructuredURLLoader(urls=urls)]
|
180 |
+
index = VectorstoreIndexCreator(
|
181 |
+
embedding=GooglePalmEmbeddings(),
|
182 |
+
text_splitter=CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)).from_loaders(loader)
|
183 |
+
|
184 |
+
chain = RetrievalQA.from_chain_type(llm=llm,
|
185 |
+
chain_type="stuff",
|
186 |
+
retriever=index.vectorstore.as_retriever(),
|
187 |
+
input_key="question")
|
188 |
+
answer = chain.run(chatTextStr)
|
189 |
+
st.write(answer)
|