Spaces:
				
			
			
	
			
			
		Sleeping
		
	
	
	
			
			
	
	
	
	
		
		
		Sleeping
		
	Create app.py
Browse files
    	
        app.py
    ADDED
    
    | @@ -0,0 +1,115 @@ | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | 
|  | |
| 1 | 
            +
            from langchain_core.prompts import PromptTemplate
         | 
| 2 | 
            +
            import os 
         | 
| 3 | 
            +
            from langchain_community.embeddings import HuggingFaceBgeEmbeddings
         | 
| 4 | 
            +
            from langchain_community.vectorstores import FAISS
         | 
| 5 | 
            +
            from langchain_community.llms.ctransformers import CTransformers
         | 
| 6 | 
            +
            from langchain.chains.retrieval_qa.base import RetrievalQA
         | 
| 7 | 
            +
            import streamlit as st
         | 
| 8 | 
            +
            import fitz  # PyMuPDF
         | 
| 9 | 
            +
            from PIL import Image
         | 
| 10 | 
            +
            import io
         | 
| 11 | 
            +
             | 
| 12 | 
            +
            DB_FAISS_PATH = 'vectorstores/'
         | 
| 13 | 
            +
            pdf_path = 'data/Gale_encyclopedia_of_medicine_vol_1.pdf'
         | 
| 14 | 
            +
             | 
| 15 | 
            +
            custom_prompt_template = '''use the following pieces of information to answer the user's questions.
         | 
| 16 | 
            +
            If you don't know the answer, please just say that don't know the answer, don't try to make uo an answer.
         | 
| 17 | 
            +
            Context : {context}
         | 
| 18 | 
            +
            Question : {question}
         | 
| 19 | 
            +
            only return the helpful answer below and nothing else.
         | 
| 20 | 
            +
            '''
         | 
| 21 | 
            +
             | 
| 22 | 
            +
            def set_custom_prompt():
         | 
| 23 | 
            +
                """
         | 
| 24 | 
            +
                Prompt template for QA retrieval for vector stores
         | 
| 25 | 
            +
                """
         | 
| 26 | 
            +
                prompt = PromptTemplate(template = custom_prompt_template,
         | 
| 27 | 
            +
                                        input_variables = ['context','question'])
         | 
| 28 | 
            +
                
         | 
| 29 | 
            +
                return prompt
         | 
| 30 | 
            +
                
         | 
| 31 | 
            +
             | 
| 32 | 
            +
            def load_llm():
         | 
| 33 | 
            +
                llm = CTransformers(
         | 
| 34 | 
            +
                    model = 'TheBloke/Llama-2-7B-Chat-GGML',
         | 
| 35 | 
            +
                    model_type = 'llama',
         | 
| 36 | 
            +
                    max_new_token = 512,
         | 
| 37 | 
            +
                    temperature = 0.5
         | 
| 38 | 
            +
                )
         | 
| 39 | 
            +
                return llm
         | 
| 40 | 
            +
             | 
| 41 | 
            +
            def retrieval_qa_chain(llm,prompt,db):
         | 
| 42 | 
            +
                qa_chain = RetrievalQA.from_chain_type(
         | 
| 43 | 
            +
                    llm = llm,
         | 
| 44 | 
            +
                    chain_type = 'stuff',
         | 
| 45 | 
            +
                    retriever = db.as_retriever(search_kwargs= {'k': 2}),
         | 
| 46 | 
            +
                    return_source_documents = True,
         | 
| 47 | 
            +
                    chain_type_kwargs = {'prompt': prompt}
         | 
| 48 | 
            +
                )
         | 
| 49 | 
            +
             | 
| 50 | 
            +
                return qa_chain
         | 
| 51 | 
            +
             | 
| 52 | 
            +
            def qa_bot():
         | 
| 53 | 
            +
                embeddings = HuggingFaceBgeEmbeddings(model_name = 'sentence-transformers/all-MiniLM-L6-v2',
         | 
| 54 | 
            +
                                                      model_kwargs = {'device':'cpu'})
         | 
| 55 | 
            +
                
         | 
