nikhmr1235 commited on
Commit
76c4d45
·
verified ·
1 Parent(s): febca85

fix for /home/user/app/app.py:157: UserWarning: You have not specified a value for the `type` parameter. Defaulting to the 'tuples' format for chatbot messages, but this is deprecated and will be removed in a future version of Gradio. Please set type='messages' instead,

Browse files
Files changed (1) hide show
  1. app.py +20 -39
app.py CHANGED
@@ -34,15 +34,6 @@ class SessionState:
34
  def is_db_ready(self):
35
  return self.db is not None
36
 
37
- # Gradio components to be enabled/disabled
38
- CHAT_COMPONENTS = None
39
- FILE_UPLOAD_COMPONENTS = None
40
-
41
- def initialize_components(file_upload_input, chat_input, chatbot):
42
- global CHAT_COMPONENTS, FILE_UPLOAD_COMPONENTS
43
- CHAT_COMPONENTS = [chat_input, chatbot]
44
- FILE_UPLOAD_COMPONENTS = [file_upload_input]
45
-
46
  # Helper function to generate a new session state
47
  def new_session():
48
  return SessionState()
@@ -50,12 +41,11 @@ def new_session():
50
  # Function to handle PDF upload and ingestion
51
  def process_pdf(pdf_file, state):
52
  try:
 
53
  if state.is_db_ready():
54
  return (
55
- f"A PDF has already been processed. Please refresh the page to upload a new one.",
56
- [],
57
- gr.ChatInterface(disabled=False),
58
- gr.File(disabled=True)
59
  )
60
 
61
  # Create a new session and directory for the user
@@ -63,7 +53,7 @@ def process_pdf(pdf_file, state):
63
  if not os.path.exists(state.vector_store_path):
64
  os.makedirs(state.vector_store_path)
65
 
66
- # Extract text from the PDF
67
  doc = fitz.open(pdf_file.name)
68
  text = ""
69
  for page in doc:
@@ -83,22 +73,21 @@ def process_pdf(pdf_file, state):
83
  )
84
 
85
  gr.Info("PDF processed successfully! You can now ask questions about the document.")
 
 
86
  return (
87
- "",
88
- [["", "PDF processed successfully! You can now ask questions."]],
89
- gr.ChatInterface(disabled=False),
90
- gr.File(disabled=True)
91
  )
92
  except Exception as e:
93
  # Clean up the directory in case of an error
94
  if os.path.exists(state.vector_store_path):
95
  shutil.rmtree(state.vector_store_path)
96
  gr.Error(f"An error occurred: {str(e)}")
 
97
  return (
98
- "",
99
- [["", f"An error occurred during processing: {str(e)}"]],
100
- gr.ChatInterface(disabled=True),
101
- gr.File(disabled=False)
102
  )
103
 
104
  # Function to handle user queries
@@ -143,7 +132,6 @@ with gr.Blocks(title="PDF Chatbot") as demo:
143
  """
144
  # PDF Chatbot
145
  Upload a PDF to start a conversation with your document.
146
- The chat interface will become active after the file is processed.
147
  """
148
  )
149
 
@@ -154,31 +142,24 @@ with gr.Blocks(title="PDF Chatbot") as demo:
154
  interactive=True
155
  )
156
 
157
- chatbot = gr.Chatbot(label="Chat History", placeholder="Upload a document to start a conversation...")
158
- chat_input = gr.Textbox(
159
- placeholder="Type your question here...",
160
- scale=7
161
- )
162
-
163
  chat_interface = gr.ChatInterface(
164
  fn=chat_with_pdf,
165
- textbox=chat_input,
166
- chatbot=chatbot,
167
  examples=["What is the main topic of the document?", "Summarize the key findings.", "Who are the authors?"],
168
  title="Chat Interface",
169
- theme="soft",
170
- # Chat is disabled until a file is processed
171
- disabled=True
172
  )
173
-
174
- # Store components in global variables for easy access
175
- initialize_components(file_upload_input, chat_input, chatbot)
176
 
177
- # Event handlers
 
 
 
178
  file_upload_input.upload(
179
  fn=process_pdf,
180
  inputs=[file_upload_input, state],
181
- outputs=[file_upload_input, chatbot, chat_interface, file_upload_input]
182
  )
183
 
184
  demo.launch()
 
34
  def is_db_ready(self):
35
  return self.db is not None
36
 
 
 
 
 
 
 
 
 
 
37
  # Helper function to generate a new session state
38
  def new_session():
39
  return SessionState()
 
41
  # Function to handle PDF upload and ingestion
42
  def process_pdf(pdf_file, state):
43
  try:
44
+ # Check if a PDF has already been processed in this session
45
  if state.is_db_ready():
46
  return (
47
+ gr.update(interactive=False),
48
+ gr.update(visible=True)
 
 
49
  )
50
 
51
  # Create a new session and directory for the user
 
53
  if not os.path.exists(state.vector_store_path):
54
  os.makedirs(state.vector_store_path)
55
 
56
+ # Extract text from the PDF using PyMuPDF (fitz)
57
  doc = fitz.open(pdf_file.name)
58
  text = ""
59
  for page in doc:
 
73
  )
74
 
75
  gr.Info("PDF processed successfully! You can now ask questions about the document.")
76
+
77
+ # Return updates to the UI components
78
  return (
79
+ gr.update(interactive=False),
80
+ gr.update(visible=True)
 
 
81
  )
82
  except Exception as e:
83
  # Clean up the directory in case of an error
84
  if os.path.exists(state.vector_store_path):
85
  shutil.rmtree(state.vector_store_path)
86
  gr.Error(f"An error occurred: {str(e)}")
87
+ # Re-enable the file upload in case of error
88
  return (
89
+ gr.update(interactive=True),
90
+ gr.update(visible=False)
 
 
91
  )
92
 
93
  # Function to handle user queries
 
132
  """
133
  # PDF Chatbot
134
  Upload a PDF to start a conversation with your document.
 
135
  """
136
  )
137
 
 
142
  interactive=True
143
  )
144
 
145
+ # Use gr.ChatInterface as a top-level component that wraps the chat logic
 
 
 
 
 
146
  chat_interface = gr.ChatInterface(
147
  fn=chat_with_pdf,
148
+ chatbot=gr.Chatbot(type="messages"),
149
+ textbox=gr.Textbox(placeholder="Type your question here...", scale=7),
150
  examples=["What is the main topic of the document?", "Summarize the key findings.", "Who are the authors?"],
151
  title="Chat Interface",
152
+ theme="soft"
 
 
153
  )
 
 
 
154
 
155
+ # Initially hide the chat interface until a file is processed
156
+ chat_interface.visible = False
157
+
158
+ # Event handler for file upload
159
  file_upload_input.upload(
160
  fn=process_pdf,
161
  inputs=[file_upload_input, state],
162
+ outputs=[file_upload_input, chat_interface]
163
  )
164
 
165
  demo.launch()