mgbam commited on
Commit
6ebe843
·
verified ·
1 Parent(s): 850d842

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -13
app.py CHANGED
@@ -28,6 +28,10 @@ from Bio import Entrez # Ensure BioPython is installed
28
  from langchain.prompts import PromptTemplate
29
  from groq import Groq
30
 
 
 
 
 
31
  # ---------------------- Initialize External Clients ---------------------------
32
  # Initialize Groq Client with API Key from environment variables
33
  client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
@@ -36,15 +40,12 @@ client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
36
  try:
37
  nlp = spacy.load("en_core_web_sm")
38
  except OSError:
39
- # Use Streamlit's message to inform the user instead of print
40
- st.write("Downloading en_core_web_sm spaCy model...")
41
- spacy.cli.download("en_core_web_sm")
 
42
  nlp = spacy.load("en_core_web_sm")
43
 
44
- # ---------------------- Streamlit Page Configuration ---------------------------
45
- # This must be the first Streamlit command in the script
46
- st.set_page_config(page_title="AI Clinical Intelligence Hub", layout="wide")
47
-
48
  # ---------------------- Base Classes and Schemas ---------------------------
49
 
50
  class ResearchInput(BaseModel):
@@ -410,7 +411,7 @@ class SimpleMedicalKnowledge(MedicalKnowledgeBase):
410
  """Search PubMed for abstracts related to the query."""
411
  try:
412
  Entrez.email = email
413
- handle = Entrez.esearch(db="pubmed", term=query, retmax=1)
414
  record = Entrez.read(handle)
415
  handle.close()
416
  if record["IdList"]:
@@ -449,13 +450,19 @@ class SimpleMedicalKnowledge(MedicalKnowledgeBase):
449
 
450
  best_match_info = self.knowledge_base.get(best_match_keyword, "No specific information is available based on the query provided.")
451
 
452
- pubmed_result = self.search_pubmed(best_match_keyword, pub_email)
453
- feedback_key = f"feedback_{query_lower}" # Unique key for feedback
 
 
 
454
 
455
  response = f"**Based on your query:** {best_match_info}\n\n"
456
 
457
- if "No abstracts found for this query on PubMed." not in pubmed_result:
458
- response += f"**PubMed Abstract:**\n{pubmed_result}"
 
 
 
459
  else:
460
  response += f"{pubmed_result}"
461
 
@@ -464,7 +471,7 @@ class SimpleMedicalKnowledge(MedicalKnowledgeBase):
464
  st.session_state[feedback_key] = {"feedback": None}
465
 
466
  # Display feedback buttons only if a valid response is generated
467
- if "error" not in pubmed_result:
468
  col1, col2 = st.columns([1, 1])
469
  with col1:
470
  if st.button("Good Result", key=f"good_{feedback_key}"):
 
28
  from langchain.prompts import PromptTemplate
29
  from groq import Groq
30
 
31
+ # ---------------------- Streamlit Page Configuration ---------------------------
32
+ # This must be the first Streamlit command in the script
33
+ st.set_page_config(page_title="AI Clinical Intelligence Hub", layout="wide")
34
+
35
  # ---------------------- Initialize External Clients ---------------------------
36
  # Initialize Groq Client with API Key from environment variables
37
  client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
 
40
  try:
41
  nlp = spacy.load("en_core_web_sm")
42
  except OSError:
43
+ # Avoid using Streamlit commands before set_page_config()
44
+ import subprocess
45
+ import sys
46
+ subprocess.run([sys.executable, "-m", "spacy", "download", "en_core_web_sm"])
47
  nlp = spacy.load("en_core_web_sm")
48
 
 
 
 
 
49
  # ---------------------- Base Classes and Schemas ---------------------------
50
 
51
  class ResearchInput(BaseModel):
 
411
  """Search PubMed for abstracts related to the query."""
412
  try:
413
  Entrez.email = email
414
+ handle = Entrez.esearch(db="pubmed", term=query, retmax=1, sort='relevance')
415
  record = Entrez.read(handle)
416
  handle.close()
417
  if record["IdList"]:
 
450
 
451
  best_match_info = self.knowledge_base.get(best_match_keyword, "No specific information is available based on the query provided.")
452
 
453
+ # Enhanced PubMed Search: Combine query and best_match_keyword for better relevance
454
+ pubmed_query = f"{query_lower} AND {best_match_keyword}"
455
+ pubmed_result = self.search_pubmed(pubmed_query, pub_email)
456
+
457
+ feedback_key = f"feedback_{query_lower}" # Creating a unique key for feedback
458
 
459
  response = f"**Based on your query:** {best_match_info}\n\n"
460
 
461
+ if "Error searching PubMed" not in pubmed_result and "No abstracts found" not in pubmed_result:
462
+ # Format the PubMed abstract with proper markdown
463
+ abstract_title = pubmed_result.split('\n')[0] # Assuming the first line is the title
464
+ abstract_body = '\n'.join(pubmed_result.split('\n')[2:]) # Skipping authors and affiliations
465
+ response += f"**PubMed Abstract:**\n\n**{abstract_title}**\n\n{abstract_body}"
466
  else:
467
  response += f"{pubmed_result}"
468
 
 
471
  st.session_state[feedback_key] = {"feedback": None}
472
 
473
  # Display feedback buttons only if a valid response is generated
474
+ if "Error searching PubMed" not in pubmed_result:
475
  col1, col2 = st.columns([1, 1])
476
  with col1:
477
  if st.button("Good Result", key=f"good_{feedback_key}"):