Turbiling commited on
Commit
70b95a1
Β·
verified Β·
1 Parent(s): 6715cbe

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +72 -55
app.py CHANGED
@@ -8,81 +8,98 @@ 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
- msg = await cl.AskFileMessage(
59
- content="πŸ“„ Upload your PDF file here:",
60
  accept=["application/pdf"],
61
  max_size_mb=20,
62
  timeout=180
63
  ).send()
64
 
65
- if msg and msg.files:
66
- file = msg.files[0]
67
- qa_chain = process_file(file.path)
 
 
 
68
 
69
- cl.user_session.set("qa_chain", qa_chain)
70
- cl.user_session.set("ready", True)
71
- await cl.Message(f"βœ… File `{file.name}` processed. You can now ask your questions.").send()
 
 
72
  else:
73
- await cl.Message("❌ No file was uploaded. Please try again.").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
  try:
 
84
  response = qa_chain(message.content)
85
  answer = response["result"]
86
- await cl.Message(content=answer).send()
87
  except Exception as e:
88
- await cl.Message(content=f"❌ An error occurred while generating a response: {str(e)}").send()
 
8
  from langchain_community.llms import HuggingFaceEndpoint
9
  from langchain.chains import RetrievalQA
10
 
11
+ # Helper function to process PDF and build the QA chain
12
  def process_file(file_path):
13
+ try:
14
+ loader = PyPDFLoader(file_path)
15
+ documents = loader.load()
16
+
17
+ cl.run_sync(cl.Message("πŸ“„ PDF loaded successfully. Splitting text...").send())
18
+
19
+ text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=50)
20
+ texts = text_splitter.split_documents(documents)
21
+
22
+ cl.run_sync(cl.Message("πŸ” Creating embeddings and FAISS vector store...").send())
23
+
24
+ embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
25
+ vectorstore = FAISS.from_documents(texts, embeddings)
26
+
27
+ custom_prompt_template = """
28
+ 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.
29
+ {context}
30
+ Question: {question}
31
+ Helpful Answer:
32
+ """
33
+ prompt = PromptTemplate(
34
+ template=custom_prompt_template,
35
+ input_variables=["context", "question"]
36
+ )
37
+
38
+ llm = HuggingFaceEndpoint(
39
+ repo_id="meta-llama/Meta-Llama-3-8B-Instruct",
40
+ temperature=0.5,
41
+ max_new_tokens=500,
42
+ top_k=50,
43
+ huggingfacehub_api_token=os.environ.get("HUGGINGFACEHUB_API_TOKEN")
44
+ )
45
+
46
+ cl.run_sync(cl.Message("βš™οΈ Building RetrievalQA chain...").send())
47
+
48
+ qa = RetrievalQA.from_chain_type(
49
+ llm=llm,
50
+ chain_type="stuff",
51
+ retriever=vectorstore.as_retriever(search_kwargs={"k": 3}),
52
+ chain_type_kwargs={"prompt": prompt},
53
+ return_source_documents=True
54
+ )
55
+
56
+ cl.run_sync(cl.Message("βœ… QA chain is ready. You can ask your question now.").send())
57
+
58
+ return qa
59
+
60
+ except Exception as e:
61
+ cl.run_sync(cl.Message(f"❌ Error processing file: {e}").send())
62
+ return None
63
+
64
+ # On chat start β€” ask user for PDF
65
  @cl.on_chat_start
66
  async def start():
67
  await cl.Message("πŸ‘‹ Welcome! Please upload a PDF to get started.").send()
68
 
69
+ file_message = await cl.AskFileMessage(
70
+ content="πŸ“„ Upload a PDF file below:",
71
  accept=["application/pdf"],
72
  max_size_mb=20,
73
  timeout=180
74
  ).send()
75
 
76
+ if file_message.files:
77
+ file = file_message.files[0]
78
+ file_path = file.path
79
+
80
+ await cl.Message(f"πŸ“‚ Received file: `{file.name}`. Now processing...").send()
81
+ qa_chain = process_file(file_path)
82
 
83
+ if qa_chain:
84
+ cl.user_session.set("qa_chain", qa_chain)
85
+ cl.user_session.set("ready", True)
86
+ else:
87
+ await cl.Message("⚠️ File could not be processed. Please try again.").send()
88
  else:
89
+ await cl.Message("⚠️ No file uploaded. Please upload a PDF file to proceed.").send()
90
 
91
+ # On message β€” handle question
92
  @cl.on_message
93
  async def handle_message(message: cl.Message):
94
  if not cl.user_session.get("ready"):
95
+ await cl.Message("⚠️ Please upload a PDF file first.").send()
96
  return
97
 
98
  qa_chain = cl.user_session.get("qa_chain")
99
  try:
100
+ await cl.Message("πŸ’¬ Thinking...").send()
101
  response = qa_chain(message.content)
102
  answer = response["result"]
103
+ await cl.Message(content=f"🧠 Answer: {answer}").send()
104
  except Exception as e:
105
+ await cl.Message(f"❌ Error during question answering: {e}").send()