Spaces:
Runtime error
Runtime error
johnpaulbin
commited on
Commit
•
7850b9e
1
Parent(s):
b36119b
Update app.py
Browse files
app.py
CHANGED
@@ -1,119 +1,95 @@
|
|
1 |
#!/usr/bin/env python
|
2 |
# coding: utf-8
|
3 |
-
# Uncomment to run on cpu
|
4 |
-
#import os
|
5 |
-
#os.environ["JAX_PLATFORM_NAME"] = "cpu"
|
6 |
import random
|
7 |
-
import
|
8 |
-
import
|
9 |
-
|
10 |
-
|
11 |
-
from
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
return (output_title, predictions)
|
100 |
-
outputs = [
|
101 |
-
gr.outputs.HTML(label=""), # To be used as title
|
102 |
-
gr.outputs.Image(label=''),
|
103 |
-
]
|
104 |
-
description = """
|
105 |
-
DALL·E-mini is an AI model that generates images from any prompt you give! Generate images from text:
|
106 |
-
"""
|
107 |
-
gr.Interface(run_inference,
|
108 |
-
inputs=[gr.inputs.Textbox(label='What do you want to see?')],
|
109 |
-
outputs=outputs,
|
110 |
-
title='DALL·E mini',
|
111 |
-
description=description,
|
112 |
-
article="<p style='text-align: center'> Created by Boris Dayma et al. 2021 | <a href='https://github.com/borisdayma/dalle-mini'>GitHub</a> | <a href='https://wandb.ai/dalle-mini/dalle-mini/reports/DALL-E-mini--Vmlldzo4NjIxODA'>Report</a></p>",
|
113 |
-
layout='vertical',
|
114 |
-
theme='huggingface',
|
115 |
-
examples=[['an armchair in the shape of an avocado'], ['snowy mountains by the sea']],
|
116 |
-
allow_flagging=False,
|
117 |
-
live=False,
|
118 |
-
# server_port=8999
|
119 |
-
).launch(share=True)
|
1 |
#!/usr/bin/env python
|
2 |
# coding: utf-8
|
|
|
|
|
|
|
3 |
import random
|
4 |
+
from dalle_mini.backend import ServiceError, get_images_from_backend
|
5 |
+
import streamlit as st
|
6 |
+
# streamlit.session_state is not available in Huggingface spaces.
|
7 |
+
# Session state hack https://huggingface.slack.com/archives/C025LJDP962/p1626527367443200?thread_ts=1626525999.440500&cid=C025LJDP962
|
8 |
+
from streamlit.report_thread import get_report_ctx
|
9 |
+
def query_cache(q_emb=None):
|
10 |
+
ctx = get_report_ctx()
|
11 |
+
session_id = ctx.session_id
|
12 |
+
session = st.server.server.Server.get_current()._get_session_info(session_id).session
|
13 |
+
if not hasattr(session, "_query_state"):
|
14 |
+
setattr(session, "_query_state", q_emb)
|
15 |
+
if q_emb:
|
16 |
+
session._query_state = q_emb
|
17 |
+
return session._query_state
|
18 |
+
def set_run_again(state):
|
19 |
+
query_cache(state)
|
20 |
+
def should_run_again():
|
21 |
+
state = query_cache()
|
22 |
+
return state if state is not None else False
|
23 |
+
st.sidebar.markdown("""
|
24 |
+
<style>
|
25 |
+
.aligncenter {
|
26 |
+
text-align: center;
|
27 |
+
}
|
28 |
+
</style>
|
29 |
+
<p class="aligncenter">
|
30 |
+
<img src="https://raw.githubusercontent.com/borisdayma/dalle-mini/main/img/logo.png"/>
|
31 |
+
</p>
|
32 |
+
""", unsafe_allow_html=True)
|
33 |
+
st.sidebar.markdown("""
|
34 |
+
___
|
35 |
+
<p style='text-align: center'>
|
36 |
+
DALL·E mini is an AI model that generates images from any prompt you give!
|
37 |
+
</p>
|
38 |
+
<p style='text-align: center'>
|
39 |
+
Created by Boris Dayma et al. 2021
|
40 |
+
<br/>
|
41 |
+
<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>
|
42 |
+
</p>
|
43 |
+
""", unsafe_allow_html=True)
|
44 |
+
st.header('DALL·E mini')
|
45 |
+
st.subheader('Generate images from text')
|
46 |
+
prompt = st.text_input("What do you want to see?")
|
47 |
+
test = st.empty()
|
48 |
+
DEBUG = False
|
49 |
+
if prompt != "" or (should_run_again and prompt != ""):
|
50 |
+
container = st.empty()
|
51 |
+
# The following mimics `streamlit.info()`.
|
52 |
+
# I tried to get the secondary background color using `components.streamlit.config.get_options_for_section("theme")["secondaryBackgroundColor"]`
|
53 |
+
# but it returns None.
|
54 |
+
container.markdown(f"""
|
55 |
+
<style> p {{ margin:0 }} div {{ margin:0 }} </style>
|
56 |
+
<div data-stale="false" class="element-container css-1e5imcs e1tzin5v1">
|
57 |
+
<div class="stAlert">
|
58 |
+
<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">
|
59 |
+
<div class="st-b7">
|
60 |
+
<div class="css-whx05o e13vu3m50">
|
61 |
+
<div data-testid="stMarkdownContainer" class="css-1ekf893 e16nr0p30">
|
62 |
+
<img src="https://raw.githubusercontent.com/borisdayma/dalle-mini/main/app/img/loading.gif" width="30"/>
|
63 |
+
Generating predictions for: <b>{prompt}</b>
|
64 |
+
</div>
|
65 |
+
</div>
|
66 |
+
</div>
|
67 |
+
</div>
|
68 |
+
</div>
|
69 |
+
</div>
|
70 |
+
<small><i>Predictions may take up to 40s under high load. Please stand by.</i></small>
|
71 |
+
""", unsafe_allow_html=True)
|
72 |
+
try:
|
73 |
+
backend_url = st.secrets["BACKEND_SERVER"]
|
74 |
+
print(f"Getting selections: {prompt}")
|
75 |
+
selected = get_images_from_backend(prompt, backend_url)
|
76 |
+
cols = st.beta_columns(4)
|
77 |
+
for i, img in enumerate(selected):
|
78 |
+
cols[i%4].image(img)
|
79 |
+
container.markdown(f"**{prompt}**")
|
80 |
+
|
81 |
+
set_run_again(st.button('Again!', key='again_button'))
|
82 |
+
|
83 |
+
except ServiceError as error:
|
84 |
+
container.text(f"Service unavailable, status: {error.status_code}")
|
85 |
+
except KeyError:
|
86 |
+
if DEBUG:
|
87 |
+
container.markdown("""
|
88 |
+
**Error: BACKEND_SERVER unset**
|
89 |
+
Please, create a file called `.streamlit/secrets.toml` inside the app's folder and include a line to configure the server URL:
|
90 |
+
```
|
91 |
+
BACKEND_SERVER="<server url>"
|
92 |
+
```
|
93 |
+
""")
|
94 |
+
else:
|
95 |
+
container.markdown('Error -5, please try again or [report it](mailto:pcuenca-dalle@guenever.net).')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|