Slaiwala commited on
Commit
f22cf24
·
verified ·
1 Parent(s): 148f093

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -10
app.py CHANGED
@@ -534,14 +534,15 @@ def retrieve_context(query: str, top_k: int = 10) -> List[Dict[str, Any]]:
534
  if results:
535
  dlog("PUBMED", "PubMed search hit")
536
  return results
537
- if _biomechish(q):
538
- wiki = wiki_summary_allow(q, sentences=3)
539
- if wiki:
540
- dlog("WIKI", "Wikipedia biomechanics fallback hit")
541
- return [{"text": wiki, "source": "wikipedia"}]
542
  dlog("RETRIEVAL", "No results found")
543
  return []
544
 
 
545
  # FAISS path
546
  q_emb = embed_model.encode([q], convert_to_numpy=True).astype("float32")
547
  if _IS_IP:
@@ -582,15 +583,16 @@ def retrieve_context(query: str, top_k: int = 10) -> List[Dict[str, Any]]:
582
  dlog("PUBMED", "PubMed search hit")
583
  return results
584
 
585
- if _biomechish(q):
586
- wiki = wiki_summary_allow(q, sentences=3)
587
- if wiki:
588
- dlog("WIKI", "Wikipedia biomechanics fallback hit")
589
- return [{"text": wiki, "source": "wikipedia"}]
590
 
591
  dlog("RETRIEVAL", "No results at all")
592
  return []
593
 
 
594
  def build_prompt(chunks: List[Dict[str, Any]], question: str) -> str:
595
  header = (
596
  "You are Askstein (orthopedic biomechanics). Use ONLY the [Context] to answer. "
@@ -610,6 +612,19 @@ def _decode_generated(out_ids, in_len: int) -> str:
610
  gen = out_ids[0][in_len:]
611
  return tokenizer_lm.decode(gen, skip_special_tokens=True).lstrip(". \n").strip()
612
 
 
 
 
 
 
 
 
 
 
 
 
 
 
613
  def _synthesize_answer(chunks: List[Dict[str, Any]], question: str) -> str:
614
  prompt = build_prompt(chunks, question)
615
  inputs = tokenizer_lm(prompt, return_tensors="pt").to(device)
 
534
  if results:
535
  dlog("PUBMED", "PubMed search hit")
536
  return results
537
+ # Wikipedia fallback (unconditional after PubMed miss)
538
+ wiki = wiki_summary_allow(q, sentences=3)
539
+ if wiki:
540
+ dlog("WIKI", "Wikipedia fallback hit")
541
+ return [{"text": wiki, "source": "wikipedia"}]
542
  dlog("RETRIEVAL", "No results found")
543
  return []
544
 
545
+
546
  # FAISS path
547
  q_emb = embed_model.encode([q], convert_to_numpy=True).astype("float32")
548
  if _IS_IP:
 
583
  dlog("PUBMED", "PubMed search hit")
584
  return results
585
 
586
+ # Wikipedia fallback (unconditional after PubMed miss)
587
+ wiki = wiki_summary_allow(q, sentences=3)
588
+ if wiki:
589
+ dlog("WIKI", "Wikipedia fallback hit")
590
+ return [{"text": wiki, "source": "wikipedia"}]
591
 
592
  dlog("RETRIEVAL", "No results at all")
593
  return []
594
 
595
+
596
  def build_prompt(chunks: List[Dict[str, Any]], question: str) -> str:
597
  header = (
598
  "You are Askstein (orthopedic biomechanics). Use ONLY the [Context] to answer. "
 
612
  gen = out_ids[0][in_len:]
613
  return tokenizer_lm.decode(gen, skip_special_tokens=True).lstrip(". \n").strip()
614
 
615
+ @lru_cache(maxsize=None)
616
+ def direct_llm_fallback(question: str) -> str:
617
+ sys_prompt = (
618
+ "You are Askstein (orthopedic biomechanics). If you lack enough domain context, say you don’t know. "
619
+ "Avoid discussing non-musculoskeletal systems (cardiology, neurology)."
620
+ )
621
+ llm_prompt = f"{sys_prompt}\n\nQuestion: {question}\nAnswer:"
622
+ inputs = tokenizer_lm(llm_prompt, return_tensors="pt").to(device)
623
+ out = _generate(inputs, grounded=False)
624
+ in_len = inputs["input_ids"].shape[-1]
625
+ return _post_clean(_decode_generated(out, in_len))
626
+
627
+
628
  def _synthesize_answer(chunks: List[Dict[str, Any]], question: str) -> str:
629
  prompt = build_prompt(chunks, question)
630
  inputs = tokenizer_lm(prompt, return_tensors="pt").to(device)