Spaces:
Sleeping
Sleeping
datnguyentien204
commited on
Commit
•
63ea0d4
1
Parent(s):
518ebae
Update pages/🤖 Medical Question Answering.py
Browse files- pages/🤖 Medical Question Answering.py +198 -194
pages/🤖 Medical Question Answering.py
CHANGED
@@ -1,194 +1,198 @@
|
|
1 |
-
import streamlit as st
|
2 |
-
import google.generativeai as genai
|
3 |
-
from dotenv import load_dotenv
|
4 |
-
import os
|
5 |
-
import PIL
|
6 |
-
from PyPDF2 import PdfReader
|
7 |
-
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
8 |
-
from langchain_google_genai import GoogleGenerativeAIEmbeddings
|
9 |
-
from langchain_community.vectorstores import FAISS
|
10 |
-
from langchain_google_genai import ChatGoogleGenerativeAI
|
11 |
-
from langchain.chains.question_answering import load_qa_chain
|
12 |
-
from langchain.prompts import PromptTemplate
|
13 |
-
|
14 |
-
load_dotenv()
|
15 |
-
os.getenv("langchain_google_genai")
|
16 |
-
os.environ['GOOGLE_API_KEY'] = 'AIzaSyA5cVv6I1HxH68CTiPGalPQHymtunvDxVY'
|
17 |
-
genai.configure(api_key="AIzaSyA5cVv6I1HxH68CTiPGalPQHymtunvDxVY")
|
18 |
-
# Function to extract text from PDF files
|
19 |
-
|
20 |
-
import os
|
21 |
-
os.environ['KMP_DUPLICATE_LIB_OK'] = 'TRUE'
|
22 |
-
|
23 |
-
def get_pdf_text(pdf_docs):
|
24 |
-
text = ""
|
25 |
-
for pdf in pdf_docs:
|
26 |
-
pdf_reader = PdfReader(pdf)
|
27 |
-
for page in pdf_reader.pages:
|
28 |
-
text += page.extract_text()
|
29 |
-
return text
|
30 |
-
|
31 |
-
# Function to split text into chunks
|
32 |
-
def get_text_chunks(text):
|
33 |
-
text_splitter = RecursiveCharacterTextSplitter(chunk_size=10000, chunk_overlap=1000)
|
34 |
-
chunks = text_splitter.split_text(text)
|
35 |
-
return chunks
|
36 |
-
|
37 |
-
# Function to create a vector store from text chunks
|
38 |
-
def get_vector_store(text_chunks):
|
39 |
-
embeddings = GoogleGenerativeAIEmbeddings(model="models/embedding-001")
|
40 |
-
vector_store = FAISS.from_texts(text_chunks, embedding=embeddings)
|
41 |
-
vector_store.save_local("faiss_index")
|
42 |
-
|
43 |
-
# Function to get the conversational chain
|
44 |
-
if "text_chunks" not in st.session_state:
|
45 |
-
st.session_state.text_chunks = None
|
46 |
-
|
47 |
-
if "vector_store" not in st.session_state:
|
48 |
-
st.session_state.vector_store = None
|
49 |
-
|
50 |
-
if "document_messages" not in st.session_state:
|
51 |
-
st.session_state.document_messages = []
|
52 |
-
|
53 |
-
|
54 |
-
def get_conversational_chain():
|
55 |
-
prompt_template = """
|
56 |
-
Answer the question as detailed as possible from the provided context, make sure to provide all the details, if the answer is not in
|
57 |
-
provided context just say, "answer is not available in the context", don't provide the wrong answer\n\n
|
58 |
-
Context:\n {context}?\n
|
59 |
-
Question: \n{question}\n
|
60 |
-
|
61 |
-
Answer:
|
62 |
-
"""
|
63 |
-
model = ChatGoogleGenerativeAI(model="gemini-pro", temperature=0.1)
|
64 |
-
prompt = PromptTemplate(template=prompt_template, input_variables=["context", "question"])
|
65 |
-
chain = load_qa_chain(model, chain_type="stuff", prompt=prompt)
|
66 |
-
return chain
|
67 |
-
|
68 |
-
# Function to handle user input
|
69 |
-
# Function to handle user input
|
70 |
-
def user_input(user_question):
|
71 |
-
embeddings = GoogleGenerativeAIEmbeddings(model="models/embedding-001")
|
72 |
-
new_db = FAISS.load_local("faiss_index", embeddings, allow_dangerous_deserialization=True)
|
73 |
-
docs = new_db.similarity_search(user_question)
|
74 |
-
chain = get_conversational_chain()
|
75 |
-
response = chain({"input_documents": docs, "question": user_question}, return_only_outputs=True)
|
76 |
-
|
77 |
-
return response["output_text"] # Return the answer as a string
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
st.
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
st.session_state.image_messages.append(
|
140 |
-
|
141 |
-
|
142 |
-
st.
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
if
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
st.
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
with st.chat_message("
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
)
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
st.
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
st.session_state.messages.append(
|
192 |
-
|
193 |
-
|
194 |
-
st.
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import google.generativeai as genai
|
3 |
+
from dotenv import load_dotenv
|
4 |
+
import os
|
5 |
+
import PIL
|
6 |
+
from PyPDF2 import PdfReader
|
7 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
8 |
+
from langchain_google_genai import GoogleGenerativeAIEmbeddings
|
9 |
+
from langchain_community.vectorstores import FAISS
|
10 |
+
from langchain_google_genai import ChatGoogleGenerativeAI
|
11 |
+
from langchain.chains.question_answering import load_qa_chain
|
12 |
+
from langchain.prompts import PromptTemplate
|
13 |
+
|
14 |
+
load_dotenv()
|
15 |
+
os.getenv("langchain_google_genai")
|
16 |
+
os.environ['GOOGLE_API_KEY'] = 'AIzaSyA5cVv6I1HxH68CTiPGalPQHymtunvDxVY'
|
17 |
+
genai.configure(api_key="AIzaSyA5cVv6I1HxH68CTiPGalPQHymtunvDxVY")
|
18 |
+
# Function to extract text from PDF files
|
19 |
+
|
20 |
+
import os
|
21 |
+
os.environ['KMP_DUPLICATE_LIB_OK'] = 'TRUE'
|
22 |
+
|
23 |
+
def get_pdf_text(pdf_docs):
|
24 |
+
text = ""
|
25 |
+
for pdf in pdf_docs:
|
26 |
+
pdf_reader = PdfReader(pdf)
|
27 |
+
for page in pdf_reader.pages:
|
28 |
+
text += page.extract_text()
|
29 |
+
return text
|
30 |
+
|
31 |
+
# Function to split text into chunks
|
32 |
+
def get_text_chunks(text):
|
33 |
+
text_splitter = RecursiveCharacterTextSplitter(chunk_size=10000, chunk_overlap=1000)
|
34 |
+
chunks = text_splitter.split_text(text)
|
35 |
+
return chunks
|
36 |
+
|
37 |
+
# Function to create a vector store from text chunks
|
38 |
+
def get_vector_store(text_chunks):
|
39 |
+
embeddings = GoogleGenerativeAIEmbeddings(model="models/embedding-001")
|
40 |
+
vector_store = FAISS.from_texts(text_chunks, embedding=embeddings)
|
41 |
+
vector_store.save_local("faiss_index")
|
42 |
+
|
43 |
+
# Function to get the conversational chain
|
44 |
+
if "text_chunks" not in st.session_state:
|
45 |
+
st.session_state.text_chunks = None
|
46 |
+
|
47 |
+
if "vector_store" not in st.session_state:
|
48 |
+
st.session_state.vector_store = None
|
49 |
+
|
50 |
+
if "document_messages" not in st.session_state:
|
51 |
+
st.session_state.document_messages = []
|
52 |
+
|
53 |
+
|
54 |
+
def get_conversational_chain():
|
55 |
+
prompt_template = """
|
56 |
+
Answer the question as detailed as possible from the provided context, make sure to provide all the details, if the answer is not in
|
57 |
+
provided context just say, "answer is not available in the context", don't provide the wrong answer\n\n
|
58 |
+
Context:\n {context}?\n
|
59 |
+
Question: \n{question}\n
|
60 |
+
|
61 |
+
Answer:
|
62 |
+
"""
|
63 |
+
model = ChatGoogleGenerativeAI(model="gemini-pro", temperature=0.1)
|
64 |
+
prompt = PromptTemplate(template=prompt_template, input_variables=["context", "question"])
|
65 |
+
chain = load_qa_chain(model, chain_type="stuff", prompt=prompt)
|
66 |
+
return chain
|
67 |
+
|
68 |
+
# Function to handle user input
|
69 |
+
# Function to handle user input
|
70 |
+
def user_input(user_question):
|
71 |
+
# embeddings = GoogleGenerativeAIEmbeddings(model="models/embedding-001")
|
72 |
+
# new_db = FAISS.load_local("faiss_index", embeddings, allow_dangerous_deserialization=True)
|
73 |
+
# docs = new_db.similarity_search(user_question)
|
74 |
+
# chain = get_conversational_chain()
|
75 |
+
# response = chain({"input_documents": docs, "question": user_question}, return_only_outputs=True)
|
76 |
+
|
77 |
+
# return response["output_text"] # Return the answer as a string
|
78 |
+
chain = get_conversational_chain()
|
79 |
+
response = chain({"input_documents": [], "question": user_question}, return_only_outputs=True)
|
80 |
+
|
81 |
+
return response["output_text"]
|
82 |
+
|
83 |
+
|
84 |
+
|
85 |
+
# Streamlit UI setup
|
86 |
+
st.markdown("<h1 style='text-align: center;'>Chào mừng tới Medical Question Answering 🎈</h1>", unsafe_allow_html=True)
|
87 |
+
|
88 |
+
|
89 |
+
with st.expander("Instructions"):
|
90 |
+
st.markdown("Truyền vào một câu hỏi liên quan đến y tế, chúng tôi sẽ giải đáp cho bạn.")
|
91 |
+
st.markdown("Bạn có thể hỏi các câu liên quan đến triệu chứng, nguyên nhân và một số phương pháp điều trị.")
|
92 |
+
|
93 |
+
|
94 |
+
|
95 |
+
|
96 |
+
|
97 |
+
with st.sidebar:
|
98 |
+
mode = st.selectbox("Chọn chức năng", ["Question with Images", "Question with Documents"])
|
99 |
+
if mode == "Question with Images":
|
100 |
+
uploaded_files = st.file_uploader("Choose medical images...", type=["jpg", "jpeg", "png", "dicom"], accept_multiple_files=True)
|
101 |
+
elif mode == "Question with Documents":
|
102 |
+
folder_path = "medicalDocuments"
|
103 |
+
if st.session_state.text_chunks is None:
|
104 |
+
pdf_docs = [os.path.join(folder_path, f) for f in os.listdir(folder_path) if f.endswith(".pdf")]
|
105 |
+
raw_text = get_pdf_text(pdf_docs)
|
106 |
+
st.session_state.text_chunks = get_text_chunks(raw_text)
|
107 |
+
st.session_state.vector_store = get_vector_store(st.session_state.text_chunks)
|
108 |
+
|
109 |
+
# Initialize session state
|
110 |
+
if "messages" not in st.session_state:
|
111 |
+
st.session_state.messages = []
|
112 |
+
|
113 |
+
if "image_messages" not in st.session_state:
|
114 |
+
st.session_state.image_messages = []
|
115 |
+
|
116 |
+
if "max_messages" not in st.session_state:
|
117 |
+
st.session_state.max_messages = 1000
|
118 |
+
|
119 |
+
# Handle "Question with Images" mode
|
120 |
+
col_1, col_2, col_3 = st.columns([8, 1, 8])
|
121 |
+
if mode == "Question with Images" and uploaded_files:
|
122 |
+
with col_1:
|
123 |
+
image = PIL.Image.open(uploaded_files[0])
|
124 |
+
st.image(image, caption="Uploaded Image", use_column_width=True)
|
125 |
+
with col_3:
|
126 |
+
# Display past messages for Question with Images
|
127 |
+
for message in st.session_state.image_messages:
|
128 |
+
with st.chat_message(message["role"]):
|
129 |
+
st.markdown(message["content"])
|
130 |
+
|
131 |
+
if prompt := st.chat_input("Ask a question about the image..."):
|
132 |
+
st.session_state.image_messages.append({"role": "user", "content": prompt})
|
133 |
+
with st.chat_message("user"):
|
134 |
+
st.markdown(prompt)
|
135 |
+
model = genai.GenerativeModel('gemini-1.5-flash')
|
136 |
+
with st.chat_message("assistant"):
|
137 |
+
try:
|
138 |
+
response = model.generate_content([prompt, image])
|
139 |
+
st.session_state.image_messages.append({"role": "assistant", "content": response.text})
|
140 |
+
st.markdown(response.text)
|
141 |
+
except Exception as e:
|
142 |
+
st.session_state.max_messages = len(st.session_state.image_messages)
|
143 |
+
st.session_state.image_messages.append(
|
144 |
+
{"role": "assistant", "content": f"Oops! There was an error: {str(e)}"}
|
145 |
+
)
|
146 |
+
st.rerun()
|
147 |
+
|
148 |
+
if "document_messages" not in st.session_state:
|
149 |
+
st.session_state.document_messages = []
|
150 |
+
|
151 |
+
# Handle "Question with Documents" mode
|
152 |
+
if mode == "Question with Documents":
|
153 |
+
# Display past messages for Document-based conversation
|
154 |
+
for message in st.session_state.document_messages:
|
155 |
+
with st.chat_message(message["role"]):
|
156 |
+
st.markdown(message["content"])
|
157 |
+
|
158 |
+
if user_question := st.chat_input("Hỏi câu hỏi từ file PDF"):
|
159 |
+
st.session_state.document_messages.append({"role": "user", "content": user_question})
|
160 |
+
with st.chat_message("user"):
|
161 |
+
st.markdown(user_question)
|
162 |
+
|
163 |
+
# Generate the response
|
164 |
+
with st.chat_message("assistant"):
|
165 |
+
try:
|
166 |
+
response = user_input(user_question)
|
167 |
+
st.session_state.document_messages.append({"role": "assistant", "content": response})
|
168 |
+
st.markdown(response)
|
169 |
+
except Exception as e:
|
170 |
+
st.session_state.document_messages.append(
|
171 |
+
{"role": "assistant", "content": f"Oops! There was an error: {str(e)}"}
|
172 |
+
)
|
173 |
+
st.rerun()
|
174 |
+
|
175 |
+
# Display past messages for non-image-based conversation
|
176 |
+
if mode != "Question with Images" and mode != "Question with Documents":
|
177 |
+
for message in st.session_state.messages:
|
178 |
+
with st.chat_message(message["role"]):
|
179 |
+
st.markdown(message["content"])
|
180 |
+
|
181 |
+
if len(st.session_state.messages) < st.session_state.max_messages:
|
182 |
+
if prompt := st.chat_input("Hôm nay bạn như thế nào?"):
|
183 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
184 |
+
with st.chat_message("user"):
|
185 |
+
st.markdown(prompt)
|
186 |
+
model = genai.GenerativeModel(model_name="gemini-pro")
|
187 |
+
with st.chat_message("assistant"):
|
188 |
+
try:
|
189 |
+
prompt_parts = [prompt]
|
190 |
+
response = model.generate_content(prompt_parts)
|
191 |
+
st.session_state.messages.append({"role": "assistant", "content": response.text})
|
192 |
+
st.markdown(response.text)
|
193 |
+
except Exception as e:
|
194 |
+
st.session_state.max_messages = len(st.session_state.messages)
|
195 |
+
st.session_state.messages.append(
|
196 |
+
{"role": "assistant", "content": f"Oops! There was an error: {str(e)}"}
|
197 |
+
)
|
198 |
+
st.rerun()
|