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