poemsforaphrodite
commited on
Commit
•
2a98cf8
1
Parent(s):
ac0ce5d
Update app.py
Browse files
app.py
CHANGED
@@ -59,7 +59,7 @@ def fetch_pdfs(city_code):
|
|
59 |
st.error(f"Failed to fetch PDFs for city code {city_code}")
|
60 |
return None
|
61 |
|
62 |
-
def download_pdf(url):
|
63 |
# Add 'https://' scheme if it's missing
|
64 |
if not url.startswith(('http://', 'https://')):
|
65 |
url = 'https://' + url
|
@@ -67,15 +67,21 @@ def download_pdf(url):
|
|
67 |
try:
|
68 |
response = requests.get(url)
|
69 |
response.raise_for_status() # Raise an exception for bad status codes
|
70 |
-
|
71 |
-
#
|
72 |
-
|
73 |
-
|
74 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
75 |
# Save the PDF content to a file
|
76 |
with open(filename, 'wb') as f:
|
77 |
f.write(response.content)
|
78 |
-
|
79 |
return filename
|
80 |
except requests.RequestException as e:
|
81 |
st.error(f"Failed to download PDF from {url}. Error: {str(e)}")
|
@@ -206,6 +212,53 @@ def chat_with_assistant(file_ids, user_message):
|
|
206 |
|
207 |
# ---------------------- Streamlit App ----------------------
|
208 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
209 |
page = st.sidebar.selectbox("Choose a page", ["Documents", "Home", "Admin"])
|
210 |
|
211 |
if page == "Home":
|
@@ -223,7 +276,8 @@ if page == "Home":
|
|
223 |
# Main chat area improvements
|
224 |
colored_header("Chat", description="Ask questions about building regulations")
|
225 |
|
226 |
-
#
|
|
|
227 |
for chat in st.session_state.chat_history:
|
228 |
with st.container():
|
229 |
if chat['role'] == 'user':
|
@@ -240,6 +294,7 @@ if page == "Home":
|
|
240 |
<div class="message-content">{chat['content']}</div>
|
241 |
</div>
|
242 |
""", unsafe_allow_html=True)
|
|
|
243 |
|
244 |
# Chat input improvements
|
245 |
with st.form("chat_form", clear_on_submit=True):
|
@@ -354,7 +409,8 @@ elif page == "Documents":
|
|
354 |
file_href = pdf['File Href']
|
355 |
doc_title = pdf['Doc Title']
|
356 |
|
357 |
-
|
|
|
358 |
if file_name:
|
359 |
file_path = f"./{file_name}"
|
360 |
file_id = upload_file_to_openai(file_path)
|
|
|
59 |
st.error(f"Failed to fetch PDFs for city code {city_code}")
|
60 |
return None
|
61 |
|
62 |
+
def download_pdf(url, doc_title):
|
63 |
# Add 'https://' scheme if it's missing
|
64 |
if not url.startswith(('http://', 'https://')):
|
65 |
url = 'https://' + url
|
|
|
67 |
try:
|
68 |
response = requests.get(url)
|
69 |
response.raise_for_status() # Raise an exception for bad status codes
|
70 |
+
|
71 |
+
# Sanitize doc_title to create a valid filename
|
72 |
+
sanitized_title = ''.join(c for c in doc_title if c.isalnum() or c in (' ', '_', '-')).rstrip()
|
73 |
+
sanitized_title = sanitized_title.replace(' ', '_')
|
74 |
+
filename = f"{sanitized_title}.pdf"
|
75 |
+
|
76 |
+
# Ensure filename is unique by appending the id_counter if necessary
|
77 |
+
if os.path.exists(filename):
|
78 |
+
filename = f"{sanitized_title}_{st.session_state.id_counter}.pdf"
|
79 |
+
st.session_state.id_counter += 1
|
80 |
+
|
81 |
# Save the PDF content to a file
|
82 |
with open(filename, 'wb') as f:
|
83 |
f.write(response.content)
|
84 |
+
|
85 |
return filename
|
86 |
except requests.RequestException as e:
|
87 |
st.error(f"Failed to download PDF from {url}. Error: {str(e)}")
|
|
|
212 |
|
213 |
# ---------------------- Streamlit App ----------------------
|
214 |
|
215 |
+
# ---------------------- Custom CSS Injection ----------------------
|
216 |
+
|
217 |
+
# Inject custom CSS to style chat messages
|
218 |
+
st.markdown("""
|
219 |
+
<style>
|
220 |
+
/* Style for the chat container */
|
221 |
+
.chat-container {
|
222 |
+
display: flex;
|
223 |
+
flex-direction: column;
|
224 |
+
}
|
225 |
+
|
226 |
+
/* Style for individual chat messages */
|
227 |
+
.chat-message {
|
228 |
+
margin-bottom: 20px; /* Increased space between messages */
|
229 |
+
}
|
230 |
+
|
231 |
+
/* Style for user messages */
|
232 |
+
.chat-message.user > div:first-child {
|
233 |
+
color: #1E90FF; /* Dodger Blue for "You" */
|
234 |
+
font-size: 1.2em;
|
235 |
+
margin-bottom: 5px;
|
236 |
+
}
|
237 |
+
|
238 |
+
/* Style for assistant messages */
|
239 |
+
.chat-message.assistant > div:first-child {
|
240 |
+
color: #32CD32; /* Lime Green for "Assistant" */
|
241 |
+
font-size: 1.2em;
|
242 |
+
margin-bottom: 5px;
|
243 |
+
}
|
244 |
+
|
245 |
+
/* Style for the message content */
|
246 |
+
.message-content {
|
247 |
+
/* Removed the background color to maintain original background */
|
248 |
+
padding: 10px;
|
249 |
+
border-radius: 5px;
|
250 |
+
/* Optionally, you can set a semi-transparent background or match it with your theme */
|
251 |
+
/* background-color: rgba(241, 241, 241, 0.8); */
|
252 |
+
}
|
253 |
+
|
254 |
+
/* Optional: Add more spacing between messages */
|
255 |
+
.chat-message.user, .chat-message.assistant {
|
256 |
+
padding-top: 10px;
|
257 |
+
padding-bottom: 10px;
|
258 |
+
}
|
259 |
+
</style>
|
260 |
+
""", unsafe_allow_html=True)
|
261 |
+
|
262 |
page = st.sidebar.selectbox("Choose a page", ["Documents", "Home", "Admin"])
|
263 |
|
264 |
if page == "Home":
|
|
|
276 |
# Main chat area improvements
|
277 |
colored_header("Chat", description="Ask questions about building regulations")
|
278 |
|
279 |
+
# Chat container with custom CSS class
|
280 |
+
st.markdown('<div class="chat-container">', unsafe_allow_html=True)
|
281 |
for chat in st.session_state.chat_history:
|
282 |
with st.container():
|
283 |
if chat['role'] == 'user':
|
|
|
294 |
<div class="message-content">{chat['content']}</div>
|
295 |
</div>
|
296 |
""", unsafe_allow_html=True)
|
297 |
+
st.markdown('</div>', unsafe_allow_html=True)
|
298 |
|
299 |
# Chat input improvements
|
300 |
with st.form("chat_form", clear_on_submit=True):
|
|
|
409 |
file_href = pdf['File Href']
|
410 |
doc_title = pdf['Doc Title']
|
411 |
|
412 |
+
# Pass doc_title to download_pdf
|
413 |
+
file_name = download_pdf(file_href, doc_title)
|
414 |
if file_name:
|
415 |
file_path = f"./{file_name}"
|
416 |
file_id = upload_file_to_openai(file_path)
|