Spaces:
Running
Running
File size: 7,291 Bytes
73aa551 7f5ac0d 056423f f1afeff 31cb392 f1afeff 31cb392 73aa551 741662f c81f208 f85a7ec fd4a8f2 391ca76 fd4a8f2 73da752 f85a7ec 73da752 f85a7ec 391ca76 73da752 391ca76 73da752 391ca76 73da752 391ca76 73da752 391ca76 f85a7ec fd4a8f2 f85a7ec 73da752 65f28d8 5769bbb 65f28d8 82c9a08 f1afeff 7f5ac0d f1afeff 7f5ac0d 31cb392 3101d1c 31cb392 f1afeff 7f5ac0d 31cb392 f1afeff 3101d1c f1afeff 3101d1c f1afeff 82c9a08 f1afeff 5064d6a f1afeff 5064d6a f1afeff 5064d6a 7b3b821 f1afeff f105406 7f5ac0d f105406 7f5ac0d f1afeff 7f5ac0d f1afeff 3101d1c f1afeff 3101d1c f1afeff |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 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 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 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 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 |
import streamlit as st
from utils.retriever import retrieve_paragraphs
from utils.generator import build_messages, _call_llm
from utils.utils import meetings_list, countries_list, projects_list
import ast
import time
import asyncio
import re
import logging
logging.basicConfig(level=logging.INFO)
# st.set_page_config(layout="wide")
st.markdown(
"""
<style>
.full-width-banner {
width: 100vw;
position: relative;
left: -50vw;
right: -50vw;
margin-left: 50%;
margin-right: 50%;
background-color: #0071BC; /* UN Blue */
padding: 15px 0;
text-align: center;
box-shadow: 0 2px 4px rgba(0,0,0,0.05);
z-index: 1000;
}
</style>
<div class="full-width-banner">
<h1 style="color:white; margin:0;">Montreal AI Decisions (MVP)</h1>
</div>
""",
unsafe_allow_html=True
)
st.markdown(
"""
<style>
/* Fix content overflow in expanders and all custom containers */
.streamlit-expanderContent, .source-block {
max-width: 700px;
word-wrap: break-word;
overflow-wrap: break-word;
white-space: pre-wrap;
font-size: 16px;
}
/* Force label size on text input and selectboxes */
label[data-testid="stWidgetLabel"] {
font-size: 20px !important;
font-weight: 600 !important;
color: #000000 !important;
}
/* Optional: Adjust placeholder font size */
input[type="text"]::placeholder {
font-size: 18px !important;
}
/* Optional: Adjust the selected option inside the dropdown */
div[role="combobox"] * {
font-size: 18px !important;
}
</style>
""",
unsafe_allow_html=True
)
# Add vertical spacing between banner and help text
st.markdown("<div style='margin-top: 40px;'></div>", unsafe_allow_html=True)
# Help text (static)
st.markdown("""<p style='text-align: left; font-weight: 600; margin-bottom: 1rem;'>Welcome to your chatbot research assistant. It helps you find and summarize specific decisions and annexes, and can also answer general questions about the text. For transparency, it provides references with links to the relevant decisions and annexes, so you can easily verify the sources. \n While this chatbot was developed with care, we recommend double-checking the links to gain a deeper understanding of the material. </p>""", unsafe_allow_html=True)
# Add vertical spacing between help text and question input
st.markdown("<div style='margin-top: 25px;'></div>", unsafe_allow_html=True)
########### Function for getting response #######################
def chat_response(query, filter_metadata=None):
"""Generate chat response based on method and inputs"""
try:
retrieved_paragraphs = retrieve_paragraphs(query, filter_metadata=filter_metadata)
context_retrieved = ast.literal_eval(retrieved_paragraphs)
# Build list of only content, no metadata
context_retrieved_formatted = "||".join(doc['answer'] for doc in context_retrieved)
context_retrieved_lst = [doc['answer'] for doc in context_retrieved]
logging.info("Context Retrieval done")
logging.info(f"Content {context_retrieved}")
messages = build_messages(query, context_retrieved_lst)
answer = asyncio.run(_call_llm(messages))
return answer, context_retrieved
except Exception as e:
error_message = f"Error processing request: {str(e)}"
return error_message
############## UI related functions #####################
def reset_page():
"""
Reset pagination back to the first page; used as on_change callback.
"""
st.session_state["page"] = 1
def contruct_metadata_filter():
filter_metadata = {}
if st.session_state['meetings_filter'] != 'All':
filter_metadata['meeting_id'] = st.session_state['meetings_filter']
## need to change the filter for coutnry and project tolist
if st.session_state['country_filter'] != 'All':
filter_metadata['Countries'] = st.session_state['country_filter']
if st.session_state['project_filter'] != 'All':
filter_metadata['Projects'] = st.session_state['project_filter']
logging.info(f"contructed metadata_filter {filter_metadata}")
return filter_metadata
def render_sources(chunks, query):
# 11.7. Render each result chunk
st.subheader("Sources")
st.write("======================================")
start_idx = 0
for idx, doc in enumerate(chunks, start=start_idx + 1):
meta = doc.get('answer_metadata', {})
title = meta.get('Decision Number', 'Unknown Project')
agencies = meta.get('Agencies', 'Unknown Agencies')
country = meta.get('country', 'Unknown Country')
snippet = doc.get('answer', '')
preview = snippet.split(maxsplit=90)[:90]
remainder = snippet[len(" ".join(preview)):]
# Wrap markdown in a div with limited width
st.markdown(f"""
<div class="source-block">
<h4>{idx}. {title}</h4>
<p><strong>Agencies:</strong> {agencies} | <strong>Country:</strong> {country}</p>
<p>{" ".join(preview)}</p>
</div>
""", unsafe_allow_html=True)
if remainder:
with st.expander("Show more"):
st.markdown(
f"<div class='source-block'>{remainder}</div>",
unsafe_allow_html=True
)
st.divider()
for key in ('meetings_filter', 'country_filter', 'project_filter'):
if key not in st.session_state:
st.session_state[key] = 'All'
if 'page' not in st.session_state:
st.session_state['page'] = 1
col_query, col_about = st.columns([8, 2])
# 10.1. Question input
with col_query:
query = st.text_input(
label="Enter your question:",
key="query",
on_change = reset_page
)
# 10.2. Filter widgets
col1, col2, col3, col4 = st.columns(4)
with col1:
meetings = sorted(meetings_list)
st.selectbox(
"Meeting",
options=['All'] + meetings,
key='meetings_filter',
on_change=reset_page
)
with col2:
countries = sorted(countries_list)
st.selectbox(
"Country",
options=['All'] + countries,
key='country_filter',
on_change=reset_page
)
with col3:
projects = sorted(projects_list)
st.selectbox(
"Projects",
options=['All'] + projects,
key='project_filter',
on_change=reset_page
)
# Only run search & display if user has entered something
if not query.strip():
st.info("Please enter a question to see results.")
st.stop()
else:
filter_metadata = contruct_metadata_filter()
if filter_metadata:
logging.info("calling with metadata filter")
answer, context_retrieved = chat_response(query, filter_metadata)
st.write(answer)
render_sources(context_retrieved, query)
else:
logging.info("calling without metadata filter")
answer, context_retrieved = chat_response(query)
st.write(answer)
render_sources(context_retrieved, query) |