Spaces:
Running
Running
danishjameel003
commited on
Delete WRONG_Question.txt
Browse files- WRONG_Question.txt +0 -130
WRONG_Question.txt
DELETED
@@ -1,130 +0,0 @@
|
|
1 |
-
# Importing dependencies
|
2 |
-
from dotenv import load_dotenv
|
3 |
-
import streamlit as st
|
4 |
-
from PyPDF2 import PdfReader
|
5 |
-
from langchain.text_splitter import CharacterTextSplitter
|
6 |
-
from langchain.embeddings import HuggingFaceEmbeddings
|
7 |
-
from langchain.vectorstores import FAISS
|
8 |
-
from langchain.prompts import PromptTemplate
|
9 |
-
from langchain.memory import ConversationBufferMemory
|
10 |
-
from langchain.chains import ConversationalRetrievalChain
|
11 |
-
from transformers import pipeline, AutoModelForCausalLM, AutoTokenizer
|
12 |
-
from htmlTemplates import css, bot_template, user_template
|
13 |
-
|
14 |
-
# Load environment variables
|
15 |
-
load_dotenv()
|
16 |
-
|
17 |
-
# Creating custom template to guide LLM model
|
18 |
-
custom_template = """Given the following conversation and a follow-up question, rephrase the follow-up question to be a standalone question, in its original language.
|
19 |
-
Chat History:
|
20 |
-
{chat_history}
|
21 |
-
Follow Up Input: {question}
|
22 |
-
Standalone question:"""
|
23 |
-
|
24 |
-
CUSTOM_QUESTION_PROMPT = PromptTemplate.from_template(custom_template)
|
25 |
-
|
26 |
-
# Extracting text from PDF
|
27 |
-
def get_pdf_text(docs):
|
28 |
-
text = ""
|
29 |
-
for pdf in docs:
|
30 |
-
pdf_reader = PdfReader(pdf)
|
31 |
-
for page in pdf_reader.pages:
|
32 |
-
text += page.extract_text()
|
33 |
-
return text
|
34 |
-
|
35 |
-
# Converting text to chunks
|
36 |
-
def get_chunks(raw_text):
|
37 |
-
text_splitter = CharacterTextSplitter(
|
38 |
-
separator="\n",
|
39 |
-
chunk_size=1000,
|
40 |
-
chunk_overlap=200,
|
41 |
-
length_function=len
|
42 |
-
)
|
43 |
-
chunks = text_splitter.split_text(raw_text)
|
44 |
-
return chunks
|
45 |
-
|
46 |
-
# Using Hugging Face embeddings model and FAISS to create vectorstore
|
47 |
-
def get_vectorstore(chunks):
|
48 |
-
embeddings = HuggingFaceEmbeddings(
|
49 |
-
model_name="sentence-transformers/all-MiniLM-L6-v2",
|
50 |
-
model_kwargs={'device': 'cpu'}
|
51 |
-
)
|
52 |
-
vectorstore = FAISS.from_texts(texts=chunks, embedding=embeddings)
|
53 |
-
return vectorstore
|
54 |
-
|
55 |
-
# Generating conversation chain
|
56 |
-
def get_conversationchain(vectorstore):
|
57 |
-
# Use a Hugging Face model for question-answering
|
58 |
-
model_name = "distilbert-base-uncased-distilled-squad" # Pretrained QA model
|
59 |
-
qa_pipeline = pipeline("question-answering", model=model_name, tokenizer=model_name)
|
60 |
-
|
61 |
-
def qa_function(question, context):
|
62 |
-
response = qa_pipeline(question=question, context=context)
|
63 |
-
return response['answer']
|
64 |
-
|
65 |
-
memory = ConversationBufferMemory(
|
66 |
-
memory_key='chat_history',
|
67 |
-
return_messages=True,
|
68 |
-
output_key='answer'
|
69 |
-
)
|
70 |
-
|
71 |
-
def conversation_chain(inputs):
|
72 |
-
question = inputs['question']
|
73 |
-
# Extract text content from Document objects
|
74 |
-
documents = vectorstore.similarity_search(question, k=5)
|
75 |
-
if not documents:
|
76 |
-
answer = "Sorry, I couldn't find relevant information in the document. Please ask a question related to the document."
|
77 |
-
memory.save_context({"user_input": question}, {"answer": answer})
|
78 |
-
return {"chat_history": memory.chat_memory.messages, "answer": answer}
|
79 |
-
|
80 |
-
context = "\n".join([doc.page_content for doc in documents]) # Extract `page_content` from each Document
|
81 |
-
answer = qa_function(question, context)
|
82 |
-
memory.save_context({"user_input": question}, {"answer": answer})
|
83 |
-
return {"chat_history": memory.chat_memory.messages, "answer": answer}
|
84 |
-
|
85 |
-
return conversation_chain
|
86 |
-
|
87 |
-
# Generating response from user queries and displaying them accordingly
|
88 |
-
def handle_question(question):
|
89 |
-
response = st.session_state.conversation({'question': question})
|
90 |
-
st.session_state.chat_history = response["chat_history"]
|
91 |
-
for i, msg in enumerate(st.session_state.chat_history):
|
92 |
-
if i % 2 == 0:
|
93 |
-
st.write(user_template.replace("{{MSG}}", msg.content), unsafe_allow_html=True)
|
94 |
-
else:
|
95 |
-
st.write(bot_template.replace("{{MSG}}", msg.content), unsafe_allow_html=True)
|
96 |
-
|
97 |
-
def main():
|
98 |
-
st.set_page_config(page_title="Chat with multiple PDFs", page_icon=":books:")
|
99 |
-
st.write(css, unsafe_allow_html=True)
|
100 |
-
|
101 |
-
if "conversation" not in st.session_state:
|
102 |
-
st.session_state.conversation = None
|
103 |
-
|
104 |
-
if "chat_history" not in st.session_state:
|
105 |
-
st.session_state.chat_history = None
|
106 |
-
|
107 |
-
st.header("CSS Edge - Intelligent Document Chatbot :books:")
|
108 |
-
question = st.text_input("Ask a question from your document:")
|
109 |
-
if question:
|
110 |
-
handle_question(question)
|
111 |
-
|
112 |
-
with st.sidebar:
|
113 |
-
st.subheader("Your documents")
|
114 |
-
docs = st.file_uploader("Upload your PDF here and click on 'Process'", accept_multiple_files=True)
|
115 |
-
if st.button("Process"):
|
116 |
-
with st.spinner("Processing..."):
|
117 |
-
# Get the PDF text
|
118 |
-
raw_text = get_pdf_text(docs)
|
119 |
-
|
120 |
-
# Get the text chunks
|
121 |
-
text_chunks = get_chunks(raw_text)
|
122 |
-
|
123 |
-
# Create vectorstore
|
124 |
-
vectorstore = get_vectorstore(text_chunks)
|
125 |
-
|
126 |
-
# Create conversation chain
|
127 |
-
st.session_state.conversation = get_conversationchain(vectorstore)
|
128 |
-
|
129 |
-
if __name__ == '__main__':
|
130 |
-
main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|