Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -72,51 +72,77 @@ def query_pdf(file,user_query,openai_api_key):
|
|
72 |
response = qa_chain.run(user_query)
|
73 |
return response
|
74 |
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
|
80 |
-
|
81 |
-
|
82 |
-
openai_api_key_input=gr.Textbox(label="Enter OpenAI API key",type ="password",placeholder="Enter your openai api key here")
|
83 |
|
84 |
-
|
85 |
-
|
86 |
-
pdf_file = gr.file(label="Upload PDF Document")
|
87 |
-
summarize_btn=gr.Button("Summarize")
|
88 |
-
summary_output=gr.Textbox(label="Summary",interactive=False)
|
89 |
-
clear_btn_summary=gr.Button("Clear Response")
|
90 |
|
|
|
|
|
91 |
|
92 |
-
|
93 |
-
|
|
|
|
|
|
|
|
|
|
|
94 |
|
95 |
-
|
96 |
-
|
|
|
97 |
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
|
|
|
|
104 |
|
105 |
-
|
106 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
107 |
|
|
|
|
|
108 |
|
109 |
-
|
110 |
-
|
111 |
|
112 |
-
|
|
|
113 |
return demo
|
114 |
|
115 |
# Run Gradio app
|
116 |
-
if __name__=="__main__":
|
117 |
demo = create_gradio_interface()
|
118 |
demo.launch(debug=True)
|
119 |
|
|
|
120 |
|
121 |
|
122 |
|
|
|
72 |
response = qa_chain.run(user_query)
|
73 |
return response
|
74 |
|
75 |
+
# Function to handle user queries and provide answers from the document
|
76 |
+
def query_pdf(file, user_query, openai_api_key):
|
77 |
+
# Set the OpenAI API key dynamically
|
78 |
+
openai.api_key = openai_api_key
|
79 |
|
80 |
+
# Load and process the PDF
|
81 |
+
documents = load_pdf(file)
|
|
|
82 |
|
83 |
+
# Create embeddings for the documents
|
84 |
+
embeddings = OpenAIEmbeddings(openai_api_key=openai_api_key)
|
|
|
|
|
|
|
|
|
85 |
|
86 |
+
# Use LangChain's FAISS Vector Store to store and search the embeddings
|
87 |
+
vector_store = FAISS.from_documents(documents, embeddings)
|
88 |
|
89 |
+
# Create a RetrievalQA chain for querying the document
|
90 |
+
llm = ChatOpenAI(model="gpt-4o", openai_api_key=openai_api_key) # Passing API key here
|
91 |
+
qa_chain = RetrievalQA.from_chain_type(
|
92 |
+
llm=llm,
|
93 |
+
chain_type="stuff",
|
94 |
+
retriever=vector_store.as_retriever()
|
95 |
+
)
|
96 |
|
97 |
+
# Query the model for the user query
|
98 |
+
response = qa_chain.run(user_query)
|
99 |
+
return response
|
100 |
|
101 |
+
# Define Gradio interface for the summarization
|
102 |
+
def create_gradio_interface():
|
103 |
+
with gr.Blocks() as demo:
|
104 |
+
gr.Markdown("### ChatPDF and Research Paper Summarizer using GPT-4 and LangChain")
|
105 |
+
|
106 |
+
# Input field for API Key
|
107 |
+
with gr.Row():
|
108 |
+
openai_api_key_input = gr.Textbox(label="Enter OpenAI API Key", type="password", placeholder="Enter your OpenAI API key here")
|
109 |
|
110 |
+
with gr.Tab("Summarize PDF"):
|
111 |
+
with gr.Row():
|
112 |
+
pdf_file = gr.File(label="Upload PDF Document")
|
113 |
+
summarize_btn = gr.Button("Summarize")
|
114 |
+
summary_output = gr.Textbox(label="Summary", interactive=False)
|
115 |
+
clear_btn_summary = gr.Button("Clear Response")
|
116 |
+
|
117 |
+
# Summarize Button Logic
|
118 |
+
summarize_btn.click(summarize_pdf, inputs=[pdf_file, openai_api_key_input], outputs=summary_output)
|
119 |
+
|
120 |
+
# Clear Response Button Logic for Summary Tab
|
121 |
+
clear_btn_summary.click(lambda: "", inputs=[], outputs=summary_output)
|
122 |
+
|
123 |
+
with gr.Tab("Ask Questions"):
|
124 |
+
with gr.Row():
|
125 |
+
pdf_file_q = gr.File(label="Upload PDF Document")
|
126 |
+
user_input = gr.Textbox(label="Enter your question")
|
127 |
+
answer_output = gr.Textbox(label="Answer", interactive=False)
|
128 |
+
clear_btn_answer = gr.Button("Clear Response")
|
129 |
|
130 |
+
# Submit Question Logic
|
131 |
+
user_input.submit(query_pdf, inputs=[pdf_file_q, user_input, openai_api_key_input], outputs=answer_output)
|
132 |
|
133 |
+
# Clear Response Button Logic for Answer Tab
|
134 |
+
clear_btn_answer.click(lambda: "", inputs=[], outputs=answer_output)
|
135 |
|
136 |
+
user_input.submit(None, None, answer_output) # Clear answer when typing new query
|
137 |
+
|
138 |
return demo
|
139 |
|
140 |
# Run Gradio app
|
141 |
+
if __name__ == "__main__":
|
142 |
demo = create_gradio_interface()
|
143 |
demo.launch(debug=True)
|
144 |
|
145 |
+
|
146 |
|
147 |
|
148 |
|