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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from gradio_client import Client, handle_file
|
| 3 |
+
import re
|
| 4 |
+
import threading
|
| 5 |
+
import time
|
| 6 |
+
|
| 7 |
+
# ---------------------------
|
| 8 |
+
# Clients
|
| 9 |
+
# ---------------------------
|
| 10 |
+
client_main = Client("selfit-camera/Omni-Image-Editor")
|
| 11 |
+
client_process = Client("Galaxydude2/aaaaa")
|
| 12 |
+
|
| 13 |
+
# ---------------------------
|
| 14 |
+
# Session State
|
| 15 |
+
# ---------------------------
|
| 16 |
+
if "gallery" not in st.session_state:
|
| 17 |
+
st.session_state.gallery = []
|
| 18 |
+
|
| 19 |
+
if "edit_mode" not in st.session_state:
|
| 20 |
+
st.session_state.edit_mode = False
|
| 21 |
+
|
| 22 |
+
if "selected_image" not in st.session_state:
|
| 23 |
+
st.session_state.selected_image = None
|
| 24 |
+
|
| 25 |
+
# ---------------------------
|
| 26 |
+
# Funktionen
|
| 27 |
+
# ---------------------------
|
| 28 |
+
def extract_url(html):
|
| 29 |
+
match = re.search(r"src='([^']+)'", html)
|
| 30 |
+
return match.group(1) if match else None
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
def generate_image(prompt):
|
| 34 |
+
try:
|
| 35 |
+
result = client_main.predict(
|
| 36 |
+
prompt=prompt,
|
| 37 |
+
aspect_ratio="16:9",
|
| 38 |
+
api_name="/text_to_image_interface"
|
| 39 |
+
)
|
| 40 |
+
return extract_url(result[0])
|
| 41 |
+
except Exception as e:
|
| 42 |
+
st.error(e)
|
| 43 |
+
return None
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
def edit_image(input_image, prompt):
|
| 47 |
+
try:
|
| 48 |
+
result = client_main.predict(
|
| 49 |
+
input_image=handle_file(input_image),
|
| 50 |
+
prompt=prompt,
|
| 51 |
+
api_name="/edit_image_interface"
|
| 52 |
+
)
|
| 53 |
+
return extract_url(result[0])
|
| 54 |
+
except Exception as e:
|
| 55 |
+
st.error(e)
|
| 56 |
+
return None
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
def process_image(input_image):
|
| 60 |
+
try:
|
| 61 |
+
result = client_process.predict(
|
| 62 |
+
image=handle_file(input_image),
|
| 63 |
+
api_name="/process_image"
|
| 64 |
+
)
|
| 65 |
+
return result
|
| 66 |
+
except Exception as e:
|
| 67 |
+
st.error(e)
|
| 68 |
+
return None
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
# ---------------------------
|
| 72 |
+
# Async Helper
|
| 73 |
+
# ---------------------------
|
| 74 |
+
def run_with_progress(func, *args):
|
| 75 |
+
result_container = {"result": None, "done": False}
|
| 76 |
+
|
| 77 |
+
def task():
|
| 78 |
+
result_container["result"] = func(*args)
|
| 79 |
+
result_container["done"] = True
|
| 80 |
+
|
| 81 |
+
thread = threading.Thread(target=task)
|
| 82 |
+
thread.start()
|
| 83 |
+
return result_container
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
# ---------------------------
|
| 87 |
+
# UI Setup
|
| 88 |
+
# ---------------------------
|
| 89 |
+
st.set_page_config(page_title="Omni Image Studio", layout="wide")
|
| 90 |
+
|
| 91 |
+
st.title("🎨 Omni Image Studio")
|
| 92 |
+
st.write("Generieren • Bearbeiten • Verarbeiten • Galerie")
|
| 93 |
+
|
| 94 |
+
tab1, tab2, tab3, tab4 = st.tabs([
|
| 95 |
+
"🖼️ Generieren",
|
| 96 |
+
"✨ Bearbeiten",
|
| 97 |
+
"🧪 Process",
|
| 98 |
+
"📚 Galerie"
|
| 99 |
+
])
|
| 100 |
+
|
| 101 |
+
# ---------------------------
|
| 102 |
+
# TAB 1: GENERATE
|
| 103 |
+
# ---------------------------
|
| 104 |
+
with tab1:
|
| 105 |
+
prompt = st.text_area("Bildbeschreibung")
|
| 106 |
+
|
| 107 |
+
if st.button("Generieren"):
|
| 108 |
+
if prompt:
|
| 109 |
+
status = st.empty()
|
| 110 |
+
progress = st.progress(0)
|
| 111 |
+
|
| 112 |
+
job = run_with_progress(generate_image, prompt)
|
| 113 |
+
|
| 114 |
+
percent = 0
|
| 115 |
+
while not job["done"]:
|
| 116 |
+
percent = min(percent + 5, 95)
|
| 117 |
+
progress.progress(percent)
|
| 118 |
+
status.text(f"Generiere Bild... {percent}%")
|
| 119 |
+
time.sleep(0.2)
|
| 120 |
+
|
| 121 |
+
progress.progress(100)
|
| 122 |
+
status.text("Fertig ✅")
|
| 123 |
+
|
| 124 |
+
img_url = job["result"]
|
| 125 |
+
|
| 126 |
+
if img_url:
|
| 127 |
+
st.image(img_url)
|
| 128 |
+
st.session_state.gallery.append({
|
| 129 |
+
"url": img_url,
|
| 130 |
+
"prompt": prompt
|
| 131 |
+
})
|
| 132 |
+
|
| 133 |
+
# ---------------------------
|
| 134 |
+
# TAB 2: EDIT
|
| 135 |
+
# ---------------------------
|
| 136 |
+
with tab2:
|
| 137 |
+
uploaded = st.file_uploader("Bild hochladen", type=["png", "jpg", "jpeg"])
|
| 138 |
+
edit_prompt = st.text_area("Bearbeitung")
|
| 139 |
+
|
| 140 |
+
if st.button("Bearbeiten"):
|
| 141 |
+
if uploaded and edit_prompt:
|
| 142 |
+
status = st.empty()
|
| 143 |
+
progress = st.progress(0)
|
| 144 |
+
|
| 145 |
+
job = run_with_progress(edit_image, uploaded, edit_prompt)
|
| 146 |
+
|
| 147 |
+
percent = 0
|
| 148 |
+
while not job["done"]:
|
| 149 |
+
percent = min(percent + 5, 95)
|
| 150 |
+
progress.progress(percent)
|
| 151 |
+
status.text(f"Bearbeite Bild... {percent}%")
|
| 152 |
+
time.sleep(0.2)
|
| 153 |
+
|
| 154 |
+
progress.progress(100)
|
| 155 |
+
status.text("Fertig ✅")
|
| 156 |
+
|
| 157 |
+
img_url = job["result"]
|
| 158 |
+
|
| 159 |
+
if img_url:
|
| 160 |
+
st.image(img_url)
|
| 161 |
+
st.session_state.gallery.append({
|
| 162 |
+
"url": img_url,
|
| 163 |
+
"prompt": edit_prompt
|
| 164 |
+
})
|
| 165 |
+
|
| 166 |
+
# ---------------------------
|
| 167 |
+
# TAB 3: PROCESS
|
| 168 |
+
# ---------------------------
|
| 169 |
+
with tab3:
|
| 170 |
+
uploaded = st.file_uploader("Bild auswählen", type=["png", "jpg", "jpeg"], key="proc")
|
| 171 |
+
|
| 172 |
+
if st.button("Verarbeiten"):
|
| 173 |
+
if uploaded:
|
| 174 |
+
status = st.empty()
|
| 175 |
+
progress = st.progress(0)
|
| 176 |
+
|
| 177 |
+
job = run_with_progress(process_image, uploaded)
|
| 178 |
+
|
| 179 |
+
percent = 0
|
| 180 |
+
while not job["done"]:
|
| 181 |
+
percent = min(percent + 5, 95)
|
| 182 |
+
progress.progress(percent)
|
| 183 |
+
status.text(f"Verarbeite Bild... {percent}%")
|
| 184 |
+
time.sleep(0.2)
|
| 185 |
+
|
| 186 |
+
progress.progress(100)
|
| 187 |
+
status.text("Fertig ✅")
|
| 188 |
+
|
| 189 |
+
result = job["result"]
|
| 190 |
+
|
| 191 |
+
if isinstance(result, str) and result.startswith("http"):
|
| 192 |
+
st.image(result)
|
| 193 |
+
st.session_state.gallery.append({
|
| 194 |
+
"url": result,
|
| 195 |
+
"prompt": "Processed Image"
|
| 196 |
+
})
|
| 197 |
+
else:
|
| 198 |
+
st.write(result)
|
| 199 |
+
|
| 200 |
+
# ---------------------------
|
| 201 |
+
# TAB 4: GALLERY
|
| 202 |
+
# ---------------------------
|
| 203 |
+
with tab4:
|
| 204 |
+
st.subheader("📸 Galerie")
|
| 205 |
+
|
| 206 |
+
if not st.session_state.gallery:
|
| 207 |
+
st.info("Keine Bilder vorhanden")
|
| 208 |
+
else:
|
| 209 |
+
cols = st.columns(3)
|
| 210 |
+
|
| 211 |
+
for i, item in enumerate(st.session_state.gallery):
|
| 212 |
+
url = item.get("url")
|
| 213 |
+
prompt_text = item.get("prompt", "Kein Prompt")
|
| 214 |
+
|
| 215 |
+
with cols[i % 3]:
|
| 216 |
+
st.image(url, caption=prompt_text)
|
| 217 |
+
|
| 218 |
+
if st.button("✏️ Edit", key=f"edit_{i}"):
|
| 219 |
+
st.session_state.selected_image = url
|
| 220 |
+
st.session_state.edit_mode = True
|
| 221 |
+
|
| 222 |
+
if st.button("🗑️ Galerie leeren"):
|
| 223 |
+
st.session_state.gallery = []
|
| 224 |
+
|
| 225 |
+
# ---------------------------
|
| 226 |
+
# GLOBAL EDIT (aus Galerie)
|
| 227 |
+
# ---------------------------
|
| 228 |
+
if st.session_state.edit_mode:
|
| 229 |
+
st.divider()
|
| 230 |
+
st.subheader("✏️ Bild aus Galerie bearbeiten")
|
| 231 |
+
|
| 232 |
+
st.image(st.session_state.selected_image)
|
| 233 |
+
|
| 234 |
+
edit_prompt_gallery = st.text_area("Bearbeitung", key="gallery_edit")
|
| 235 |
+
|
| 236 |
+
if st.button("🚀 Anwenden"):
|
| 237 |
+
status = st.empty()
|
| 238 |
+
progress = st.progress(0)
|
| 239 |
+
|
| 240 |
+
job = run_with_progress(
|
| 241 |
+
edit_image,
|
| 242 |
+
st.session_state.selected_image,
|
| 243 |
+
edit_prompt_gallery
|
| 244 |
+
)
|
| 245 |
+
|
| 246 |
+
percent = 0
|
| 247 |
+
while not job["done"]:
|
| 248 |
+
percent = min(percent + 5, 95)
|
| 249 |
+
progress.progress(percent)
|
| 250 |
+
status.text(f"Bearbeite Bild... {percent}%")
|
| 251 |
+
time.sleep(0.2)
|
| 252 |
+
|
| 253 |
+
progress.progress(100)
|
| 254 |
+
status.text("Fertig ✅")
|
| 255 |
+
|
| 256 |
+
result = job["result"]
|
| 257 |
+
|
| 258 |
+
if result:
|
| 259 |
+
st.image(result)
|
| 260 |
+
st.session_state.gallery.append({
|
| 261 |
+
"url": result,
|
| 262 |
+
"prompt": edit_prompt_gallery
|
| 263 |
+
})
|
| 264 |
+
|
| 265 |
+
if st.button("❌ Abbrechen"):
|
| 266 |
+
st.session_state.edit_mode = False
|