Spaces:
Runtime error
Runtime error
PDF-Summarizer-and-ChatPDF
#1
by saurav384 - opened
- app.py +0 -59
- pdfchat.py +0 -59
app.py
DELETED
|
@@ -1,59 +0,0 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
from PyPDF2 import PdfReader
|
| 3 |
-
from langchain.text_splitter import CharacterTextSplitter
|
| 4 |
-
from langchain.embeddings.openai import OpenAIEmbeddings
|
| 5 |
-
from langchain.vectorstores import FAISS
|
| 6 |
-
from langchain.chains.question_answering import load_qa_chain
|
| 7 |
-
from langchain.chat_models import ChatOpenAI
|
| 8 |
-
from langchain.callbacks import get_openai_callback
|
| 9 |
-
|
| 10 |
-
api_key = "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" # your API key
|
| 11 |
-
|
| 12 |
-
def main():
|
| 13 |
-
st.set_page_config(page_title="PDF Summarizer")
|
| 14 |
-
st.header("PDF Summarizer")
|
| 15 |
-
|
| 16 |
-
pdf = st.file_uploader("Upload PDF file here", type="pdf")
|
| 17 |
-
|
| 18 |
-
if pdf is not None:
|
| 19 |
-
pdf_reader = PdfReader(pdf)
|
| 20 |
-
text = ""
|
| 21 |
-
|
| 22 |
-
for page in pdf_reader.pages:
|
| 23 |
-
text += page.extract_text()
|
| 24 |
-
|
| 25 |
-
text_splitter = CharacterTextSplitter(
|
| 26 |
-
separator="\n",
|
| 27 |
-
chunk_size=1000,
|
| 28 |
-
chunk_overlap=200,
|
| 29 |
-
length_function=len
|
| 30 |
-
)
|
| 31 |
-
|
| 32 |
-
chunks = text_splitter.split_text(text)
|
| 33 |
-
|
| 34 |
-
embeddings = OpenAIEmbeddings(
|
| 35 |
-
openai_api_key=api_key,
|
| 36 |
-
openai_api_base="https://openrouter.ai/api/v1"
|
| 37 |
-
)
|
| 38 |
-
|
| 39 |
-
knowledge_base = FAISS.from_texts(chunks, embeddings)
|
| 40 |
-
user_question = st.text_input("Ask Question to uploaded PDF!")
|
| 41 |
-
|
| 42 |
-
if user_question:
|
| 43 |
-
docs = knowledge_base.similarity_search(user_question)
|
| 44 |
-
|
| 45 |
-
llm = ChatOpenAI(
|
| 46 |
-
openai_api_key=api_key,
|
| 47 |
-
openai_api_base="https://openrouter.ai/api/v1",
|
| 48 |
-
model_name="gpt-4o-mini" # ✅ must be model_name
|
| 49 |
-
)
|
| 50 |
-
|
| 51 |
-
chain = load_qa_chain(llm, chain_type="stuff")
|
| 52 |
-
|
| 53 |
-
with get_openai_callback() as cb:
|
| 54 |
-
response = chain.run(input_documents=docs, question=user_question)
|
| 55 |
-
print(cb)
|
| 56 |
-
|
| 57 |
-
st.write(response)
|
| 58 |
-
|
| 59 |
-
main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pdfchat.py
DELETED
|
@@ -1,59 +0,0 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
from PyPDF2 import PdfReader
|
| 3 |
-
from langchain.text_splitter import CharacterTextSplitter
|
| 4 |
-
from langchain.embeddings.openai import OpenAIEmbeddings
|
| 5 |
-
from langchain.vectorstores import FAISS
|
| 6 |
-
from langchain.chains.question_answering import load_qa_chain
|
| 7 |
-
from langchain.chat_models import ChatOpenAI
|
| 8 |
-
from langchain.callbacks import get_openai_callback
|
| 9 |
-
|
| 10 |
-
api_key = "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" # your API key
|
| 11 |
-
|
| 12 |
-
def main():
|
| 13 |
-
st.set_page_config(page_title="PDF Summarizer")
|
| 14 |
-
st.header("PDF Summarizer")
|
| 15 |
-
|
| 16 |
-
pdf = st.file_uploader("Upload PDF file here", type="pdf")
|
| 17 |
-
|
| 18 |
-
if pdf is not None:
|
| 19 |
-
pdf_reader = PdfReader(pdf)
|
| 20 |
-
text = ""
|
| 21 |
-
|
| 22 |
-
for page in pdf_reader.pages:
|
| 23 |
-
text += page.extract_text()
|
| 24 |
-
|
| 25 |
-
text_splitter = CharacterTextSplitter(
|
| 26 |
-
separator="\n",
|
| 27 |
-
chunk_size=1000,
|
| 28 |
-
chunk_overlap=200,
|
| 29 |
-
length_function=len
|
| 30 |
-
)
|
| 31 |
-
|
| 32 |
-
chunks = text_splitter.split_text(text)
|
| 33 |
-
|
| 34 |
-
embeddings = OpenAIEmbeddings(
|
| 35 |
-
openai_api_key=api_key,
|
| 36 |
-
openai_api_base="https://openrouter.ai/api/v1"
|
| 37 |
-
)
|
| 38 |
-
|
| 39 |
-
knowledge_base = FAISS.from_texts(chunks, embeddings)
|
| 40 |
-
user_question = st.text_input("Ask Question to uploaded PDF!")
|
| 41 |
-
|
| 42 |
-
if user_question:
|
| 43 |
-
docs = knowledge_base.similarity_search(user_question)
|
| 44 |
-
|
| 45 |
-
llm = ChatOpenAI(
|
| 46 |
-
openai_api_key=api_key,
|
| 47 |
-
openai_api_base="https://openrouter.ai/api/v1",
|
| 48 |
-
model_name="gpt-4o-mini" # ✅ must be model_name
|
| 49 |
-
)
|
| 50 |
-
|
| 51 |
-
chain = load_qa_chain(llm, chain_type="stuff")
|
| 52 |
-
|
| 53 |
-
with get_openai_callback() as cb:
|
| 54 |
-
response = chain.run(input_documents=docs, question=user_question)
|
| 55 |
-
print(cb)
|
| 56 |
-
|
| 57 |
-
st.write(response)
|
| 58 |
-
|
| 59 |
-
main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|