Simon Strandgaard commited on
Commit
b69e7a3
·
1 Parent(s): f051fa2

Experiments storing multiple settings in BrowserState

Browse files
app.py CHANGED
@@ -9,5 +9,5 @@ if __name__ == "__main__":
9
  # print_gradio_info()
10
  # from src.plan.app_text2plan import run_app_text2plan
11
  # run_app_text2plan()
12
- from src.huggingface_spaces.app_state2 import demo
13
  demo.launch()
 
9
  # print_gradio_info()
10
  # from src.plan.app_text2plan import run_app_text2plan
11
  # run_app_text2plan()
12
+ from src.huggingface_spaces.app_state4 import demo
13
  demo.launch()
src/huggingface_spaces/app_state3.py CHANGED
@@ -1,9 +1,6 @@
1
  """
2
  Multi-user experiment. User data is isolated from other users.
3
- When the page is open in multiple tabs, the API key is shared across all tabs.
4
- When the user reloads the page, the API key is remembered.
5
-
6
- However when the server is restarted, the API key is lost.
7
  """
8
  import random
9
  import string
 
1
  """
2
  Multi-user experiment. User data is isolated from other users.
3
+ untested on Hugging Face Spaces.
 
 
 
4
  """
5
  import random
6
  import string
src/huggingface_spaces/app_state4.py ADDED
@@ -0,0 +1,71 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Multi-user experiment. User data is isolated from other users.
3
+ When the page is open in multiple tabs, the API key is shared across all tabs.
4
+ When the user reloads the page, the API key is remembered.
5
+ When the server is restarted, the API key is remembered.
6
+
7
+ Store multiple settings.
8
+ """
9
+ import gradio as gr
10
+ import json
11
+
12
+ def store_settings(api_key, setting1, setting2, browser_state):
13
+ # Load existing settings if any
14
+ try:
15
+ settings = json.loads(browser_state) if browser_state else {}
16
+ except json.JSONDecodeError:
17
+ settings = {}
18
+
19
+ # Update settings with new values
20
+ settings.update({
21
+ "api_key": api_key,
22
+ "setting1": setting1,
23
+ "setting2": setting2
24
+ })
25
+
26
+ # Convert back to JSON string to store
27
+ stored_value = json.dumps(settings)
28
+ return stored_value, f"Settings stored: {settings}"
29
+
30
+ def perform_action(browser_state):
31
+ if browser_state:
32
+ try:
33
+ settings = json.loads(browser_state)
34
+ return f"Action performed using settings: {settings}"
35
+ except json.JSONDecodeError:
36
+ return "Stored settings are corrupted."
37
+ else:
38
+ return "No settings stored. Please enter and store them first."
39
+
40
+ with gr.Blocks() as demo:
41
+ gr.Markdown("## Settings Persistence using BrowserState")
42
+
43
+ # Create a BrowserState component.
44
+ # The initial value is an empty string, and the key "Test" is used to store/retrieve the value.
45
+ browser_state = gr.BrowserState("", storage_key="Test", secret="Test")
46
+
47
+ with gr.Row():
48
+ api_key_input = gr.Textbox(label="Enter your API Key", elem_id="api_key_input")
49
+ setting1_input = gr.Textbox(label="Setting 1", elem_id="setting1_input")
50
+ setting2_input = gr.Textbox(label="Setting 2", elem_id="setting2_input")
51
+ store_button = gr.Button("Store Settings")
52
+
53
+ action_button = gr.Button("Perform Action")
54
+ output_text = gr.Textbox(label="Output")
55
+
56
+ # Save settings on button click.
57
+ store_button.click(
58
+ store_settings,
59
+ inputs=[api_key_input, setting1_input, setting2_input, browser_state],
60
+ outputs=[browser_state, output_text]
61
+ )
62
+
63
+ # Use the stored settings on action button click.
64
+ action_button.click(
65
+ perform_action,
66
+ inputs=browser_state,
67
+ outputs=output_text
68
+ )
69
+
70
+ if __name__ == "__main__":
71
+ demo.launch()