Pedro Cuenca commited on
Commit
8be786e
2 Parent(s): 635b1bf 1985070

Merge branch 'app-progress' into main

Browse files
Files changed (2) hide show
  1. app/app.py +25 -27
  2. app/img/loading.gif +0 -0
app/app.py CHANGED
@@ -1,31 +1,11 @@
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>
@@ -55,12 +35,31 @@ st.subheader('Generate images from text')
55
 
56
  prompt = st.text_input("What do you want to see?")
57
 
58
- #TODO: I think there's an issue where we can't run twice the same inference (not due to caching) - may need to use st.form
59
-
60
  DEBUG = False
61
- if prompt != "" or (should_run_again and prompt != ""):
62
  container = st.empty()
63
- container.markdown(f"Generating predictions for: **{prompt}**")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64
 
65
  try:
66
  backend_url = st.secrets["BACKEND_SERVER"]
@@ -72,8 +71,7 @@ if prompt != "" or (should_run_again and prompt != ""):
72
  cols[i%4].image(img)
73
 
74
  container.markdown(f"**{prompt}**")
75
-
76
- set_run_again(st.button('Again!', key='again_button'))
77
 
78
  except ServiceError as error:
79
  container.text(f"Service unavailable, status: {error.status_code}")
 
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>
 
35
 
36
  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"]`
44
+ # but it returns None.
45
+ container.markdown(f"""
46
+ <style> p {{ margin:0 }} div {{ margin:0 }} </style>
47
+ <div data-stale="false" class="element-container css-1e5imcs e1tzin5v1">
48
+ <div class="stAlert">
49
+ <div role="alert" data-baseweb="notification" class="st-ae st-af st-ag st-ah st-ai st-aj st-ak st-g3 st-am st-b8 st-ao st-ap st-aq st-ar st-as st-at st-au st-av st-aw st-ax st-ay st-az st-b9 st-b1 st-b2 st-b3 st-b4 st-b5 st-b6">
50
+ <div class="st-b7">
51
+ <div class="css-whx05o e13vu3m50">
52
+ <div data-testid="stMarkdownContainer" class="css-1ekf893 e16nr0p30">
53
+ <img src="https://raw.githubusercontent.com/borisdayma/dalle-mini/main/app/img/loading.gif" width="30"/>
54
+ Generating predictions for: <b>{prompt}</b>
55
+ </div>
56
+ </div>
57
+ </div>
58
+ </div>
59
+ </div>
60
+ </div>
61
+ <small><i>Predictions may take up to 40s under high load. Please, stand by.</i></small>
62
+ """, unsafe_allow_html=True)
63
 
64
  try:
65
  backend_url = st.secrets["BACKEND_SERVER"]
 
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}")
app/img/loading.gif ADDED