Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,82 +1,96 @@
|
|
| 1 |
-
|
| 2 |
-
import streamlit as st
|
| 3 |
from PyPDF2 import PdfReader
|
| 4 |
from langchain.text_splitter import CharacterTextSplitter
|
| 5 |
-
from
|
| 6 |
-
from
|
| 7 |
from langchain.chains import ConversationalRetrievalChain
|
| 8 |
from langchain.memory import ConversationBufferMemory
|
| 9 |
-
from langchain_community.llms import HuggingFacePipeline
|
| 10 |
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer, pipeline
|
| 11 |
-
from
|
| 12 |
|
| 13 |
-
# -----------------
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
|
|
|
| 22 |
|
| 23 |
-
# ----------------- Main function -----------------
|
| 24 |
-
def main():
|
| 25 |
-
st.set_page_config(page_title="PDF Chatbot", layout="wide")
|
| 26 |
-
st.title("📑 Chat with Multiple PDFs")
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
)
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
if user_question:
|
| 72 |
-
response = st.session_state.conversation({"question": user_question})
|
| 73 |
-
answer = response["answer"]
|
| 74 |
-
st.session_state.chat_history.append((user_question, answer))
|
| 75 |
-
|
| 76 |
-
# Display chat
|
| 77 |
-
for i, (q, a) in enumerate(st.session_state.chat_history):
|
| 78 |
-
message(q, is_user=True, key=f"user_{i}")
|
| 79 |
-
message(a, is_user=False, key=f"bot_{i}")
|
| 80 |
-
|
| 81 |
-
if __name__ == "__main__":
|
| 82 |
-
main()
|
|
|
|
| 1 |
+
import gradio as gr
|
|
|
|
| 2 |
from PyPDF2 import PdfReader
|
| 3 |
from langchain.text_splitter import CharacterTextSplitter
|
| 4 |
+
from langchain.embeddings import HuggingFaceEmbeddings
|
| 5 |
+
from langchain.vectorstores import FAISS
|
| 6 |
from langchain.chains import ConversationalRetrievalChain
|
| 7 |
from langchain.memory import ConversationBufferMemory
|
|
|
|
| 8 |
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer, pipeline
|
| 9 |
+
from langchain.llms import HuggingFacePipeline
|
| 10 |
|
| 11 |
+
# -----------------------
|
| 12 |
+
# Load LLM model locally
|
| 13 |
+
# -----------------------
|
| 14 |
+
model_name = "google/flan-t5-small" # keep small for Spaces, you can change
|
| 15 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 16 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
|
| 17 |
|
| 18 |
+
pipe = pipeline("text2text-generation", model=model, tokenizer=tokenizer, max_length=512)
|
| 19 |
+
llm = HuggingFacePipeline(pipeline=pipe)
|
| 20 |
|
| 21 |
+
# Global variables
|
| 22 |
+
db = None
|
| 23 |
+
conversation = None
|
| 24 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
+
# -----------------------
|
| 27 |
+
# Step 1: Process PDFs
|
| 28 |
+
# -----------------------
|
| 29 |
+
def process_pdfs(files):
|
| 30 |
+
global db, conversation
|
| 31 |
+
|
| 32 |
+
text = ""
|
| 33 |
+
for file in files:
|
| 34 |
+
pdf = PdfReader(file.name)
|
| 35 |
+
for page in pdf.pages:
|
| 36 |
+
text += page.extract_text() or ""
|
| 37 |
+
|
| 38 |
+
# Split text
|
| 39 |
+
splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
|
| 40 |
+
chunks = splitter.split_text(text)
|
| 41 |
+
|
| 42 |
+
# Embeddings
|
| 43 |
+
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
|
| 44 |
+
|
| 45 |
+
# Vector DB
|
| 46 |
+
db = FAISS.from_texts(chunks, embeddings)
|
| 47 |
+
|
| 48 |
+
# Memory
|
| 49 |
+
memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
|
| 50 |
+
|
| 51 |
+
# Conversation Chain
|
| 52 |
+
conversation = ConversationalRetrievalChain.from_llm(
|
| 53 |
+
llm=llm,
|
| 54 |
+
retriever=db.as_retriever(),
|
| 55 |
+
memory=memory
|
| 56 |
)
|
| 57 |
|
| 58 |
+
return "✅ PDFs processed! You can now start chatting."
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
# -----------------------
|
| 62 |
+
# Step 2: Chat Function
|
| 63 |
+
# -----------------------
|
| 64 |
+
def chat(user_input):
|
| 65 |
+
global conversation, db
|
| 66 |
+
|
| 67 |
+
if conversation is None or db is None:
|
| 68 |
+
return "⚠️ Please upload and process PDFs first.", []
|
| 69 |
+
|
| 70 |
+
result = conversation({"question": user_input})
|
| 71 |
+
answer = result["answer"]
|
| 72 |
+
|
| 73 |
+
return answer, result["chat_history"]
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
# -----------------------
|
| 77 |
+
# Gradio UI
|
| 78 |
+
# -----------------------
|
| 79 |
+
with gr.Blocks() as demo:
|
| 80 |
+
gr.Markdown("## 📚 Multiple PDF Chatbot")
|
| 81 |
+
|
| 82 |
+
with gr.Row():
|
| 83 |
+
pdfs = gr.File(file_types=[".pdf"], file_count="multiple", label="Upload PDFs")
|
| 84 |
+
process_btn = gr.Button("Process PDFs")
|
| 85 |
+
|
| 86 |
+
status = gr.Textbox(label="Status", interactive=False)
|
| 87 |
+
|
| 88 |
+
chatbot = gr.Chatbot()
|
| 89 |
+
user_msg = gr.Textbox(label="Ask a question about your PDFs")
|
| 90 |
+
send_btn = gr.Button("Send")
|
| 91 |
+
|
| 92 |
+
# Actions
|
| 93 |
+
process_btn.click(process_pdfs, inputs=[pdfs], outputs=[status])
|
| 94 |
+
send_btn.click(chat, inputs=[user_msg], outputs=[chatbot, chatbot])
|
| 95 |
+
|
| 96 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|