File size: 3,260 Bytes
20e1dc2 32780ee 20e1dc2 dcf8d80 20e1dc2 32780ee 20e1dc2 32780ee 20e1dc2 32780ee 20e1dc2 32780ee 4027446 32780ee 4027446 32780ee dc8dce7 32780ee 20e1dc2 32780ee a0ce053 32780ee edd3984 32780ee edd3984 32780ee |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
from langchain.embeddings import HuggingFaceEmbeddings
from langchain.vectorstores import FAISS
from langchain import HuggingFaceHub
from langchain.chains import RetrievalQA
import streamlit as st
st.set_page_config(page_title = "Hospital Regulatory Chat", page_icon=":hospital:")
DB_FAISS_PATH = '.'
def get_vectorstore():
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2",
model_kwargs={'device': 'cpu'})
vector_store = FAISS.load_local(DB_FAISS_PATH, embeddings)
return vector_store
vector_store = get_vectorstore()
llm = HuggingFaceHub(repo_id = "meta-llama/Llama-2-7b-chat-hf",model_kwargs={"temperature":0.5}) #, "max_length":512})
qa_chain = RetrievalQA.from_chain_type(llm=llm,
chain_type='stuff',
retriever=vector_store.as_retriever(search_kwargs={'k': 10}),
#retriever=vector_store.as_retriever(search_kwargs={"score_threshold": .01}),
return_source_documents = True
)
source_dictionary = {"data\CMS_SOMA.pdf":"[CMS State Operations Manual Appendix A](https://www.cms.gov/regulations-and-guidance/guidance/manuals/downloads/som107ap_a_hospitals.pdf)",
"data\DOH-RCW.pdf":"[Revised Code of Washington (RCW) Chapter 70.41](https://app.leg.wa.gov/rcw/default.aspx?cite=70.41)",
"data\WAC 246-320.pdf":"[Washington Administrative Code (WAC) 246-320](https://app.leg.wa.gov/WAC/default.aspx?cite=246-320)"}
with st.container():
st.title("Hospital Regulation Chat")
with st.sidebar:
st.subheader("Find regulations for hospitals in the state of Washington.")
st.markdown("""
We look into three sources to find top ten most relevant excerpts:
- [CMS State Operations Manual Appendix A](https://www.cms.gov/regulations-and-guidance/guidance/manuals/downloads/som107ap_a_hospitals.pdf)
- [Revised Code of Washington (RCW) Chapter 70.41](https://app.leg.wa.gov/rcw/default.aspx?cite=70.41)
- [Washington Administrative Code (WAC) 246-320](https://app.leg.wa.gov/WAC/default.aspx?cite=246-320)
""") #, unsafe_allow_html=True)
st.write("This is tool is meant to assist healthcare workers to the extent it can. Please note that the page numbers may be occasionally slightly off, use the matching excerpts to find the reference if this happens.")
st.markdown("**Ask your question and :red[click 'Find excerpts'.]**")
prompt = st.text_input("e.g. What are the rules regarding a Quality Improvement, or QAPI program?")
if (st.button("Find excerpts")):
answer = qa_chain({"query":prompt})
n = len(answer['source_documents'])
for i in range(n):
with st.container():
st.subheader(source_dictionary[answer['source_documents'][i].metadata['source']])
page_no = "**Page: " + str(answer['source_documents'][i].metadata['page']) + "**"
st.markdown(page_no)
st.write("...")
st.write(answer['source_documents'][i].page_content)
st.write("...")
st.write('---------------------------------\n\n') |