mgbam commited on
Commit
274d11f
·
verified ·
1 Parent(s): d11ca72

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -116
app.py CHANGED
@@ -1,128 +1,51 @@
1
  import streamlit as st
2
- import os
3
-
4
- from config import (
5
- OPENAI_API_KEY,
6
- OPENAI_DEFAULT_MODEL,
7
- MAX_PUBMED_RESULTS
8
- )
9
- from pubmed_rag import (
10
- search_pubmed, fetch_pubmed_abstracts, chunk_and_summarize,
11
- upsert_documents, semantic_search
12
- )
13
- from models import chat_with_openai
14
  from image_pipeline import analyze_medical_image
 
15
 
16
- ###############################################################################
17
- # STREAMLIT SETUP #
18
- ###############################################################################
19
  st.set_page_config(page_title="Advanced Medical AI", layout="wide")
20
 
21
  def main():
22
- st.title("Advanced Medical AI: Multi-Modal RAG & Image Diagnostics")
23
-
24
- st.markdown("""
25
- **Features**:
26
- 1. **PubMed RAG**: Retrieve and summarize medical literature, store in a vector DB,
27
- and use advanced semantic search for context.
28
- 2. **LLM Q&A**: Leverage OpenAI for final question-answering with RAG context.
29
- 3. **Medical Image Analysis**: Use `HuggingFaceTB/SmolVLM-500M-Instruct` for diagnostic insights.
30
- 4. **Multi-Lingual & Extended Triage**: Placeholder expansions for real-time translation or advanced triage logic.
31
- 5. **Production-Ready**: Modular, concurrent, disclaimers, and synergy across tasks.
32
- """)
33
-
34
- menu = ["PubMed RAG Q&A", "Medical Image Analysis", "Semantic Search (Vector DB)"]
35
- choice = st.sidebar.selectbox("Select Task", menu)
36
-
37
- if choice == "PubMed RAG Q&A":
38
- pubmed_rag_qna()
39
- elif choice == "Medical Image Analysis":
40
- medical_image_analysis()
41
- else:
42
- vector_db_search_ui()
43
-
44
- st.markdown("---")
45
- st.markdown("""
46
- **Disclaimer**: This is an **advanced demonstration** for educational or research purposes only.
47
- Always consult a qualified healthcare professional for personal medical decisions.
48
- """)
49
-
50
- def pubmed_rag_qna():
51
- st.subheader("PubMed Retrieval-Augmented Q&A")
52
- query = st.text_area(
53
- "Ask a medical question (e.g., 'What are the latest treatments for type 2 diabetes?'):",
54
- height=100
55
- )
56
- max_art = st.slider("Number of PubMed Articles to Retrieve", 1, 10, 5)
57
-
58
- if st.button("Search & Summarize"):
59
- if not query.strip():
60
- st.warning("Please enter a query.")
61
- return
62
-
63
- with st.spinner("Searching PubMed..."):
64
- pmids = search_pubmed(query, max_art)
65
-
66
- if not pmids:
67
- st.error("No articles found. Try another query.")
68
- return
69
-
70
- with st.spinner("Fetching and Summarizing..."):
71
- raw_abstracts = fetch_pubmed_abstracts(pmids)
72
- # Summarize each
73
- summarized = {}
74
- for pmid, text in raw_abstracts.items():
75
- if text.startswith("Error"):
76
- summarized[pmid] = text
77
- else:
78
- summary = chunk_and_summarize(text)
79
- summarized[pmid] = summary
80
-
81
- st.subheader("Summaries")
82
- for i, (pmid, summary) in enumerate(summarized.items(), start=1):
83
- st.markdown(f"**[Ref{i}] PMID {pmid}**")
84
- st.write(summary)
85
-
86
- # Upsert into vector DB
87
- upsert_documents(summarized) # store raw or summarized texts
88
-
89
- # Build system prompt
90
- system_prompt = "You are an advanced medical assistant with the following references:\n"
91
- for i, (pmid, summary) in enumerate(summarized.items(), start=1):
92
- system_prompt += f"[Ref{i}] PMID {pmid}: {summary}\n"
93
- system_prompt += "\nUsing these references, provide an evidence-based answer."
94
-
95
- with st.spinner("Generating final answer..."):
96
- final_answer = chat_with_openai(system_prompt, query)
97
- st.subheader("Final Answer")
98
- st.write(final_answer)
99
-
100
- def medical_image_analysis():
101
- st.subheader("Medical Image Analysis")
102
- uploaded_file = st.file_uploader("Upload a Medical Image (PNG/JPG)", type=["png", "jpg", "jpeg"])
103
- if uploaded_file is not None:
104
- st.image(uploaded_file, caption="Uploaded Image", use_column_width=True)
105
- if st.button("Analyze Image"):
106
- with st.spinner("Analyzing..."):
107
  result = analyze_medical_image(uploaded_file)