| 56 | 
            +
                
         | 
| 57 | 
            +
                db = FAISS.load_local(DB_FAISS_PATH, embeddings, allow_dangerous_deserialization=True)
         | 
| 58 | 
            +
                llm = load_llm()
         | 
| 59 | 
            +
                qa_prompt = set_custom_prompt()
         | 
| 60 | 
            +
                qa = retrieval_qa_chain(llm,qa_prompt, db)
         | 
| 61 | 
            +
             | 
| 62 | 
            +
                return qa
         | 
| 63 | 
            +
             | 
| 64 | 
            +
            def final_result(query):
         | 
| 65 | 
            +
                qa_result = qa_bot()
         | 
| 66 | 
            +
                response = qa_result({'query' : query})
         | 
| 67 | 
            +
             | 
| 68 | 
            +
                return response
         | 
| 69 | 
            +
             | 
| 70 | 
            +
            def get_pdf_page_as_image(pdf_path, page_number):
         | 
| 71 | 
            +
                document = fitz.open(pdf_path)
         | 
| 72 | 
            +
                page = document.load_page(page_number)  
         | 
| 73 | 
            +
                pix = page.get_pixmap()
         | 
| 74 | 
            +
                img = Image.open(io.BytesIO(pix.tobytes()))
         | 
| 75 | 
            +
                return img
         | 
| 76 | 
            +
             | 
| 77 | 
            +
            # Streamlit webpage title
         | 
| 78 | 
            +
            st.title('Medical Chatbot')
         | 
| 79 | 
            +
             | 
| 80 | 
            +
            # User input
         | 
| 81 | 
            +
            user_query = st.text_input("Please enter your question:")
         | 
| 82 | 
            +
             | 
| 83 | 
            +
            # Button to get answer
         | 
| 84 | 
            +
            if st.button('Get Answer'):
         | 
| 85 | 
            +
                if user_query:
         | 
| 86 | 
            +
                    # Call the function from your chatbot script
         | 
| 87 | 
            +
                    response = final_result(user_query)
         | 
| 88 | 
            +
                    if response:
         | 
| 89 | 
            +
                        # Displaying the response
         | 
| 90 | 
            +
                        st.write("### Answer")
         | 
| 91 | 
            +
                        st.write(response['result'])
         | 
| 92 | 
            +
             | 
| 93 | 
            +
                        # Displaying source document details if available
         | 
| 94 | 
            +
                        if 'source_documents' in response:
         | 
| 95 | 
            +
                            st.write("### Source Document Information")
         | 
| 96 | 
            +
                            for doc in response['source_documents']:
         | 
| 97 | 
            +
                                # Retrieve and format page content by replacing '\n' with new line
         | 
| 98 | 
            +
                                formatted_content = doc.page_content.replace("\\n", "\n")
         | 
| 99 | 
            +
                                st.write("#### Document Content")
         | 
| 100 | 
            +
                                st.text_area(label="Page Content", value=formatted_content, height=300)
         | 
| 101 | 
            +
             | 
| 102 | 
            +
                                # Retrieve source and page from metadata
         | 
| 103 | 
            +
                                source = doc.metadata['source']
         | 
| 104 | 
            +
                                page = doc.metadata['page']
         | 
| 105 | 
            +
                                st.write(f"Source: {source}")
         | 
| 106 | 
            +
                                st.write(f"Page Number: {page+1}")
         | 
| 107 | 
            +
                                
         | 
| 108 | 
            +
                                # Display the PDF page as an image
         | 
| 109 | 
            +
                                pdf_page_image = get_pdf_page_as_image(pdf_path, page)
         | 
| 110 | 
            +
                                st.image(pdf_page_image, caption=f"Page {page+1} from {source}")
         | 
| 111 | 
            +
                                
         | 
| 112 | 
            +
                    else:
         | 
| 113 | 
            +
                        st.write("Sorry, I couldn't find an answer to your question.")
         | 
| 114 | 
            +
                else:
         | 
| 115 | 
            +
                    st.write("Please enter a question to get an answer.")
         |