Spaces:
Runtime error
Runtime error
Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
from langchain_community.document_loaders import PyPDFLoader
|
| 4 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 5 |
+
from langchain.embeddings import HuggingFaceEmbeddings
|
| 6 |
+
from langchain.chat_models import ChatOpenAI
|
| 7 |
+
|
| 8 |
+
from langchain.retrievers.document_compressors import LLMChainExtractor
|
| 9 |
+
from langchain.retrievers.multi_query import MultiQueryRetriever
|
| 10 |
+
from langchain.retrievers import ContextualCompressionRetriever
|
| 11 |
+
from langchain.prompts.chat import ChatPromptTemplate, HumanMessagePromptTemplate
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
from langchain.vectorstores import Chroma
|
| 16 |
+
|
| 17 |
+
with open('../../openai_api_key.txt') as f:
|
| 18 |
+
api_key = f.read()
|
| 19 |
+
os.environ['OPENAI_API_KEY'] = api_key
|
| 20 |
+
|
| 21 |
+
chat = ChatOpenAI()
|
| 22 |
+
|
| 23 |
+
embedding_function = HuggingFaceEmbeddings(model_name = "BAAI/bge-large-en-v1.5",model_kwargs={'device': 'cpu'},encode_kwargs={"normalize_embeddings": True})
|
| 24 |
+
|
| 25 |
+
def add_docs(path):
|
| 26 |
+
|
| 27 |
+
loader = PyPDFLoader(file_path=path)
|
| 28 |
+
docs = loader.load_and_split(text_splitter=RecursiveCharacterTextSplitter(chunk_size = 500,
|
| 29 |
+
chunk_overlap = 100,
|
| 30 |
+
length_function = len,
|
| 31 |
+
is_separator_regex=False))
|
| 32 |
+
model_vectorstore = Chroma
|
| 33 |
+
db = model_vectorstore.from_documents(documents=docs,embedding= embedding_function, persist_directory="output/general_knowledge")
|
| 34 |
+
return db
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
def answer_query(message, chat_history):
|
| 38 |
+
base_compressor = LLMChainExtractor.from_llm(chat)
|
| 39 |
+
db = Chroma(persist_directory = "output/general_knowledge", embedding_function=embedding_function)
|
| 40 |
+
base_retriever = db.as_retriever()
|
| 41 |
+
mq_retriever = MultiQueryRetriever.from_llm(retriever = base_retriever, llm=chat)
|
| 42 |
+
compression_retriever = ContextualCompressionRetriever(base_compressor=base_compressor, base_retriever=mq_retriever)
|
| 43 |
+
|
| 44 |
+
matched_docs = compression_retriever.get_relevant_documents(query = message)
|
| 45 |
+
|
| 46 |
+
context = ""
|
| 47 |
+
|
| 48 |
+
for doc in matched_docs:
|
| 49 |
+
page_content = doc.page_content
|
| 50 |
+
context+=page_content
|
| 51 |
+
context += "\n\n"
|
| 52 |
+
template = """
|
| 53 |
+
Answer the following question only by using the context given below in the triple backticks, do not use any other information to answer the question.
|
| 54 |
+
If you can't answer the given question with the given context, you can return an emtpy string ('')
|
| 55 |
+
|
| 56 |
+
Context: ```{context}```
|
| 57 |
+
----------------------------
|
| 58 |
+
Question: {query}
|
| 59 |
+
----------------------------
|
| 60 |
+
Answer: """
|
| 61 |
+
|
| 62 |
+
human_message_prompt = HumanMessagePromptTemplate.from_template(template=template)
|
| 63 |
+
chat_prompt = ChatPromptTemplate.from_messages([human_message_prompt])
|
| 64 |
+
prompt = chat_prompt.format_prompt(query = message, context = context)
|
| 65 |
+
response = chat(messages=prompt.to_messages()).content
|
| 66 |
+
|
| 67 |
+
chat_history.append((message,response))
|
| 68 |
+
return "", chat_history
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
with gr.Blocks() as demo:
|
| 73 |
+
gr.HTML("<h1 align = 'center'>Smart Assistant</h1>")
|
| 74 |
+
|
| 75 |
+
with gr.Row():
|
| 76 |
+
|
| 77 |
+
upload_files = gr.File(label = 'Upload a PDF',file_types=['.pdf'],file_count='single')
|
| 78 |
+
|
| 79 |
+
chatbot = gr.Chatbot()
|
| 80 |
+
msg = gr.Textbox(label = "Enter your question here")
|
| 81 |
+
upload_files.upload(add_docs,upload_files)
|
| 82 |
+
msg.submit(answer_query,[msg,chatbot],[msg,chatbot])
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
if __name__ == "__main__":
|
| 86 |
+
demo.launch(share = True)
|