Update backend.py
Browse files- 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
|
9 |
docs_cache = {}
|
10 |
|
11 |
def process_medical_query(query: str):
|
12 |
"""
|
13 |
-
Processes a clinical query
|
14 |
|
15 |
-
1.
|
16 |
-
2.
|
17 |
|
18 |
Returns:
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
Designed for clinical use with clear, concise responses.
|
25 |
"""
|
26 |
-
# Retrieve relevant documents
|
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
|
34 |
context_text = "\n\n".join(relevant_docs)
|
35 |
prompt = f"Question: {query}\nContext: {context_text}\nAnswer:"
|
36 |
|
37 |
-
|
38 |
-
|
|
|
|
|
|
|
|
|
39 |
if generation and isinstance(generation, list):
|
40 |
answer = generation[0]["generated_text"]
|
41 |
else:
|
42 |
answer = "No answer found."
|
43 |
|
44 |
-
#
|
45 |
-
|
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, ""
|
|
|
|