KajetanNiewczas commited on
Commit
eaa6fbd
·
1 Parent(s): 338df3b

Html overlay if session expires

Browse files
src/interface/__init__.py CHANGED
@@ -2,6 +2,6 @@
2
 
3
  from .gradio_interface import demo
4
  from .upload_files import upload_files
5
- from .session_manager import create_session, with_update_session
6
 
7
- __all__ = ["upload_files", "demo", "create_session", "with_update_session"]
 
2
 
3
  from .gradio_interface import demo
4
  from .upload_files import upload_files
5
+ from .session_manager import create_session, with_update_session, check_session_status
6
 
7
+ __all__ = ["upload_files", "demo", "create_session", "with_update_session", "check_session_status"]
src/interface/gradio_interface.py CHANGED
@@ -9,7 +9,9 @@ from ..generator import generate_presentation
9
  from ..telemetry import submit_feedback
10
  from .download_files import download_files
11
  from .upload_files import upload_files
12
- from .session_manager import create_session, with_update_session
 
 
13
 
14
 
15
  def upload_and_update_list(files: list, uploaded_list, _session_id) -> tuple[str, list[str]]:
@@ -63,7 +65,6 @@ def encode_image(image_path) -> str:
63
  data = f.read()
64
  return f"data:image/png;base64,{base64.b64encode(data).decode()}"
65
 
66
-
67
  banner_base64 = encode_image("gfx/long_logo.png")
68
  logo_base64 = encode_image("gfx/icon_logo.png")
69
  github_base64 = encode_image("gfx/github-mark-white.png")
@@ -87,20 +88,69 @@ theme = gr.themes.Monochrome(
87
  ),
88
  ).set(body_background_fill="*neutral_50", body_text_color="*neutral_950")
89
 
90
- JS_FUNC = """
91
- function refresh() {
92
- const url = new URL(window.location);
93
 
 
 
 
 
 
94
  if (url.searchParams.get('__theme') !== 'light') {
95
  url.searchParams.set('__theme', 'light');
96
  window.location.href = url.href;
97
  }
98
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
99
  """
100
 
101
 
102
  with gr.Blocks(theme=theme, js=JS_FUNC) as demo:
 
103
  session_id = gr.State()
 
 
 
 
 
 
 
 
 
 
104
  uploaded_files_state = gr.State([])
105
  presentation_folder_state = gr.State("")
106
 
@@ -360,7 +410,8 @@ with gr.Blocks(theme=theme, js=JS_FUNC) as demo:
360
 
361
  demo.load(
362
  fn=create_session,
363
- outputs=session_id
 
364
  )
365
 
366
  upload_button.click(
 
9
  from ..telemetry import submit_feedback
10
  from .download_files import download_files
11
  from .upload_files import upload_files
12
+ from .session_manager import create_session, with_update_session, check_session_status
13
+
14
+ SESSION_TIMEOUT = 30 #600
15
 
16
 
17
  def upload_and_update_list(files: list, uploaded_list, _session_id) -> tuple[str, list[str]]:
 
65
  data = f.read()
66
  return f"data:image/png;base64,{base64.b64encode(data).decode()}"
67
 
 
68
  banner_base64 = encode_image("gfx/long_logo.png")
69
  logo_base64 = encode_image("gfx/icon_logo.png")
70
  github_base64 = encode_image("gfx/github-mark-white.png")
 
88
  ),
89
  ).set(body_background_fill="*neutral_50", body_text_color="*neutral_950")
90
 
 
 
 
91
 
