Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,44 +1,95 @@
|
|
| 1 |
import os
|
| 2 |
import chainlit as cl
|
| 3 |
-
from langchain_community.vectorstores import FAISS
|
| 4 |
-
from langchain_community.embeddings import HuggingFaceEmbeddings
|
| 5 |
-
from langchain_groq import ChatGroq
|
| 6 |
from langchain_community.document_loaders import PyPDFLoader
|
| 7 |
-
from
|
| 8 |
-
from langchain.
|
| 9 |
-
from
|
| 10 |
-
from
|
|
|
|
|
|
|
| 11 |
|
| 12 |
-
|
|
|
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
pages = loader.load_and_split()
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
documents = text_splitter.split_documents(pages)
|
| 23 |
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
vectorstore = FAISS.from_documents(documents, embeddings)
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
| 36 |
)
|
| 37 |
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
|
|
|
|
| 1 |
import os
|
| 2 |
import chainlit as cl
|
|
|
|
|
|
|
|
|
|
| 3 |
from langchain_community.document_loaders import PyPDFLoader
|
| 4 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 5 |
+
from langchain.vectorstores import FAISS
|
| 6 |
+
from langchain.embeddings import HuggingFaceEmbeddings
|
| 7 |
+
from langchain.prompts import PromptTemplate
|
| 8 |
+
from langchain.llms import HuggingFaceEndpoint
|
| 9 |
+
from langchain.chains import RetrievalQA
|
| 10 |
|
| 11 |
+
# Global variable to store the QA chain
|
| 12 |
+
qa_chain = None
|
| 13 |
|
| 14 |
+
# Load and process the PDF file
|
| 15 |
+
def process_file(file_path):
|
| 16 |
+
loader = PyPDFLoader(file_path)
|
| 17 |
+
documents = loader.load()
|
|
|
|
| 18 |
|
| 19 |
+
text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=50)
|
| 20 |
+
texts = text_splitter.split_documents(documents)
|
|
|
|
| 21 |
|
| 22 |
+
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
|
| 23 |
+
vectorstore = FAISS.from_documents(texts, embeddings)
|
|
|
|
| 24 |
|
| 25 |
+
custom_prompt_template = """
|
| 26 |
+
Use the following pieces of context to answer the question at the end. If you don't know the answer, just say that you don't know, don't try to make up an answer.
|
| 27 |
+
|
| 28 |
+
{context}
|
| 29 |
+
|
| 30 |
+
Question: {question}
|
| 31 |
+
|
| 32 |
+
Helpful Answer:
|
| 33 |
+
"""
|
| 34 |
+
prompt = PromptTemplate(
|
| 35 |
+
template=custom_prompt_template,
|
| 36 |
+
input_variables=["context", "question"]
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
llm = HuggingFaceEndpoint(
|
| 40 |
+
repo_id="meta-llama/Meta-Llama-3-8B-Instruct",
|
| 41 |
+
temperature=0.5,
|
| 42 |
+
max_new_tokens=500,
|
| 43 |
+
top_k=50,
|
| 44 |
+
huggingfacehub_api_token=os.environ.get("HUGGINGFACEHUB_API_TOKEN")
|
| 45 |
+
)
|
| 46 |
|
| 47 |
+
qa = RetrievalQA.from_chain_type(
|
| 48 |
+
llm=llm,
|
| 49 |
+
chain_type="stuff",
|
| 50 |
+
retriever=vectorstore.as_retriever(search_kwargs={"k": 3}),
|
| 51 |
+
chain_type_kwargs={"prompt": prompt},
|
| 52 |
+
return_source_documents=True
|
| 53 |
)
|
| 54 |
|
| 55 |
+
return qa
|
| 56 |
+
|
| 57 |
+
# Chainlit on_chat_start event
|
| 58 |
+
@cl.on_chat_start
|
| 59 |
+
def start():
|
| 60 |
+
global qa_chain
|
| 61 |
+
qa_chain = None
|
| 62 |
+
cl.user_session.set("ready", False)
|
| 63 |
+
cl.user_session.set("file_name", "")
|
| 64 |
+
cl.user_session.set("qa_chain", None)
|
| 65 |
+
cl.Message(content="👋 Welcome! Upload a PDF file to get started.").send()
|
| 66 |
|
| 67 |
+
# Chainlit file upload handler
|
| 68 |
+
@cl.on_file_upload
|
| 69 |
+
def on_file_upload(file):
|
| 70 |
+
global qa_chain
|
| 71 |
+
file_path = file.path
|
| 72 |
+
qa_chain = process_file(file_path)
|
| 73 |
+
cl.user_session.set("qa_chain", qa_chain)
|
| 74 |
+
cl.user_session.set("file_name", file.name)
|
| 75 |
+
cl.user_session.set("ready", True)
|
| 76 |
+
cl.Message(content=f"✅ File `{file.name}` uploaded and processed. You can now ask questions.").send()
|
| 77 |
+
|
| 78 |
+
# Chainlit message handler
|
| 79 |
+
@cl.on_message
|
| 80 |
+
def handle_message(message: cl.Message):
|
| 81 |
+
if not cl.user_session.get("ready"):
|
| 82 |
+
cl.Message(content="⚠️ Please upload a PDF file before asking questions.").send()
|
| 83 |
+
return
|
| 84 |
+
|
| 85 |
+
qa_chain = cl.user_session.get("qa_chain")
|
| 86 |
+
if qa_chain is None:
|
| 87 |
+
cl.Message(content="⚠️ Something went wrong. Please upload the PDF again.").send()
|
| 88 |
+
return
|
| 89 |
+
|
| 90 |
+
query = message.content
|
| 91 |
+
response = qa_chain(query)
|
| 92 |
+
answer = response["result"]
|
| 93 |
+
cl.Message(content=answer).send()
|
| 94 |
|
| 95 |
|