Update app.py
Browse files
app.py
CHANGED
|
@@ -4,7 +4,6 @@ import threading
|
|
| 4 |
import time
|
| 5 |
import os
|
| 6 |
import uuid
|
| 7 |
-
import tempfile
|
| 8 |
import shutil
|
| 9 |
|
| 10 |
# ---------------------------
|
|
@@ -32,7 +31,7 @@ if "prompt_history" not in st.session_state:
|
|
| 32 |
st.session_state.prompt_history = []
|
| 33 |
|
| 34 |
# ---------------------------
|
| 35 |
-
# THEME
|
| 36 |
# ---------------------------
|
| 37 |
st.set_page_config(layout="wide")
|
| 38 |
|
|
@@ -63,30 +62,65 @@ def save_local_copy(src_path):
|
|
| 63 |
|
| 64 |
|
| 65 |
def prepare_image(input_image):
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
|
| 76 |
# ---------------------------
|
| 77 |
# API FUNCTIONS
|
| 78 |
# ---------------------------
|
| 79 |
def generate_image(prompt):
|
| 80 |
-
return
|
|
|
|
| 81 |
prompt=prompt,
|
| 82 |
api_name="/generate_image"
|
| 83 |
)
|
| 84 |
|
| 85 |
|
| 86 |
def edit_image(input_image, prompt):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
image_path = prepare_image(input_image)
|
|
|
|
|
|
|
| 88 |
|
| 89 |
-
return
|
|
|
|
| 90 |
input_image=handle_file(image_path),
|
| 91 |
edit_prompt=prompt,
|
| 92 |
api_name="/edit_image"
|
|
@@ -94,9 +128,16 @@ def edit_image(input_image, prompt):
|
|
| 94 |
|
| 95 |
|
| 96 |
def process_image(input_image):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
image_path = prepare_image(input_image)
|
|
|
|
|
|
|
| 98 |
|
| 99 |
-
return
|
|
|
|
| 100 |
image=handle_file(image_path),
|
| 101 |
api_name="/process_image"
|
| 102 |
)
|
|
@@ -120,9 +161,9 @@ def show_progress(job):
|
|
| 120 |
status = st.empty()
|
| 121 |
percent = 0
|
| 122 |
|
| 123 |
-
|
| 124 |
"π Processing...",
|
| 125 |
-
"π¨ Generating
|
| 126 |
"π§ AI thinking...",
|
| 127 |
"β¨ Almost done..."
|
| 128 |
]
|
|
@@ -131,17 +172,15 @@ def show_progress(job):
|
|
| 131 |
while not job["done"]:
|
| 132 |
percent = min(percent + 5, 95)
|
| 133 |
bar.progress(percent)
|
| 134 |
-
|
| 135 |
-
status.text(messages[i % len(messages)])
|
| 136 |
i += 1
|
| 137 |
-
|
| 138 |
time.sleep(0.3)
|
| 139 |
|
| 140 |
bar.progress(100)
|
| 141 |
status.text("β
Done!")
|
| 142 |
|
| 143 |
# ---------------------------
|
| 144 |
-
#
|
| 145 |
# ---------------------------
|
| 146 |
page = st.radio(
|
| 147 |
"Navigation",
|
|
@@ -158,11 +197,10 @@ st.session_state.page = page
|
|
| 158 |
if page == "πΌοΈ Generieren":
|
| 159 |
prompt = st.text_area("Prompt")
|
| 160 |
|
| 161 |
-
# π§ Prompt History
|
| 162 |
if st.session_state.prompt_history:
|
| 163 |
-
st.subheader("π§
|
| 164 |
for i, p in enumerate(reversed(st.session_state.prompt_history[-5:])):
|
| 165 |
-
if st.button(p, key=f"
|
| 166 |
prompt = p
|
| 167 |
|
| 168 |
if st.button("Generieren"):
|
|
@@ -177,16 +215,12 @@ if page == "πΌοΈ Generieren":
|
|
| 177 |
if result:
|
| 178 |
st.image(result)
|
| 179 |
|
| 180 |
-
# π₯ Download
|
| 181 |
with open(result, "rb") as f:
|
| 182 |
st.download_button("π₯ Download", f, file_name="generated.png")
|
| 183 |
|
| 184 |
path = save_local_copy(result)
|
| 185 |
if path:
|
| 186 |
-
st.session_state.gallery.append({
|
| 187 |
-
"path": path,
|
| 188 |
-
"prompt": prompt
|
| 189 |
-
})
|
| 190 |
|
| 191 |
# ---------------------------
|
| 192 |
# EDIT
|
|
@@ -214,10 +248,7 @@ elif page == "β¨ Bearbeiten":
|
|
| 214 |
|
| 215 |
path = save_local_copy(result)
|
| 216 |
if path:
|
| 217 |
-
st.session_state.gallery.append({
|
| 218 |
-
"path": path,
|
| 219 |
-
"prompt": prompt
|
| 220 |
-
})
|
| 221 |
|
| 222 |
# ---------------------------
|
| 223 |
# ANALYSE
|
|
@@ -243,10 +274,7 @@ elif page == "π§ͺ Analyse":
|
|
| 243 |
|
| 244 |
if os.path.exists(result):
|
| 245 |
path = save_local_copy(result)
|
| 246 |
-
st.session_state.gallery.append({
|
| 247 |
-
"path": path,
|
| 248 |
-
"prompt": "Analyse"
|
| 249 |
-
})
|
| 250 |
|
| 251 |
# ---------------------------
|
| 252 |
# GALLERY
|
|
@@ -261,7 +289,6 @@ elif page == "π Galerie":
|
|
| 261 |
with cols[i % 3]:
|
| 262 |
st.image(item["path"], caption=item["prompt"])
|
| 263 |
|
| 264 |
-
# π₯ Download
|
| 265 |
with open(item["path"], "rb") as f:
|
| 266 |
st.download_button("π₯", f, file_name=f"img_{i}.png", key=f"d{i}")
|
| 267 |
|
|
|
|
| 4 |
import time
|
| 5 |
import os
|
| 6 |
import uuid
|
|
|
|
| 7 |
import shutil
|
| 8 |
|
| 9 |
# ---------------------------
|
|
|
|
| 31 |
st.session_state.prompt_history = []
|
| 32 |
|
| 33 |
# ---------------------------
|
| 34 |
+
# UI / THEME
|
| 35 |
# ---------------------------
|
| 36 |
st.set_page_config(layout="wide")
|
| 37 |
|
|
|
|
| 62 |
|
| 63 |
|
| 64 |
def prepare_image(input_image):
|
| 65 |
+
try:
|
| 66 |
+
if hasattr(input_image, "getvalue"):
|
| 67 |
+
path = os.path.join(IMAGE_DIR, f"{uuid.uuid4()}.png")
|
| 68 |
+
with open(path, "wb") as f:
|
| 69 |
+
f.write(input_image.getvalue())
|
| 70 |
+
return path
|
| 71 |
+
|
| 72 |
+
elif isinstance(input_image, str) and os.path.exists(input_image):
|
| 73 |
+
return input_image
|
| 74 |
+
|
| 75 |
+
return None
|
| 76 |
+
|
| 77 |
+
except Exception as e:
|
| 78 |
+
st.error(f"β Prepare Error: {e}")
|
| 79 |
+
return None
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
# ---------------------------
|
| 83 |
+
# π₯ ROBUST API CALL
|
| 84 |
+
# ---------------------------
|
| 85 |
+
def robust_predict(func, *args, retries=3, delay=2, **kwargs):
|
| 86 |
+
for attempt in range(retries):
|
| 87 |
+
try:
|
| 88 |
+
return func(*args, **kwargs)
|
| 89 |
+
|
| 90 |
+
except Exception as e:
|
| 91 |
+
if attempt < retries - 1:
|
| 92 |
+
st.warning(f"β οΈ Retry {attempt+1}/{retries}...")
|
| 93 |
+
time.sleep(delay)
|
| 94 |
+
else:
|
| 95 |
+
st.error("β API dauerhaft fehlgeschlagen")
|
| 96 |
|
| 97 |
+
with st.expander("π Debug Details"):
|
| 98 |
+
st.code(str(e))
|
| 99 |
+
|
| 100 |
+
return None
|
| 101 |
|
| 102 |
# ---------------------------
|
| 103 |
# API FUNCTIONS
|
| 104 |
# ---------------------------
|
| 105 |
def generate_image(prompt):
|
| 106 |
+
return robust_predict(
|
| 107 |
+
client.predict,
|
| 108 |
prompt=prompt,
|
| 109 |
api_name="/generate_image"
|
| 110 |
)
|
| 111 |
|
| 112 |
|
| 113 |
def edit_image(input_image, prompt):
|
| 114 |
+
if not input_image or not prompt:
|
| 115 |
+
st.warning("β Bild & Prompt erforderlich")
|
| 116 |
+
return None
|
| 117 |
+
|
| 118 |
image_path = prepare_image(input_image)
|
| 119 |
+
if not image_path:
|
| 120 |
+
return None
|
| 121 |
|
| 122 |
+
return robust_predict(
|
| 123 |
+
client.predict,
|
| 124 |
input_image=handle_file(image_path),
|
| 125 |
edit_prompt=prompt,
|
| 126 |
api_name="/edit_image"
|
|
|
|
| 128 |
|
| 129 |
|
| 130 |
def process_image(input_image):
|
| 131 |
+
if not input_image:
|
| 132 |
+
st.warning("β Bild erforderlich")
|
| 133 |
+
return None
|
| 134 |
+
|
| 135 |
image_path = prepare_image(input_image)
|
| 136 |
+
if not image_path:
|
| 137 |
+
return None
|
| 138 |
|
| 139 |
+
return robust_predict(
|
| 140 |
+
client_process.predict,
|
| 141 |
image=handle_file(image_path),
|
| 142 |
api_name="/process_image"
|
| 143 |
)
|
|
|
|
| 161 |
status = st.empty()
|
| 162 |
percent = 0
|
| 163 |
|
| 164 |
+
msgs = [
|
| 165 |
"π Processing...",
|
| 166 |
+
"π¨ Generating...",
|
| 167 |
"π§ AI thinking...",
|
| 168 |
"β¨ Almost done..."
|
| 169 |
]
|
|
|
|
| 172 |
while not job["done"]:
|
| 173 |
percent = min(percent + 5, 95)
|
| 174 |
bar.progress(percent)
|
| 175 |
+
status.text(msgs[i % len(msgs)])
|
|
|
|
| 176 |
i += 1
|
|
|
|
| 177 |
time.sleep(0.3)
|
| 178 |
|
| 179 |
bar.progress(100)
|
| 180 |
status.text("β
Done!")
|
| 181 |
|
| 182 |
# ---------------------------
|
| 183 |
+
# NAVIGATION
|
| 184 |
# ---------------------------
|
| 185 |
page = st.radio(
|
| 186 |
"Navigation",
|
|
|
|
| 197 |
if page == "πΌοΈ Generieren":
|
| 198 |
prompt = st.text_area("Prompt")
|
| 199 |
|
|
|
|
| 200 |
if st.session_state.prompt_history:
|
| 201 |
+
st.subheader("π§ History")
|
| 202 |
for i, p in enumerate(reversed(st.session_state.prompt_history[-5:])):
|
| 203 |
+
if st.button(p, key=f"h{i}"):
|
| 204 |
prompt = p
|
| 205 |
|
| 206 |
if st.button("Generieren"):
|
|
|
|
| 215 |
if result:
|
| 216 |
st.image(result)
|
| 217 |
|
|
|
|
| 218 |
with open(result, "rb") as f:
|
| 219 |
st.download_button("π₯ Download", f, file_name="generated.png")
|
| 220 |
|
| 221 |
path = save_local_copy(result)
|
| 222 |
if path:
|
| 223 |
+
st.session_state.gallery.append({"path": path, "prompt": prompt})
|
|
|
|
|
|
|
|
|
|
| 224 |
|
| 225 |
# ---------------------------
|
| 226 |
# EDIT
|
|
|
|
| 248 |
|
| 249 |
path = save_local_copy(result)
|
| 250 |
if path:
|
| 251 |
+
st.session_state.gallery.append({"path": path, "prompt": prompt})
|
|
|
|
|
|
|
|
|
|
| 252 |
|
| 253 |
# ---------------------------
|
| 254 |
# ANALYSE
|
|
|
|
| 274 |
|
| 275 |
if os.path.exists(result):
|
| 276 |
path = save_local_copy(result)
|
| 277 |
+
st.session_state.gallery.append({"path": path, "prompt": "Analyse"})
|
|
|
|
|
|
|
|
|
|
| 278 |
|
| 279 |
# ---------------------------
|
| 280 |
# GALLERY
|
|
|
|
| 289 |
with cols[i % 3]:
|
| 290 |
st.image(item["path"], caption=item["prompt"])
|
| 291 |
|
|
|
|
| 292 |
with open(item["path"], "rb") as f:
|
| 293 |
st.download_button("π₯", f, file_name=f"img_{i}.png", key=f"d{i}")
|
| 294 |
|