SamarthPujari commited on
Commit
7fddc7b
·
verified ·
1 Parent(s): f29d46e

Update Gradio_UI.py

Browse files
Files changed (1) hide show
  1. Gradio_UI.py +34 -20
Gradio_UI.py CHANGED
@@ -264,6 +264,33 @@ class GradioUI:
264
  with gr.Blocks(fill_height=True) as demo:
265
  stored_messages = gr.State([])
266
  file_uploads_log = gr.State([])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
267
  chatbot = gr.Chatbot(
268
  label="Agent",
269
  type="messages",
@@ -275,24 +302,14 @@ class GradioUI:
275
  scale=1,
276
  )
277
 
278
- if self.file_upload_folder is not None:
279
- upload_file = gr.File(label="Upload PDF or Doc", file_types=['.pdf', '.docx', '.txt'])
280
- upload_status = gr.Textbox(label="Upload Status", interactive=False, visible=True)
281
- upload_file.change(
282
- self.upload_file,
283
- [upload_file, file_uploads_log],
284
- [upload_status, file_uploads_log],
285
- )
286
-
287
- text_input = gr.Textbox(lines=1, label="Chat Message", placeholder="Ask your question here...")
288
 
 
289
  def on_submit(text, files_log):
290
- if not text.strip():
291
- return gr.update(value="Please enter a question before submitting."), ""
292
  combined_prompt = (
293
  text
294
  + (
295
- f"\n\nI have the following files you can use: {files_log}"
296
  if len(files_log) > 0
297
  else ""
298
  )
@@ -301,17 +318,14 @@ class GradioUI:
301
 
302
  text_input.submit(
303
  on_submit,
304
- [text_input, file_uploads_log],
305
- [stored_messages, text_input],
306
  ).then(
307
  self.interact_with_agent,
308
- [stored_messages, chatbot],
309
- [chatbot],
310
  )
311
 
312
  demo.launch(debug=True, share=True, **kwargs)
313
 
314
-
315
-
316
-
317
  __all__ = ["stream_to_gradio", "GradioUI"]
 
264
  with gr.Blocks(fill_height=True) as demo:
265
  stored_messages = gr.State([])
266
  file_uploads_log = gr.State([])
267
+
268
+ # Upload UI row (only if file_upload_folder is set)
269
+ if self.file_upload_folder is not None:
270
+ with gr.Row():
271
+ upload_file = gr.File(
272
+ label="Upload a file (PDF, DOCX, TXT)",
273
+ file_types=['.pdf', '.docx', '.txt'],
274
+ interactive=True,
275
+ )
276
+ upload_status = gr.Textbox(
277
+ label="Upload Status",
278
+ interactive=False,
279
+ visible=True,
280
+ max_lines=1,
281
+ show_label=True,
282
+ )
283
+ # Hook upload event
284
+ upload_file.change(
285
+ self.upload_file,
286
+ inputs=[upload_file, file_uploads_log],
287
+ outputs=[upload_status, file_uploads_log],
288
+ )
289
+ else:
290
+ # If no upload folder provided, these widgets don’t exist
291
+ upload_file = None
292
+ upload_status = None
293
+
294
  chatbot = gr.Chatbot(
295
  label="Agent",
296
  type="messages",
 
302
  scale=1,
303
  )
304
 
305
+ text_input = gr.Textbox(lines=1, label="Chat Message")
 
 
 
 
 
 
 
 
 
306
 
307
+ # When user submits the text input
308
  def on_submit(text, files_log):
 
 
309
  combined_prompt = (
310
  text
311
  + (
312
+ f"\nYou have been provided with these files, which might be helpful or not: {files_log}"
313
  if len(files_log) > 0
314
  else ""
315
  )
 
318
 
319
  text_input.submit(
320
  on_submit,
321
+ inputs=[text_input, file_uploads_log],
322
+ outputs=[stored_messages, text_input],
323
  ).then(
324
  self.interact_with_agent,
325
+ inputs=[stored_messages, chatbot],
326
+ outputs=chatbot,
327
  )
328
 
329
  demo.launch(debug=True, share=True, **kwargs)
330
 
 
 
 
331
  __all__ = ["stream_to_gradio", "GradioUI"]