92
+ # Additional JavaScript to handle session management and UI updates
93
+ JS_FUNC = r"""
94
+ () => {
95
+ // Force light theme
96
+ const url = new URL(window.location);
97
  if (url.searchParams.get('__theme') !== 'light') {
98
  url.searchParams.set('__theme', 'light');
99
  window.location.href = url.href;
100
  }
101
+
102
+ const overlayDiv = document.createElement("div");
103
+ overlayDiv.id = "session-overlay";
104
+ overlayDiv.style = `
105
+ display: flex;
106
+ position: fixed;
107
+ top: 0; left: 0; width: 100vw; height: 100vh;
108
+ justify-content: center;
109
+ align-items: center;
110
+ background-color: rgba(0, 0, 0, 0.7);
111
+ color: white;
112
+ font-size: 1.5rem;
113
+ text-align: center;
114
+ z-index: 999999;
115
+ `;
116
+ overlayDiv.innerHTML = `
117
+ ❌ Session expired.<br/>🔄 Please refresh the page to start over.
118
+ `;
119
+
120
+ const checker = setInterval(() => {
121
+ const checkBtn = document.querySelector('#check-session-button');
122
+ if (checkBtn) checkBtn.click();
123
+
124
+ const statusBox = document.querySelector('#session-status textarea');
125
+ if (!statusBox) return;
126
+
127
+ const value = statusBox.value;
128
+
129
+ if (value === "expired") {
130
+ document.body.appendChild(overlayDiv);
131
+ console.log("Session expired, please refresh the page.");
132
+
133
+ clearInterval(checker);
134
+ }
135
+ }, 5000);
136
+
137
+ }
138
  """
139
 
140
 
141
  with gr.Blocks(theme=theme, js=JS_FUNC) as demo:
142
+ # Session management
143
  session_id = gr.State()
144
+ session_timeout = gr.State(SESSION_TIMEOUT)
145
+ status_output = gr.Textbox(visible=False, elem_id="session-status")
146
+ check_btn = gr.Button(visible=False, elem_id="check-session-button")
147
+ check_btn.click(
148
+ fn=check_session_status,
149
+ inputs=[session_id],
150
+ outputs=[status_output],
151
+ )
152
+ ###
153
+
154
  uploaded_files_state = gr.State([])
155
  presentation_folder_state = gr.State("")
156
 
 
410
 
411
  demo.load(
412
  fn=create_session,
413
+ inputs=session_timeout,
414
+ outputs=session_id,
415
  )
416
 
417
  upload_button.click(
src/interface/session_manager.py CHANGED
@@ -10,7 +10,7 @@ LOGGER = Logger.get_logger()
10
  # Global session manager
11
  session_data = {}
12
 
13
- def create_session() -> str:
14
  """Create a new session and return its ID."""
15
 
16
  # Create new session
@@ -21,7 +21,7 @@ def create_session() -> str:
21
  LOGGER.info("🪪 Created new session with ID: %s", session_id)
22
 
23
  # Start clean-up watcher
24
- threading.Thread(target=watch_session, args=(session_id,), daemon=True).start()
25
 
26
  return session_id
27
 
@@ -36,6 +36,7 @@ def watch_session(session_id, timeout=600):
36
  if time.time() - session["last_active"] > timeout:
37
  del session_data[session_id]
38
  LOGGER.info("🧹 Deleted session with ID: %s", session_id)
 
39
  break
40
 
41
  def update_session(session_id):
@@ -56,3 +57,6 @@ def with_update_session(fn):
56
  LOGGER.error("❌ Failed to upadate session ID: %s", session_id, exc_info=e)
57
  return fn(*args, **kwargs)
58
  return wrapper
 
 
 
 
10
  # Global session manager
11
  session_data = {}
12
 
13
+ def create_session(timeout=600) -> str:
14
  """Create a new session and return its ID."""
15
 
16
  # Create new session
 
21
  LOGGER.info("🪪 Created new session with ID: %s", session_id)
22
 
23
  # Start clean-up watcher
24
+ threading.Thread(target=watch_session, args=(session_id, timeout), daemon=True).start()
25
 
26
  return session_id
27
 
 
36
  if time.time() - session["last_active"] > timeout:
37
  del session_data[session_id]
38
  LOGGER.info("🧹 Deleted session with ID: %s", session_id)
39
+ # Whatever will be deleted later, we could delay it a little bit
40
  break
41
 
42
  def update_session(session_id):
 
57
  LOGGER.error("❌ Failed to upadate session ID: %s", session_id, exc_info=e)
58
  return fn(*args, **kwargs)
59
  return wrapper
60
+
61
+ def check_session_status(session_id: str) -> str:
62
+ return "active" if session_id in session_data else "expired"