File size: 8,263 Bytes
c4a3b63
 
 
 
3503531
 
a5593f0
3503531
 
f0f8078
3503531
 
 
c4a3b63
a5593f0
f0f8078
c4a3b63
 
f0f8078
c4a3b63
 
 
 
 
 
 
7f3d2e1
 
 
a5593f0
 
 
 
3c543cd
a5593f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c4a3b63
f0f8078
c4a3b63
a5593f0
 
 
 
 
 
 
 
 
3c543cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3503531
3c543cd
 
 
 
3503531
ab1fc99
 
 
c4a3b63
3c543cd
 
a5593f0
 
 
c4a3b63
 
 
3c543cd
 
 
 
a5593f0
3c543cd
 
ab1fc99
3c543cd
 
a5593f0
 
 
 
c4a3b63
 
 
3c543cd
 
 
 
a5593f0
3c543cd
 
c4a3b63
3c543cd
 
a5593f0
 
 
c4a3b63
 
a5593f0
c4a3b63
f0f8078
 
c4a3b63
 
f0f8078
 
c4a3b63
ab1fc99
f0f8078
 
 
 
a5593f0
 
f0f8078
a4d80d9
3c543cd
a5593f0
3c543cd
a5593f0
 
 
 
 
f0f8078
a5593f0
 
3c543cd
a5593f0
 
c4a3b63
a5593f0
 
c4a3b63
 
3c543cd
c4a3b63
7f3d2e1
 
ab1fc99
 
7f3d2e1
 
 
 
c4a3b63
 
ab1fc99
c4a3b63
7f3d2e1
f0f8078
c4a3b63
a5593f0
3c543cd
a5593f0
3c543cd
a5593f0
 
c4a3b63
a5593f0
 
 
f0f8078
 
c4a3b63
a5593f0
 
 
 
c4a3b63
a5593f0
 
 
 
 
3c543cd
c4a3b63
 
ab1fc99
c4a3b63
7f3d2e1
 
3503531
ab1fc99
7f3d2e1
f0f8078
7f3d2e1
f0f8078
c4a3b63
 
f0f8078
 
c4a3b63
a5593f0
 
 
 
c4a3b63
a5593f0
 
 
 
 
3c543cd
c4a3b63
 
3503531
c4a3b63
ab1fc99
 
3503531
ab1fc99
 
f0f8078
c4a3b63
ab1fc99
f0f8078
 
c4a3b63
f0f8078
c4a3b63
a5593f0
 
 
 
 
c4a3b63
a5593f0
 
3c543cd
c4a3b63
 
ab1fc99
c4a3b63
7f3d2e1
c4a3b63
f0f8078
c4a3b63
 
 
 
 
f0f8078
c4a3b63
a5593f0
 
 
f0f8078
ab1fc99
a5593f0
f0f8078
 
 
ab1fc99
a5593f0
f0f8078
 
 
c4a3b63
f0f8078
7f3d2e1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
import streamlit as st
from gradio_client import Client, handle_file
import threading
import time
import os
import uuid
import shutil

# ---------------------------
# CONFIG
# ---------------------------
IMAGE_DIR = "data/images"
os.makedirs(IMAGE_DIR, exist_ok=True)

client = Client("Galaxydude2/a")
client_process = Client("SLSLK/n8")

# ---------------------------
# SESSION
# ---------------------------
if "gallery" not in st.session_state:
    st.session_state.gallery = []

if "selected_image" not in st.session_state:
    st.session_state.selected_image = None

if "page" not in st.session_state:
    st.session_state.page = "πŸ–ΌοΈ Generieren"

if "prompt_history" not in st.session_state:
    st.session_state.prompt_history = []

# ---------------------------
# UI / THEME
# ---------------------------
st.set_page_config(layout="wide")

st.markdown("""
<style>
.stApp { background-color: #0f0f1a; color: #e0e0ff; }
h1 { color: #ff2e82; text-align:center; }
.stButton > button {
    background-color: #ff2e82;
    color: white;
    border-radius: 10px;
}
.stButton > button:hover { background-color: #ff5ca8; }
</style>
""", unsafe_allow_html=True)

st.markdown("<h1>🎨 Omni Image Studio</h1>", unsafe_allow_html=True)

# ---------------------------
# HELPERS
# ---------------------------
def save_local_copy(src_path):
    if os.path.exists(src_path):
        new_path = os.path.join(IMAGE_DIR, f"{uuid.uuid4()}.png")
        shutil.copy(src_path, new_path)
        return new_path
    return None


def prepare_image(input_image):
    try:
        if hasattr(input_image, "getvalue"):
            path = os.path.join(IMAGE_DIR, f"{uuid.uuid4()}.png")
            with open(path, "wb") as f:
                f.write(input_image.getvalue())
            return path

        elif isinstance(input_image, str) and os.path.exists(input_image):
            return input_image

        return None

    except Exception as e:
        st.error(f"❌ Prepare Error: {e}")
        return None


# ---------------------------
# πŸ”₯ ROBUST API CALL
# ---------------------------
def robust_predict(func, *args, retries=3, delay=2, **kwargs):
    for attempt in range(retries):
        try:
            return func(*args, **kwargs)

        except Exception as e:
            if attempt < retries - 1:
                st.warning(f"⚠️ Retry {attempt+1}/{retries}...")
                time.sleep(delay)
            else:
                st.error("❌ API dauerhaft fehlgeschlagen")

                with st.expander("πŸ” Debug Details"):
                    st.code(str(e))

                return None

