Amanpandey04 commited on
Commit
2b98bb2
·
verified ·
1 Parent(s): c85506b
Files changed (1) hide show
  1. app.py +16 -15
app.py CHANGED
@@ -104,32 +104,32 @@ def generate_image(pipe, prompt, num_inference_steps=30):
104
  def main():
105
  st.title("🎨 Stable Diffusion Image Generator")
106
 
 
 
 
 
 
 
107
  # Sidebar for configuration
108
  st.sidebar.header("Configuration")
109
 
110
  # HuggingFace token input
111
  hf_token = st.sidebar.text_input("Enter HuggingFace Token:", type="password")
112
 
113
- # Initialize session state
114
- if 'pipe' not in st.session_state:
115
- st.session_state.pipe = None
116
-
117
- # Initialize button
118
- if st.sidebar.button("Initialize Model"):
119
- if not hf_token:
120
- st.sidebar.error("Please enter your HuggingFace token")
121
- else:
122
- st.session_state.pipe = setup_stable_diffusion(hf_token)
123
- if st.session_state.pipe is not None:
124
- st.sidebar.success("Model initialized successfully!")
125
 
126
  # Main interface
127
  prompt = st.text_input("Enter your prompt:", "A stunning landscape with mountains and a lake at sunset")
128
  num_steps = st.slider("Number of inference steps:", min_value=20, max_value=100, value=30)
129
 
130
  if st.button("Generate Image"):
131
- if st.session_state.pipe is None:
132
- st.error("Please initialize the model first using the sidebar!")
133
  else:
134
  try:
135
  image = generate_image(st.session_state.pipe, prompt, num_steps)
@@ -151,4 +151,5 @@ def main():
151
  torch.cuda.empty_cache()
152
 
153
  if __name__ == "__main__":
154
- main()
 
 
104
  def main():
105
  st.title("🎨 Stable Diffusion Image Generator")
106
 
107
+ # Initialize session state
108
+ if 'pipe' not in st.session_state:
109
+ st.session_state.pipe = None
110
+ if 'model_initialized' not in st.session_state:
111
+ st.session_state.model_initialized = False
112
+
113
  # Sidebar for configuration
114
  st.sidebar.header("Configuration")
115
 
116
  # HuggingFace token input
117
  hf_token = st.sidebar.text_input("Enter HuggingFace Token:", type="password")
118
 
119
+ # Auto-initialize when token is entered
120
+ if hf_token and not st.session_state.model_initialized:
121
+ st.session_state.pipe = setup_stable_diffusion(hf_token)
122
+ if st.session_state.pipe is not None:
123
+ st.session_state.model_initialized = True
124
+ st.sidebar.success("Model initialized successfully!")
 
 
 
 
 
 
125
 
126
  # Main interface
127
  prompt = st.text_input("Enter your prompt:", "A stunning landscape with mountains and a lake at sunset")
128
  num_steps = st.slider("Number of inference steps:", min_value=20, max_value=100, value=30)
129
 
130
  if st.button("Generate Image"):
131
+ if not st.session_state.model_initialized:
132
+ st.error("Please enter your HuggingFace token in the sidebar to initialize the model!")
133
  else:
134
  try:
135
  image = generate_image(st.session_state.pipe, prompt, num_steps)
 
151
  torch.cuda.empty_cache()
152
 
153
  if __name__ == "__main__":
154
+ main()
155
+