Spaces:
Sleeping
Sleeping
drkareemkamal
commited on
Commit
•
9e8cdbb
1
Parent(s):
98a9ebf
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# import libraries
|
2 |
+
import os
|
3 |
+
from dotenv import load_dotenv
|
4 |
+
import streamlit as st
|
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 langchain_community.llms import CTransformers
|
13 |
+
from langchain_community.embeddings.huggingface import HuggingFaceBgeEmbeddings
|
14 |
+
|
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 |
+
Content : {context}
|
40 |
+
Question : {question}
|
41 |
+
only return the helpful answer below and nothing else.
|
42 |
+
'''
|
43 |
+
|
44 |
+
def set_custom_prompt():
|
45 |
+
prompt = PromptTemplate(template=custom_prompt_template, input_variables=['context', 'question'])
|
46 |
+
return prompt
|
47 |
+
|
48 |
+
# Load LLM model
|
49 |
+
llm_model = CTransformers(model='TheBloke/Llama-2-7B-Chat-GGML',
|
50 |
+
model_type = 'llama',
|
51 |
+
max_new_token = 512,
|
52 |
+
temperature=0.5)
|
53 |
+
|
54 |
+
# Create retrieval QA chain
|
55 |
+
def retrieval_qa_chain():
|
56 |
+
prompt = set_custom_prompt()
|
57 |
+
chain = load_qa_chain(llm_model, chain_type='stuff', prompt=prompt)
|
58 |
+
return chain
|
59 |
+
|
60 |
+
# Search answers from Vector DB
|
61 |
+
def retrieve_answer(query):
|
62 |
+
doc_search = retrieve_query(query)
|
63 |
+
chain = retrieval_qa_chain()
|
64 |
+
response = chain.run(input_documents=doc_search, question=query)
|
65 |
+
return response
|
66 |
+
|
67 |
+
queries = st.text_input('write a medical questions ?')
|
68 |
+
# Example usage
|
69 |
+
submit = st.button('submit')
|
70 |
+
# Read and process documents
|
71 |
+
# doc = read_doc('documents/')
|
72 |
+
# documents = chunk_data(docs=doc)
|
73 |
+
# create_index(documents)
|
74 |
+
if submit :
|
75 |
+
if queries :
|
76 |
+
# Query and get answer
|
77 |
+
#our_query = 'What is cause of Eczema?'
|
78 |
+
answer = retrieve_answer(queries)
|
79 |
+
st.write(answer)
|