Rahatara commited on
Commit
e467c81
·
verified ·
1 Parent(s): 0f79c5c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -39
app.py CHANGED
@@ -56,12 +56,12 @@ def add_text(history: List[Tuple[str, str]], text: str) -> List[Tuple[str, str]]
56
  return history
57
 
58
  # Function to get response from the model
59
- def get_response(history, query, file):
60
- if not file:
61
- raise gr.Error(message="Upload a PDF")
62
- chain = app(file)
63
  try:
64
- result = chain.invoke(
65
  {"question": query, "chat_history": app.chat_history}
66
  )
67
  app.chat_history.append((query, result["answer"]))
@@ -108,7 +108,7 @@ def set_api_key(api_key):
108
  with open(saved_file_path, 'rb') as saved_file:
109
  app.process_file(saved_file)
110
  app.build_chain(saved_file)
111
- return f"API Key set to {api_key[:4]}...{api_key[-4:]}"
112
 
113
  # Gradio interface
114
  with gr.Blocks() as demo:
@@ -124,15 +124,38 @@ with gr.Blocks() as demo:
124
  outputs=[api_key_status]
125
  )
126
 
127
- with gr.Row():
128
  btn = gr.UploadButton("📁 Upload a PDF", file_types=[".pdf"])
129
  show_img = gr.Image(label="Uploaded PDF")
 
 
 
 
 
 
 
 
130
  process_btn = gr.Button("Process PDF")
131
  show_img_processed = gr.Image(label="Processed PDF")
132
  process_status = gr.Textbox(label="Processing Status", interactive=False)
 
 
 
 
 
 
 
 
133
  build_vector_btn = gr.Button("Build Vector Database")
134
  status_text = gr.Textbox(label="Status", value="", interactive=False)
135
- with gr.Row():
 
 
 
 
 
 
 
136
  chatbot = gr.Chatbot(elem_id="chatbot")
137
  txt = gr.Textbox(
138
  show_label=False,
@@ -143,38 +166,20 @@ with gr.Blocks() as demo:
143
  refresh_btn = gr.Button("Refresh Chat", scale=1)
144
  source_texts_output = gr.Textbox(label="Source Texts", interactive=False)
145
 
146
- btn.upload(
147
- fn=purge_chat_and_render_first,
148
- inputs=[btn],
149
- outputs=[show_img, chatbot],
150
- )
151
-
152
- process_btn.click(
153
- fn=lambda file: (app.process_file(file), "Processing complete!"),
154
- inputs=[btn],
155
- outputs=[show_img_processed, process_status],
156
- )
157
-
158
- build_vector_btn.click(
159
- fn=app.build_chain,
160
- inputs=[btn],
161
- outputs=[status_text],
162
- )
163
-
164
- submit_btn.click(
165
- fn=add_text,
166
- inputs=[chatbot, txt],
167
- outputs=[chatbot],
168
- queue=False,
169
- ).success(
170
- fn=get_response, inputs=[chatbot, txt, btn], outputs=[chatbot, source_texts_output]
171
- )
172
 
173
- refresh_btn.click(
174
- fn=refresh_chat,
175
- inputs=[],
176
- outputs=[chatbot],
177
- )
178
 
179
  with gr.Tab("Current RAG"):
180
  with gr.Column():
 
56
  return history
57
 
58
  # Function to get response from the model
59
+ def get_response(history, query):
60
+ if app.chain is None:
61
+ raise gr.Error("The chain has not been built yet. Please ensure the vector database is built before querying.")
62
+
63
  try:
64
+ result = app.chain.invoke(
65
  {"question": query, "chat_history": app.chat_history}
66
  )
67
  app.chat_history.append((query, result["answer"]))
 
108
  with open(saved_file_path, 'rb') as saved_file:
109
  app.process_file(saved_file)
110
  app.build_chain(saved_file)
111
+ return f"API Key set to {api_key[:4]}...{api_key[-4:]} and vector database built successfully!"
112
 
113
  # Gradio interface
114
  with gr.Blocks() as demo:
 
124
  outputs=[api_key_status]
125
  )
126
 
127
+ with gr.Tab("Upload PDF"):
128
  btn = gr.UploadButton("📁 Upload a PDF", file_types=[".pdf"])
129
  show_img = gr.Image(label="Uploaded PDF")
130
+
131
+ btn.upload(
132
+ fn=purge_chat_and_render_first,
133
+ inputs=[btn],
134
+ outputs=[show_img, chatbot],
135
+ )
136
+
137
+ with gr.Tab("Process PDF"):
138
  process_btn = gr.Button("Process PDF")
139
  show_img_processed = gr.Image(label="Processed PDF")
140
  process_status = gr.Textbox(label="Processing Status", interactive=False)
141
+
142
+ process_btn.click(
143
+ fn=lambda file: (app.process_file(file), "Processing complete!"),
144
+ inputs=[btn],
145
+ outputs=[show_img_processed, process_status],
146
+ )
147
+
148
+ with gr.Tab("Build Vector Database"):
149
  build_vector_btn = gr.Button("Build Vector Database")
150
  status_text = gr.Textbox(label="Status", value="", interactive=False)
151
+
152
+ build_vector_btn.click(
153
+ fn=app.build_chain,
154
+ inputs=[btn],
155
+ outputs=[status_text],
156
+ )
157
+
158
+ with gr.Tab("Chat"):
159
  chatbot = gr.Chatbot(elem_id="chatbot")
160
  txt = gr.Textbox(
161
  show_label=False,
 
166
  refresh_btn = gr.Button("Refresh Chat", scale=1)
167
  source_texts_output = gr.Textbox(label="Source Texts", interactive=False)
168
 
169
+ submit_btn.click(
170
+ fn=add_text,
171
+ inputs=[chatbot, txt],
172
+ outputs=[chatbot],
173
+ queue=False,
174
+ ).success(
175
+ fn=get_response, inputs=[chatbot, txt], outputs=[chatbot, source_texts_output]
176
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
177
 
178
+ refresh_btn.click(
179
+ fn=refresh_chat,
180
+ inputs=[],
181
+ outputs=[chatbot],
182
+ )
183
 
184
  with gr.Tab("Current RAG"):
185
  with gr.Column():