boris commited on
Commit
74974be
1 Parent(s): 5b16588

feat: simplify app

Browse files
Files changed (1) hide show
  1. app/app.py +32 -44
app/app.py CHANGED
@@ -2,31 +2,10 @@
2
  # coding: utf-8
3
 
4
  from dalle_mini.backend import ServiceError, get_images_from_backend
5
-
6
  import streamlit as st
7
 
8
- # streamlit.session_state is not available in Huggingface spaces.
9
- # Session state hack https://huggingface.slack.com/archives/C025LJDP962/p1626527367443200?thread_ts=1626525999.440500&cid=C025LJDP962
10
-
11
- from streamlit.report_thread import get_report_ctx
12
- def query_cache(q_emb=None):
13
- ctx = get_report_ctx()
14
- session_id = ctx.session_id
15
- session = st.server.server.Server.get_current()._get_session_info(session_id).session
16
- if not hasattr(session, "_query_state"):
17
- setattr(session, "_query_state", q_emb)
18
- if q_emb:
19
- session._query_state = q_emb
20
- return session._query_state
21
-
22
- def set_run_again(state):
23
- query_cache(state)
24
-
25
- def should_run_again():
26
- state = query_cache()
27
- return state if state is not None else False
28
-
29
- st.sidebar.markdown("""
30
  <style>
31
  .aligncenter {
32
  text-align: center;
@@ -35,8 +14,11 @@ st.sidebar.markdown("""
35
  <p class="aligncenter">
36
  <img src="https://raw.githubusercontent.com/borisdayma/dalle-mini/main/img/logo.png"/>
37
  </p>
38
- """, unsafe_allow_html=True)
39
- st.sidebar.markdown("""
 
 
 
40
  ___
41
  <p style='text-align: center'>
42
  DALL·E mini is an AI model that generates images from any prompt you give!
@@ -47,21 +29,20 @@ Created by Boris Dayma et al. 2021
47
  <br/>
48
  <a href="https://github.com/borisdayma/dalle-mini" target="_blank">GitHub</a> | <a href="https://wandb.ai/dalle-mini/dalle-mini/reports/DALL-E-mini--Vmlldzo4NjIxODA" target="_blank">Project Report</a>
49
  </p>
50
- """, unsafe_allow_html=True)
 
 
51
 
52
- st.header('DALL·E mini')
53
- st.subheader('Generate images from text')
54
 
55
  prompt = st.text_input("What do you want to see?")
56
 
57
- test = st.empty()
58
  DEBUG = False
59
- if prompt != "" or (should_run_again and prompt != ""):
60
  container = st.empty()
61
- # The following mimics `streamlit.info()`.
62
- # I tried to get the secondary background color using `components.streamlit.config.get_options_for_section("theme")["secondaryBackgroundColor"]`
63
- # but it returns None.
64
- container.markdown(f"""
65
  <style> p {{ margin:0 }} div {{ margin:0 }} </style>
66
  <div data-stale="false" class="element-container css-1e5imcs e1tzin5v1">
67
  <div class="stAlert">
@@ -78,32 +59,39 @@ if prompt != "" or (should_run_again and prompt != ""):
78
  </div>
79
  </div>
80
  <small><i>Predictions may take up to 40s under high load. Please stand by.</i></small>
81
- """, unsafe_allow_html=True)
 
 
82
 
83
  try:
84
  backend_url = st.secrets["BACKEND_SERVER"]
85
  print(f"Getting selections: {prompt}")
86
  selected = get_images_from_backend(prompt, backend_url)
87
 
88
- cols = st.columns(4)
 
 
89
  for i, img in enumerate(selected):
90
- cols[i%4].image(img)
91
-
92
  container.markdown(f"**{prompt}**")
93
-
94
- set_run_again(st.button('Again!', key='again_button'))
95
-
96
  except ServiceError as error:
97
  container.text(f"Service unavailable, status: {error.status_code}")
98
  except KeyError:
99
  if DEBUG:
100
- container.markdown("""
 
101
  **Error: BACKEND_SERVER unset**
102
 
103
  Please, create a file called `.streamlit/secrets.toml` inside the app's folder and include a line to configure the server URL:
104
  ```
105
  BACKEND_SERVER="<server url>"
106
  ```
107
- """)
 
108
  else:
109
- container.markdown('Error -5, please try again or [report it](mailto:pcuenca-dalle@guenever.net).')
 
 
 
2
  # coding: utf-8
3
 
4
  from dalle_mini.backend import ServiceError, get_images_from_backend
 
5
  import streamlit as st
6
 
7
+ st.sidebar.markdown(
8
+ """
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
  <style>
10
  .aligncenter {
11
  text-align: center;
 
14
  <p class="aligncenter">
15
  <img src="https://raw.githubusercontent.com/borisdayma/dalle-mini/main/img/logo.png"/>
16
  </p>
17
+ """,
18
+ unsafe_allow_html=True,
19
+ )
20
+ st.sidebar.markdown(
21
+ """
22
  ___
23
  <p style='text-align: center'>
24
  DALL·E mini is an AI model that generates images from any prompt you give!
 
29
  <br/>
30
  <a href="https://github.com/borisdayma/dalle-mini" target="_blank">GitHub</a> | <a href="https://wandb.ai/dalle-mini/dalle-mini/reports/DALL-E-mini--Vmlldzo4NjIxODA" target="_blank">Project Report</a>
31
  </p>
32
+ """,
33
+ unsafe_allow_html=True,
34
+ )
35
 
36
+ st.header("DALL·E mini")
37
+ st.subheader("Generate images from text")
38
 
39
  prompt = st.text_input("What do you want to see?")
40
 
 
41
  DEBUG = False
42
+ if prompt != "":
43
  container = st.empty()
44
+ container.markdown(
45
+ 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">
 
59
  </div>
60
  </div>
61
  <small><i>Predictions may take up to 40s under high load. Please stand by.</i></small>
62
+ """,
63
+ unsafe_allow_html=True,
64
+ )
65
 
66
  try:
67
  backend_url = st.secrets["BACKEND_SERVER"]
68
  print(f"Getting selections: {prompt}")
69
  selected = get_images_from_backend(prompt, backend_url)
70
 
71
+ margin = 0.1 # for better position of zoom in arrow
72
+ n_columns = 3
73
+ cols = st.columns([1] + [margin, 1] * (n_columns - 1))
74
  for i, img in enumerate(selected):
75
+ cols[(i % n_columns) * 2].image(img)
 
76
  container.markdown(f"**{prompt}**")
77
+
78
+ st.button("Again!", key="again_button")
79
+
80
  except ServiceError as error:
81
  container.text(f"Service unavailable, status: {error.status_code}")
82
  except KeyError:
83
  if DEBUG:
84
+ container.markdown(
85
+ """
86
  **Error: BACKEND_SERVER unset**
87
 
88
  Please, create a file called `.streamlit/secrets.toml` inside the app's folder and include a line to configure the server URL:
89
  ```
90
  BACKEND_SERVER="<server url>"
91
  ```
92
+ """
93
+ )
94
  else:
95
+ container.markdown(
96
+ "Error -5, please try again or [report it](mailto:pcuenca-dalle@guenever.net)."
97
+ )