Skip to content

feat: Enhance Talk to PDF application #18

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .refactory.tags.cache.v3/cache.db
Binary file not shown.
Binary file added .refactory.tags.cache.v3/cache.db-shm
Binary file not shown.
Binary file added .refactory.tags.cache.v3/cache.db-wal
Binary file not shown.
161 changes: 126 additions & 35 deletions 0_🔌API_KEY.py
Original file line number Diff line number Diff line change
@@ -1,35 +1,126 @@
import streamlit as st
from streamlit_extras.switch_page_button import switch_page
import os
import time
import tempfile
import openai
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader, ServiceContext, set_global_service_context
from llama_index.llms.openai import OpenAI
from functions import sidebar_stuff1


st.set_page_config(page_title="Talk to PDF", page_icon=":robot_face:", layout="wide")
st.title("Talk to your PDF 🤖 📑️")


st.write("#### Enter your OpenAI api key below :")
api_key = st.text_input("Enter your OpenAI API key (https://platform.openai.com/account/api-keys)", type="password")
st.session_state['api_key'] = api_key

if not api_key :
st.sidebar.warning("⚠️ Please enter OpenAI API key")
else:
openai.api_key = api_key

submit = st.button("Submit",use_container_width=True)
if submit:
st.sidebar.success("✅ API key entered successfully")
time.sleep(1.5)
switch_page('upload pdf')
sidebar_stuff1()





"""
API Key Configuration Page for Talk to PDF Application

This module handles the OpenAI API key configuration and validation for the Talk to PDF application.
It provides a user interface for API key input and manages the transition to the PDF upload page
once the key is successfully validated.
"""

import time
from typing import Optional

import openai
import streamlit as st
from streamlit_extras.switch_page_button import switch_page

from functions import sidebar_stuff1
from llama_index.core import (
VectorStoreIndex,
SimpleDirectoryReader,
ServiceContext,
set_global_service_context
)
from llama_index.llms.openai import OpenAI

# Configuration Constants
PAGE_TITLE = "Talk to PDF"
PAGE_ICON = ":robot_face:"
API_KEY_PLACEHOLDER = "Enter your OpenAI API key (https://platform.openai.com/account/api-keys)"
SUCCESS_MESSAGE = "✅ API key entered successfully"
WARNING_MESSAGE = "⚠️ Please enter OpenAI API key"
REDIRECT_DELAY = 1.5

class APIKeyManager:
"""Manages the OpenAI API key configuration and validation."""

@staticmethod
def validate_api_key(api_key: str) -> bool:
"""
Validates the provided OpenAI API key.

Args:
api_key: The API key to validate

Returns:
bool: True if the key is not empty, False otherwise
"""
return bool(api_key and api_key.strip())

@staticmethod
def save_api_key(api_key: str) -> None:
"""
Saves the API key to session state and configures OpenAI.

Args:
api_key: The API key to save
"""
st.session_state['api_key'] = api_key
openai.api_key = api_key

class PageConfig:
"""Handles page configuration and UI setup."""

@staticmethod
def setup_page() -> None:
"""Configures the Streamlit page settings."""
st.set_page_config(
page_title=PAGE_TITLE,
page_icon=PAGE_ICON,
layout="wide"
)
st.title("Talk to your PDF 🤖 📑️")

@staticmethod
def render_api_key_input() -> Optional[str]:
"""
Renders the API key input field.

Returns:
Optional[str]: The entered API key or None
"""
st.write("#### Enter your OpenAI api key below :")
return st.text_input(
API_KEY_PLACEHOLDER,
type="password"
)

@staticmethod
def render_submit_button() -> bool:
"""
Renders the submit button.

Returns:
bool: True if button is clicked, False otherwise
"""
return st.button("Submit", use_container_width=True)

def main() -> None:
"""Main function to run the API key configuration page."""

# Initialize page configuration
page_config = PageConfig()
page_config.setup_page()

# Initialize API key manager
api_manager = APIKeyManager()

# Render API key input
api_key = page_config.render_api_key_input()

# Handle API key validation
if not api_manager.validate_api_key(api_key):
st.sidebar.warning(WARNING_MESSAGE)
else:
api_manager.save_api_key(api_key)

# Handle form submission
if page_config.render_submit_button() and api_key:
st.sidebar.success(SUCCESS_MESSAGE)
time.sleep(REDIRECT_DELAY)
switch_page('upload pdf')

