mtyrrell commited on
Commit
33a218d
1 Parent(s): ab8363f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -70
app.py CHANGED
@@ -46,21 +46,11 @@ def get_document_store():
46
  index=doc_file_name)
47
  return document_store
48
 
49
- # @st.cache(allow_output_mutation=True)
50
- # def get_retriever(document_store):
51
- # # Initialize the Pinecone retriever configuration here
52
- # retriever = EmbeddingRetriever(
53
- # document_store=document_store,
54
- # embedding_model="sentence-transformers/multi-qa-mpnet-base-dot-v1",
55
- # model_format="sentence_transformers",
56
- # progress_bar=False,
57
- # )
58
- # return retriever
59
-
60
- # Get (or initialize and get) the document store and retriever
61
  document_store = get_document_store()
62
- # retriever = get_retriever(document_store)
63
 
 
64
  retriever = EmbeddingRetriever(
65
  document_store=document_store,
66
  embedding_model="sentence-transformers/multi-qa-mpnet-base-dot-v1",
@@ -68,17 +58,6 @@ retriever = EmbeddingRetriever(
68
  progress_bar=False,
69
  )
70
 
71
- # template = PromptTemplate(
72
- # prompt="""
73
- # Answer the given question using the following documents. \
74
- # Formulate your answer in the style of an academic report. \
75
- # Provide example quotes and citations using extracted text from the documents. \
76
- # Use facts and numbers from the documents in your answer. \
77
- # Reference information used from documents at the end of each applicable sentence (ex: [source: document_name]), where 'document_name' is the text provided at the start of each document (demarcated by '- &&&' and '&&&:')'. \
78
- # If no relevant information to answer the question is present in the documents, just say you don't have enough information to answer. \
79
- # Context: {' - '.join(['&&& '+d.meta['document']+' ref. '+str(d.meta['ref_id'])+' &&&: '+d.content for d in documents])}; Question: {query}; Answer:""",
80
- # )
81
-
82
  prompt_template="Answer the given question using the following documents. \
83
  Formulate your answer in the style of an academic report. \
84
  Provide example quotes and citations using extracted text from the documents. \
@@ -103,40 +82,6 @@ examples = [
103
  "In addition to gender, children, and youth, is there any mention of other groups facing disproportional impacts from climate change due to their geographic location, socio-economic status, age, gender, health, and occupation?"
104
  ]
105
 
106
- # def get_docs(input_query, country = None):
107
- # '''
108
- # Construct a hacky query to focus the retriever on the target country (see notes below)
109
- # We take the top 150 k because we want to make sure we have 10 pertaining to the selected country
110
- # '''
111
- # if country == 'All Countries':
112
- # query = input_query
113
- # else:
114
- # query = "For the country of "+country+", "+input_query
115
- # # Retrieve top k documents
116
- # docs = retriever.retrieve(query=query,top_k = 150)
117
- # # Break out the key fields and convert to pandas for filtering
118
- # docs = [{**x.meta,"score":x.score,"content":x.content} for x in docs]
119
- # df_docs = pd.DataFrame(docs)
120
- # if country != 'All Countries':
121
- # df_docs = df_docs.query('country in @country')
122
- # # Take the top 10
123
- # df_docs = df_docs.head(10)
124
- # # Get ourselves an index setup from which to base the source reference number from (in the prompt and matching afterwards)
125
- # df_docs = df_docs.reset_index()
126
- # df_docs['ref_id'] = df_docs.index + 1 # start the index at 1
127
- # # Convert back to Document format
128
- # ls_dict = []
129
- # # Iterate over df and add relevant fields to the dict object
130
- # for index, row in df_docs.iterrows():
131
- # # Create a Document object for each row
132
- # doc = Document(
133
- # row['content'],
134
- # meta={'country': row['country'],'document': row['document'], 'page': row['page'], 'file_name': row['file_name'], 'ref_id': row['ref_id'], 'score': row['score']}
135
- # )
136
-
137
- # # Append the Document object to the documents list
138
- # ls_dict.append(doc)
139
- # return(ls_dict)
140
 
141
  def get_docs(input_query, country = [], vulnerability_cat = []):
142
  if not country:
@@ -189,9 +134,9 @@ def get_refs(docs, res):
189
  ref_id = doc['meta']['ref_id']
190
  if ref_id in ref_ids:
191
  if doc['meta']['document'] == "Supplementary":
192
- result_str += "**Ref. " + str(ref_id) + " [" + doc['meta']['country'] + " " + doc['meta']['document'] + ':' + doc['meta']['file_name'] + ' p' + str(doc['meta']['page']) + "]:** " + "*'" + doc['content'] + "'*<br> <br>" # Add <br> for a line break
193
  else:
194
- result_str += "**Ref. " + str(ref_id) + " [" + doc['meta']['country'] + " " + doc['meta']['document'] + ' p' + str(doc['meta']['page']) + "]:** " + "*'" + doc['content'] + "'*<br> <br>" # Add <br> for a line break
195
 
196
  return result_str
197
 
@@ -227,19 +172,15 @@ def run_query(input_text, country, model_sel):
227
 
228
  with st.sidebar:
229
  # Dropdown selectbox
230
- country = st.sidebar.multiselect('Select a country:', country_options)
 
231
  st.markdown(
232
  """
233
- * *For a comparative analysis of multiple countries, select **'All Countries'***
234
- * *Then be sure to mention the country names of interest in your query*
 
235
  """
236
  )
237
- vulnerabilities_cat = st.sidebar.multiselect('Select a vulnerabilities category:', vulnerability_options)
238
- # choice = st.sidebar.radio(label = 'Select the Document',
239
- # help = 'You can upload the document \
240
- # or else you can try a example document',
241
- # options = ('Upload Document', 'Try Example'),
242
- # horizontal = True)
243
 
244
  with st.container():
245
  st.markdown("<h2 style='text-align: center;'> Climate Policy Documents: Vulnerabilities Analysis Q&A </h2>", unsafe_allow_html=True)
@@ -252,9 +193,16 @@ with st.expander("ℹ️ - About this app", expanded=False):
252
 
253
  **DISCLAIMER:** *This prototype tool based on LLMs (Language Models) is provided "as is" for experimental and exploratory purposes only, and should not be used for critical or production applications. Users are advised that the tool may contain errors, bugs, or limitations and should be used with caution and awareness of potential risks, and the developers make no warranties or guarantees regarding its performance, reliability, or suitability for any specific purpose.*
254
  """)
255
-
256
  # Display the text passages as radio buttons
257
  selected_example = st.radio("Example questions", examples)
 
 
 
 
 
 
 
 
258
 
259
 
260
 
 
46
  index=doc_file_name)
47
  return document_store
48
 
49
+
50
+ # Get (or initialize and get) the document store
 
 
 
 
 
 
 
 
 
 
51
  document_store = get_document_store()
 
52
 
53
+ # Instantiate retriever
54
  retriever = EmbeddingRetriever(
55
  document_store=document_store,
56
  embedding_model="sentence-transformers/multi-qa-mpnet-base-dot-v1",
 
58
  progress_bar=False,
59
  )
60
 
 
 
 
 
 
 
 
 
 
 
 
61
  prompt_template="Answer the given question using the following documents. \
62
  Formulate your answer in the style of an academic report. \
63
  Provide example quotes and citations using extracted text from the documents. \
 
82
  "In addition to gender, children, and youth, is there any mention of other groups facing disproportional impacts from climate change due to their geographic location, socio-economic status, age, gender, health, and occupation?"
83
  ]
84
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85
 
86
  def get_docs(input_query, country = [], vulnerability_cat = []):
87
  if not country:
 
134
  ref_id = doc['meta']['ref_id']
135
  if ref_id in ref_ids:
136
  if doc['meta']['document'] == "Supplementary":
137
+ result_str += "**Ref. " + str(ref_id) + " [" + doc['meta']['country'] + " " + doc['meta']['document'] + ':' + doc['meta']['file_name'] + ' p' + str(doc['meta']['page']) + '; vulnerabilities: ' + doc['meta']['vulnerability_cat'] + "]:** " + "*'" + doc['content'] + "'*<br> <br>" # Add <br> for a line break
138
  else:
139
+ result_str += "**Ref. " + str(ref_id) + " [" + doc['meta']['country'] + " " + doc['meta']['document'] + ' p' + str(doc['meta']['page']) + '; vulnerabilities: ' + doc['meta']['vulnerability_cat'] + "]:** " + "*'" + doc['content'] + "'*<br> <br>" # Add <br> for a line break
140
 
141
  return result_str
142
 
 
172
 
173
  with st.sidebar:
174
  # Dropdown selectbox
175
+ country = st.sidebar.multiselect('Filter by country:', country_options)
176
+ vulnerabilities_cat = st.sidebar.multiselect('Filter by vulnerabilities category:', vulnerability_options)
177
  st.markdown(
178
  """
179
+ * *These selections will filter the data matched against your query*
180
+ * *For a comparative analysis of multiple countries or vulnerability categories, select the items you require or select **'All Categories'***
181
+ * *Be careful in using the vulnerabilities category filter, as many of the categories are not well represented in the documents. Therefore, this will severly limit the data available for analysis*
182
  """
183
  )
 
 
 
 
 
 
184
 
185
  with st.container():
186
  st.markdown("<h2 style='text-align: center;'> Climate Policy Documents: Vulnerabilities Analysis Q&A </h2>", unsafe_allow_html=True)
 
193
 
194
  **DISCLAIMER:** *This prototype tool based on LLMs (Language Models) is provided "as is" for experimental and exploratory purposes only, and should not be used for critical or production applications. Users are advised that the tool may contain errors, bugs, or limitations and should be used with caution and awareness of potential risks, and the developers make no warranties or guarantees regarding its performance, reliability, or suitability for any specific purpose.*
195
  """)
 
196
  # Display the text passages as radio buttons
197
  selected_example = st.radio("Example questions", examples)
198
+ st.write(
199
+ """
200
+ You can request comparative analyses between countries by filtering by country, and using more advanced prompts. For example:
201
+
202
+ *Provide a comparative analysis between Angola and Kenya with regard to specific initiatives presented in the context to address the needs of groups such women and children to the effects climate change.*
203
+
204
+ Make sure your filters match the countries you have specified for the analysis!
205
+ """)
206
 
207
 
208