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itter import RecursiveCharacterTextSplitter
|
| 5 |
+
from langchain_community.vectorstores import FAISS
|
| 6 |
+
from langchain.chains import ConversationalRetrievalChain
|
| 7 |
+
from langchain_community.embeddings import HuggingFaceEmbeddings
|
| 8 |
+
from langchain.memory import ConversationBufferMemory
|
| 9 |
+
from langchain_community.llms import HuggingFaceEndpoint
|
| 10 |
+
from langchain_community.document_loaders import PyPDFLoader
|
| 11 |
+
|
| 12 |
+
def load_doc(list_file_path):
|
| 13 |
+
loaders = [PyPDFLoader(x) for x in list_file_path]
|
| 14 |
+
pages = []
|
| 15 |
+
for loader in loaders:
|
| 16 |
+
pages.extend(loader.load())
|
| 17 |
+
text_splitter = RecursiveCharacterTextSplitter(
|
| 18 |
+
chunk_size=1024, chunk_overlap=64
|
| 19 |
+
)
|
| 20 |
+
doc_splits = text_splitter.split_documents(pages)
|
| 21 |
+
return doc_splits
|
| 22 |
+
|
| 23 |
+
def create_db(splits):
|
| 24 |
+
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
|
| 25 |
+
vectordb = FAISS.from_documents(splits, embeddings)
|
| 26 |
+
return vectordb
|
| 27 |
+
|
| 28 |
+
def initialize_chatbot(vector_db):
|
| 29 |
+
memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
|
| 30 |
+
retriever = vector_db.as_retriever()
|
| 31 |
+
llm = HuggingFaceEndpoint(
|
| 32 |
+
repo_id="mistralai/Mistral-7B-Instruct-v0.2",
|
| 33 |
+
huggingfacehub_api_token=os.environ.get("HUGGINGFACE_API_TOKEN"),
|
| 34 |
+
temperature=0.5,
|
| 35 |
+
max_new_tokens=512,
|
| 36 |
+
task="text-generation" # Explicitly specify the task type
|
| 37 |
+
)
|
| 38 |
+
qa_chain = ConversationalRetrievalChain.from_llm(
|
| 39 |
+
llm=llm,
|
| 40 |
+
retriever=retriever,
|
| 41 |
+
memory=memory,
|
| 42 |
+
verbose=False
|
| 43 |
+
)
|
| 44 |
+
return qa_chain
|
| 45 |
+
|
| 46 |
+
def process_and_initialize(files):
|
| 47 |
+
if not files:
|
| 48 |
+
return None, None, "Please upload a file first."
|
| 49 |
+
|
| 50 |
+
try:
|
| 51 |
+
list_file_path = [file.name for file in files if file is not None]
|
| 52 |
+
doc_splits = load_doc(list_file_path)
|
| 53 |
+
db = create_db(doc_splits)
|
| 54 |
+
qa = initialize_chatbot(db)
|
| 55 |
+
return db, qa, "Database created! Ready for questions."
|
| 56 |
+
except Exception as e:
|
| 57 |
+
return None, None, f"Processing error: {str(e)}"
|
| 58 |
+
|
| 59 |
+
def user_query_typing_effect(query, qa_chain, chatbot):
|
| 60 |
+
history = chatbot or []
|
| 61 |
+
try:
|
| 62 |
+
response = qa_chain.invoke({"question": query, "chat_history": []})
|
| 63 |
+
assistant_response = response["answer"]
|
| 64 |
+
history.append({"role": "user", "content": query})
|
| 65 |
+
history.append({"role": "assistant", "content": ""})
|
| 66 |
+
for i in range(len(assistant_response)):
|
| 67 |
+
history[-1]["content"] += assistant_response[i]
|
| 68 |
+
yield history, ""
|
| 69 |
+
time.sleep(0.03)
|
| 70 |
+
except Exception as e:
|
| 71 |
+
history.append({"role": "assistant", "content": f"Error: {str(e)}"})
|
| 72 |
+
yield history, ""
|
| 73 |
+
|
| 74 |
+
def demo():
|
| 75 |
+
custom_css = """
|
| 76 |
+
body {
|
| 77 |
+
background-color: #FF8C00;
|
| 78 |
+
font-family: Arial, sans-serif;
|
| 79 |
+
}
|
| 80 |
+
.gradio-container {
|
| 81 |
+
border-radius: 15px;
|
| 82 |
+
box-shadow: 0px 4px 20px rgba(0, 0, 0, 0.3);
|
| 83 |
+
padding: 20px;
|
| 84 |
+
}
|
| 85 |
+
footer {
|
| 86 |
+
visibility: hidden;
|
| 87 |
+
}
|
| 88 |
+
.chatbot {
|
| 89 |
+
border: 2px solid #000;
|
| 90 |
+
border-radius: 10px;
|
| 91 |
+
background-color: #FFF5E1;
|
| 92 |
+
}
|
| 93 |
+
"""
|
| 94 |
+
with gr.Blocks(css=custom_css) as app:
|
| 95 |
+
vector_db = gr.State()
|
| 96 |
+
qa_chain = gr.State()
|
| 97 |
+
gr.Markdown("### π **PDF & TXT E-Book** π")
|
| 98 |
+
gr.Markdown("#### Upload your document and ask questions interactively!")
|
| 99 |
+
with gr.Row():
|
| 100 |
+
with gr.Column(scale=1):
|
| 101 |
+
txt_file = gr.Files(
|
| 102 |
+
label="π Upload Documents",
|
| 103 |
+
file_types=[".txt", ".pdf"],
|
| 104 |
+
type="filepath"
|
| 105 |
+
)
|
| 106 |
+
analyze_btn = gr.Button("π Process Documents")
|
| 107 |
+
status = gr.Textbox(
|
| 108 |
+
label="π Status",
|
| 109 |
+
placeholder="Status updates will appear here...",
|
| 110 |
+
interactive=False
|
| 111 |
+
)
|
| 112 |
+
with gr.Column(scale=3):
|
| 113 |
+
chatbot = gr.Chatbot(
|
| 114 |
+
label="π€ Chat with your data",
|
| 115 |
+
height=600,
|
| 116 |
+
bubble_full_width=False,
|
| 117 |
+
show_label=False,
|
| 118 |
+
render_markdown=True,
|
| 119 |
+
type="messages",
|
| 120 |
+
elem_classes=["chatbot"]
|
| 121 |
+
)
|
| 122 |
+
query_input = gr.Textbox(
|
| 123 |
+
label="Ask a question",
|
| 124 |
+
placeholder="Ask about the document...",
|
| 125 |
+
show_label=False,
|
| 126 |
+
container=False
|
| 127 |
+
)
|
| 128 |
+
query_btn = gr.Button("Ask")
|
| 129 |
+
analyze_btn.click(
|
| 130 |
+
fn=process_and_initialize,
|
| 131 |
+
inputs=[txt_file],
|
| 132 |
+
outputs=[vector_db, qa_chain, status],
|
| 133 |
+
show_progress="minimal"
|
| 134 |
+
)
|
| 135 |
+
query_btn.click(
|
| 136 |
+
fn=user_query_typing_effect,
|
| 137 |
+
inputs=[query_input, qa_chain, chatbot],
|
| 138 |
+
outputs=[chatbot, query_input],
|
| 139 |
+
show_progress="minimal"
|
| 140 |
+
)
|
| 141 |
+
query_input.submit(
|
| 142 |
+
fn=user_query_typing_effect,
|
| 143 |
+
inputs=[query_input, qa_chain, chatbot],
|
| 144 |
+
outputs=[chatbot, query_input],
|
| 145 |
+
show_progress="minimal"
|
| 146 |
+
)
|
| 147 |
+
app.launch()
|
| 148 |
+
|
| 149 |
+
if __name__ == "__main__":
|
| 150 |
+
demo()
|