meraj12 commited on
Commit
7632fc7
·
verified ·
1 Parent(s): 582aa2c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -89
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 using .bin files
43
- use_auth_token=False,
44
- safety_checker=None
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() # Reduce memory usage
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
- # Initialize session state
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 with settings
74
  with st.sidebar:
75
  st.header("Settings")
76
- num_steps = st.slider("Inference Steps", 10, 50, 25)
77
- guidance_scale = st.slider("Guidance Scale", 3.0, 15.0, 7.5)
78
-
79
  # Main interface
80
  prompt = st.text_area(
81
- "Enter your romantic text or poetry:",
82
  height=150,
83
- placeholder="Example: 'Your eyes sparkle like stars in the night sky...'"
84
  )
85
-
86
- col1, col2 = st.columns([1, 3])
87
- with col1:
88
- if st.button("Generate Image", type="primary"):
89
- st.session_state.generated_image = None
90
- st.session_state.error = None
91
-
92
- if not prompt.strip():
93
- st.session_state.error = "Please enter some text first!"
94
- else:
95
- try:
96
- with st.spinner("Creating your artwork... (this may take a few minutes)"):
97
- pipe = load_model()
98
- image = pipe(
99
- prompt,
100
- num_inference_steps=num_steps,
101
- guidance_scale=guidance_scale
102
- ).images[0]
103
-
104
- st.session_state.generated_image = image
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
- # Add download button
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
- label="Download Image",
127
- data=byte_im,
128
- file_name="poetry_image.png",
129
- mime="image/png"
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__":