Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,80 +1,43 @@
|
|
| 1 |
import os
|
| 2 |
-
|
| 3 |
-
from langchain.document_loaders import PyPDFLoader
|
| 4 |
-
from langchain.embeddings import OpenAIEmbeddings
|
| 5 |
-
from langchain.vectorstores import FAISS
|
| 6 |
-
from langchain.chat_models import ChatOpenAI
|
| 7 |
-
from langchain.chains import RetrievalQA
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
|
|
|
| 14 |
|
| 15 |
-
|
| 16 |
-
loader = PyPDFLoader(file_path)
|
| 17 |
-
pages = loader.load_and_split()
|
| 18 |
-
return pages
|
| 19 |
|
| 20 |
@cl.on_chat_start
|
| 21 |
async def start():
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
).send()
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
file = files[0]
|
| 29 |
file_path = file.path
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
# Create vector store
|
| 34 |
-
embeddings = OpenAIEmbeddings()
|
| 35 |
-
vectordb = FAISS.from_documents(pages, embeddings)
|
| 36 |
-
|
| 37 |
-
retriever = vectordb.as_retriever()
|
| 38 |
-
|
| 39 |
-
if USE_GROQ:
|
| 40 |
-
# Custom RAG using Groq API
|
| 41 |
-
cl.user_session.set("retriever", retriever)
|
| 42 |
-
else:
|
| 43 |
-
# Standard LangChain RetrievalQA with OpenAI
|
| 44 |
-
qa_chain = RetrievalQA.from_chain_type(
|
| 45 |
-
llm=ChatOpenAI(temperature=0.2),
|
| 46 |
-
retriever=retriever
|
| 47 |
-
)
|
| 48 |
-
cl.user_session.set("chain", qa_chain)
|
| 49 |
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
if USE_GROQ:
|
| 53 |
-
retriever = cl.user_session.get("retriever")
|
| 54 |
-
docs = retriever.get_relevant_documents(msg.content)
|
| 55 |
-
context = "\n".join([doc.page_content for doc in docs][:3])
|
| 56 |
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
"Content-Type": "application/json"
|
| 60 |
-
}
|
| 61 |
|
| 62 |
-
|
| 63 |
-
"messages": [
|
| 64 |
-
{"role": "system", "content": f"You are a helpful assistant answering questions from a PDF. Use this context:\n{context}"},
|
| 65 |
-
{"role": "user", "content": msg.content}
|
| 66 |
-
],
|
| 67 |
-
"model": "llama3-8b-8192",
|
| 68 |
-
"temperature": 0.5
|
| 69 |
-
}
|
| 70 |
|
| 71 |
-
|
| 72 |
-
|
|
|
|
| 73 |
|
| 74 |
-
|
| 75 |
-
await cl.Message(content=reply).send()
|
| 76 |
|
| 77 |
-
else:
|
| 78 |
-
chain = cl.user_session.get("chain")
|
| 79 |
-
response = chain.run(msg.content)
|
| 80 |
-
await cl.Message(content=response).send()
|
|
|
|
| 1 |
import os
|
| 2 |
+
os.environ["CHAINLIT_STORAGE_DIR"] = "/app/.chainlit"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
+
from langchain_community.document_loaders import PyPDFLoader
|
| 5 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 6 |
+
from langchain.embeddings import HuggingFaceEmbeddings
|
| 7 |
+
from langchain.vectorstores import FAISS
|
| 8 |
+
from langchain.chains.question_answering import load_qa_chain
|
| 9 |
+
from langchain.llms import OpenAI # replace with Groq if needed
|
| 10 |
|
| 11 |
+
import chainlit as cl
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
@cl.on_chat_start
|
| 14 |
async def start():
|
| 15 |
+
await cl.Message("👋 Upload a PDF to ask questions!").send()
|
| 16 |
+
|
| 17 |
+
@cl.on_message
|
| 18 |
+
async def handle_message(message: cl.Message):
|
| 19 |
+
files = await cl.AskFileMessage("📎 Upload your PDF", accept=["application/pdf"]).send()
|
| 20 |
+
if not files:
|
| 21 |
+
await cl.Message("❌ No file received.").send()
|
| 22 |
+
return
|
| 23 |
|
| 24 |
file = files[0]
|
| 25 |
file_path = file.path
|
| 26 |
|
| 27 |
+
loader = PyPDFLoader(file_path)
|
| 28 |
+
documents = loader.load()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
+
splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
|
| 31 |
+
chunks = splitter.split_documents(documents)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
+
embeddings = HuggingFaceEmbeddings()
|
| 34 |
+
vectordb = FAISS.from_documents(chunks, embeddings)
|
|
|
|
|
|
|
| 35 |
|
| 36 |
+
qa_chain = load_qa_chain(OpenAI(temperature=0), chain_type="stuff") # or Groq
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
+
query = message.content
|
| 39 |
+
docs = vectordb.similarity_search(query)
|
| 40 |
+
answer = qa_chain.run(input_documents=docs, question=query)
|
| 41 |
|
| 42 |
+
await cl.Message(answer).send()
|
|
|
|
| 43 |
|
|
|
|
|
|
|
|
|
|
|
|