Upload bot.py
Browse files
bot.py
CHANGED
@@ -4,10 +4,9 @@ import os
|
|
4 |
import re
|
5 |
import time
|
6 |
import base64
|
7 |
-
from patterns import count_patterns, list_patterns, pdf_request_patterns, greetings,farewell
|
8 |
from google.cloud import storage
|
9 |
-
|
10 |
-
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "/Users/ninadmandavkar/Downloads/fynd_prod.json"
|
11 |
|
12 |
# Base GCS path and local download path
|
13 |
BASE_GCS_PATH = "gs://fynd-assets-private/documents/daytrader/"
|
@@ -64,7 +63,6 @@ def typing_effect(text):
|
|
64 |
typed_text_placeholder.markdown(styled_text, unsafe_allow_html=True)
|
65 |
|
66 |
def chatbot_response(user_input):
|
67 |
-
|
68 |
lower_input = user_input.lower()
|
69 |
|
70 |
if any(greet in lower_input for greet in greetings):
|
@@ -126,66 +124,95 @@ def download_pdf(file_path):
|
|
126 |
return base64.b64encode(pdf_data).decode('utf-8') # Encode to Base64 and decode to string
|
127 |
|
128 |
def main():
|
129 |
-
st.markdown(
|
130 |
-
"""
|
131 |
-
<h1 style="
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
">
|
138 |
-
|
139 |
-
</h1>
|
140 |
-
""",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
141 |
unsafe_allow_html=True
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
|
|
|
|
|
|
178 |
else:
|
179 |
-
typing_effect("
|
180 |
-
|
181 |
-
typing_effect("There was an error fetching the PDF. Please check the name and try again.")
|
182 |
-
st.write(result.stderr)
|
183 |
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
|
|
|
|
189 |
|
190 |
if __name__ == "__main__":
|
191 |
main()
|
|
|
4 |
import re
|
5 |
import time
|
6 |
import base64
|
7 |
+
from patterns import count_patterns, list_patterns, pdf_request_patterns, greetings, farewell # Import patterns
|
8 |
from google.cloud import storage
|
9 |
+
from tempfile import NamedTemporaryFile
|
|
|
10 |
|
11 |
# Base GCS path and local download path
|
12 |
BASE_GCS_PATH = "gs://fynd-assets-private/documents/daytrader/"
|
|
|
63 |
typed_text_placeholder.markdown(styled_text, unsafe_allow_html=True)
|
64 |
|
65 |
def chatbot_response(user_input):
|
|
|
66 |
lower_input = user_input.lower()
|
67 |
|
68 |
if any(greet in lower_input for greet in greetings):
|
|
|
124 |
return base64.b64encode(pdf_data).decode('utf-8') # Encode to Base64 and decode to string
|
125 |
|
126 |
def main():
|
127 |
+
# st.markdown(
|
128 |
+
# """
|
129 |
+
# <h1 style="
|
130 |
+
# background-image: linear-gradient(to right, #800000, #ff0000);
|
131 |
+
# -webkit-background-clip: text;
|
132 |
+
# -webkit-text-fill-color: transparent;
|
133 |
+
# font-size: 3rem; /* Adjust size as needed */
|
134 |
+
# text-align: center; /* Center the title */
|
135 |
+
# ">
|
136 |
+
# Fynder
|
137 |
+
# </h1>
|
138 |
+
# """,
|
139 |
+
# unsafe_allow_html=True
|
140 |
+
# )
|
141 |
+
|
142 |
+
# Upload JSON credentials
|
143 |
+
uploaded_file = st.file_uploader("Upload your fynd_prod.json file", type="json")
|
144 |
+
|
145 |
+
if uploaded_file is not None:
|
146 |
+
# Save the file temporarily
|
147 |
+
with NamedTemporaryFile(delete=False, suffix=".json") as temp_file:
|
148 |
+
temp_file.write(uploaded_file.read())
|
149 |
+
json_path = temp_file.name
|
150 |
+
|
151 |
+
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = json_path
|
152 |
+
|
153 |
+
st.markdown(
|
154 |
+
"""
|
155 |
+
<h1 style="
|
156 |
+
background-image: linear-gradient(to right, #800000, #ff0000);
|
157 |
+
-webkit-background-clip: text;
|
158 |
+
-webkit-text-fill-color: transparent;
|
159 |
+
font-size: 3rem; /* Adjust size as needed */
|
160 |
+
text-align: center; /* Center the title */
|
161 |
+
">
|
162 |
+
Fynder
|
163 |
+
</h1>
|
164 |
+
""",
|
165 |
unsafe_allow_html=True
|
166 |
+
)
|
167 |
+
|
168 |
+
|
169 |
+
user_input = st.text_input("", value="", help="Type your prompt here")
|
170 |
+
|
171 |
+
if user_input:
|
172 |
+
response = chatbot_response(user_input)
|
173 |
+
|
174 |
+
if isinstance(response, tuple):
|
175 |
+
bot_response, pdf_name = response
|
176 |
+
typing_effect(bot_response)
|
177 |
+
|
178 |
+
result = fetch_pdf_from_gcs(pdf_name)
|
179 |
+
if result.returncode == 0:
|
180 |
+
downloaded_files = [f for f in os.listdir(LOCAL_DOWNLOAD_PATH) if pdf_name in f and f.endswith('.pdf')]
|
181 |
+
|
182 |
+
if downloaded_files:
|
183 |
+
for pdf_file in downloaded_files:
|
184 |
+
file_path = os.path.join(LOCAL_DOWNLOAD_PATH, pdf_file)
|
185 |
+
pdf_data = download_pdf(file_path)
|
186 |
+
|
187 |
+
# Create a custom download button with gradient styling
|
188 |
+
download_button_html = f"""
|
189 |
+
<a href="data:application/pdf;base64,{pdf_data}" download="{pdf_file}"
|
190 |
+
style="
|
191 |
+
display: inline-block;
|
192 |
+
padding: 10px 20px;
|
193 |
+
color: white;
|
194 |
+
text-decoration: none;
|
195 |
+
border-radius: 20px;
|
196 |
+
background-image: linear-gradient(to right, #800000, #ff0000);
|
197 |
+
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
|
198 |
+
">
|
199 |
+
Download {pdf_file}
|
200 |
+
</a>
|
201 |
+
"""
|
202 |
+
st.markdown(download_button_html, unsafe_allow_html=True)
|
203 |
+
else:
|
204 |
+
typing_effect("No files matching that name were found.")
|
205 |
else:
|
206 |
+
typing_effect("There was an error fetching the PDF. Please check the name and try again.")
|
207 |
+
st.write(result.stderr)
|
|
|
|
|
208 |
|
209 |
+
elif isinstance(response, list): # When the response is a list of files
|
210 |
+
for pdf_file in response:
|
211 |
+
typing_effect(pdf_file) # Apply typing effect to each file
|
212 |
+
else:
|
213 |
+
typing_effect(response)
|
214 |
+
else:
|
215 |
+
st.markdown("")
|
216 |
|
217 |
if __name__ == "__main__":
|
218 |
main()
|