Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,64 +1,85 @@
|
|
| 1 |
import os
|
| 2 |
import chainlit as cl
|
| 3 |
-
from langchain_community.vectorstores import FAISS
|
| 4 |
from langchain_community.document_loaders import PyPDFLoader
|
| 5 |
-
from
|
| 6 |
-
from
|
|
|
|
|
|
|
|
|
|
| 7 |
from langchain.chains import RetrievalQA
|
| 8 |
-
from langchain.chat_models import ChatOpenAI
|
| 9 |
-
|
| 10 |
-
# Initialize embedding model
|
| 11 |
-
embedding_model = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")
|
| 12 |
|
| 13 |
-
#
|
| 14 |
-
|
|
|
|
|
|
|
| 15 |
|
|
|
|
|
|
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
global retriever
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
|
|
|
| 31 |
)
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
|
|
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
-
|
| 45 |
-
uploaded_file = files[0]
|
| 46 |
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
|
| 52 |
-
#
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
|
|
|
|
|
|
| 57 |
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
| 63 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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_community.vectorstores import FAISS
|
| 6 |
+
from langchain_community.embeddings import HuggingFaceEmbeddings
|
| 7 |
+
from langchain.prompts import PromptTemplate
|
| 8 |
+
from langchain_community.llms import HuggingFaceEndpoint
|
| 9 |
from langchain.chains import RetrievalQA
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
+
# PDF processing
|
| 12 |
+
def process_file(file_path):
|
| 13 |
+
loader = PyPDFLoader(file_path)
|
| 14 |
+
documents = loader.load()
|
| 15 |
|
| 16 |
+
text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=50)
|
| 17 |
+
texts = text_splitter.split_documents(documents)
|
| 18 |
|
| 19 |
+
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
|
| 20 |
+
vectorstore = FAISS.from_documents(texts, embeddings)
|
|
|
|
| 21 |
|
| 22 |
+
custom_prompt_template = """
|
| 23 |
+
Use the following pieces of context to answer the question at the end.
|
| 24 |
+
If you don't know the answer, just say you don't know.
|
| 25 |
|
| 26 |
+
{context}
|
| 27 |
+
Question: {question}
|
| 28 |
+
Helpful Answer:
|
| 29 |
+
"""
|
| 30 |
+
prompt = PromptTemplate(
|
| 31 |
+
template=custom_prompt_template,
|
| 32 |
+
input_variables=["context", "question"]
|
| 33 |
)
|
| 34 |
|
| 35 |
+
llm = HuggingFaceEndpoint(
|
| 36 |
+
repo_id="meta-llama/Meta-Llama-3-8B-Instruct",
|
| 37 |
+
temperature=0.5,
|
| 38 |
+
max_new_tokens=500,
|
| 39 |
+
top_k=50,
|
| 40 |
+
huggingfacehub_api_token=os.environ.get("HUGGINGFACEHUB_API_TOKEN")
|
| 41 |
+
)
|
| 42 |
|
| 43 |
+
qa = RetrievalQA.from_chain_type(
|
| 44 |
+
llm=llm,
|
| 45 |
+
chain_type="stuff",
|
| 46 |
+
retriever=vectorstore.as_retriever(search_kwargs={"k": 3}),
|
| 47 |
+
chain_type_kwargs={"prompt": prompt},
|
| 48 |
+
return_source_documents=True
|
| 49 |
+
)
|
| 50 |
|
| 51 |
+
return qa
|
|
|
|
| 52 |
|
| 53 |
+
# Chat start
|
| 54 |
+
@cl.on_chat_start
|
| 55 |
+
async def start():
|
| 56 |
+
await cl.Message("π Welcome! Please upload a PDF to get started.").send()
|
| 57 |
|
| 58 |
+
# Let user upload file
|
| 59 |
+
msg = await cl.AskFileMessage(
|
| 60 |
+
content="π Upload your PDF file here:",
|
| 61 |
+
accept=["application/pdf"],
|
| 62 |
+
max_size_mb=20,
|
| 63 |
+
timeout=180
|
| 64 |
+
).send()
|
| 65 |
|
| 66 |
+
if msg and msg.files:
|
| 67 |
+
file = msg.files[0]
|
| 68 |
+
qa_chain = process_file(file.path)
|
| 69 |
|
| 70 |
+
# Save QA chain to session
|
| 71 |
+
cl.user_session.set("qa_chain", qa_chain)
|
| 72 |
+
cl.user_session.set("ready", True)
|
| 73 |
+
await cl.Message(f"β
File `{file.name}` processed. You can now ask your questions.").send()
|
| 74 |
|
| 75 |
+
# On user message
|
| 76 |
+
@cl.on_message
|
| 77 |
+
async def handle_message(message: cl.Message):
|
| 78 |
+
if not cl.user_session.get("ready"):
|
| 79 |
+
await cl.Message("β οΈ Please upload a PDF first.").send()
|
| 80 |
+
return
|
| 81 |
|
| 82 |
+
qa_chain = cl.user_session.get("qa_chain")
|
| 83 |
+
response = qa_chain(message.content)
|
| 84 |
+
answer = response["result"]
|
| 85 |
+
await cl.Message(content=answer).send()
|