Delete test_qwen_edit.py
Browse files- test_qwen_edit.py +0 -243
test_qwen_edit.py
DELETED
|
@@ -1,243 +0,0 @@
|
|
| 1 |
-
#!/usr/bin/env python3
|
| 2 |
-
"""
|
| 3 |
-
Qwen Image Edit Test Script mit UI
|
| 4 |
-
Testet die offizielle Qwen-Image-Edit-2509 Implementation
|
| 5 |
-
"""
|
| 6 |
-
|
| 7 |
-
import os
|
| 8 |
-
import sys
|
| 9 |
-
import torch
|
| 10 |
-
from PIL import Image
|
| 11 |
-
import gradio as gr
|
| 12 |
-
import tempfile
|
| 13 |
-
|
| 14 |
-
# Versuche verschiedene Import-Methoden für Qwen
|
| 15 |
-
try:
|
| 16 |
-
from diffusers import QwenImageEditPipeline
|
| 17 |
-
print("✅ QwenImageEditPipeline aus diffusers importiert")
|
| 18 |
-
except ImportError:
|
| 19 |
-
try:
|
| 20 |
-
# Alternative: Direkt aus transformers
|
| 21 |
-
from transformers import QwenImageEditPipeline
|
| 22 |
-
print("✅ QwenImageEditPipeline aus transformers importiert")
|
| 23 |
-
except ImportError:
|
| 24 |
-
print("❌ QwenImageEditPipeline nicht gefunden - verwende alternative Methode")
|
| 25 |
-
QwenImageEditPipeline = None
|
| 26 |
-
|
| 27 |
-
class QwenImageEditTester:
|
| 28 |
-
def __init__(self):
|
| 29 |
-
self.pipeline = None
|
| 30 |
-
self.device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 31 |
-
print(f"Using device: {self.device}")
|
| 32 |
-
|
| 33 |
-
def load_pipeline(self):
|
| 34 |
-
"""Lädt die Qwen-Image-Edit Pipeline"""
|
| 35 |
-
try:
|
| 36 |
-
print("Loading Qwen-Image-Edit pipeline...")
|
| 37 |
-
|
| 38 |
-
if QwenImageEditPipeline is None:
|
| 39 |
-
# Alternative: Verwende StableDiffusionInpaintPipeline als Fallback
|
| 40 |
-
from diffusers import StableDiffusionInpaintPipeline
|
| 41 |
-
print("⚠️ Verwende StableDiffusionInpaintPipeline als Fallback")
|
| 42 |
-
self.pipeline = StableDiffusionInpaintPipeline.from_pretrained(
|
| 43 |
-
"runwayml/stable-diffusion-inpainting",
|
| 44 |
-
torch_dtype=torch.bfloat16
|
| 45 |
-
)
|
| 46 |
-
else:
|
| 47 |
-
self.pipeline = QwenImageEditPipeline.from_pretrained("Qwen/Qwen-Image-Edit")
|
| 48 |
-
|
| 49 |
-
print("Pipeline loaded successfully!")
|
| 50 |
-
|
| 51 |
-
# Pipeline konfigurieren
|
| 52 |
-
self.pipeline.to(torch.bfloat16)
|
| 53 |
-
self.pipeline.to(self.device)
|
| 54 |
-
self.pipeline.set_progress_bar_config(disable=None)
|
| 55 |
-
|
| 56 |
-
print("Pipeline configured successfully!")
|
| 57 |
-
return "✅ Pipeline erfolgreich geladen!"
|
| 58 |
-
|
| 59 |
-
except Exception as e:
|
| 60 |
-
error_msg = f"❌ Fehler beim Laden der Pipeline: {str(e)}"
|
| 61 |
-
print(error_msg)
|
| 62 |
-
return error_msg
|
| 63 |
-
|
| 64 |
-
def test_image_edit(self, image, prompt, cfg_scale, steps, seed, negative_prompt):
|
| 65 |
-
"""Testet die Bildbearbeitung mit verschiedenen Einstellungen"""
|
| 66 |
-
if self.pipeline is None:
|
| 67 |
-
return None, "❌ Pipeline nicht geladen! Bitte zuerst 'Pipeline laden' klicken."
|
| 68 |
-
|
| 69 |
-
if image is None:
|
| 70 |
-
return None, "❌ Bitte ein Bild hochladen!"
|
| 71 |
-
|
| 72 |
-
try:
|
| 73 |
-
print(f"Testing with: CFG={cfg_scale}, Steps={steps}, Seed={seed}")
|
| 74 |
-
|
| 75 |
-
# Bild konvertieren falls nötig
|
| 76 |
-
if isinstance(image, str):
|
| 77 |
-
image = Image.open(image)
|
| 78 |
-
|
| 79 |
-
# RGB konvertieren
|
| 80 |
-
image = image.convert("RGB")
|
| 81 |
-
|
| 82 |
-
# Generator für deterministische Ergebnisse
|
| 83 |
-
generator = torch.manual_seed(seed) if seed > 0 else None
|
| 84 |
-
|
| 85 |
-
# Pipeline Inputs
|
| 86 |
-
inputs = {
|
| 87 |
-
"image": image,
|
| 88 |
-
"prompt": prompt,
|
| 89 |
-
"generator": generator,
|
| 90 |
-
"true_cfg_scale": float(cfg_scale),
|
| 91 |
-
"negative_prompt": negative_prompt if negative_prompt.strip() else " ",
|
| 92 |
-
"num_inference_steps": int(steps),
|
| 93 |
-
}
|
| 94 |
-
|
| 95 |
-
print(f"Pipeline inputs: {inputs}")
|
| 96 |
-
|
| 97 |
-
# Inference
|
| 98 |
-
with torch.inference_mode():
|
| 99 |
-
output = self.pipeline(**inputs)
|
| 100 |
-
result_image = output.images[0]
|
| 101 |
-
|
| 102 |
-
print("✅ Bildbearbeitung erfolgreich!")
|
| 103 |
-
return result_image, "✅ Bildbearbeitung erfolgreich!"
|
| 104 |
-
|
| 105 |
-
except Exception as e:
|
| 106 |
-
error_msg = f"❌ Fehler bei der Bildbearbeitung: {str(e)}"
|
| 107 |
-
print(error_msg)
|
| 108 |
-
return None, error_msg
|
| 109 |
-
|
| 110 |
-
def create_ui(self):
|
| 111 |
-
"""Erstellt die Gradio UI"""
|
| 112 |
-
|
| 113 |
-
with gr.Blocks(title="Qwen Image Edit Test", theme=gr.themes.Soft()) as demo:
|
| 114 |
-
gr.Markdown("# 🎨 Qwen Image Edit Test")
|
| 115 |
-
gr.Markdown("Testet die offizielle Qwen-Image-Edit-2509 Implementation")
|
| 116 |
-
|
| 117 |
-
with gr.Row():
|
| 118 |
-
with gr.Column():
|
| 119 |
-
# Pipeline Status
|
| 120 |
-
pipeline_status = gr.Textbox(
|
| 121 |
-
label="Pipeline Status",
|
| 122 |
-
value="Pipeline nicht geladen",
|
| 123 |
-
interactive=False
|
| 124 |
-
)
|
| 125 |
-
|
| 126 |
-
load_btn = gr.Button("🔄 Pipeline laden", variant="primary")
|
| 127 |
-
|
| 128 |
-
# Bild Upload
|
| 129 |
-
input_image = gr.Image(
|
| 130 |
-
label="Input Bild",
|
| 131 |
-
type="pil",
|
| 132 |
-
height=300
|
| 133 |
-
)
|
| 134 |
-
|
| 135 |
-
# Prompt
|
| 136 |
-
prompt = gr.Textbox(
|
| 137 |
-
label="Prompt",
|
| 138 |
-
value="Change the style of this image into realistic photography",
|
| 139 |
-
lines=3
|
| 140 |
-
)
|
| 141 |
-
|
| 142 |
-
# Negative Prompt
|
| 143 |
-
negative_prompt = gr.Textbox(
|
| 144 |
-
label="Negative Prompt",
|
| 145 |
-
value=" ",
|
| 146 |
-
lines=2
|
| 147 |
-
)
|
| 148 |
-
|
| 149 |
-
with gr.Column():
|
| 150 |
-
# Parameter
|
| 151 |
-
cfg_scale = gr.Slider(
|
| 152 |
-
label="CFG Scale",
|
| 153 |
-
minimum=1.0,
|
| 154 |
-
maximum=10.0,
|
| 155 |
-
value=4.0,
|
| 156 |
-
step=0.1
|
| 157 |
-
)
|
| 158 |
-
|
| 159 |
-
steps = gr.Slider(
|
| 160 |
-
label="Steps",
|
| 161 |
-
minimum=1,
|
| 162 |
-
maximum=100,
|
| 163 |
-
value=50,
|
| 164 |
-
step=1
|
| 165 |
-
)
|
| 166 |
-
|
| 167 |
-
seed = gr.Number(
|
| 168 |
-
label="Seed",
|
| 169 |
-
value=0,
|
| 170 |
-
precision=0
|
| 171 |
-
)
|
| 172 |
-
|
| 173 |
-
# Buttons
|
| 174 |
-
generate_btn = gr.Button("🎨 Bild bearbeiten", variant="primary")
|
| 175 |
-
|
| 176 |
-
# Output
|
| 177 |
-
output_image = gr.Image(
|
| 178 |
-
label="Output Bild",
|
| 179 |
-
height=300
|
| 180 |
-
)
|
| 181 |
-
|
| 182 |
-
output_status = gr.Textbox(
|
| 183 |
-
label="Status",
|
| 184 |
-
interactive=False
|
| 185 |
-
)
|
| 186 |
-
|
| 187 |
-
# Event Handlers
|
| 188 |
-
load_btn.click(
|
| 189 |
-
fn=self.load_pipeline,
|
| 190 |
-
outputs=[pipeline_status]
|
| 191 |
-
)
|
| 192 |
-
|
| 193 |
-
generate_btn.click(
|
| 194 |
-
fn=self.test_image_edit,
|
| 195 |
-
inputs=[
|
| 196 |
-
input_image,
|
| 197 |
-
prompt,
|
| 198 |
-
cfg_scale,
|
| 199 |
-
steps,
|
| 200 |
-
seed,
|
| 201 |
-
negative_prompt
|
| 202 |
-
],
|
| 203 |
-
outputs=[output_image, output_status]
|
| 204 |
-
)
|
| 205 |
-
|
| 206 |
-
# Beispiel Prompts
|
| 207 |
-
gr.Markdown("""
|
| 208 |
-
### 💡 Beispiel Prompts:
|
| 209 |
-
- "Change the style of this image into realistic photography"
|
| 210 |
-
- "Make this image look like a watercolor painting"
|
| 211 |
-
- "Convert this to a cartoon style"
|
| 212 |
-
- "Change the background to a beach scene"
|
| 213 |
-
""")
|
| 214 |
-
|
| 215 |
-
return demo
|
| 216 |
-
|
| 217 |
-
def main():
|
| 218 |
-
"""Hauptfunktion"""
|
| 219 |
-
print("🚀 Starte Qwen Image Edit Test...")
|
| 220 |
-
|
| 221 |
-
# Prüfe CUDA
|
| 222 |
-
if torch.cuda.is_available():
|
| 223 |
-
print(f"✅ CUDA verfügbar: {torch.cuda.get_device_name()}")
|
| 224 |
-
print(f"✅ CUDA Memory: {torch.cuda.get_device_properties(0).total_memory / 1024**3:.1f} GB")
|
| 225 |
-
else:
|
| 226 |
-
print("⚠️ CUDA nicht verfügbar, verwende CPU")
|
| 227 |
-
|
| 228 |
-
# Tester erstellen
|
| 229 |
-
tester = QwenImageEditTester()
|
| 230 |
-
|
| 231 |
-
# UI erstellen und starten
|
| 232 |
-
demo = tester.create_ui()
|
| 233 |
-
|
| 234 |
-
print("🌐 Starte Web-Interface...")
|
| 235 |
-
demo.launch(
|
| 236 |
-
server_name="0.0.0.0",
|
| 237 |
-
server_port=8188,
|
| 238 |
-
share=False,
|
| 239 |
-
show_error=True
|
| 240 |
-
)
|
| 241 |
-
|
| 242 |
-
if __name__ == "__main__":
|
| 243 |
-
main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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