Th3Nic3Guy commited on
Commit
abfce84
·
1 Parent(s): e962bef

updating code

Browse files
Files changed (1) hide show
  1. app.py +31 -4
app.py CHANGED
@@ -101,15 +101,22 @@ with gr.Blocks() as demo:
101
  with gr.Column():
102
  gr.Image(
103
  "./aem_logo.jpeg",
104
- width=300, label="AEM", show_download_button=False,
 
 
105
  show_fullscreen_button=False,
106
  show_label=False,
107
  interactive=False,
108
  show_share_button=False,
109
  )
110
 
111
- session = gr.BrowserState(["", False])
112
-
 
 
 
 
 
113
  with gr.Accordion(
114
  'Chat With Agent',
115
  ) as chat:
@@ -122,4 +129,24 @@ with gr.Blocks() as demo:
122
  fill_height=True,
123
  )
124
 
125
- demo.launch(share=False, debug=True,)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
101
  with gr.Column():
102
  gr.Image(
103
  "./aem_logo.jpeg",
104
+ width=300,
105
+ label="AEM",
106
+ show_download_button=False,
107
  show_fullscreen_button=False,
108
  show_label=False,
109
  interactive=False,
110
  show_share_button=False,
111
  )
112
 
113
+ # session = gr.State(["", ], time_to_live=24 * 60*60)
114
+ session = gr.BrowserState(None, storage_key="session_id")
115
+ show_session = gr.Textbox(
116
+ visible=False,
117
+ interactive=False,
118
+ label="Session ID",
119
+ )
120
  with gr.Accordion(
121
  'Chat With Agent',
122
  ) as chat:
 
129
  fill_height=True,
130
  )
131
 
132
+ def initialize_session(existing_id):
133
+ """
134
+ Runs ONCE per browser session on page load.
135
+ Checks if an ID already exists in BrowserState (local storage).
136
+ If not, generates a new one.
137
+ Returns the ID to populate the BrowserState and other components.
138
+ """
139
+ if existing_id:
140
+ print(f"Found existing ID in BrowserState: {existing_id}")
141
+ return existing_id, existing_id
142
+ else:
143
+ new_id = generate_session_id()
144
+ print(f"No existing ID found, returning NEW ID: {new_id}")
145
+ return new_id, new_id
146
+
147
+ demo.load( # pylint: disable=no-member
148
+ initialize_session,
149
+ [session],
150
+ outputs=[session, show_session],
151
+ )
152
+ demo.launch(share=False, debug=True,)