108
  st.subheader("Diagnostic Insight")
109
  st.write(result)
110
 
111
- def vector_db_search_ui():
112
- st.subheader("Semantic Search in Vector DB")
113
- user_query = st.text_input("Enter a query to find relevant documents", "")
114
- top_k = st.slider("Number of results", 1, 10, 3)
115
- if st.button("Search"):
116
- if not user_query.strip():
117
- st.warning("Please enter a query.")
118
- return
119
- with st.spinner("Performing semantic search..."):
120
- results = semantic_search(user_query, top_k=top_k)
121
- st.subheader("Search Results")
122
- for i, doc in enumerate(results, start=1):
123
- st.markdown(f"**Result {i}** - PMID {doc['pmid']} (Distance: {doc['score']:.4f})")
124
- st.write(doc["text"])
125
- st.write("---")
126
-
127
  if __name__ == "__main__":
128
  main()
 
1
  import streamlit as st
2
+ from pubmed_rag import search_pubmed, fetch_pubmed_abstracts, summarize_text
 
 
 
 
 
 
 
 
 
 
 
3
  from image_pipeline import analyze_medical_image
4
+ from models import chat_with_openai
5
 
 
 
 
6
  st.set_page_config(page_title="Advanced Medical AI", layout="wide")
7
 
8
  def main():
9
+ st.title("Advanced Medical AI")
10
+ st.sidebar.title("Features")
11
+ task = st.sidebar.selectbox("Choose a task:", ["PubMed Q&A", "Medical Image Analysis"])
12
+
13
+ if task == "PubMed Q&A":
14
+ st.subheader("PubMed Question Answering")
15
+ query = st.text_input("Enter your medical question:", "What are the latest treatments for diabetes?")
16
+ max_results = st.slider("Number of PubMed articles to retrieve:", 1, 10, 5)
17
+
18
+ if st.button("Run Query"):
19
+ with st.spinner("Searching PubMed..."):
20
+ pmids = search_pubmed(query, max_results)
21
+ if not pmids:
22
+ st.error("No results found. Try another query.")
23
+ return
24
+
25
+ with st.spinner("Fetching and summarizing abstracts..."):
26
+ abstracts = fetch_pubmed_abstracts(pmids)
27
+ summaries = {pmid: summarize_text(abstract) for pmid, abstract in abstracts.items()}
28
+
29
+ st.subheader("PubMed Summaries")
30
+ for pmid, summary in summaries.items():
31
+ st.write(f"**PMID {pmid}**: {summary}")
32
+
33
+ system_message = "You are a medical assistant with access to PubMed summaries."
34
+ user_message = query
35
+ with st.spinner("Generating answer..."):
36
+ answer = chat_with_openai(system_message, user_message)
37
+ st.subheader("AI-Powered Answer")
38
+ st.write(answer)
39
+
40
+ elif task == "Medical Image Analysis":
41
+ st.subheader("Medical Image Analysis")
42
+ uploaded_file = st.file_uploader("Upload a medical image (PNG/JPG):", type=["png", "jpg", "jpeg"])
43
+ if uploaded_file:
44
+ st.image(uploaded_file, caption="Uploaded Image", use_column_width=True)
45
+ with st.spinner("Analyzing image..."):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46
  result = analyze_medical_image(uploaded_file)
47
  st.subheader("Diagnostic Insight")
48
  st.write(result)
49
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50
  if __name__ == "__main__":
51
  main()