Update app.py
Browse files
app.py
CHANGED
@@ -1,132 +1,86 @@
|
|
1 |
# app.py
|
2 |
-
#Add before any other imports
|
3 |
-
from huggingface_hub import cached_download, hf_hub_download
|
4 |
-
import warnings
|
5 |
-
warnings.filterwarnings("ignore", category=DeprecationWarning)
|
6 |
-
|
7 |
-
import streamlit as st
|
8 |
-
from diffusers import StableDiffusionPipeline
|
9 |
-
import torch
|
10 |
-
|
11 |
import streamlit as st
|
12 |
import torch
|
|
|
|
|
13 |
from diffusers import StableDiffusionPipeline
|
14 |
|
15 |
-
|
16 |
-
# Model configuration
|
17 |
MODEL_ID = "CompVis/stable-diffusion-v1-4"
|
|
|
|
|
18 |
|
19 |
-
@st.cache_resource(show_spinner=False)
|
20 |
-
def load_model():
|
21 |
-
return StableDiffusionPipeline.from_pretrained(
|
22 |
-
MODEL_ID,
|
23 |
-
torch_dtype=torch.float32,
|
24 |
-
use_auth_token=False,
|
25 |
-
revision="fp16",
|
26 |
-
use_safetensors=False, # Force use of .bin files
|
27 |
-
custom_pipeline="stable_diffusion",
|
28 |
-
safety_checker=None
|
29 |
-
).to("cpu")
|
30 |
-
|
31 |
-
# Rest of your Streamlit code remains the same...
|
32 |
-
# Add this at the top of your app.py
|
33 |
-
import warnings
|
34 |
-
warnings.filterwarnings("ignore", message="cached_download is deprecated")
|
35 |
-
|
36 |
-
# Modify your model loading to use legacy features
|
37 |
@st.cache_resource(show_spinner=False)
|
38 |
def load_model():
|
39 |
pipe = StableDiffusionPipeline.from_pretrained(
|
40 |
MODEL_ID,
|
41 |
torch_dtype=torch.float32,
|
42 |
-
use_safetensors=False, # Force
|
43 |
-
|
44 |
-
|
45 |
-
)
|
46 |
-
# Rest of your loading code...
|
47 |
-
# Cache the model loading to avoid reloading on every interaction
|
48 |
-
@st.cache_resource
|
49 |
-
def load_model():
|
50 |
-
model_id = "CompVis/stable-diffusion-v1-4"
|
51 |
-
pipe = StableDiffusionPipeline.from_pretrained(
|
52 |
-
model_id,
|
53 |
-
torch_dtype=torch.float32,
|
54 |
-
use_safetensors=True,
|
55 |
-
safety_checker=None
|
56 |
)
|
57 |
pipe = pipe.to("cpu")
|
58 |
-
pipe.enable_attention_slicing()
|
59 |
return pipe
|
60 |
|
61 |
def main():
|
62 |
st.set_page_config(page_title="Poetry to Image", page_icon="🎨")
|
63 |
-
|
64 |
st.title("✨ Romantic Text to Image Converter")
|
65 |
-
st.markdown("Turn your poetic words into beautiful images (CPU version)")
|
66 |
|
67 |
-
#
|
68 |
if 'generated_image' not in st.session_state:
|
69 |
st.session_state.generated_image = None
|
70 |
if 'error' not in st.session_state:
|
71 |
st.session_state.error = None
|
72 |
-
|
73 |
-
# Sidebar
|
74 |
with st.sidebar:
|
75 |
st.header("Settings")
|
76 |
-
num_steps = st.slider("Inference Steps", 10, 50,
|
77 |
-
guidance_scale = st.slider("Guidance Scale", 3.0, 15.0,
|
78 |
-
|
79 |
# Main interface
|
80 |
prompt = st.text_area(
|
81 |
-
"Enter your romantic text
|
82 |
height=150,
|
83 |
-
placeholder="Example: 'Your eyes sparkle like stars
|
84 |
)
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
except Exception as e:
|
106 |
-
st.session_state.error = f"Error: {str(e)}"
|
107 |
-
|
108 |
# Display results
|
109 |
if st.session_state.error:
|
110 |
st.error(st.session_state.error)
|
111 |
|
112 |
if st.session_state.generated_image:
|
113 |
st.subheader("Generated Image")
|
114 |
-
st.image(
|
115 |
-
st.session_state.generated_image,
|
116 |
-
use_column_width=True,
|
117 |
-
caption="Your generated artwork"
|
118 |
-
)
|
119 |
|
120 |
-
#
|
121 |
buf = io.BytesIO()
|
122 |
st.session_state.generated_image.save(buf, format="PNG")
|
123 |
-
byte_im = buf.getvalue()
|
124 |
-
|
125 |
st.download_button(
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
)
|
131 |
|
132 |
if __name__ == "__main__":
|
|
|
1 |
# app.py
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
import streamlit as st
|
3 |
import torch
|
4 |
+
import io
|
5 |
+
from PIL import Image
|
6 |
from diffusers import StableDiffusionPipeline
|
7 |
|
8 |
+
# Configuration
|
|
|
9 |
MODEL_ID = "CompVis/stable-diffusion-v1-4"
|
10 |
+
DEFAULT_STEPS = 25
|
11 |
+
DEFAULT_GUIDANCE = 7.5
|
12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
@st.cache_resource(show_spinner=False)
|
14 |
def load_model():
|
15 |
pipe = StableDiffusionPipeline.from_pretrained(
|
16 |
MODEL_ID,
|
17 |
torch_dtype=torch.float32,
|
18 |
+
use_safetensors=False, # Force .bin files for compatibility
|
19 |
+
safety_checker=None,
|
20 |
+
requires_safety_checker=False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
)
|
22 |
pipe = pipe.to("cpu")
|
23 |
+
pipe.enable_attention_slicing()
|
24 |
return pipe
|
25 |
|
26 |
def main():
|
27 |
st.set_page_config(page_title="Poetry to Image", page_icon="🎨")
|
|
|
28 |
st.title("✨ Romantic Text to Image Converter")
|
|
|
29 |
|
30 |
+
# Session state initialization
|
31 |
if 'generated_image' not in st.session_state:
|
32 |
st.session_state.generated_image = None
|
33 |
if 'error' not in st.session_state:
|
34 |
st.session_state.error = None
|
35 |
+
|
36 |
+
# Sidebar controls
|
37 |
with st.sidebar:
|
38 |
st.header("Settings")
|
39 |
+
num_steps = st.slider("Inference Steps", 10, 50, DEFAULT_STEPS)
|
40 |
+
guidance_scale = st.slider("Guidance Scale", 3.0, 15.0, DEFAULT_GUIDANCE)
|
41 |
+
|
42 |
# Main interface
|
43 |
prompt = st.text_area(
|
44 |
+
"Enter your romantic text:",
|
45 |
height=150,
|
46 |
+
placeholder="Example: 'Your eyes sparkle like stars...'"
|
47 |
)
|
48 |
+
|
49 |
+
if st.button("Generate Image", type="primary"):
|
50 |
+
st.session_state.generated_image = None
|
51 |
+
st.session_state.error = None
|
52 |
+
|
53 |
+
if not prompt.strip():
|
54 |
+
st.session_state.error = "Please enter some text!"
|
55 |
+
else:
|
56 |
+
try:
|
57 |
+
with st.spinner("Creating artwork (3-8 minutes)..."):
|
58 |
+
pipe = load_model()
|
59 |
+
image = pipe(
|
60 |
+
prompt,
|
61 |
+
num_inference_steps=num_steps,
|
62 |
+
guidance_scale=guidance_scale
|
63 |
+
).images[0]
|
64 |
+
st.session_state.generated_image = image
|
65 |
+
except Exception as e:
|
66 |
+
st.session_state.error = f"Error: {str(e)}"
|
67 |
+
|
|
|
|
|
|
|
68 |
# Display results
|
69 |
if st.session_state.error:
|
70 |
st.error(st.session_state.error)
|
71 |
|
72 |
if st.session_state.generated_image:
|
73 |
st.subheader("Generated Image")
|
74 |
+
st.image(st.session_state.generated_image, use_column_width=True)
|
|
|
|
|
|
|
|
|
75 |
|
76 |
+
# Download functionality
|
77 |
buf = io.BytesIO()
|
78 |
st.session_state.generated_image.save(buf, format="PNG")
|
|
|
|
|
79 |
st.download_button(
|
80 |
+
"Download Image",
|
81 |
+
buf.getvalue(),
|
82 |
+
"poetry_image.png",
|
83 |
+
"image/png"
|
84 |
)
|
85 |
|
86 |
if __name__ == "__main__":
|