Spaces:
Runtime error
Runtime error
Upload folder using huggingface_hub
Browse files
app.py
CHANGED
|
@@ -38,6 +38,17 @@ def main():
|
|
| 38 |
|
| 39 |
if 'current_results' not in st.session_state:
|
| 40 |
st.session_state.current_results = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
# File uploader
|
| 43 |
uploaded_file = st.file_uploader("Choose a target image", type=['png', 'jpg', 'jpeg'])
|
|
@@ -58,46 +69,122 @@ def main():
|
|
| 58 |
# Initialize optimization
|
| 59 |
is_completed, prompt, generated_image = st.session_state.optimizer.initialize(target_image)
|
| 60 |
st.session_state.current_results = (is_completed, prompt, generated_image)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
# Display optimization progress
|
| 63 |
if st.session_state.optimization_started:
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
-
#
|
| 70 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
|
| 72 |
# Progress metrics
|
| 73 |
col1, col2, col3 = st.columns(3)
|
| 74 |
with col1:
|
| 75 |
-
|
|
|
|
|
|
|
| 76 |
with col2:
|
| 77 |
if len(st.session_state.optimizer.history) > 0:
|
| 78 |
similarity = st.session_state.optimizer.history[-1]['similarity']
|
| 79 |
st.metric("Similarity", f"{similarity:.2%}")
|
| 80 |
with col3:
|
| 81 |
-
|
|
|
|
| 82 |
|
| 83 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
if not is_completed:
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
st.session_state.
|
| 88 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
else:
|
| 90 |
st.success("Optimization completed! Click 'Reset' to try another image.")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
|
| 92 |
# Reset button
|
| 93 |
if st.button("Reset"):
|
| 94 |
st.session_state.optimization_started = False
|
| 95 |
st.session_state.current_results = None
|
|
|
|
|
|
|
| 96 |
st.rerun()
|
| 97 |
|
| 98 |
-
# Display history
|
| 99 |
if len(st.session_state.optimizer.history) > 0:
|
| 100 |
-
st.subheader("Optimization History")
|
|
|
|
| 101 |
for idx, hist_entry in enumerate(st.session_state.optimizer.history):
|
| 102 |
st.markdown(f"### Step {idx + 1}")
|
| 103 |
col1, col2 = st.columns([2, 3])
|
|
|
|
| 38 |
|
| 39 |
if 'current_results' not in st.session_state:
|
| 40 |
st.session_state.current_results = None
|
| 41 |
+
|
| 42 |
+
# Auto mode state
|
| 43 |
+
if 'auto_mode' not in st.session_state:
|
| 44 |
+
st.session_state.auto_mode = False
|
| 45 |
+
|
| 46 |
+
if 'auto_paused' not in st.session_state:
|
| 47 |
+
st.session_state.auto_paused = False
|
| 48 |
+
|
| 49 |
+
# Auto mode step control - use this to control when to step vs when to display
|
| 50 |
+
if 'auto_should_step' not in st.session_state:
|
| 51 |
+
st.session_state.auto_should_step = False
|
| 52 |
|
| 53 |
# File uploader
|
| 54 |
uploaded_file = st.file_uploader("Choose a target image", type=['png', 'jpg', 'jpeg'])
|
|
|
|
| 69 |
# Initialize optimization
|
| 70 |
is_completed, prompt, generated_image = st.session_state.optimizer.initialize(target_image)
|
| 71 |
st.session_state.current_results = (is_completed, prompt, generated_image)
|
| 72 |
+
st.rerun()
|
| 73 |
+
else:
|
| 74 |
+
# Show disabled button or status when optimization has started
|
| 75 |
+
st.button("Start Optimization", disabled=True, help="Optimization in progress")
|
| 76 |
|
| 77 |
# Display optimization progress
|
| 78 |
if st.session_state.optimization_started:
|
| 79 |
+
if st.session_state.current_results is not None:
|
| 80 |
+
with col2:
|
| 81 |
+
st.subheader("Generated Image")
|
| 82 |
+
is_completed, prompt, generated_image = st.session_state.current_results
|
| 83 |
+
st.image(generated_image, width='stretch')
|
| 84 |
+
|
| 85 |
+
# Display prompt and controls
|
| 86 |
+
st.text_area("Current Prompt", prompt, height=100)
|
| 87 |
+
else:
|
| 88 |
+
# Show loading state
|
| 89 |
+
with col2:
|
| 90 |
+
st.subheader("Generated Image")
|
| 91 |
+
st.info("Initializing optimization...")
|
| 92 |
+
st.text_area("Current Prompt", "Generating initial prompt...", height=100)
|
| 93 |
+
is_completed = False
|
| 94 |
+
prompt = ""
|
| 95 |
|
| 96 |
+
# Auto mode controls
|
| 97 |
+
st.subheader("Auto Mode Controls")
|
| 98 |
+
col_auto1, col_auto2 = st.columns(2)
|
| 99 |
+
|
| 100 |
+
with col_auto1:
|
| 101 |
+
auto_mode = st.checkbox("Auto-progress steps", value=st.session_state.auto_mode)
|
| 102 |
+
if auto_mode != st.session_state.auto_mode:
|
| 103 |
+
st.session_state.auto_mode = auto_mode
|
| 104 |
+
if auto_mode:
|
| 105 |
+
st.session_state.auto_paused = False
|
| 106 |
+
st.session_state.auto_should_step = True # Start by stepping
|
| 107 |
+
st.rerun()
|
| 108 |
+
|
| 109 |
+
with col_auto2:
|
| 110 |
+
if st.session_state.auto_mode:
|
| 111 |
+
if st.session_state.auto_paused:
|
| 112 |
+
if st.button("鈻讹笍 Resume", key="resume_btn"):
|
| 113 |
+
st.session_state.auto_paused = False
|
| 114 |
+
st.rerun()
|
| 115 |
+
else:
|
| 116 |
+
if st.button("鈴革笍 Pause", key="pause_btn"):
|
| 117 |
+
st.session_state.auto_paused = True
|
| 118 |
+
st.rerun()
|
| 119 |
|
| 120 |
# Progress metrics
|
| 121 |
col1, col2, col3 = st.columns(3)
|
| 122 |
with col1:
|
| 123 |
+
# Show current iteration: completed steps + 1 if still in progress
|
| 124 |
+
current_iteration = len(st.session_state.optimizer.history) + (0 if is_completed else 1)
|
| 125 |
+
st.metric("Iteration", current_iteration)
|
| 126 |
with col2:
|
| 127 |
if len(st.session_state.optimizer.history) > 0:
|
| 128 |
similarity = st.session_state.optimizer.history[-1]['similarity']
|
| 129 |
st.metric("Similarity", f"{similarity:.2%}")
|
| 130 |
with col3:
|
| 131 |
+
status = "Completed" if is_completed else "Paused" if st.session_state.auto_paused else "In Progress"
|
| 132 |
+
st.metric("Status", status)
|
| 133 |
|
| 134 |
+
# Progress bar
|
| 135 |
+
st.subheader("Progress")
|
| 136 |
+
max_iterations = st.session_state.optimizer.max_iterations
|
| 137 |
+
progress_value = min(current_iteration / max_iterations, 1.0) if max_iterations > 0 else 0.0
|
| 138 |
+
st.progress(progress_value, text=f"Step {current_iteration} of {max_iterations}")
|
| 139 |
+
|
| 140 |
+
# Next step logic
|
| 141 |
if not is_completed:
|
| 142 |
+
# Auto mode logic - mimic pause button behavior
|
| 143 |
+
if st.session_state.auto_mode and not st.session_state.auto_paused:
|
| 144 |
+
if st.session_state.auto_should_step:
|
| 145 |
+
# Execute the step
|
| 146 |
+
is_completed, prompt, generated_image = st.session_state.optimizer.step()
|
| 147 |
+
st.session_state.current_results = (is_completed, prompt, generated_image)
|
| 148 |
+
# Set flag to NOT step on next render (let history display)
|
| 149 |
+
st.session_state.auto_should_step = False
|
| 150 |
+
st.rerun()
|
| 151 |
+
else:
|
| 152 |
+
# Don't step, just display current state and history, then set flag to step next time
|
| 153 |
+
st.session_state.auto_should_step = True
|
| 154 |
+
# Use a small delay then rerun to continue auto mode
|
| 155 |
+
import time
|
| 156 |
+
time.sleep(0.5) # Give user time to see the history
|
| 157 |
+
st.rerun()
|
| 158 |
+
|
| 159 |
+
# Manual mode
|
| 160 |
+
elif not st.session_state.auto_mode:
|
| 161 |
+
if st.button("Next Step"):
|
| 162 |
+
is_completed, prompt, generated_image = st.session_state.optimizer.step()
|
| 163 |
+
st.session_state.current_results = (is_completed, prompt, generated_image)
|
| 164 |
+
st.rerun()
|
| 165 |
+
|
| 166 |
+
# Show status when auto mode is paused
|
| 167 |
+
elif st.session_state.auto_paused:
|
| 168 |
+
st.info("Auto mode is paused. Click Resume to continue or uncheck Auto-progress to use manual mode.")
|
| 169 |
else:
|
| 170 |
st.success("Optimization completed! Click 'Reset' to try another image.")
|
| 171 |
+
# Turn off auto mode when completed
|
| 172 |
+
if st.session_state.auto_mode:
|
| 173 |
+
st.session_state.auto_mode = False
|
| 174 |
+
st.session_state.auto_paused = False
|
| 175 |
|
| 176 |
# Reset button
|
| 177 |
if st.button("Reset"):
|
| 178 |
st.session_state.optimization_started = False
|
| 179 |
st.session_state.current_results = None
|
| 180 |
+
st.session_state.auto_mode = False
|
| 181 |
+
st.session_state.auto_paused = False
|
| 182 |
st.rerun()
|
| 183 |
|
| 184 |
+
# Display history - simple approach
|
| 185 |
if len(st.session_state.optimizer.history) > 0:
|
| 186 |
+
st.subheader(f"Optimization History ({len(st.session_state.optimizer.history)} steps)")
|
| 187 |
+
|
| 188 |
for idx, hist_entry in enumerate(st.session_state.optimizer.history):
|
| 189 |
st.markdown(f"### Step {idx + 1}")
|
| 190 |
col1, col2 = st.columns([2, 3])
|