Spaces:
Sleeping
Sleeping
Update app.py
#7
by Muthuraja18 - opened
app.py
CHANGED
|
@@ -1,31 +1,36 @@
|
|
| 1 |
import streamlit as st
|
|
|
|
| 2 |
|
| 3 |
-
# β
|
| 4 |
from langchain_community.document_loaders import PyPDFLoader, TextLoader
|
| 5 |
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
| 6 |
from langchain_community.embeddings import HuggingFaceEmbeddings
|
| 7 |
from langchain_community.vectorstores import FAISS
|
| 8 |
-
from langchain_community.llms import HuggingFacePipeline
|
| 9 |
-
|
| 10 |
from langchain.chains import RetrievalQA
|
|
|
|
| 11 |
from transformers import pipeline
|
|
|
|
| 12 |
|
| 13 |
|
| 14 |
# -------------------------------
|
| 15 |
-
# Load Documents
|
| 16 |
# -------------------------------
|
| 17 |
def load_documents(uploaded_files):
|
| 18 |
documents = []
|
|
|
|
| 19 |
for file in uploaded_files:
|
| 20 |
-
|
|
|
|
|
|
|
| 21 |
f.write(file.getbuffer())
|
| 22 |
|
| 23 |
if file.name.endswith(".pdf"):
|
| 24 |
-
loader = PyPDFLoader(
|
| 25 |
else:
|
| 26 |
-
loader = TextLoader(
|
| 27 |
|
| 28 |
documents.extend(loader.load())
|
|
|
|
| 29 |
return documents
|
| 30 |
|
| 31 |
|
|
@@ -41,43 +46,42 @@ def split_documents(documents):
|
|
| 41 |
|
| 42 |
|
| 43 |
# -------------------------------
|
| 44 |
-
# Create Vector Store
|
| 45 |
# -------------------------------
|
| 46 |
def create_vectorstore(chunks):
|
| 47 |
embeddings = HuggingFaceEmbeddings(
|
| 48 |
-
model_name="sentence-transformers/all-MiniLM-L6-v2"
|
| 49 |
)
|
| 50 |
return FAISS.from_documents(chunks, embeddings)
|
| 51 |
|
| 52 |
|
| 53 |
# -------------------------------
|
| 54 |
-
# Load
|
| 55 |
# -------------------------------
|
| 56 |
def load_llm():
|
| 57 |
pipe = pipeline(
|
| 58 |
-
"text2text-generation",
|
| 59 |
-
model="
|
| 60 |
-
max_length=
|
| 61 |
)
|
| 62 |
return HuggingFacePipeline(pipeline=pipe)
|
| 63 |
|
| 64 |
|
| 65 |
# -------------------------------
|
| 66 |
-
# Build QA Chain
|
| 67 |
# -------------------------------
|
| 68 |
def build_qa(vectorstore):
|
| 69 |
llm = load_llm()
|
| 70 |
retriever = vectorstore.as_retriever()
|
| 71 |
|
| 72 |
-
|
| 73 |
llm=llm,
|
| 74 |
retriever=retriever
|
| 75 |
)
|
| 76 |
-
return qa
|
| 77 |
|
| 78 |
|
| 79 |
# -------------------------------
|
| 80 |
-
#
|
| 81 |
# -------------------------------
|
| 82 |
st.set_page_config(page_title="RAG Chatbot", layout="wide")
|
| 83 |
st.title("π Chat with Your Documents (RAG)")
|
|
@@ -94,12 +98,15 @@ if uploaded_files:
|
|
| 94 |
vectorstore = create_vectorstore(chunks)
|
| 95 |
qa_chain = build_qa(vectorstore)
|
| 96 |
|
| 97 |
-
st.success("Documents ready!")
|
| 98 |
|
| 99 |
query = st.text_input("Ask a question from your documents")
|
| 100 |
|
| 101 |
if query:
|
| 102 |
with st.spinner("Generating answer..."):
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import os
|
| 3 |
|
| 4 |
+
# β
Imports
|
| 5 |
from langchain_community.document_loaders import PyPDFLoader, TextLoader
|
| 6 |
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
| 7 |
from langchain_community.embeddings import HuggingFaceEmbeddings
|
| 8 |
from langchain_community.vectorstores import FAISS
|
|
|
|
|
|
|
| 9 |
from langchain.chains import RetrievalQA
|
| 10 |
+
|
| 11 |
from transformers import pipeline
|
| 12 |
+
from langchain_community.llms import HuggingFacePipeline
|
| 13 |
|
| 14 |
|
| 15 |
# -------------------------------
|
| 16 |
+
# Load Documents (SAFE PATH)
|
| 17 |
# -------------------------------
|
| 18 |
def load_documents(uploaded_files):
|
| 19 |
documents = []
|
| 20 |
+
|
| 21 |
for file in uploaded_files:
|
| 22 |
+
file_path = os.path.join("/tmp", file.name)
|
| 23 |
+
|
| 24 |
+
with open(file_path, "wb") as f:
|
| 25 |
f.write(file.getbuffer())
|
| 26 |
|
| 27 |
if file.name.endswith(".pdf"):
|
| 28 |
+
loader = PyPDFLoader(file_path)
|
| 29 |
else:
|
| 30 |
+
loader = TextLoader(file_path)
|
| 31 |
|
| 32 |
documents.extend(loader.load())
|
| 33 |
+
|
| 34 |
return documents
|
| 35 |
|
| 36 |
|
|
|
|
| 46 |
|
| 47 |
|
| 48 |
# -------------------------------
|
| 49 |
+
# Create Vector Store (LOCAL)
|
| 50 |
# -------------------------------
|
| 51 |
def create_vectorstore(chunks):
|
| 52 |
embeddings = HuggingFaceEmbeddings(
|
| 53 |
+
model_name="sentence-transformers/all-MiniLM-L6-v2" # works without token
|
| 54 |
)
|
| 55 |
return FAISS.from_documents(chunks, embeddings)
|
| 56 |
|
| 57 |
|
| 58 |
# -------------------------------
|
| 59 |
+
# Load LOCAL LLM (VERY LIGHT)
|
| 60 |
# -------------------------------
|
| 61 |
def load_llm():
|
| 62 |
pipe = pipeline(
|
| 63 |
+
"text2text-generation",
|
| 64 |
+
model="sshleifer/tiny-t5", # π₯ super light, no auth needed
|
| 65 |
+
max_length=256
|
| 66 |
)
|
| 67 |
return HuggingFacePipeline(pipeline=pipe)
|
| 68 |
|
| 69 |
|
| 70 |
# -------------------------------
|
| 71 |
+
# Build QA Chain
|
| 72 |
# -------------------------------
|
| 73 |
def build_qa(vectorstore):
|
| 74 |
llm = load_llm()
|
| 75 |
retriever = vectorstore.as_retriever()
|
| 76 |
|
| 77 |
+
return RetrievalQA.from_chain_type(
|
| 78 |
llm=llm,
|
| 79 |
retriever=retriever
|
| 80 |
)
|
|
|
|
| 81 |
|
| 82 |
|
| 83 |
# -------------------------------
|
| 84 |
+
# UI
|
| 85 |
# -------------------------------
|
| 86 |
st.set_page_config(page_title="RAG Chatbot", layout="wide")
|
| 87 |
st.title("π Chat with Your Documents (RAG)")
|
|
|
|
| 98 |
vectorstore = create_vectorstore(chunks)
|
| 99 |
qa_chain = build_qa(vectorstore)
|
| 100 |
|
| 101 |
+
st.success("β
Documents ready!")
|
| 102 |
|
| 103 |
query = st.text_input("Ask a question from your documents")
|
| 104 |
|
| 105 |
if query:
|
| 106 |
with st.spinner("Generating answer..."):
|
| 107 |
+
try:
|
| 108 |
+
result = qa_chain.run(query)
|
| 109 |
+
st.write("### Answer:")
|
| 110 |
+
st.write(result)
|
| 111 |
+
except Exception as e:
|
| 112 |
+
st.error(str(e))
|