nikhmr1235 commited on
Commit
3aacd49
·
verified ·
1 Parent(s): 2eb2e08

attempt to fix chatinterface being invisble after file uploaded

Browse files
Files changed (1) hide show
  1. app.py +24 -28
app.py CHANGED
@@ -24,18 +24,12 @@ class PDFChatbot:
24
  def process_pdf(self, pdf_file):
25
  try:
26
  if self.state.is_db_ready():
27
- return (
28
- gr.update(interactive=False),
29
- gr.update(visible=True)
30
- )
31
 
32
  file_size_mb = os.path.getsize(pdf_file.name) / (1024 * 1024)
33
  if file_size_mb >= 75:
34
  gr.Error("File size exceeds the 75 MB limit. Please upload a smaller PDF.")
35
- return (
36
- gr.update(interactive=True),
37
- gr.update(visible=False)
38
- )
39
 
40
  self.state = SessionState()
41
  doc = fitz.open(pdf_file.name)
@@ -55,19 +49,12 @@ class PDFChatbot:
55
  )
56
 
57
  gr.Info("PDF processed successfully! You can now ask questions about the document.")
58
-
59
- return (
60
- gr.update(interactive=False, value=None), # Clear the file upload input
61
- gr.update(visible=True)
62
- )
63
  except Exception as e:
64
  if os.path.exists(self.state.vector_store_path):
65
  shutil.rmtree(self.state.vector_store_path)
66
  gr.Error(f"An error occurred: {str(e)}")
67
- return (
68
- gr.update(interactive=True),
69
- gr.update(visible=False)
70
- )
71
 
72
  def chat_with_pdf(self, message, history):
73
  if not self.state.is_db_ready():
@@ -129,21 +116,30 @@ with gr.Blocks(title="PDF Chatbot") as demo:
129
  interactive=True
130
  )
131
 
132
- chat_group = gr.Group(visible=False)
133
- with chat_group:
134
- chat_interface = gr.ChatInterface(
135
- fn=chatbot.chat_with_pdf,
136
- chatbot=gr.Chatbot(type="messages"),
137
- textbox=gr.Textbox(placeholder="Type your question here...", scale=7),
138
- examples=[["What is the main topic of the document?"], ["Summarize the key findings."], ["Who are the authors?"]],
139
- title="Chat Interface",
140
- theme="soft"
141
- )
 
 
142
 
143
  file_upload_input.upload(
144
  fn=chatbot.process_pdf,
145
  inputs=[file_upload_input],
146
- outputs=[file_upload_input, chat_group]
 
 
 
 
 
 
 
147
  )
148
 
149
  demo.launch()
 
24
  def process_pdf(self, pdf_file):
25
  try:
26
  if self.state.is_db_ready():
27
+ return gr.update(interactive=False)
 
 
 
28
 
29
  file_size_mb = os.path.getsize(pdf_file.name) / (1024 * 1024)
30
  if file_size_mb >= 75:
31
  gr.Error("File size exceeds the 75 MB limit. Please upload a smaller PDF.")
32
+ return gr.update(interactive=True)
 
 
 
33
 
34
  self.state = SessionState()
35
  doc = fitz.open(pdf_file.name)
 
49
  )
50
 
51
  gr.Info("PDF processed successfully! You can now ask questions about the document.")
52
+ return gr.update(interactive=False)
 
 
 
 
53
  except Exception as e:
54
  if os.path.exists(self.state.vector_store_path):
55
  shutil.rmtree(self.state.vector_store_path)
56
  gr.Error(f"An error occurred: {str(e)}")
57
+ return gr.update(interactive=True)
 
 
 
58
 
59
  def chat_with_pdf(self, message, history):
60
  if not self.state.is_db_ready():
 
116
  interactive=True
117
  )
118
 
119
+ chat_interface = gr.ChatInterface(
120
+ fn=chatbot.chat_with_pdf,
121
+ chatbot=gr.Chatbot(type="messages"),
122
+ textbox=gr.Textbox(placeholder="Type your question here...", scale=7),
123
+ examples=[["What is the main topic of the document?"], ["Summarize the key findings."], ["Who are the authors?"]],
124
+ title="Chat Interface",
125
+ theme="soft",
126
+ visible=False
127
+ )
128
+
129
+ def show_chat_interface(show):
130
+ return gr.update(visible=True)
131
 
132
  file_upload_input.upload(
133
  fn=chatbot.process_pdf,
134
  inputs=[file_upload_input],
135
+ outputs=[file_upload_input]
136
+ )
137
+
138
+ file_upload_input.upload(
139
+ fn=show_chat_interface,
140
+ inputs=[gr.Number(value=1, visible=False)],
141
+ outputs=[chat_interface],
142
+ preprocess=False
143
  )
144
 
145
  demo.launch()