Pedro Cuenca commited on
Commit
8e37fb8
1 Parent(s): 813a400

Revert "Remove session handling hack."

Browse files

This reverts commit 1985070cad8718dcbf1006e277626c52d3a12c86.

Files changed (1) hide show
  1. app/app.py +24 -3
app/app.py CHANGED
@@ -1,11 +1,31 @@
1
  #!/usr/bin/env python
2
  # coding: utf-8
3
 
 
4
  from dalle_mini.backend import ServiceError, get_images_from_backend
 
5
  import streamlit as st
6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
  def should_run_again():
8
- return st.session_state.get("again", False)
 
9
 
10
  st.sidebar.markdown("""
11
  <style>
@@ -37,7 +57,7 @@ prompt = st.text_input("What do you want to see?")
37
 
38
  test = st.empty()
39
  DEBUG = False
40
- if prompt != "" or (should_run_again() and prompt != ""):
41
  container = st.empty()
42
  # The following mimics `streamlit.info()`.
43
  # I tried to get the secondary background color using `components.streamlit.config.get_options_for_section("theme")["secondaryBackgroundColor"]`
@@ -71,7 +91,8 @@ if prompt != "" or (should_run_again() and prompt != ""):
71
  cols[i%4].image(img)
72
 
73
  container.markdown(f"**{prompt}**")
74
- st.session_state["again"] = st.button('Again!', key='again_button')
 
75
 
76
  except ServiceError as error:
77
  container.text(f"Service unavailable, status: {error.status_code}")
 
1
  #!/usr/bin/env python
2
  # coding: utf-8
3
 
4
+ import random
5
  from dalle_mini.backend import ServiceError, get_images_from_backend
6
+
7
  import streamlit as st
8
 
9
+ # streamlit.session_state is not available in Huggingface spaces.
10
+ # Session state hack https://huggingface.slack.com/archives/C025LJDP962/p1626527367443200?thread_ts=1626525999.440500&cid=C025LJDP962
11
+
12
+ from streamlit.report_thread import get_report_ctx
13
+ def query_cache(q_emb=None):
14
+ ctx = get_report_ctx()
15
+ session_id = ctx.session_id
16
+ session = st.server.server.Server.get_current()._get_session_info(session_id).session
17
+ if not hasattr(session, "_query_state"):
18
+ setattr(session, "_query_state", q_emb)
19
+ if q_emb:
20
+ session._query_state = q_emb
21
+ return session._query_state
22
+
23
+ def set_run_again(state):
24
+ query_cache(state)
25
+
26
  def should_run_again():
27
+ state = query_cache()
28
+ return state if state is not None else False
29
 
30
  st.sidebar.markdown("""
31
  <style>
 
57
 
58
  test = st.empty()
59
  DEBUG = False
60
+ if prompt != "" or (should_run_again and prompt != ""):
61
  container = st.empty()
62
  # The following mimics `streamlit.info()`.
63
  # I tried to get the secondary background color using `components.streamlit.config.get_options_for_section("theme")["secondaryBackgroundColor"]`
 
91
  cols[i%4].image(img)
92
 
93
  container.markdown(f"**{prompt}**")
94
+
95
+ set_run_again(st.button('Again!', key='again_button'))
96
 
97
  except ServiceError as error:
98
  container.text(f"Service unavailable, status: {error.status_code}")