drkareemkamal commited on
Commit
118f704
·
verified ·
1 Parent(s): 21cc216

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +77 -0
app.py ADDED
@@ -0,0 +1,77 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # import libraries
2
+ import os
3
+ from dotenv import load_dotenv
4
+
5
+ import pinecone
6
+ from langchain.document_loaders import PyPDFDirectoryLoader
7
+ from langchain.text_splitter import RecursiveCharacterTextSplitter
8
+ from langchain.embeddings.openai import OpenAIEmbeddings
9
+ from langchain_pinecone import PineconeVectorStore
10
+ from langchain.prompts import PromptTemplate
11
+ from langchain.chains.question_answering import load_qa_chain
12
+ from ctransformers import CTransformers
13
+
14
+ load_dotenv()
15
+
16
+ embeddings = HuggingFaceBgeEmbeddings(model_name = 'sentence-transformers/all-MiniLM-L6-v2',
17
+ model_kwargs = {'device':'cpu'})
18
+
19
+ os.environ['PINECONE_API_KEY'] = 'afb0bb4d-3c15-461b-91a4-fb12fb1f25f2'
20
+ index_name = 'harisonvecot'
21
+
22
+ vectorstore = PineconeVectorStore(index_name=index_name,embedding=embeddings)
23
+
24
+ # Create the vector index from documents
25
+ def create_index(documents):
26
+ vectorstore.add_documents(documents)
27
+
28
+ # Retrieve query from Pinecone
29
+ def retrieve_query(query, k=2):
30
+ matching_results = vectorstore.similarity_search(query, k=k)
31
+ return matching_results
32
+
33
+ # Custom prompt template
34
+ custom_prompt_template = '''
35
+ use the following pieces of information to answer the user's questions.
36
+ If you don't know the answer, please just say that you don't know the answer, don't try to make up an answer.
37
+ Content : {context}
38
+ Question : {question}
39
+ only return the helpful answer below and nothing else.
40
+ '''
41
+
42
+ def set_custom_prompt():
43
+ prompt = PromptTemplate(template=custom_prompt_template, input_variables=['context', 'question'])
44
+ return prompt
45
+
46
+ # Load LLM model
47
+ llm_model = CTransformers(model_name='TheBloke/Llama-2-7B-Chat-GGML',
48
+ model_type = 'llama',
49
+ max_new_token = 512,
50
+ temperature=0.5)
51
+
52
+ # Create retrieval QA chain
53
+ def retrieval_qa_chain():
54
+ prompt = set_custom_prompt()
55
+ chain = load_qa_chain(llm_model, chain_type='stuff', prompt=prompt)
56
+ return chain
57
+
58
+ # Search answers from Vector DB
59
+ def retrieve_answer(query):
60
+ doc_search = retrieve_query(query)
61
+ chain = retrieval_qa_chain()
62
+ response = chain.run(input_documents=doc_search, question=query)
63
+ return response
64
+
65
+ queries = st.text_input('write a medical questions ?')
66
+ # Example usage
67
+ submit = st.button('submit')
68
+ # Read and process documents
69
+ # doc = read_doc('documents/')
70
+ # documents = chunk_data(docs=doc)
71
+ # create_index(documents)
72
+ if submit :
73
+ if queries :
74
+ # Query and get answer
75
+ #our_query = 'What is cause of Eczema?'
76
+ answer = retrieve_answer(queries)
77
+ st.write(answer)