Turbiling commited on
Commit
92ceee8
·
verified ·
1 Parent(s): 1ebb5b2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -40
app.py CHANGED
@@ -2,16 +2,13 @@ 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()
@@ -22,19 +19,13 @@ def process_file(file_path):
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",
@@ -44,7 +35,7 @@ def process_file(file_path):
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}),
@@ -52,44 +43,46 @@ def process_file(file_path):
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
 
 
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
+ # Helper function to process PDF
 
 
 
12
  def process_file(file_path):
13
  loader = PyPDFLoader(file_path)
14
  documents = loader.load()
 
19
  embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
20
  vectorstore = FAISS.from_documents(texts, embeddings)
21
 
22
+ prompt_template = """
23
+ Use the following pieces of context to answer the question at the end. If you don't know the answer, just say you don't know.
 
24
  {context}
 
25
  Question: {question}
 
26
  Helpful Answer:
27
  """
28
+ prompt = PromptTemplate(input_variables=["context", "question"], template=prompt_template)
 
 
 
29
 
30
  llm = HuggingFaceEndpoint(
31
  repo_id="meta-llama/Meta-Llama-3-8B-Instruct",
 
35
  huggingfacehub_api_token=os.environ.get("HUGGINGFACEHUB_API_TOKEN")
36
  )
37
 
38
+ qa_chain = RetrievalQA.from_chain_type(
39
  llm=llm,
40
  chain_type="stuff",
41
  retriever=vectorstore.as_retriever(search_kwargs={"k": 3}),
 
43
  return_source_documents=True
44
  )
45
 
46
+ return qa_chain
47
 
48
+ # Chat start
49
  @cl.on_chat_start
50
+ async def start():
51
+ await cl.Message(content="👋 Welcome! Please upload a PDF file to begin.").send()
52
+ files = await cl.AskFileMessage(
53
+ content="📄 Upload a PDF to get started.",
54
+ accept=["application/pdf"],
55
+ max_size_mb=10,
56
+ timeout=180
57
+ ).send()
58
+
59
+ if not files:
60
+ await cl.Message(content="⚠️ No file was uploaded.").send()
61
+ return
62
+
63
+ file = files[0]
64
  file_path = file.path
65
+
66
  qa_chain = process_file(file_path)
67
  cl.user_session.set("qa_chain", qa_chain)
 
68
  cl.user_session.set("ready", True)
 
69
 
70
+ await cl.Message(content=f"✅ `{file.name}` processed successfully. You can now ask questions.").send()
71
+
72
+ # Handle questions
73
  @cl.on_message
74
+ async def handle_message(message: cl.Message):
75
  if not cl.user_session.get("ready"):
76
+ await cl.Message(content="⚠️ Please upload a PDF file first.").send()
77
  return
78
 
79
  qa_chain = cl.user_session.get("qa_chain")
80
+ if not qa_chain:
81
+ await cl.Message(content="⚠️ Error: QA chain not initialized.").send()
82
  return
83
 
84
  query = message.content
85
  response = qa_chain(query)
86
+ await cl.Message(content=response["result"]).send()
 
87
 
88