drkareemkamal commited on
Commit
0669835
1 Parent(s): aa24272

Create app.py

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