# Render sidebar content
sidebar_stuff1()

if __name__ == "__main__":
main()
114 changes: 94 additions & 20 deletions LEARN.md
Original file line number Diff line number Diff line change
@@ -1,29 +1,103 @@
# Talk to PDF 🤖 📑️

This is the README file for the "Talk to PDF" code. The code is written in Python and uses the Streamlit library to create an interactive web application. The application allows users to ask questions about the content of a PDF file using natural language and receive instant answers powered by an AI question-answering system.
[![Python 3.11](https://img.shields.io/badge/python-3.11-blue.svg)](https://www.python.org/downloads/)
[![Streamlit](https://img.shields.io/badge/Streamlit-1.38.0-FF4B4B.svg)](https://streamlit.io)
[![OpenAI](https://img.shields.io/badge/OpenAI-1.43.0-412991.svg)](https://openai.com)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)

## Usage
An intelligent document interaction system that enables natural language conversations with PDF documents using AI-powered question-answering capabilities.

1. **Step 1: Enter your OpenAI API key**
- Open the application and find the "Step 1: Enter your OpenAI API key" section.
- Obtain an OpenAI API key from the OpenAI platform if you don't have one.
- Enter your API key in the text input field.
- Click the "Submit" button to set the OpenAI API key.
## 🌟 Features

2. **Step 2: Upload your PDF**
- In the "Step 2: Upload your PDF" section, click the "Browse Files" button.
- Select a PDF file from your device to upload.
- Wait for the PDF to finish uploading.
- Natural language interaction with PDF documents
- Real-time AI-powered question answering
- Context-aware conversation handling
- Support for multiple PDF documents
- Customizable AI model parameters
- User-friendly web interface
- Streaming responses for better user experience

3. **Ask a question**
- Once the PDF is uploaded, you will see an input box labeled "Ask a question."
- Enter your question about the PDF content in the input box.
## 📋 Requirements

4. **Get the answer**
- Click the "Ask" button to submit your question.
- The application will use the uploaded PDF and the question to generate a response using the AI question-answering system.
- The response will be displayed on the screen.
- Python 3.11 or higher
- OpenAI API key
- Modern web browser
- Internet connection

Feel free to reach out to the author, [@Obelisk_1531](https://twitter.com/Obelisk_1531), for any questions or feedback.
## 🚀 Quick Start

Enjoy interacting with your PDFs using natural language! 🚀📄
### Installation

1. Clone the repository:
```bash
git clone https://github.com/yourusername/talk-to-pdf.git
cd talk-to-pdf
```

2. Install dependencies:
```bash
pip install -r requirements.txt
```

### Usage Guide

1. **API Key Setup**
- Launch the application
- Navigate to the API key configuration section
- Enter your OpenAI API key (Get one from [OpenAI Platform](https://platform.openai.com/account/api-keys))
- Click "Submit" to save your configuration

2. **Document Upload**
- Go to the PDF upload section
- Click "Browse Files" or drag and drop your PDF
- Wait for the document processing to complete
- Multiple PDFs are supported

3. **Interactive Chat**
- Type your question in the chat input box
- Click "Send" or press Enter
- View the AI-generated response
- Continue the conversation naturally

4. **Advanced Options**
- Adjust model temperature for response variety
- Choose between different GPT models
- Configure chat context settings

## 🛠️ Technical Architecture

- **Frontend**: Streamlit web application
- **Backend**: Python with LlamaIndex
- **AI Engine**: OpenAI GPT models
- **Document Processing**: PDF parsing and vector indexing
- **Storage**: Vector store for document embeddings

## 🤝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.

1. Fork the repository
2. Create your feature branch (`git checkout -b feature/AmazingFeature`)
3. Commit your changes (`git commit -m 'Add some AmazingFeature'`)
4. Push to the branch (`git push origin feature/AmazingFeature`)
5. Open a Pull Request

## 📝 License

This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.

## 🔗 Contact

Author: Kaushal - [@holy_kau](https://twitter.com/holy_kau)

Project Link: [https://github.com/yourusername/talk-to-pdf](https://github.com/yourusername/talk-to-pdf)

## 🙏 Acknowledgments

- OpenAI for providing the GPT models
- Streamlit team for the amazing web framework
- LlamaIndex for document processing capabilities

---

Made with ❤️ by Kaushal | [Support my work](https://www.buymeacoffee.com/kaushal.ai)
Loading