datnguyentien204 commited on
Commit
63ea0d4
1 Parent(s): 518ebae

Update pages/🤖 Medical Question Answering.py

Browse files
Files changed (1) hide show
  1. 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
- # Streamlit UI setup
82
- st.markdown("<h1 style='text-align: center;'>Chào mừng tới Medical Question Answering 🎈</h1>", unsafe_allow_html=True)
83
-
84
-
85
- with st.expander("Instructions"):
86
- 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.")
87
- 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ị.")
88
-
89
-
90
-
91
-
92
-
93
- with st.sidebar:
94
- mode = st.selectbox("Chọn chức năng", ["Question with Images", "Question with Documents"])
95
- if mode == "Question with Images":
96
- uploaded_files = st.file_uploader("Choose medical images...", type=["jpg", "jpeg", "png", "dicom"], accept_multiple_files=True)
97
- elif mode == "Question with Documents":
98
- folder_path = "medicalDocuments"
99
- if st.session_state.text_chunks is None:
100
- pdf_docs = [os.path.join(folder_path, f) for f in os.listdir(folder_path) if f.endswith(".pdf")]
101
- raw_text = get_pdf_text(pdf_docs)
102
- st.session_state.text_chunks = get_text_chunks(raw_text)
103
- st.session_state.vector_store = get_vector_store(st.session_state.text_chunks)
104
-
105
- # Initialize session state
106
- if "messages" not in st.session_state:
107
- st.session_state.messages = []
108
-
109
- if "image_messages" not in st.session_state:
110
- st.session_state.image_messages = []
111
-
112
- if "max_messages" not in st.session_state:
113
- st.session_state.max_messages = 1000
114
-
115
- # Handle "Question with Images" mode
116
- col_1, col_2, col_3 = st.columns([8, 1, 8])
117
- if mode == "Question with Images" and uploaded_files:
118
- with col_1:
119
- image = PIL.Image.open(uploaded_files[0])
120
- st.image(image, caption="Uploaded Image", use_column_width=True)
121
- with col_3:
122
- # Display past messages for Question with Images
123
- for message in st.session_state.image_messages:
124
- with st.chat_message(message["role"]):
125
- st.markdown(message["content"])
126
-
127
- if prompt := st.chat_input("Ask a question about the image..."):
128
- st.session_state.image_messages.append({"role": "user", "content": prompt})
129
- with st.chat_message("user"):
130
- st.markdown(prompt)
131
- model = genai.GenerativeModel('gemini-1.5-flash')
132
- with st.chat_message("assistant"):
133
- try:
134
- response = model.generate_content([prompt, image])
135
- st.session_state.image_messages.append({"role": "assistant", "content": response.text})
136
- st.markdown(response.text)
137
- except Exception as e:
138
- st.session_state.max_messages = len(st.session_state.image_messages)
139
- st.session_state.image_messages.append(
140
- {"role": "assistant", "content": f"Oops! There was an error: {str(e)}"}
141
- )
142
- st.rerun()
143
-
144
- if "document_messages" not in st.session_state:
145
- st.session_state.document_messages = []
146
-
147
- # Handle "Question with Documents" mode
148
- if mode == "Question with Documents":
149
- # Display past messages for Document-based conversation
150
- for message in st.session_state.document_messages:
151
- with st.chat_message(message["role"]):
152
- st.markdown(message["content"])
153
-
154
- if user_question := st.chat_input("Hỏi câu hỏi từ file PDF"):
155
- st.session_state.document_messages.append({"role": "user", "content": user_question})
156
- with st.chat_message("user"):
157
- st.markdown(user_question)
158
-
159
- # Generate the response
160
- with st.chat_message("assistant"):
161
- try:
162
- response = user_input(user_question)
163
- st.session_state.document_messages.append({"role": "assistant", "content": response})
164
- st.markdown(response)
165
- except Exception as e:
166
- st.session_state.document_messages.append(
167
- {"role": "assistant", "content": f"Oops! There was an error: {str(e)}"}
168
- )
169
- st.rerun()
170
-
171
- # Display past messages for non-image-based conversation
172
- if mode != "Question with Images" and mode != "Question with Documents":
173
- for message in st.session_state.messages:
174
- with st.chat_message(message["role"]):
175
- st.markdown(message["content"])
176
-
177
- if len(st.session_state.messages) < st.session_state.max_messages:
178
- if prompt := st.chat_input("Hôm nay bạn như thế nào?"):
179
- st.session_state.messages.append({"role": "user", "content": prompt})
180
- with st.chat_message("user"):
181
- st.markdown(prompt)
182
- model = genai.GenerativeModel(model_name="gemini-pro")
183
- with st.chat_message("assistant"):
184
- try:
185
- prompt_parts = [prompt]
186
- response = model.generate_content(prompt_parts)
187
- st.session_state.messages.append({"role": "assistant", "content": response.text})
188
- st.markdown(response.text)
189
- except Exception as e:
190
- st.session_state.max_messages = len(st.session_state.messages)
191
- st.session_state.messages.append(
192
- {"role": "assistant", "content": f"Oops! There was an error: {str(e)}"}
193
- )
194
- st.rerun()
 
 
 
 
 
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()