Spaces:
Sleeping
Sleeping
File size: 2,682 Bytes
b10004d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 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 72 73 74 75 76 77 78 79 80 |
import streamlit as st
from langchain.embeddings import HuggingFaceEmbeddings
from langchain.vectorstores import FAISS
from langchain.llms import HuggingFaceHub
from langchain.chains import RetrievalQA
from langchain.text_splitter import CharacterTextSplitter
import fitz
import os
from langchain.schema import Document
def process_pdf(file):
"""Extract text from PDF, split into chunks, and create embeddings."""
try:
# Save the uploaded file temporarily
with open("temp_pdf.pdf", "wb") as f:
f.write(file.getbuffer())
text = ""
with fitz.open("temp_pdf.pdf") as doc:
for page in doc:
text += page.get_text()
# Create Document objects for the text splitter
texts = [Document(page_content=text)]
# Split text into smaller chunks
text_splitter = CharacterTextSplitter(
separator="\n",
chunk_size=300,
chunk_overlap=30
)
documents = text_splitter.split_documents(texts)
embeddings = HuggingFaceEmbeddings()
vectorstore = FAISS.from_documents(documents, embeddings)
os.remove("temp_pdf.pdf")
return vectorstore
except Exception as e:
st.error(f"Error processing PDF: {e}")
return None
# --- Streamlit UI ---
st.title("PDF Chatbot")
uploaded_file = st.file_uploader("Upload a PDF", type=["pdf"])
if uploaded_file:
vectorstore = process_pdf(uploaded_file)
if vectorstore:
# --- Chat Functionality ---
llm = HuggingFaceHub(
repo_id="google/flan-t5-xxl",
model_kwargs={"temperature": 0.7, "max_length": 512},
huggingfacehub_api_token=HF_TOKEN # Replace with your actual API token
)
qa_chain = RetrievalQA.from_chain_type(
llm=llm,
chain_type="stuff",
retriever=vectorstore.as_retriever(search_kwargs={"k": 2})
)
user_question = st.text_input("Ask a question about the PDF:")
if user_question:
with st.spinner("Generating answer..."):
response = qa_chain({"query": user_question})
answer = response['result']
st.write(answer)
# --- Feedback Mechanism ---
st.write("Was this answer helpful?")
col1, col2 = st.columns(2)
with col1:
if st.button("π"):
st.write("Thanks for the feedback!")
with col2:
if st.button("π"):
st.write("We appreciate your feedback. We'll work on improving!")
|