Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,3 @@
|
|
| 1 |
-
#Eine neue Stable Diffusion (SD) Generation kommt mit neuem Tokenizer, mehrsprachiger Unterstützung, längerem Kontext
|
| 2 |
-
#und deutlich besserem Prompt-Verständnis - (Änderung Architektur).
|
| 3 |
-
#Eine deutsche Alternative zur Umsetzung von Text-Bild zu Bild ist Flux - mit einer völlig anderen Architektur als SD!
|
| 4 |
import gradio as gr
|
| 5 |
from diffusers import StableDiffusionPipeline, StableDiffusionImg2ImgPipeline
|
| 6 |
from diffusers import StableDiffusionInpaintPipeline
|
|
@@ -149,6 +146,77 @@ class ImageToImageProgressCallback:
|
|
| 149 |
self.progress(progress_percent / 100, desc="Generierung läuft - CPU benötigt bis zu 20 Minuten!")
|
| 150 |
return callback_kwargs
|
| 151 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
# === FUNKTIONEN ===
|
| 153 |
def text_to_image(prompt, steps, guidance_scale, progress=gr.Progress()):
|
| 154 |
try:
|
|
@@ -357,6 +425,24 @@ def main_ui():
|
|
| 357 |
.image-upload .svelte-1p4f8co {
|
| 358 |
display: block !important;
|
| 359 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 360 |
"""
|
| 361 |
) as demo:
|
| 362 |
|
|
@@ -488,11 +574,22 @@ def main_ui():
|
|
| 488 |
gr.Markdown("**Lade ein Bild hoch und beschreibe die gewünschte Veränderung:**")
|
| 489 |
|
| 490 |
with gr.Row():
|
|
|
|
| 491 |
img_input = gr.Image(
|
| 492 |
type="pil",
|
| 493 |
label="Eingabebild",
|
| 494 |
height=300,
|
| 495 |
-
sources=["upload"] # Nur Upload-Button anzeigen
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 496 |
)
|
| 497 |
|
| 498 |
with gr.Row():
|
|
@@ -536,42 +633,46 @@ def main_ui():
|
|
| 536 |
face_preserve = gr.Checkbox(
|
| 537 |
label="Gesicht, Tier, Gegenstand beibehalten",
|
| 538 |
value=True,
|
| 539 |
-
info="
|
| 540 |
)
|
| 541 |
|
| 542 |
with gr.Row():
|
| 543 |
gr.Markdown("**Bildelementbereich anpassen**")
|
| 544 |
|
|
|
|
| 545 |
with gr.Row():
|
| 546 |
-
|
| 547 |
-
|
| 548 |
-
|
| 549 |
-
|
| 550 |
-
|
| 551 |
-
|
| 552 |
-
|
| 553 |
-
|
| 554 |
-
|
| 555 |
-
|
| 556 |
-
|
| 557 |
-
|
| 558 |
-
|
| 559 |
-
|
| 560 |
-
|
| 561 |
-
|
| 562 |
-
|
| 563 |
-
|
| 564 |
-
|
| 565 |
-
|
| 566 |
-
|
| 567 |
-
|
| 568 |
-
|
| 569 |
-
|
|
|
|
| 570 |
|
| 571 |
with gr.Row():
|
| 572 |
gr.Markdown(
|
| 573 |
"**Achtung:**\n"
|
| 574 |
-
"•
|
|
|
|
|
|
|
| 575 |
"• **Koordinaten nur bei erkennbaren Verzerrungen anpassen** (Bereiche leicht verschieben)"
|
| 576 |
)
|
| 577 |
|
|
@@ -585,13 +686,49 @@ def main_ui():
|
|
| 585 |
type="pil"
|
| 586 |
)
|
| 587 |
|
| 588 |
-
# Event-Handler für
|
|
|
|
| 589 |
img_input.change(
|
| 590 |
-
fn=
|
| 591 |
inputs=[img_input],
|
| 592 |
-
outputs=[bbox_x1, bbox_y1, bbox_x2, bbox_y2]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 593 |
)
|
| 594 |
|
|
|
|
| 595 |
transform_btn.click(
|
| 596 |
fn=img_to_image,
|
| 597 |
inputs=[
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from diffusers import StableDiffusionPipeline, StableDiffusionImg2ImgPipeline
|
| 3 |
from diffusers import StableDiffusionInpaintPipeline
|
|
|
|
| 146 |
self.progress(progress_percent / 100, desc="Generierung läuft - CPU benötigt bis zu 20 Minuten!")
|
| 147 |
return callback_kwargs
|
| 148 |
|
| 149 |
+
# === NEUE FUNKTIONEN FÜR DIE FEATURES ===
|
| 150 |
+
def create_preview_image(image, bbox_coords, face_preserve, mode_color):
|
| 151 |
+
"""Erstellt eine Vorschau mit farbigem Rahmen basierend auf dem Modus"""
|
| 152 |
+
if image is None:
|
| 153 |
+
return None
|
| 154 |
+
|
| 155 |
+
# Erstelle eine Kopie für die Vorschau
|
| 156 |
+
preview = image.copy()
|
| 157 |
+
draw = ImageDraw.Draw(preview)
|
| 158 |
+
|
| 159 |
+
# Rahmenfarbe basierend auf Modus
|
| 160 |
+
if mode_color == "red":
|
| 161 |
+
border_color = (255, 0, 0, 180) # Rot mit Transparenz
|
| 162 |
+
mode_text = "NUR BILDELEMENT VERÄNDERN"
|
| 163 |
+
else:
|
| 164 |
+
border_color = (0, 255, 0, 180) # Grün mit Transparenz
|
| 165 |
+
mode_text = "BILDELEMENT BEIBEHALTEN"
|
| 166 |
+
|
| 167 |
+
# Zeichne den Rahmen um das gesamte Bild
|
| 168 |
+
border_width = 8
|
| 169 |
+
draw.rectangle([0, 0, preview.width-1, preview.height-1],
|
| 170 |
+
outline=border_color, width=border_width)
|
| 171 |
+
|
| 172 |
+
# Zeichne Bounding Box wenn Koordinaten vorhanden
|
| 173 |
+
if bbox_coords and all(coord is not None for coord in bbox_coords):
|
| 174 |
+
x1, y1, x2, y2 = bbox_coords
|
| 175 |
+
|
| 176 |
+
# Rahmen für Bounding Box
|
| 177 |
+
box_color = (255, 255, 0, 200) # Gelb für Bounding Box
|
| 178 |
+
draw.rectangle([x1, y1, x2, y2], outline=box_color, width=3)
|
| 179 |
+
|
| 180 |
+
# Text-Label für den Modus
|
| 181 |
+
text_color = (255, 255, 255)
|
| 182 |
+
bg_color = (0, 0, 0, 160)
|
| 183 |
+
|
| 184 |
+
# Hintergrund für Text
|
| 185 |
+
text_bbox = draw.textbbox((x1, y1 - 25), mode_text)
|
| 186 |
+
draw.rectangle([text_bbox[0]-5, text_bbox[1]-2, text_bbox[2]+5, text_bbox[3]+2],
|
| 187 |
+
fill=bg_color)
|
| 188 |
+
|
| 189 |
+
# Text zeichnen
|
| 190 |
+
draw.text((x1, y1 - 25), mode_text, fill=text_color)
|
| 191 |
+
|
| 192 |
+
return preview
|
| 193 |
+
|
| 194 |
+
def update_live_preview(image, bbox_x1, bbox_y1, bbox_x2, bbox_y2, face_preserve):
|
| 195 |
+
"""Aktualisiert die Live-Vorschau bei Koordinaten-Änderungen"""
|
| 196 |
+
if image is None:
|
| 197 |
+
return None
|
| 198 |
+
|
| 199 |
+
bbox_coords = [bbox_x1, bbox_y1, bbox_x2, bbox_y2]
|
| 200 |
+
|
| 201 |
+
# Bestimme Rahmenfarbe basierend auf Modus
|
| 202 |
+
mode_color = "green" if face_preserve else "red"
|
| 203 |
+
|
| 204 |
+
return create_preview_image(image, bbox_coords, face_preserve, mode_color)
|
| 205 |
+
|
| 206 |
+
def process_image_upload(image):
|
| 207 |
+
"""Verarbeitet Bild-Upload und gibt Bild + Koordinaten zurück"""
|
| 208 |
+
if image is None:
|
| 209 |
+
return None, None, None, None, None
|
| 210 |
+
|
| 211 |
+
# Auto-Koordinaten generieren
|
| 212 |
+
bbox = auto_detect_face_area(image)
|
| 213 |
+
bbox_x1, bbox_y1, bbox_x2, bbox_y2 = bbox
|
| 214 |
+
|
| 215 |
+
# Vorschau mit grünem Rahmen (Standard: Gesicht beibehalten)
|
| 216 |
+
preview = create_preview_image(image, bbox, True, "green")
|
| 217 |
+
|
| 218 |
+
return preview, bbox_x1, bbox_y1, bbox_x2, bbox_y2
|
| 219 |
+
|
| 220 |
# === FUNKTIONEN ===
|
| 221 |
def text_to_image(prompt, steps, guidance_scale, progress=gr.Progress()):
|
| 222 |
try:
|
|
|
|
| 425 |
.image-upload .svelte-1p4f8co {
|
| 426 |
display: block !important;
|
| 427 |
}
|
| 428 |
+
.preview-box {
|
| 429 |
+
border: 2px dashed #ccc;
|
| 430 |
+
padding: 10px;
|
| 431 |
+
border-radius: 8px;
|
| 432 |
+
margin: 10px 0;
|
| 433 |
+
}
|
| 434 |
+
.mode-red {
|
| 435 |
+
border: 3px solid #ff4444 !important;
|
| 436 |
+
}
|
| 437 |
+
.mode-green {
|
| 438 |
+
border: 3px solid #44ff44 !important;
|
| 439 |
+
}
|
| 440 |
+
.coordinate-sliders {
|
| 441 |
+
background: #f8f9fa;
|
| 442 |
+
padding: 15px;
|
| 443 |
+
border-radius: 8px;
|
| 444 |
+
margin: 10px 0;
|
| 445 |
+
}
|
| 446 |
"""
|
| 447 |
) as demo:
|
| 448 |
|
|
|
|
| 574 |
gr.Markdown("**Lade ein Bild hoch und beschreibe die gewünschte Veränderung:**")
|
| 575 |
|
| 576 |
with gr.Row():
|
| 577 |
+
# NEUE: Bild-Upload mit Vorschau
|
| 578 |
img_input = gr.Image(
|
| 579 |
type="pil",
|
| 580 |
label="Eingabebild",
|
| 581 |
height=300,
|
| 582 |
+
sources=["upload"], # Nur Upload-Button anzeigen
|
| 583 |
+
elem_id="image-upload"
|
| 584 |
+
)
|
| 585 |
+
|
| 586 |
+
with gr.Row():
|
| 587 |
+
# NEUE: Vorschau-Bild mit Live-Updates
|
| 588 |
+
preview_output = gr.Image(
|
| 589 |
+
label="🎯 Live-Vorschau mit Maske",
|
| 590 |
+
height=300,
|
| 591 |
+
interactive=False,
|
| 592 |
+
show_download_button=False
|
| 593 |
)
|
| 594 |
|
| 595 |
with gr.Row():
|
|
|
|
| 633 |
face_preserve = gr.Checkbox(
|
| 634 |
label="Gesicht, Tier, Gegenstand beibehalten",
|
| 635 |
value=True,
|
| 636 |
+
info="🟢 GRÜN: Bildelement bleibt erhalten, Hintergrund wird verändert | 🔴 ROT: Nur Bildelement wird verändert"
|
| 637 |
)
|
| 638 |
|
| 639 |
with gr.Row():
|
| 640 |
gr.Markdown("**Bildelementbereich anpassen**")
|
| 641 |
|
| 642 |
+
# NEUE: Koordinaten-Schieberegler mit Live-Update
|
| 643 |
with gr.Row():
|
| 644 |
+
with gr.Column():
|
| 645 |
+
bbox_x1 = gr.Slider(
|
| 646 |
+
label="Links (x1)",
|
| 647 |
+
minimum=0, maximum=512, value=100, step=1,
|
| 648 |
+
info="Linke Kante des Bildelementbereichs"
|
| 649 |
+
)
|
| 650 |
+
with gr.Column():
|
| 651 |
+
bbox_y1 = gr.Slider(
|
| 652 |
+
label="Oben (y1)",
|
| 653 |
+
minimum=0, maximum=512, value=100, step=1,
|
| 654 |
+
info="Obere Kante des Bildelementbereichs"
|
| 655 |
+
)
|
| 656 |
+
with gr.Row():
|
| 657 |
+
with gr.Column():
|
| 658 |
+
bbox_x2 = gr.Slider(
|
| 659 |
+
label="Rechts (x2)",
|
| 660 |
+
minimum=0, maximum=512, value=300, step=1,
|
| 661 |
+
info="Rechte Kante des Bildelementbereichs"
|
| 662 |
+
)
|
| 663 |
+
with gr.Column():
|
| 664 |
+
bbox_y2 = gr.Slider(
|
| 665 |
+
label="Unten (y2)",
|
| 666 |
+
minimum=0, maximum=512, value=300, step=1,
|
| 667 |
+
info="Untere Kante des Bildelementbereichs"
|
| 668 |
+
)
|
| 669 |
|
| 670 |
with gr.Row():
|
| 671 |
gr.Markdown(
|
| 672 |
"**Achtung:**\n"
|
| 673 |
+
"• **🆕 Automatische Bildelementerkennung** setzt Koordinaten beim Upload\n"
|
| 674 |
+
"• **🆕 Live-Vorschau** zeigt farbige Rahmen je nach Modus (🔴 Rot / 🟢 Grün)\n"
|
| 675 |
+
"• **🆕 Koordinaten-Schieberegler** für präzise Anpassung mit Live-Update\n"
|
| 676 |
"• **Koordinaten nur bei erkennbaren Verzerrungen anpassen** (Bereiche leicht verschieben)"
|
| 677 |
)
|
| 678 |
|
|
|
|
| 686 |
type="pil"
|
| 687 |
)
|
| 688 |
|
| 689 |
+
# NEUE: Event-Handler für alle Live-Updates
|
| 690 |
+
# Bild-Upload: Auto-Koordinaten + Vorschau
|
| 691 |
img_input.change(
|
| 692 |
+
fn=process_image_upload,
|
| 693 |
inputs=[img_input],
|
| 694 |
+
outputs=[preview_output, bbox_x1, bbox_y1, bbox_x2, bbox_y2]
|
| 695 |
+
)
|
| 696 |
+
|
| 697 |
+
# Live-Updates bei Koordinaten-Änderungen
|
| 698 |
+
coordinate_inputs = [img_input, bbox_x1, bbox_y1, bbox_x2, bbox_y2, face_preserve]
|
| 699 |
+
|
| 700 |
+
bbox_x1.change(
|
| 701 |
+
fn=update_live_preview,
|
| 702 |
+
inputs=coordinate_inputs,
|
| 703 |
+
outputs=preview_output
|
| 704 |
+
)
|
| 705 |
+
|
| 706 |
+
bbox_y1.change(
|
| 707 |
+
fn=update_live_preview,
|
| 708 |
+
inputs=coordinate_inputs,
|
| 709 |
+
outputs=preview_output
|
| 710 |
+
)
|
| 711 |
+
|
| 712 |
+
bbox_x2.change(
|
| 713 |
+
fn=update_live_preview,
|
| 714 |
+
inputs=coordinate_inputs,
|
| 715 |
+
outputs=preview_output
|
| 716 |
+
)
|
| 717 |
+
|
| 718 |
+
bbox_y2.change(
|
| 719 |
+
fn=update_live_preview,
|
| 720 |
+
inputs=coordinate_inputs,
|
| 721 |
+
outputs=preview_output
|
| 722 |
+
)
|
| 723 |
+
|
| 724 |
+
# Live-Update bei Modus-Änderung
|
| 725 |
+
face_preserve.change(
|
| 726 |
+
fn=update_live_preview,
|
| 727 |
+
inputs=coordinate_inputs,
|
| 728 |
+
outputs=preview_output
|
| 729 |
)
|
| 730 |
|
| 731 |
+
# Transform-Button (UNVERÄNDERT - gibt OUTPUT zurück)
|
| 732 |
transform_btn.click(
|
| 733 |
fn=img_to_image,
|
| 734 |
inputs=[
|