Spaces:
Runtime error
Runtime error
Download app.py from afatuddin/ChatWithPDF: direct link, hf CLI and curl.
- Browser
- Download file 8.57 kB
-
https://huggingface.co/spaces/afatuddin/ChatWithPDF/resolve/main/app.py
- Command line
-
hf download hf://spaces/afatuddin/ChatWithPDF/app.py
-
curl -L -o app.py https://huggingface.co/spaces/afatuddin/ChatWithPDF/resolve/main/app.py
8.57 kB
| import streamlit as st | |
| from PyPDF2 import PdfReader | |
| import os | |
| from dotenv import load_dotenv | |
| # Load environment variables from .env file | |
| load_dotenv() | |
| # --- UPDATED IMPORTS (LANGCHAIN v1.1 COMPATIBLE) --- | |
| from langchain_core.documents import Document | |
| from langchain_text_splitters import RecursiveCharacterTextSplitter | |
| from langchain_community.vectorstores import FAISS | |
| from langchain_core.prompts import ChatPromptTemplate | |
| from langchain_huggingface import HuggingFaceEndpoint, HuggingFaceEndpointEmbeddings, ChatHuggingFace | |
| # ... rest of your code ... | |
| def get_pdf_text(_pdf_docs): | |
| """ | |
| Extracts text from PDFs and creates Document objects with metadata. | |
| Returns a list of Document objects, not a string. | |
| """ | |
| documents = [] | |
| for pdf in _pdf_docs: | |
| pdf_reader = PdfReader(pdf) | |
| pdf_filename = pdf.name | |
| for i, page in enumerate(pdf_reader.pages): | |
| text = page.extract_text() | |
| if text: | |
| # Create a Document with metadata (page number starts at 1) | |
| doc = Document( | |
| page_content=text, | |
| metadata={"page": i + 1, "source": pdf_filename} | |
| ) | |
| documents.append(doc) | |
| return documents | |
| def get_text_chunks(_documents): | |
| """ | |
| Splits Document objects into chunks while preserving metadata. | |
| """ | |
| text_splitter = RecursiveCharacterTextSplitter( | |
| chunk_size=2000, # 10k is often too big for precise page tracking; 2k is better | |
| chunk_overlap=200 | |
| ) | |
| # split_documents ensures metadata (page num) is copied to new chunks | |
| chunks = text_splitter.split_documents(_documents) | |
| return chunks | |
| # --- Hugging Face Model Functions --- | |
| def get_hf_embeddings(): | |
| hf_token = os.getenv("HF_API_TOKEN") or st.secrets.get("HF_API_TOKEN") | |
| if not hf_token: | |
| st.error("HF_API_TOKEN not found in environment variables or secrets.toml.") | |
| return None | |
| return HuggingFaceEndpointEmbeddings( | |
| model="BAAI/bge-small-en-v1.5", | |
| huggingfacehub_api_token=hf_token | |
| ) | |
| def get_hf_llm(): | |
| hf_token = os.getenv("HF_API_TOKEN") or st.secrets.get("HF_API_TOKEN") | |
| if not hf_token: | |
| return None | |
| # Get model selection from session state | |
| model_choice = st.session_state.get("selected_model", "mistralai/Mistral-7B-Instruct-v0.2") | |
| llm = HuggingFaceEndpoint( | |
| huggingfacehub_api_token=hf_token, | |
| repo_id=model_choice, | |
| temperature=0.3, | |
| max_new_tokens=512, | |
| task="conversational" | |
| ) | |
| return ChatHuggingFace(llm=llm) | |
| # --- Vector Store Functions (UPDATED) --- | |
| def get_vector_store(_text_chunks): | |
| """Creates FAISS vector store from Document objects and stores in session state.""" | |
| if not _text_chunks: | |
| st.error("No text chunks found.") | |
| return | |
| embeddings = get_hf_embeddings() | |
| if embeddings is None: | |
| return | |
| try: | |
| # Create vector store in memory | |
| vector_store = FAISS.from_documents(documents=_text_chunks, embedding=embeddings) | |
| # Store in session state (persists during the session) | |
| st.session_state.vector_store = vector_store | |
| st.success("Vector store created and loaded successfully.") | |
| except Exception as e: | |
| st.error(f"Error creating vector store: {e}") | |
| def load_faiss_index(): | |
| """Load vector store from session state.""" | |
| try: | |
| # Check if vector store exists in session state | |
| if "vector_store" in st.session_state and st.session_state.vector_store is not None: | |
| return st.session_state.vector_store | |
| return None | |
| except Exception as e: | |
| # Silent fail on startup if index doesn't exist yet | |
| return None | |
| # --- Conversational Chain --- | |
| def get_conversational_chain(): | |
| model = get_hf_llm() | |
| if model is None: | |
| return None | |
| system_prompt = """You are a helpful AI assistant that answers questions based on the provided document content. | |
| ### Context from Documents: | |
| {context} | |
| ### Instructions: | |
| 1. Read the context carefully and extract relevant information to answer the user's question. | |
| 2. Base your answer ONLY on the information provided in the context above. | |
| 3. If the information needed to answer the question is not in the context, respond with: "I couldn't find this information in the provided documents. Please upload documents that contain the relevant information." | |
| 4. Be concise and direct in your answers. | |
| 5. If asking for clarification helps, you can ask follow-up questions. | |
| 6. Always cite which page or section your information comes from when possible.""" | |
| prompt = ChatPromptTemplate.from_messages([ | |
| ("system", system_prompt), | |
| ("human", "{input}") | |
| ]) | |
| # Create a simple chain using the pipe operator (|) | |
| chain = prompt | model | |
| return chain | |
| # --- User Input Handler (UPDATED) --- | |
| def user_input(user_question, db): | |
| if db is None: | |
| st.warning("Vector store is not loaded.") | |
| return | |
| try: | |
| # 1. Get relevant documents | |
| docs = db.similarity_search(user_question, k=4) | |
| # 2. Get the response | |
| chain = get_conversational_chain() | |
| if chain is None: | |
| st.error("Check API Key") | |
| return | |
| response = chain.invoke({ | |
| "context": docs, | |
| "input": user_question | |
| }) | |
| # 3. Display Response | |
| st.markdown("### Reply:") | |
| # Extract just the content text from the response | |
| if hasattr(response, 'content'): | |
| # Response is an AIMessage object | |
| st.write(response.content) | |
| elif isinstance(response, dict) and "answer" in response: | |
| st.write(response["answer"]) | |
| elif isinstance(response, dict) and "content" in response: | |
| st.write(response["content"]) | |
| else: | |
| # Try to get string representation | |
| st.write(str(response)) | |
| # 4. Display Retrieved Documents (Debug Info) | |
| except Exception as e: | |
| st.error(f"Error: {e}") | |
| import traceback | |
| st.error(traceback.format_exc()) | |
| # --- Main App --- | |
| def main(): | |
| st.set_page_config(page_title="Chat PDF (Hugging Face)") | |
| st.header("Chat with PDF using Hugging Face Models") | |
| # Initialize session state variables | |
| if "faiss_db" not in st.session_state: | |
| st.session_state.faiss_db = None | |
| if "vector_store" not in st.session_state: | |
| st.session_state.vector_store = None | |
| if "selected_model" not in st.session_state: | |
| st.session_state.selected_model = "mistralai/Mistral-7B-Instruct-v0.2" | |
| with st.sidebar: | |
| st.title("Menu:") | |
| # Model Selection Dropdown | |
| model_options = { | |
| "Mistral 7B": "mistralai/Mistral-7B-Instruct-v0.2", | |
| "Llama 3.2 3B": "meta-llama/Llama-3.2-3B-Instruct" | |
| } | |
| selected_model_name = st.selectbox( | |
| "Choose Model:", | |
| options=list(model_options.keys()), | |
| index=0 | |
| ) | |
| st.session_state.selected_model = model_options[selected_model_name] | |
| st.caption(f"Selected: `{st.session_state.selected_model}`") | |
| st.divider() | |
| pdf_docs = st.file_uploader("Upload PDFs", accept_multiple_files=True) | |
| if st.button("Process PDFs"): | |
| if pdf_docs: | |
| with st.spinner("Processing..."): | |
| # Clear previous cache to avoid using old vector stores | |
| st.cache_data.clear() | |
| st.cache_resource.clear() | |
| # 1. Get Documents (not string) | |
| raw_documents = get_pdf_text(pdf_docs) | |
| # 2. Split Documents (preserves metadata) | |
| text_chunks = get_text_chunks(raw_documents) | |
| # 3. Create Vector Store (stored in session state) | |
| get_vector_store(text_chunks) | |
| st.session_state.faiss_db = load_faiss_index() | |
| else: | |
| st.warning("Upload a PDF first.") | |
| user_question = st.text_input("Ask a Question") | |
| if user_question: | |
| if st.session_state.faiss_db: | |
| user_input(user_question, st.session_state.faiss_db) | |
| else: | |
| st.warning("Process PDFs first.") | |
| if __name__ == "__main__": | |
| main() |