Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import pdfplumber
|
| 3 |
+
import openai
|
| 4 |
+
from dotenv import load_dotenv
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
+
load_dotenv() # Load environment variables from .env file
|
| 8 |
+
|
| 9 |
+
# Safe retrieval of API key from environment variables
|
| 10 |
+
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 11 |
+
|
| 12 |
+
# Streamlit UI setup for the application
|
| 13 |
+
st.title("Advanced PDF-Based Application")
|
| 14 |
+
st.markdown("Select the functionality you want to use from the sidebar.")
|
| 15 |
+
|
| 16 |
+
# Sidebar for mode selection and file uploading
|
| 17 |
+
with st.sidebar:
|
| 18 |
+
mode = st.radio("Choose a mode:", ["PDF Summarizer", "Question Answering"])
|
| 19 |
+
uploaded_files = st.file_uploader("Upload PDF files", accept_multiple_files=True, type=['pdf'], on_change=lambda: st.experimental_rerun())
|
| 20 |
+
|
| 21 |
+
# Initializing documents list
|
| 22 |
+
documents = []
|
| 23 |
+
|
| 24 |
+
# Progress bar for file processing
|
| 25 |
+
if uploaded_files:
|
| 26 |
+
with st.spinner('Processing PDF files...'):
|
| 27 |
+
progress_bar = st.progress(0)
|
| 28 |
+
total_files = len(uploaded_files)
|
| 29 |
+
for i, uploaded_file in enumerate(uploaded_files):
|
| 30 |
+
with pdfplumber.open(uploaded_file) as pdf:
|
| 31 |
+
full_text = ""
|
| 32 |
+
for page in pdf.pages[:50]: # Process each page up to a limit of 50 pages
|
| 33 |
+
full_text += page.extract_text() or ""
|
| 34 |
+
documents.append(full_text)
|
| 35 |
+
progress_bar.progress((i + 1) / total_files)
|
| 36 |
+
st.success("PDFs processed successfully. Proceed based on the selected mode.")
|
| 37 |
+
progress_bar.empty()
|
| 38 |
+
|
| 39 |
+
# Using tabs to separate features
|
| 40 |
+
tab1, tab2 = st.tabs(["Question Answering", "PDF Summarizer"])
|
| 41 |
+
|
| 42 |
+
with tab1:
|
| 43 |
+
if mode == "Question Answering":
|
| 44 |
+
question = st.text_input("Enter your question here:")
|
| 45 |
+
if question and documents:
|
| 46 |
+
combined_text = "\n".join(documents[:3])
|
| 47 |
+
messages = [
|
| 48 |
+
{"role": "system", "content": "You are a helpful assistant."},
|
| 49 |
+
{"role": "user", "content": question},
|
| 50 |
+
{"role": "system", "content": combined_text}
|
| 51 |
+
]
|
| 52 |
+
response = openai.ChatCompletion.create(
|
| 53 |
+
model="gpt-4",
|
| 54 |
+
messages=messages,
|
| 55 |
+
max_tokens=500
|
| 56 |
+
)
|
| 57 |
+
st.write("Answer:", response.choices[0].message['content'])
|
| 58 |
+
|
| 59 |
+
with tab2:
|
| 60 |
+
if mode == "PDF Summarizer" and documents:
|
| 61 |
+
summaries = []
|
| 62 |
+
for doc in documents[:3]:
|
| 63 |
+
messages = [
|
| 64 |
+
{"role": "system", "content": "You are a helpful assistant tasked to summarize documents."},
|
| 65 |
+
{"role": "user", "content": "Summarize the following text brifly:\n" + doc}
|
| 66 |
+
]
|
| 67 |
+
response = openai.ChatCompletion.create(
|
| 68 |
+
model="gpt-4",
|
| 69 |
+
messages=messages,
|
| 70 |
+
max_tokens=1024
|
| 71 |
+
)
|
| 72 |
+
summaries.append(response.choices[0].message['content'].strip())
|
| 73 |
+
|
| 74 |
+
for idx, summary in enumerate(summaries):
|
| 75 |
+
st.write(f"Summary {idx+1}:", summary)
|