Simon Strandgaard
commited on
Commit
·
f6e1a1e
1
Parent(s):
4ad3389
multi user experiment
Browse files- app.py +4 -2
- src/huggingface_spaces/app_state1.py +41 -0
app.py
CHANGED
|
@@ -3,5 +3,7 @@ During development, to mimic the same behavior as on Hugging Face Spaces.
|
|
| 3 |
PROMPT> IS_HUGGINGFACE_SPACES=true python app.py
|
| 4 |
"""
|
| 5 |
if __name__ == "__main__":
|
| 6 |
-
from src.plan.app_text2plan import run_app_text2plan
|
| 7 |
-
run_app_text2plan()
|
|
|
|
|
|
|
|
|
| 3 |
PROMPT> IS_HUGGINGFACE_SPACES=true python app.py
|
| 4 |
"""
|
| 5 |
if __name__ == "__main__":
|
| 6 |
+
# from src.plan.app_text2plan import run_app_text2plan
|
| 7 |
+
# run_app_text2plan()
|
| 8 |
+
from src.huggingface_spaces.app_state1 import demo
|
| 9 |
+
demo.launch()
|
src/huggingface_spaces/app_state1.py
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Multi-user experiment. User data is isolated from other users.
|
| 3 |
+
|
| 4 |
+
However when the user reloads the page, the API key is lost.
|
| 5 |
+
"""
|
| 6 |
+
import gradio as gr
|
| 7 |
+
|
| 8 |
+
def store_api_key(api_key, state):
|
| 9 |
+
# Save the API key in session-specific state.
|
| 10 |
+
state = api_key
|
| 11 |
+
return state, f"API key stored for this session: {api_key}"
|
| 12 |
+
|
| 13 |
+
def perform_action(state):
|
| 14 |
+
# Use the stored API key for some action.
|
| 15 |
+
if state:
|
| 16 |
+
# For demonstration, we simply return a message using the API key.
|
| 17 |
+
return f"Action performed using API key: {state}"
|
| 18 |
+
else:
|
| 19 |
+
return "No API key provided. Please store your API key first."
|
| 20 |
+
|
| 21 |
+
with gr.Blocks() as demo:
|
| 22 |
+
gr.Markdown("# Per-Session API Key Demo")
|
| 23 |
+
|
| 24 |
+
# Create a session state to store the API key.
|
| 25 |
+
state = gr.State(None)
|
| 26 |
+
|
| 27 |
+
with gr.Row():
|
| 28 |
+
api_key_input = gr.Textbox(label="Enter your API Key", placeholder="your-api-key")
|
| 29 |
+
store_button = gr.Button("Store API Key")
|
| 30 |
+
|
| 31 |
+
action_button = gr.Button("Perform Action")
|
| 32 |
+
output_text = gr.Textbox(label="Output")
|
| 33 |
+
|
| 34 |
+
# When the store button is clicked, update the session state.
|
| 35 |
+
store_button.click(store_api_key, inputs=[api_key_input, state], outputs=[state, output_text])
|
| 36 |
+
|
| 37 |
+
# When the action button is clicked, use the session state (API key) to perform an action.
|
| 38 |
+
action_button.click(perform_action, inputs=state, outputs=output_text)
|
| 39 |
+
|
| 40 |
+
if __name__ == "__main__":
|
| 41 |
+
demo.launch()
|