npc0 commited on
Commit
4984061
·
verified ·
1 Parent(s): a4b714f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -31
app.py CHANGED
@@ -57,15 +57,16 @@ class GUI:
57
  out = gr.Markdown()
58
  inp = gr.File(file_types=['.epub'], visible=False, label="Upload ePub File")
59
 
60
- # Event handlers - Updated for Gradio 5
61
  demo.load(
62
  fn=self.on_load,
63
  outputs=[welcome_md, api_key_section, inp, clear_key_btn]
64
  )
65
 
66
- # Updated login handling for Gradio 5
67
- login_btn.click(
68
- fn=self.handle_login_click,
 
69
  outputs=[welcome_md, api_key_section, inp, clear_key_btn]
70
  )
71
 
@@ -195,38 +196,68 @@ class GUI:
195
  )
196
  return self.handle_login_click(request)
197
 
198
- def handle_login_click(self, request: gr.Request = None):
199
- """Handle user login - Updated for Gradio 5"""
200
- if not hasattr(request, 'username') or not request.username:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
201
  return (
202
- gr.update(value='# ePub Summarization Tool\n\nPlease login to access the tool.'),
203
- gr.update(visible=False), # api_key_section
204
  gr.update(visible=False), # inp
205
  gr.update(visible=False) # clear_key_btn
206
  )
207
 
208
- self.current_user = self._get_user_id(request)
209
- user_name = request.username
210
-
211
- # Check if user has a saved API key
212
- saved_key = self._get_saved_key(self.current_user)
213
- if saved_key:
214
- self.api_key = saved_key
215
- if self._initialize_client():
216
- return (
217
- gr.update(value=f'# ePub Summarization Tool\n\nWelcome back {user_name}! ✅ Your saved API key is loaded and ready.'),
218
- gr.update(visible=False), # api_key_section
219
- gr.update(visible=True), # inp
220
- gr.update(visible=True) # clear_key_btn
221
- )
222
-
223
- # No saved key or failed to initialize
224
- return (
225
- gr.update(value=f'# ePub Summarization Tool\n\nWelcome {user_name}! Please set up your Poe API key below.'),
226
- gr.update(visible=True), # api_key_section
227
- gr.update(visible=False), # inp
228
- gr.update(visible=False) # clear_key_btn
229
- )
230
 
231
  def _initialize_client(self):
232
  """Initialize the Poe API client"""
 
57
  out = gr.Markdown()
58
  inp = gr.File(file_types=['.epub'], visible=False, label="Upload ePub File")
59
 
60
+ # Event handlers - Simplified for Gradio 5
61
  demo.load(
62
  fn=self.on_load,
63
  outputs=[welcome_md, api_key_section, inp, clear_key_btn]
64
  )
65
 
66
+ # Use timer to check login state (workaround for Gradio 5 login issues)
67
+ timer = gr.Timer(value=2.0, active=True)
68
+ timer.tick(
69
+ fn=self.check_login_state,
70
  outputs=[welcome_md, api_key_section, inp, clear_key_btn]
71
  )
72
 
 
196
  )
197
  return self.handle_login_click(request)
198
 
199
+ def check_login_state(self, request: gr.Request = None):
200
+ """Check login state periodically - workaround for Gradio 5"""
201
+ try:
202
+ if hasattr(request, 'username') and request.username:
203
+ return self.handle_user_logged_in(request)
204
+ else:
205
+ return self.handle_user_not_logged_in()
206
+ except Exception as e:
207
+ print(f"Error checking login state: {e}")
208
+ return self.handle_user_not_logged_in()
209
+
210
+ def handle_user_not_logged_in(self):
211
+ """Handle when user is not logged in"""
212
+ return (
213
+ gr.update(value='# ePub Summarization Tool\n\nPlease login with Hugging Face to access the tool.'),
214
+ gr.update(visible=False), # api_key_section
215
+ gr.update(visible=False), # inp
216
+ gr.update(visible=False) # clear_key_btn
217
+ )
218
+
219
+ def handle_user_logged_in(self, request):
220
+ """Handle when user is logged in"""
221
+ if not self.current_user or self.current_user != self._get_user_id(request):
222
+ # New login or different user
223
+ self.current_user = self._get_user_id(request)
224
+ user_name = request.username
225
+
226
+ # Check if user has a saved API key
227
+ saved_key = self._get_saved_key(self.current_user)
228
+ if saved_key:
229
+ self.api_key = saved_key
230
+ if self._initialize_client():
231
+ return (
232
+ gr.update(value=f'# ePub Summarization Tool\n\nWelcome back {user_name}! ✅ Your saved API key is loaded and ready.'),
233
+ gr.update(visible=False), # api_key_section
234
+ gr.update(visible=True), # inp
235
+ gr.update(visible=True) # clear_key_btn
236
+ )
237
+
238
+ # No saved key or failed to initialize
239
  return (
240
+ gr.update(value=f'# ePub Summarization Tool\n\nWelcome {user_name}! Please set up your Poe API key below.'),
241
+ gr.update(visible=True), # api_key_section
242
  gr.update(visible=False), # inp
243
  gr.update(visible=False) # clear_key_btn
244
  )
245
 
246
+ # User already processed, return current state
247
+ if self.client:
248
+ return (
249
+ gr.update(value=f'# ePub Summarization Tool\n\nWelcome {request.username}! Ready to process ePub files.'),
250
+ gr.update(visible=False), # api_key_section
251
+ gr.update(visible=True), # inp
252
+ gr.update(visible=True) # clear_key_btn
253
+ )
254
+ else:
255
+ return (
256
+ gr.update(value=f'# ePub Summarization Tool\n\nWelcome {request.username}! Please set up your Poe API key below.'),
257
+ gr.update(visible=True), # api_key_section
258
+ gr.update(visible=False), # inp
259
+ gr.update(visible=False) # clear_key_btn
260
+ )
 
 
 
 
 
 
 
261
 
262
  def _initialize_client(self):
263
  """Initialize the Poe API client"""