Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import openai
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from langchain.chains import RetrievalQA
|
| 4 |
+
from langchain.llms import OpenAI
|
| 5 |
+
from langchain.document_loaders import PyPDFLoader
|
| 6 |
+
from langchain.embeddings.openai import OpenAIEmbeddings
|
| 7 |
+
from langchain.vectorstores import FAISS
|
| 8 |
+
from langchain.chat_models import ChatOpenAI
|
| 9 |
+
from PyPDF2 import PdfReader
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
def load_pdf(file):
|
| 13 |
+
loader=PyPDFLoader(file.name)
|
| 14 |
+
documents=loader.load()
|
| 15 |
+
return documents
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
def summarize_pdf(file, openai_api_key):
|
| 19 |
+
openai.api_key=openai_api_key
|
| 20 |
+
documents=load_pdf(file)
|
| 21 |
+
embeddings=OpenAIEmbeddings(openai_api_key=openai_api_key)
|
| 22 |
+
vector_store=FAISS.from_documents(documents,embeddings)
|
| 23 |
+
llm=ChatOpenAI(model="gpt-4o", openai_api_key=openai_api_key)
|
| 24 |
+
qa_chain=RetrievalQA.from_chain_type(
|
| 25 |
+
llm=llm,
|
| 26 |
+
chain_type="stuff",
|
| 27 |
+
retriever=vector_store.as_retriever()
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
response=qa_chain.run("Summarize the content of the research paper.")
|
| 31 |
+
|
| 32 |
+
return response
|
| 33 |
+
|
| 34 |
+
# Function to handle user queries and provide answers from the document
|
| 35 |
+
def query_pdf(file, user_query, openai_api_key):
|
| 36 |
+
|
| 37 |
+
# Set the OpenAI API key dynamically
|
| 38 |
+
openai.api_key = openai_api_key
|
| 39 |
+
|
| 40 |
+
# Load and process the PDF
|
| 41 |
+
documents = load_pdf(file)
|
| 42 |
+
|
| 43 |
+
# Create embeddings for the documents
|
| 44 |
+
embeddings = OpenAIEmbeddings(openai_api_key=openai_api_key)
|
| 45 |
+
|
| 46 |
+
# Use LangChain's FAISS Vector Store to store and search the embeddings
|
| 47 |
+
vector_store = FAISS.from_documents(documents, embeddings)
|
| 48 |
+
|
| 49 |
+
# Create a RetrievalQA chain for querying the document
|
| 50 |
+
llm = ChatOpenAI(model="gpt-4o", openai_api_key=openai_api_key) # Passing API key here
|
| 51 |
+
qa_chain = RetrievalQA.from_chain_type(
|
| 52 |
+
llm=llm,
|
| 53 |
+
chain_type="stuff",
|
| 54 |
+
retriever=vector_store.as_retriever()
|
| 55 |
+
)
|
| 56 |
+
|
| 57 |
+
# Query the model for the user query
|
| 58 |
+
response = qa_chain.run(user_query)
|
| 59 |
+
|
| 60 |
+
return response
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
# Define Gradio interface for the summarization
|
| 65 |
+
def create_gradio_interface():
|
| 66 |
+
with gr.Blocks() as demo:
|
| 67 |
+
gr.Markdown("### ChatPDF and Research Paper Summarizer using GPT-4 and LangChain")
|
| 68 |
+
|
| 69 |
+
# Input field for API Key
|
| 70 |
+
with gr.Row():
|
| 71 |
+
openai_api_key_input = gr.Textbox(label="Enter OpenAI API Key", type="password", placeholder="Enter your OpenAI API key here")
|
| 72 |
+
|
| 73 |
+
with gr.Tab("Summarize PDF"):
|
| 74 |
+
with gr.Row():
|
| 75 |
+
pdf_file = gr.File(label="Upload PDF Document")
|
| 76 |
+
summarize_btn = gr.Button("Summarize")
|
| 77 |
+
summary_output = gr.Textbox(label="Summary", interactive=False)
|
| 78 |
+
clear_btn_summary = gr.Button("Clear Response")
|
| 79 |
+
|
| 80 |
+
# Summarize Button Logic
|
| 81 |
+
summarize_btn.click(summarize_pdf, inputs=[pdf_file, openai_api_key_input], outputs=summary_output)
|
| 82 |
+
|
| 83 |
+
# Clear Response Button Logic for Summary Tab
|
| 84 |
+
clear_btn_summary.click(lambda: "", inputs=[], outputs=summary_output)
|
| 85 |
+
|
| 86 |
+
with gr.Tab("Ask Questions"):
|
| 87 |
+
with gr.Row():
|
| 88 |
+
pdf_file_q = gr.File(label="Upload PDF Document")
|
| 89 |
+
user_input = gr.Textbox(label="Enter your question")
|
| 90 |
+
answer_output = gr.Textbox(label="Answer", interactive=False)
|
| 91 |
+
clear_btn_answer = gr.Button("Clear Response")
|
| 92 |
+
|
| 93 |
+
# Submit Question Logic
|
| 94 |
+
user_input.submit(query_pdf, inputs=[pdf_file_q, user_input, openai_api_key_input], outputs=answer_output)
|
| 95 |
+
|
| 96 |
+
# Clear Response Button Logic for Answer Tab
|
| 97 |
+
clear_btn_answer.click(lambda: "", inputs=[], outputs=answer_output)
|
| 98 |
+
|
| 99 |
+
user_input.submit(None, None, answer_output) # Clear answer when typing new query
|
| 100 |
+
|
| 101 |
+
return demo
|
| 102 |
+
|
| 103 |
+
# Run Gradio app
|
| 104 |
+
if __name__ == "__main__":
|
| 105 |
+
demo = create_gradio_interface()
|
| 106 |
+
demo.launch(debug=True)
|
| 107 |
+
|
| 108 |
+
|
| 109 |
+
|
| 110 |
+
|
| 111 |
+
|