Update app.py
Browse files
app.py
CHANGED
@@ -1,84 +1,92 @@
|
|
|
|
1 |
import streamlit as st
|
2 |
-
import
|
3 |
-
import
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# app.py
|
2 |
import streamlit as st
|
3 |
+
from diffusers import StableDiffusionPipeline
|
4 |
+
import torch
|
5 |
+
|
6 |
+
# Cache the model loading to avoid reloading on every interaction
|
7 |
+
@st.cache_resource
|
8 |
+
def load_model():
|
9 |
+
model_id = "CompVis/stable-diffusion-v1-4"
|
10 |
+
pipe = StableDiffusionPipeline.from_pretrained(
|
11 |
+
model_id,
|
12 |
+
torch_dtype=torch.float32,
|
13 |
+
use_safetensors=True,
|
14 |
+
safety_checker=None
|
15 |
+
)
|
16 |
+
pipe = pipe.to("cpu")
|
17 |
+
pipe.enable_attention_slicing() # Reduce memory usage
|
18 |
+
return pipe
|
19 |
+
|
20 |
+
def main():
|
21 |
+
st.set_page_config(page_title="Poetry to Image", page_icon="🎨")
|
22 |
+
|
23 |
+
st.title("✨ Romantic Text to Image Converter")
|
24 |
+
st.markdown("Turn your poetic words into beautiful images (CPU version)")
|
25 |
+
|
26 |
+
# Initialize session state
|
27 |
+
if 'generated_image' not in st.session_state:
|
28 |
+
st.session_state.generated_image = None
|
29 |
+
if 'error' not in st.session_state:
|
30 |
+
st.session_state.error = None
|
31 |
+
|
32 |
+
# Sidebar with settings
|
33 |
+
with st.sidebar:
|
34 |
+
st.header("Settings")
|
35 |
+
num_steps = st.slider("Inference Steps", 10, 50, 25)
|
36 |
+
guidance_scale = st.slider("Guidance Scale", 3.0, 15.0, 7.5)
|
37 |
+
|
38 |
+
# Main interface
|
39 |
+
prompt = st.text_area(
|
40 |
+
"Enter your romantic text or poetry:",
|
41 |
+
height=150,
|
42 |
+
placeholder="Example: 'Your eyes sparkle like stars in the night sky...'"
|
43 |
+
)
|
44 |
+
|
45 |
+
col1, col2 = st.columns([1, 3])
|
46 |
+
with col1:
|
47 |
+
if st.button("Generate Image", type="primary"):
|
48 |
+
st.session_state.generated_image = None
|
49 |
+
st.session_state.error = None
|
50 |
+
|
51 |
+
if not prompt.strip():
|
52 |
+
st.session_state.error = "Please enter some text first!"
|
53 |
+
else:
|
54 |
+
try:
|
55 |
+
with st.spinner("Creating your artwork... (this may take a few minutes)"):
|
56 |
+
pipe = load_model()
|
57 |
+
image = pipe(
|
58 |
+
prompt,
|
59 |
+
num_inference_steps=num_steps,
|
60 |
+
guidance_scale=guidance_scale
|
61 |
+
).images[0]
|
62 |
+
|
63 |
+
st.session_state.generated_image = image
|
64 |
+
except Exception as e:
|
65 |
+
st.session_state.error = f"Error: {str(e)}"
|
66 |
+
|
67 |
+
# Display results
|
68 |
+
if st.session_state.error:
|
69 |
+
st.error(st.session_state.error)
|
70 |
+
|
71 |
+
if st.session_state.generated_image:
|
72 |
+
st.subheader("Generated Image")
|
73 |
+
st.image(
|
74 |
+
st.session_state.generated_image,
|
75 |
+
use_column_width=True,
|
76 |
+
caption="Your generated artwork"
|
77 |
+
)
|
78 |
+
|
79 |
+
# Add download button
|
80 |
+
buf = io.BytesIO()
|
81 |
+
st.session_state.generated_image.save(buf, format="PNG")
|
82 |
+
byte_im = buf.getvalue()
|
83 |
+
|
84 |
+
st.download_button(
|
85 |
+
label="Download Image",
|
86 |
+
data=byte_im,
|
87 |
+
file_name="poetry_image.png",
|
88 |
+
mime="image/png"
|
89 |
+
)
|
90 |
+
|
91 |
+
if __name__ == "__main__":
|
92 |
+
main()
|