drkareemkamal commited on
Commit
ef5c8bf
1 Parent(s): a505b98

Delete app.py

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