{"id":13585015,"url":"https://github.com/kaushalpowar/talk_to_pdf","last_synced_at":"2025-04-07T06:32:13.262Z","repository":{"id":162527854,"uuid":"629983140","full_name":"kaushalpowar/talk_to_pdf","owner":"kaushalpowar","description":"Talk to your pdf using OpenAI","archived":false,"fork":false,"pushed_at":"2024-10-10T19:13:54.000Z","size":2350,"stargazers_count":8,"open_issues_count":0,"forks_count":5,"subscribers_count":2,"default_branch":"main","last_synced_at":"2024-11-06T02:38:18.591Z","etag":null,"topics":["ai","ghdesktop","github","gpt-3","learn","llm","nlp","nlp-machine-learning","opeanai"],"latest_commit_sha":null,"homepage":"https://talk-to-pdf.streamlit.app/","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/kaushalpowar.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-04-19T12:29:01.000Z","updated_at":"2024-10-10T19:13:58.000Z","dependencies_parsed_at":null,"dependency_job_id":"d619f922-6a63-4f8a-9bdb-52dccad137e1","html_url":"https://github.com/kaushalpowar/talk_to_pdf","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kaushalpowar%2Ftalk_to_pdf","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kaushalpowar%2Ftalk_to_pdf/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kaushalpowar%2Ftalk_to_pdf/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kaushalpowar%2Ftalk_to_pdf/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kaushalpowar","download_url":"https://codeload.github.com/kaushalpowar/talk_to_pdf/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247607257,"owners_count":20965942,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ai","ghdesktop","github","gpt-3","learn","llm","nlp","nlp-machine-learning","opeanai"],"created_at":"2024-08-01T15:04:41.121Z","updated_at":"2025-04-07T06:32:13.253Z","avatar_url":"https://github.com/kaushalpowar.png","language":"Python","funding_links":[],"categories":["Python"],"sub_categories":[],"readme":"# Talk to PDF 🤖📑\n\n\n\n[![Streamlit App](https://static.streamlit.io/badges/streamlit_badge_black_white.svg)](https://talk-to-pdf.streamlit.app/)\n\n## Project Summary\n\nTalk to PDF is an innovative web application that enables natural language interaction with PDF documents through an AI-powered interface. The project leverages cutting-edge technologies including OpenAI's language models and LlamaIndex for document processing to create a seamless question-answering system for PDF content.\n\nThe application follows a multi-page architecture built on Streamlit, with three primary components:\n1. API Configuration Interface\n2. Document Upload and Processing System\n3. Interactive Chat Interface\n\nThe system processes uploaded PDFs through a sophisticated pipeline that includes document indexing, vector storage creation, and context-aware response generation. This architecture enables users to extract information from PDFs through natural conversation rather than manual searching.\n\n### Technical Architecture\n\nThe application is structured using a modular approach with clear separation of concerns:\n\n```python\ntalk_to_pdf/\n├── 0_🔌API_KEY.py          # API configuration entry point\n├── functions.py            # Shared utility functions\n└── pages/\n    ├── 1_📑UPLOAD_PDF.py   # Document processing\n    └── 2_💬CHAT_WITH_PDF.py # Chat interface\n```\n\nThe system utilizes Streamlit's session state management for maintaining application state and LlamaIndex for document processing and retrieval operations. This architecture ensures efficient document handling and responsive user interactions.\n\n## Key Features\n\n### 1. Intelligent PDF Processing\n\nThe application implements advanced PDF processing capabilities using LlamaIndex and OpenAI's embedding models. The document processing pipeline includes:\n\n```python\ndef load_document(uploaded_files):\n    temp_dir = tempfile.TemporaryDirectory()\n    for file in uploaded_files:\n        temp_filepath = os.path.join(temp_dir.name, file.name)\n        with open(temp_filepath, \"wb\") as f:\n            f.write(file.getvalue())\n    reader = SimpleDirectoryReader(input_dir=temp_dir.name)\n    docs = reader.load_data()\n    return docs\n```\n\nThis implementation ensures efficient document handling while maintaining document integrity and security.\n\n### 2. Context-Aware Question Answering\n\nThe system employs a sophisticated chat engine that maintains conversation context and generates accurate responses:\n\n```python\ncustom_prompt = PromptTemplate(\"\"\"\\\nGiven a conversation (between Human and Assistant) and a follow up message from Human, \\\nrewrite the message to be a standalone question that captures all relevant context \\\nfrom the conversation.\n\"\"\")\n\nchat_engine = CondenseQuestionChatEngine.from_defaults(\n    query_engine=query_engine, \n    condense_question_prompt=custom_prompt,\n    chat_history=custom_chat_history\n)\n```\n\nThis feature enables natural conversation flow while maintaining context accuracy throughout the interaction.\n\n### 3. Real-Time Response Streaming\n\nThe application implements streaming responses for improved user experience:\n\n```python\ndef conversational_chat(query):\n    streaming_response = chat_engine.stream_chat(query)\n    response_tokens = []\n    for token in streaming_response.response_gen:\n        response_tokens.append(token)\n    return ''.join(response_tokens)\n```\n\nThis implementation provides immediate feedback while processing complex queries.\n\n### 4. Flexible Model Selection\n\nUsers can customize their experience by selecting different language models and adjusting response parameters:\n\n```python\nmodel_name = st.selectbox(\"Select the model you want to use\",\n                         (\"gpt-3.5-turbo\",\"gpt-4\"))\ntemperature = st.slider(\"Set temperature\", 0.1, 1.0, 0.5, 0.1)\n```\n\n## Benefits\n\n### 1. Enhanced Document Analysis Efficiency\n\nThe application significantly reduces the time required to extract information from PDF documents through:\n- Instant access to document content through natural language queries\n- Context-aware responses that understand document structure\n- Efficient document indexing for quick retrieval of relevant information\n\n### 2. User-Friendly Interface\n\nThe application provides several usability advantages:\n- Progressive disclosure of functionality through a step-by-step interface\n- Clear visual feedback for all operations\n- Intuitive chat-based interaction model\n- Customizable response parameters for different use cases\n\n### 3. Technical Advantages\n\nThe implementation offers several technical benefits:\n- Scalable architecture supporting multiple document formats\n- Efficient memory management through temporary file handling\n- Secure document processing with proper cleanup\n- Modular design enabling easy feature additions and modifications\n\n### 4. Integration Capabilities\n\nThe application's architecture facilitates easy integration with existing systems through:\n- Clear API-based communication\n- Standardized document processing pipeline\n- Modular component structure\n- Session-based state management\n\nThe Talk to PDF project represents a significant advancement in document interaction technology, combining sophisticated AI capabilities with a user-friendly interface to create a powerful tool for document analysis and information extraction.\n\n# Talk to PDF - Architectural Overview\n\n## System Architecture\n\nThe Talk to PDF application implements a modern, modular architecture built on the Streamlit framework, leveraging OpenAI's language models and LlamaIndex for document processing. The system follows a three-tier architecture pattern with clear separation of concerns:\n\n1. **Presentation Layer**: Streamlit-based user interface\n2. **Processing Layer**: Document indexing and query processing\n3. **Integration Layer**: OpenAI API integration and vector storage\n\n### Architecture Diagram\n\n```mermaid\ngraph TD\n    A[User Interface] --\u003e B[API Key Configuration]\n    B --\u003e C[Document Processing]\n    C --\u003e D[Vector Store Index]\n    D --\u003e E[Chat Interface]\n    E --\u003e F[Query Engine]\n    F --\u003e G[OpenAI API]\n    G --\u003e E\n```\n\n## Core Components\n\n### 1. API Key Configuration Module\n\nThe API key configuration module (`0_🔌API_KEY.py`) serves as the entry point for the application, implementing a secure way to handle OpenAI API credentials:\n\n```python\ndef handle_api_key():\n    api_key = st.text_input(\"Enter your OpenAI API key\", type=\"password\")\n    st.session_state['api_key'] = api_key\n    if not api_key:\n        st.sidebar.warning(\"⚠️ Please enter OpenAI API key\")\n    else:\n        openai.api_key = api_key\n```\n\nThis component features:\n- Secure password-masked input\n- Session state persistence\n- Automatic validation\n- Seamless navigation flow\n\n### 2. Document Processing Engine\n\nThe document processing engine (`pages/1_📑UPLOAD_PDF.py`) handles PDF upload and indexing operations. It utilizes LlamaIndex for efficient document processing:\n\n```python\ndef query_engine(docs, model_name, temperature):    \n    llm = OpenAI(model=model_name, temperature=temperature)\n    with st.spinner(\"Indexing document...\"):\n        index = VectorStoreIndex.from_documents(docs, llm=llm)\n    with st.spinner(\"Creating query engine...\"):\n        query_engine = index.as_query_engine()\n    return query_engine\n```\n\nKey features include:\n- Multiple PDF file support\n- Automatic document indexing\n- Vector store creation\n- Configurable model parameters\n\n### 3. Chat Interface System\n\nThe chat interface (`pages/2_💬CHAT_WITH_PDF.py`) implements an interactive conversation system with context-aware responses:\n\n```python\ncustom_prompt = PromptTemplate(\"\"\"\\\nGiven a conversation (between Human and Assistant) and a follow up message from Human, \\\nrewrite the message to be a standalone question that captures all relevant context \\\nfrom the conversation.\n\n\u003cChat History\u003e \n{chat_history}\n\n\u003cFollow Up Message\u003e\n{question}\n\n\u003cStandalone question\u003e\n\"\"\")\n```\n\nNotable features:\n- Streaming responses\n- Chat history management\n- Context-aware question processing\n- Custom prompt templates\n\n## Data Flow Architecture\n\n### 1. Input Processing Flow\n\nThe application implements a sequential data flow pattern:\n\n1. **API Key Validation**\n   - User inputs API key\n   - System validates and stores in session state\n   - Enables access to document processing\n\n2. **Document Processing Pipeline**\n   - PDF upload triggers document reader\n   - Content extraction and preprocessing\n   - Vector index generation\n   - Storage in session state\n\n3. **Query Processing Chain**\n   - User input captured\n   - Context integration\n   - Query reformation\n   - Response generation and streaming\n\n### 2. State Management\n\nThe application utilizes Streamlit's session state for persistent data management:\n\n```python\nif 'history' not in st.session_state:\n    st.session_state['history'] = []\nif 'generated' not in st.session_state:\n    st.session_state['generated'] = [\"Hello! Ask me anything about the uploaded document 🤗\"]\nif 'past' not in st.session_state:\n    st.session_state['past'] = [\"Hey! 👋\"]\n```\n\nKey state components:\n- API key storage\n- Document index persistence\n- Chat history management\n- Query engine state\n\n## Technical Implementation Details\n\n### 1. Vector Store Implementation\n\nThe system uses LlamaIndex's VectorStoreIndex for efficient document querying:\n\n```python\ndef load_document(uploaded_files):\n    temp_dir = tempfile.TemporaryDirectory()\n    for file in uploaded_files:\n        temp_filepath = os.path.join(temp_dir.name, file.name)\n        with open(temp_filepath, \"wb\") as f:\n            f.write(file.getvalue())\n    reader = SimpleDirectoryReader(input_dir=temp_dir.name)\n    docs = reader.load_data()\n    return docs\n```\n\n### 2. Chat Engine Configuration\n\nThe chat engine implements a custom configuration for context-aware responses:\n\n```python\nchat_engine = CondenseQuestionChatEngine.from_defaults(\n    query_engine=query_engine, \n    condense_question_prompt=custom_prompt,\n    chat_history=custom_chat_history\n)\n```\n\n## Architectural Decisions and Rationale\n\n### 1. Technology Choices\n\n- **Streamlit**: Selected for rapid development and interactive UI capabilities\n- **LlamaIndex**: Chosen for efficient document processing and vector storage\n- **OpenAI Integration**: Provides powerful language understanding capabilities\n\n### 2. Design Patterns\n\nThe application implements several key design patterns:\n\n1. **Modular Architecture**\n   - Separate pages for distinct functionality\n   - Centralized utility functions\n   - Clear component boundaries\n\n2. **State Management Pattern**\n   - Session-based state persistence\n   - Centralized state management\n   - Clear state initialization\n\n3. **Stream Processing Pattern**\n   - Real-time response streaming\n   - Asynchronous document processing\n   - Progressive UI updates\n\nThis architecture ensures scalability, maintainability, and a smooth user experience while maintaining robust security and performance characteristics.\n\n# Component Breakdown: Talk to PDF System\n\n## API Configuration Module\n\nThe API Configuration module serves as the initial entry point for the Talk to PDF application, handling OpenAI API key validation and storage. This component is crucial for enabling the AI-powered functionality throughout the application.\n\n### Primary Functions\n\n1. **API Key Input and Validation**\n   - Provides a secure input interface for users to enter their OpenAI API key\n   - Validates the key format and stores it in the session state\n   - Manages the transition to the PDF upload page upon successful configuration\n\n### Implementation Details\n\nThe module is implemented in `0_🔌API_KEY.py` and utilizes Streamlit's session state management for persistent storage. Key features include:\n\n```python\nst.set_page_config(page_title=\"Talk to PDF\", page_icon=\":robot_face:\", layout=\"wide\")\nst.title(\"Talk to your PDF 🤖 📑️\")\n\napi_key = st.text_input(\"Enter your OpenAI API key\", type=\"password\")\nst.session_state['api_key'] = api_key\n\nif not api_key:\n    st.sidebar.warning(\"⚠️ Please enter OpenAI API key\")\nelse:\n    openai.api_key = api_key\n```\n\nThe module implements secure key storage using password-masked input and provides immediate feedback through the sidebar. Upon successful key submission, it triggers a page transition:\n\n```python\nsubmit = st.button(\"Submit\", use_container_width=True)\nif submit:\n    st.sidebar.success(\"✅ API key entered successfully\")\n    time.sleep(1.5)\n    switch_page('upload pdf')\n```\n\n## Document Processing Module\n\nThe Document Processing module handles PDF file uploads, document indexing, and vector store creation. This component transforms raw PDF documents into queryable knowledge bases.\n\n### Primary Functions\n\n1. **Document Upload Handling**\n   - Manages file uploads through Streamlit's file uploader\n   - Validates PDF file format\n   - Creates temporary storage for document processing\n\n2. **Document Indexing**\n   - Processes PDF content using LlamaIndex\n   - Creates vector embeddings for efficient querying\n   - Establishes the query engine for chat functionality\n\n### Implementation Details\n\nLocated in `pages/1_📑UPLOAD_PDF.py`, the module implements sophisticated document processing:\n\n```python\ndef load_document(uploaded_files):\n    temp_dir = tempfile.TemporaryDirectory()\n    for file in uploaded_files:\n        temp_filepath = os.path.join(temp_dir.name, file.name)\n        with open(temp_filepath, \"wb\") as f:\n            f.write(file.getvalue())\n\n    reader = SimpleDirectoryReader(input_dir=temp_dir.name)\n    docs = reader.load_data()\n    return docs\n```\n\nThe indexing process utilizes OpenAI's language models for creating searchable document representations:\n\n```python\ndef query_engine(docs, model_name, temperature):    \n    llm = OpenAI(model=model_name, temperature=temperature)\n    Settings.llm = llm\n    with st.spinner(\"Indexing document...\"):\n        index = VectorStoreIndex.from_documents(docs, llm=llm)\n    with st.spinner(\"Creating query engine...\"):\n        query_engine = index.as_query_engine()\n    \n    st.session_state['index'] = index\n    st.session_state['query_engine'] = query_engine\n    return query_engine\n```\n\n## Chat Interface Module\n\nThe Chat Interface module provides an interactive environment for users to query their PDF documents using natural language. This component handles the conversation flow and response generation.\n\n### Primary Functions\n\n1. **Chat Management**\n   - Maintains conversation history\n   - Handles user input processing\n   - Manages response streaming and display\n\n2. **Context-Aware Question Answering**\n   - Reformulates questions to maintain context\n   - Generates relevant responses using the query engine\n   - Streams responses for better user experience\n\n### Implementation Details\n\nImplemented in `pages/2_💬CHAT_WITH_PDF.py`, the module uses a custom prompt template for context-aware responses:\n\n```python\ncustom_prompt = PromptTemplate(\"\"\"\\\nGiven a conversation (between Human and Assistant) and a follow up message from Human, \\\nrewrite the message to be a standalone question that captures all relevant context \\\nfrom the conversation.\n\n\u003cChat History\u003e \n{chat_history}\n\n\u003cFollow Up Message\u003e\n{question}\n\n\u003cStandalone question\u003e\n\"\"\")\n```\n\nThe chat engine implementation includes streaming capabilities for real-time response generation:\n\n```python\ndef conversational_chat(query):\n    streaming_response = chat_engine.stream_chat(query)\n    response_tokens = []\n    for token in streaming_response.response_gen:\n        response_tokens.append(token)\n    return ''.join(response_tokens)\n```\n\n### Component Interactions\n\nThe three modules work together in a sequential flow:\n\n1. The API Configuration module initializes the OpenAI client and enables AI functionality\n2. The Document Processing module uses the configured API to create document indices\n3. The Chat Interface module leverages both the API configuration and document indices to provide interactive question-answering capabilities\n\nThis architecture ensures a smooth user experience while maintaining separation of concerns and modularity in the codebase.\n\n## Error Handling and State Management\n\nEach component implements robust error handling and state management:\n\n- API Configuration validates keys and provides clear feedback\n- Document Processing includes upload validation and processing status indicators\n- Chat Interface maintains conversation state and handles streaming errors gracefully\n\nThe application uses Streamlit's session state for persistent storage across components, ensuring a seamless user experience throughout the interaction flow.\n\n# Technology Stack Documentation - Talk to PDF\n\n## Core Technologies\n\n### Python\n- **Version**: 3.11 (specified in devcontainer.json)\n- **Role**: Primary programming language for the application\n- **Justification**: Python was chosen for its extensive machine learning and NLP libraries, excellent web framework support through Streamlit, and seamless integration with OpenAI's APIs. The language's readability and extensive package ecosystem make it ideal for rapid development of AI-powered applications.\n\n### Streamlit\n- **Version**: 1.38.0\n- **Role**: Web application framework and user interface\n- **Justification**: Streamlit provides a rapid development environment for data-focused applications with minimal frontend code. Its built-in components and session state management make it perfect for creating interactive AI applications.\n\nExample usage from `0_🔌API_KEY.py`:\n```python\nst.set_page_config(page_title=\"Talk to PDF\", page_icon=\":robot_face:\", layout=\"wide\")\nst.title(\"Talk to your PDF 🤖 📑️\")\napi_key = st.text_input(\"Enter your OpenAI API key\", type=\"password\")\n```\n\n### OpenAI Integration\n- **Version**: 1.43.0\n- **Role**: Natural language processing and question answering\n- **Justification**: OpenAI's GPT models provide state-of-the-art natural language understanding and generation capabilities, essential for accurate PDF content analysis and question answering.\n\nImplementation example from `functions.py`:\n```python\ndef query_engine(docs, model_name, temperature):    \n    llm = OpenAI(model=model_name, temperature=temperature)\n    Settings.llm = llm\n    index = VectorStoreIndex.from_documents(docs, llm=llm)\n    query_engine = index.as_query_engine()\n    return query_engine\n```\n\n## Document Processing Stack\n\n### LlamaIndex\n- **Version**: 0.11.3\n- **Role**: Document indexing and retrieval system\n- **Justification**: LlamaIndex provides sophisticated document processing capabilities with built-in support for various document types and vector storage systems. It seamlessly integrates with OpenAI's embeddings for efficient document querying.\n\nKey components:\n- `VectorStoreIndex`: Document indexing and retrieval\n- `SimpleDirectoryReader`: PDF file processing\n- `CondenseQuestionChatEngine`: Context-aware question answering\n\nExample implementation:\n```python\nfrom llama_index.core import VectorStoreIndex, SimpleDirectoryReader\nreader = SimpleDirectoryReader(input_dir=temp_dir.name)\ndocs = reader.load_data()\nindex = VectorStoreIndex.from_documents(docs, llm=llm)\n```\n\n### PyPDF\n- **Version**: 4.3.1\n- **Role**: PDF file processing and text extraction\n- **Justification**: Provides robust PDF parsing capabilities with support for various PDF formats and structures.\n\n## UI Components and Extensions\n\n### Streamlit Extensions\n- **streamlit-chat**: Version 0.1.1 - Provides chat interface components\n- **streamlit-extras**: Version 0.4.7 - Additional UI utilities\n- **streamlit-faker**: Version 0.0.3 - Test data generation\n- **markdownlit**: Version 0.0.7 - Enhanced markdown rendering\n\nThese extensions enhance the base Streamlit functionality with specialized components for chat interfaces and improved user experience.\n\n## Dependency Management\n\n### Requirements Management\nThe project uses `pip` and `requirements.txt` for dependency management. The requirements file is compiled using `pip-compile`, ensuring reproducible builds across environments.\n\nKey dependencies are organized into categories:\n- Core dependencies (Python packages)\n- UI components (Streamlit and extensions)\n- AI/ML libraries (OpenAI, LlamaIndex)\n- Utility packages (typing, packaging, etc.)\n\nInstallation process:\n```bash\npip install -r requirements.txt\n```\n\n### Version Control\nDependencies are strictly versioned to ensure consistency:\n```txt\nstreamlit==1.38.0\nopenai==1.43.0\nllama-index==0.11.3\npypdf==4.3.1\n```\n\n\n## 📝 License\n\nThis project is [MIT](https://choosealicense.com/licenses/mit/) licensed.\n\n## 📬 Contact\n\nAuthor - [Kaushal](https://www.linkedin.com/in/kaushal-powar-a52b1a159/)\n\nProject Link: [https://github.com/yourusername/talk-to-pdf](https://github.com/yourusername/talk-to-pdf)\n\n---\n\nEnjoy interacting with your PDFs using natural language! 🚀📄\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkaushalpowar%2Ftalk_to_pdf","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkaushalpowar%2Ftalk_to_pdf","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkaushalpowar%2Ftalk_to_pdf/lists"}