mgbam commited on
Commit
4031a24
·
verified ·
1 Parent(s): 85d173c

Update backend.py

Browse files
Files changed (1) hide show
  1. backend.py +25 -18
backend.py CHANGED
@@ -1,3 +1,10 @@
 
 
 
 
 
 
 
1
  from transformers import pipeline
2
  from retrieval import get_relevant_pubmed_docs
3
 
@@ -5,43 +12,43 @@ from retrieval import get_relevant_pubmed_docs
5
  MODEL_NAME = "microsoft/BioGPT-Large-PubMedQA"
6
  qa_pipeline = pipeline("text-generation", model=MODEL_NAME)
7
 
8
- # In-memory cache for documents (used later in the knowledge graph).
9
  docs_cache = {}
10
 
11
  def process_medical_query(query: str):
12
  """
13
- Processes a clinical query in two steps:
14
 
15
- 1. Retrieve relevant PubMed abstracts using a retrieval pipeline.
16
- 2. Generate an answer using BioGPT, leveraging the retrieved abstracts as context.
17
 
18
  Returns:
19
- - The generated answer.
20
- - An empty list for sub-questions (omitted for speed).
21
- - The initial answer (same as final in this simplified pipeline).
22
- - An empty critique (omitted).
23
-
24
- Designed for clinical use with clear, concise responses.
25
  """
26
- # Retrieve relevant documents via PubMed and Chroma.
27
  relevant_docs = get_relevant_pubmed_docs(query)
28
  docs_cache[query] = relevant_docs
29
 
30
  if not relevant_docs:
31
  return ("No documents found for this query.", [], "", "")
32
 
33
- # Combine the retrieved abstracts as context.
34
  context_text = "\n\n".join(relevant_docs)
35
  prompt = f"Question: {query}\nContext: {context_text}\nAnswer:"
36
 
37
- # Generate an answer using BioGPT.
38
- generation = qa_pipeline(prompt, max_new_tokens=100, truncation=True)
 
 
 
 
39
  if generation and isinstance(generation, list):
40
  answer = generation[0]["generated_text"]
41
  else:
42
  answer = "No answer found."
43
 
44
- # In this streamlined pipeline, we return the same answer for all outputs.
45
- sub_questions = [] # No sub-questions generated.
46
- critique = "" # No self-critique performed.
47
- return answer, sub_questions, answer, critique
 
1
+ """
2
+ backend.py
3
+ ----------
4
+ This module handles the core processing of clinical queries. It retrieves relevant PubMed abstracts
5
+ via the retrieval pipeline and then uses BioGPT to generate a clinical answer. Designed for speed and clarity.
6
+ """
7
+
8
  from transformers import pipeline
9
  from retrieval import get_relevant_pubmed_docs
10
 
 
12
  MODEL_NAME = "microsoft/BioGPT-Large-PubMedQA"
13
  qa_pipeline = pipeline("text-generation", model=MODEL_NAME)
14
 
15
+ # In-memory cache for retrieved documents (used for knowledge graph visualization).
16
  docs_cache = {}
17
 
18
  def process_medical_query(query: str):
19
  """
20
+ Processes a clinical query by:
21
 
22
+ 1. Retrieving relevant PubMed abstracts.
23
+ 2. Generating an answer using BioGPT.
24
 
25
  Returns:
26
+ - final_answer: The generated answer.
27
+ - sub_questions: (Empty list; previously used for self-critique but omitted for speed.)
28
+ - initial_answer: The same as final_answer in this streamlined version.
29
+ - critique: (Empty string; omitted for performance.)
 
 
30
  """
31
+ # Retrieve relevant documents using the retrieval pipeline.
32
  relevant_docs = get_relevant_pubmed_docs(query)
33
  docs_cache[query] = relevant_docs
34
 
35
  if not relevant_docs:
36
  return ("No documents found for this query.", [], "", "")
37
 
38
+ # Combine abstracts into a context.
39
  context_text = "\n\n".join(relevant_docs)
40
  prompt = f"Question: {query}\nContext: {context_text}\nAnswer:"
41
 
42
+ try:
43
+ generation = qa_pipeline(prompt, max_new_tokens=100, truncation=True)
44
+ except Exception as e:
45
+ generation = None
46
+ print(f"[ERROR] BioGPT generation failed: {e}")
47
+
48
  if generation and isinstance(generation, list):
49
  answer = generation[0]["generated_text"]
50
  else:
51
  answer = "No answer found."
52
 
53
+ # Return the answer along with empty placeholders for sub-questions and critique.
54
+ return answer, [], answer, ""