# ---------------------------
# API FUNCTIONS
# ---------------------------
def generate_image(prompt):
    return robust_predict(
        client.predict,
        prompt=prompt,
        api_name="/generate_image"
    )


def edit_image(input_image, prompt):
    if not input_image or not prompt:
        st.warning("❗ Bild & Prompt erforderlich")
        return None

    image_path = prepare_image(input_image)
    if not image_path:
        return None

    return robust_predict(
        client.predict,
        input_image=handle_file(image_path),
        edit_prompt=prompt,
        api_name="/edit_image"
    )


def process_image(input_image):
    if not input_image:
        st.warning("❗ Bild erforderlich")
        return None

    image_path = prepare_image(input_image)
    if not image_path:
        return None

    return robust_predict(
        client_process.predict,
        image=handle_file(image_path),
        api_name="/process_image"
    )

# ---------------------------
# ASYNC + PROGRESS
# ---------------------------
def run_async(func, *args):
    result = {"done": False, "data": None}

    def task():
        result["data"] = func(*args)
        result["done"] = True

    threading.Thread(target=task).start()
    return result


def show_progress(job):
    bar = st.progress(0)
    status = st.empty()
    percent = 0

    msgs = [
        "πŸ”„ Processing...",
        "🎨 Generating...",
        "🧠 AI thinking...",
        "✨ Almost done..."
    ]

    i = 0
    while not job["done"]:
        percent = min(percent + 5, 95)
        bar.progress(percent)
        status.text(msgs[i % len(msgs)])
        i += 1
        time.sleep(0.3)

    bar.progress(100)
    status.text("βœ… Done!")

# ---------------------------
# NAVIGATION
# ---------------------------
page = st.radio(
    "Navigation",
    ["πŸ–ΌοΈ Generieren", "✨ Bearbeiten", "πŸ§ͺ Analyse", "πŸ“š Galerie"],
    index=["πŸ–ΌοΈ Generieren", "✨ Bearbeiten", "πŸ§ͺ Analyse", "πŸ“š Galerie"].index(st.session_state.page),
    horizontal=True
)

st.session_state.page = page

# ---------------------------
# GENERATE
# ---------------------------
if page == "πŸ–ΌοΈ Generieren":
    prompt = st.text_area("Prompt")

    if st.session_state.prompt_history:
        st.subheader("🧠 History")
        for i, p in enumerate(reversed(st.session_state.prompt_history[-5:])):
            if st.button(p, key=f"h{i}"):
                prompt = p

    if st.button("Generieren"):
        if prompt:
            st.session_state.prompt_history.append(prompt)

        job = run_async(generate_image, prompt)
        show_progress(job)

        result = job["data"]

        if result:
            st.image(result)

            with open(result, "rb") as f:
                st.download_button("πŸ“₯ Download", f, file_name="generated.png")

            path = save_local_copy(result)
            if path:
                st.session_state.gallery.append({"path": path, "prompt": prompt})

# ---------------------------
# EDIT
# ---------------------------
elif page == "✨ Bearbeiten":
    if st.session_state.selected_image:
        st.image(st.session_state.selected_image)
        input_image = st.session_state.selected_image
    else:
        input_image = st.file_uploader("Upload Image")

    prompt = st.text_area("Edit Prompt")

    if st.button("Bearbeiten"):
        job = run_async(edit_image, input_image, prompt)
        show_progress(job)

        result = job["data"]

        if result:
            st.image(result)

            with open(result, "rb") as f:
                st.download_button("πŸ“₯ Download", f, file_name="edited.png")

            path = save_local_copy(result)
            if path:
                st.session_state.gallery.append({"path": path, "prompt": prompt})

# ---------------------------
# ANALYSE
# ---------------------------
elif page == "πŸ§ͺ Analyse":
    if st.session_state.selected_image:
        st.image(st.session_state.selected_image)
        input_image = st.session_state.selected_image
    else:
        input_image = st.file_uploader("Upload Image")

    if st.button("Analysieren"):
        job = run_async(process_image, input_image)
        show_progress(job)

        result = job["data"]

        if result:
            st.image(result)

            with open(result, "rb") as f:
                st.download_button("πŸ“₯ Download", f, file_name="analysis.png")

            if os.path.exists(result):
                path = save_local_copy(result)
                st.session_state.gallery.append({"path": path, "prompt": "Analyse"})

# ---------------------------
# GALLERY
# ---------------------------
elif page == "πŸ“š Galerie":
    if not st.session_state.gallery:
        st.info("Keine Bilder")
    else:
        cols = st.columns(3)

        for i, item in enumerate(st.session_state.gallery):
            with cols[i % 3]:
                st.image(item["path"], caption=item["prompt"])

                with open(item["path"], "rb") as f:
                    st.download_button("πŸ“₯", f, file_name=f"img_{i}.png", key=f"d{i}")

                c1, c2 = st.columns(2)

                if c1.button("✏️", key=f"e{i}"):
                    st.session_state.selected_image = item["path"]
                    st.session_state.page = "✨ Bearbeiten"
                    st.rerun()

                if c2.button("πŸ§ͺ", key=f"a{i}"):
                    st.session_state.selected_image = item["path"]
                    st.session_state.page = "πŸ§ͺ Analyse"
                    st.rerun()

        if st.button("πŸ—‘οΈ Clear"):
            st.session_state.gallery = []