Maia Pelletier commited on
Commit
ee5f55f
·
1 Parent(s): de5b225

temporarily remove inline references (needs to be cleaned up)

Browse files
Files changed (1) hide show
  1. app.py +4 -20
app.py CHANGED
@@ -269,26 +269,10 @@ Answer:"""
269
  try:
270
  response_text = self._generate_with_chat(prompt, max_new_tokens=512)
271
  if response_text:
272
- # Resolve [Context N] to actual source labels in the body
273
- # Expand bare/grouped "Context N, M" forms to individual [Context N] tags.
274
- # Use negative lookbehind (?<!\[) so already-bracketed [Context N] are not double-wrapped.
275
- def _expand_grouped_context(m):
276
- indices = [int(x.strip()) for x in re.split(r'[,\s]+', m.group(1)) if x.strip().isdigit()]
277
- return " ".join(f"[Context {idx}]" for idx in indices)
278
- response_text = re.sub(r'(?<!\[)\bContext\s+([\d][,\s\d]*)', _expand_grouped_context, response_text)
279
- # Now replace all [Context N] with source labels/links
280
- for i, source_label in context_index_to_source.items():
281
- url = context_index_to_url.get(i)
282
- replacement = f"[{source_label}]({url})" if url else source_label
283
- response_text = response_text.replace(f"[Context {i}]", replacement)
284
- # Deduplicate repeated inline markdown links — keep first as link, rest as plain text
285
- seen_inline = set()
286
- def _dedup_link(m):
287
- if m.group(0) in seen_inline:
288
- return m.group(1)
289
- seen_inline.add(m.group(0))
290
- return m.group(0)
291
- response_text = re.sub(r'\[([^\]]+)\]\([^)]+\)', _dedup_link, response_text)
292
  # Only append references when the model actually answered
293
  no_answer_phrase = "The provided context does not contain enough information to answer this question."
294
  no_answer_replacement = "The organic documents provided do not contain enough information to answer your question. For more information, try taking a look at the [Canadian Food Inspection Agency](https://inspection.canada.ca/en/food-labels/organic-products/), the [Certified Organic Growers of Canada](https://cog.ca/), or the [Canada Organic Trade Association](https://canada-organic.ca/en/organic)."
 
269
  try:
270
  response_text = self._generate_with_chat(prompt, max_new_tokens=512)
271
  if response_text:
272
+ # Strip all inline [Context N] references and bare "Context N" mentions from the body
273
+ response_text = re.sub(r'\[Context\s+\d+\]', '', response_text)
274
+ response_text = re.sub(r'(?<!\[)\bContext\s+[\d][,\s\d]*', '', response_text)
275
+ response_text = re.sub(r'\s{2,}', ' ', response_text)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
276
  # Only append references when the model actually answered
277
  no_answer_phrase = "The provided context does not contain enough information to answer this question."
278
  no_answer_replacement = "The organic documents provided do not contain enough information to answer your question. For more information, try taking a look at the [Canadian Food Inspection Agency](https://inspection.canada.ca/en/food-labels/organic-products/), the [Certified Organic Growers of Canada](https://cog.ca/), or the [Canada Organic Trade Association](https://canada-organic.ca/en/organic)."