shukdevdatta123 commited on
Commit
bf6d94a
·
verified ·
1 Parent(s): 9040ebc

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +101 -0
app.py ADDED
@@ -0,0 +1,101 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import openai
2
+ import gradio as gr
3
+ from langchain.chains import ConversationalChain
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 langchain.prompts import ChatPromptTemplate, MessagesPlaceholder
10
+ from PyPDF2 import PdfReader
11
+ import os
12
+
13
+ # Function to load and process the PDF document
14
+ def load_pdf(file):
15
+ # Load the PDF using PyPDF2 or LangChain's built-in loader
16
+ loader = PyPDFLoader(file.name)
17
+ documents = loader.load()
18
+ return documents
19
+
20
+ # Summarization function using GPT-4
21
+ def summarize_pdf(file, openai_api_key):
22
+ # Set the API key dynamically
23
+ openai.api_key = openai_api_key
24
+
25
+ documents = load_pdf(file)
26
+
27
+ # Create embeddings for the documents
28
+ embeddings = OpenAIEmbeddings()
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 conversational chain that allows us to query the document
34
+ llm = ChatOpenAI(model="gpt-4") # Using GPT-4 as the LLM
35
+ conversational_chain = ConversationalChain(
36
+ llm=llm,
37
+ vectorstore=vector_store,
38
+ verbose=True
39
+ )
40
+
41
+ # Query the model for a summary
42
+ response = conversational_chain.run("Summarize the content of the research paper.")
43
+ return response
44
+
45
+ # Function to handle user queries and provide answers from the document
46
+ def query_pdf(file, user_query, openai_api_key):
47
+ # Set the API key dynamically
48
+ openai.api_key = openai_api_key
49
+
50
+ documents = load_pdf(file)
51
+
52
+ # Create embeddings for the documents
53
+ embeddings = OpenAIEmbeddings()
54
+
55
+ # Use LangChain's FAISS Vector Store to store and search the embeddings
56
+ vector_store = FAISS.from_documents(documents, embeddings)
57
+
58
+ # Create a conversational chain that allows us to query the document
59
+ llm = ChatOpenAI(model="gpt-4") # Using GPT-4 as the LLM
60
+ conversational_chain = ConversationalChain(
61
+ llm=llm,
62
+ vectorstore=vector_store,
63
+ verbose=True
64
+ )
65
+
66
+ # Query the model for the user query
67
+ response = conversational_chain.run(user_query)
68
+ return response
69
+
70
+ # Define Gradio interface for the summarization
71
+ def create_gradio_interface():
72
+ with gr.Blocks() as demo:
73
+ gr.Markdown("### ChatPDF and Research Paper Summarizer using GPT-4 and LangChain")
74
+
75
+ # Input field for API Key
76
+ with gr.Row():
77
+ openai_api_key_input = gr.Textbox(label="Enter OpenAI API Key", type="password", placeholder="Enter your OpenAI API key here")
78
+
79
+ with gr.Tab("Summarize PDF"):
80
+ with gr.Row():
81
+ pdf_file = gr.File(label="Upload PDF Document")
82
+ summarize_btn = gr.Button("Summarize")
83
+ summary_output = gr.Textbox(label="Summary", interactive=False)
84
+
85
+ summarize_btn.click(summarize_pdf, inputs=[pdf_file, openai_api_key_input], outputs=summary_output)
86
+
87
+ with gr.Tab("Ask Questions"):
88
+ with gr.Row():
89
+ pdf_file_q = gr.File(label="Upload PDF Document")
90
+ user_input = gr.Textbox(label="Enter your question")
91
+ answer_output = gr.Textbox(label="Answer", interactive=False)
92
+
93
+ user_input.submit(query_pdf, inputs=[pdf_file_q, user_input, openai_api_key_input], outputs=answer_output)
94
+ user_input.submit(None, None, answer_output) # Clear answer when typing new query
95
+
96
+ return demo
97
+
98
+ # Run Gradio app
99
+ if __name__ == "__main__":
100
+ demo = create_gradio_interface()
101
+ demo.launch(debug=True)