Wall06 commited on
Commit
5b06b42
·
verified ·
1 Parent(s): 21a58c6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -29
app.py CHANGED
@@ -1,66 +1,68 @@
1
  import gradio as gr
2
- from langchain.vectorstores import FAISS
3
- from langchain.embeddings import HuggingFaceEmbeddings
4
- from langchain.chains import RetrievalQA
5
- from langchain.llms import HuggingFacePipeline
6
  from transformers import pipeline
7
 
8
- # ---------------- LOAD EMBEDDINGS ----------------
 
 
 
 
 
9
  embeddings = HuggingFaceEmbeddings(
10
  model_name="sentence-transformers/all-MiniLM-L6-v2"
11
  )
12
 
13
- # ---------------- LOAD VECTOR STORE ----------------
14
  db = FAISS.load_local(
15
  "vectorstore/faiss_index",
16
  embeddings,
17
  allow_dangerous_deserialization=True
18
  )
19
 
20
- # ---------------- LOAD LLM ----------------
21
- generator = pipeline(
 
22
  "text-generation",
23
- model="mistralai/Mistral-7B-Instruct-v0.2",
24
  max_new_tokens=512,
25
  temperature=0.2,
26
  )
27
 
28
- llm = HuggingFacePipeline(pipeline=generator)
29
 
30
- # ---------------- RAG CHAIN ----------------
31
- qa = RetrievalQA.from_chain_type(
32
  llm=llm,
33
  retriever=db.as_retriever(search_kwargs={"k": 3}),
34
  chain_type="stuff",
35
  )
36
 
37
- # ---------------- CHAT FUNCTION ----------------
38
- def chat(query, history):
39
- if not query.strip():
40
  return history
41
 
42
- answer = qa.run(query)
43
-
44
- history.append((query, answer))
45
  return history
46
 
47
- # ---------------- GRADIO UI ----------------
48
- with gr.Blocks(title="RAG Document Chatbot") as demo:
49
  gr.Markdown(
50
  """
51
- # 📚 RAG Document Chatbot
52
- Answers are **strictly based on the provided documents**.
53
  """
54
  )
55
 
56
- chatbot = gr.Chatbot(height=400)
57
- query_box = gr.Textbox(
58
- placeholder="Ask a question from the documents...",
59
- label="Your Question",
60
  )
61
- clear = gr.Button("Clear Chat")
62
 
63
- query_box.submit(chat, [query_box, chatbot], chatbot)
64
- clear.click(lambda: [], None, chatbot)
 
 
65
 
66
  demo.launch()
 
1
  import gradio as gr
 
 
 
 
2
  from transformers import pipeline
3
 
4
+ from langchain.chains import RetrievalQA
5
+ from langchain_community.vectorstores import FAISS
6
+ from langchain_community.embeddings import HuggingFaceEmbeddings
7
+ from langchain_community.llms import HuggingFacePipeline
8
+
9
+ # ------------------ LOAD EMBEDDINGS ------------------
10
  embeddings = HuggingFaceEmbeddings(
11
  model_name="sentence-transformers/all-MiniLM-L6-v2"
12
  )
13
 
14
+ # ------------------ LOAD VECTOR STORE ------------------
15
  db = FAISS.load_local(
16
  "vectorstore/faiss_index",
17
  embeddings,
18
  allow_dangerous_deserialization=True
19
  )
20
 
21
+ # ------------------ LOAD LLM ------------------
22
+ # NOTE: Use a LIGHT model for HF CPU
23
+ text_gen_pipeline = pipeline(
24
  "text-generation",
25
+ model="microsoft/phi-2",
26
  max_new_tokens=512,
27
  temperature=0.2,
28
  )
29
 
30
+ llm = HuggingFacePipeline(pipeline=text_gen_pipeline)
31
 
32
+ # ------------------ RAG CHAIN ------------------
33
+ qa_chain = RetrievalQA.from_chain_type(
34
  llm=llm,
35
  retriever=db.as_retriever(search_kwargs={"k": 3}),
36
  chain_type="stuff",
37
  )
38
 
39
+ # ------------------ CHAT FUNCTION ------------------
40
+ def chat(user_message, history):
41
+ if not user_message.strip():
42
  return history
43
 
44
+ answer = qa_chain.run(user_message)
45
+ history.append((user_message, answer))
 
46
  return history
47
 
48
+ # ------------------ GRADIO UI ------------------
49
+ with gr.Blocks(title="Document RAG Chatbot") as demo:
50
  gr.Markdown(
51
  """
52
+ # 📚 Document RAG Chatbot
53
+ Answers are generated **strictly from the provided documents** using Retrieval-Augmented Generation.
54
  """
55
  )
56
 
57
+ chatbot = gr.Chatbot(height=420)
58
+ query = gr.Textbox(
59
+ label="Ask a question",
60
+ placeholder="Ask something from the documents..."
61
  )
 
62
 
63
+ clear_btn = gr.Button("Clear Chat")
64
+
65
+ query.submit(chat, [query, chatbot], chatbot)
66
+ clear_btn.click(lambda: [], None, chatbot)
67
 
68
  demo.launch()