Astridkraft commited on
Commit
8fc0cc9
·
verified ·
1 Parent(s): 6e96983

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +109 -36
app.py CHANGED
@@ -3,12 +3,20 @@ import gradio as gr
3
  from diffusers import StableDiffusionPipeline, StableDiffusionImg2ImgPipeline
4
  import torch
5
  from PIL import Image
 
 
 
 
 
 
6
 
7
  # === AUTO HARDWARE ===
8
  device = "cuda" if torch.cuda.is_available() else "cpu"
9
  torch_dtype = torch.float16 if device == "cuda" else torch.float32
10
- IMG_SIZE = 512 if device == "cuda" else 384
11
- STEPS = 50 if device == "cuda" else 40
 
 
12
 
13
  # === GLOBALE VARIABLEN ===
14
  pipe_txt2img = None
@@ -17,32 +25,52 @@ pipe_img2img = None
17
  def load_txt2img():
18
  global pipe_txt2img
19
  if pipe_txt2img is None:
20
- print("Lade Text-to-Image Pipeline...")
21
- pipe_txt2img = StableDiffusionPipeline.from_pretrained(
22
- "runwayml/stable-diffusion-v1-5",
23
- torch_dtype=torch_dtype,
24
- # Kompatibilität mit diffusers 0.29.2
25
- use_safetensors=True
26
- ).to(device)
27
- pipe_txt2img.enable_attention_slicing()
 
 
 
 
 
 
 
28
  return pipe_txt2img
29
 
30
  def load_img2img():
31
  global pipe_img2img
32
  if pipe_img2img is None:
33
- print("Lade Img2Img Pipeline...")
34
- pipe_img2img = StableDiffusionImg2ImgPipeline.from_pretrained(
35
- "runwayml/stable-diffusion-v1-5",
36
- torch_dtype=torch_dtype,
37
- use_safetensors=True
38
- ).to(device)
39
- pipe_img2img.enable_attention_slicing()
 
 
 
 
 
 
 
 
40
  return pipe_img2img
41
 
42
  # === FUNKTIONEN ===
43
  def text_to_image(prompt):
44
  try:
 
 
 
 
45
  pipe = load_txt2img()
 
46
  result = pipe(
47
  prompt=prompt,
48
  height=IMG_SIZE,
@@ -50,15 +78,23 @@ def text_to_image(prompt):
50
  num_inference_steps=STEPS,
51
  guidance_scale=7.5,
52
  )
 
 
53
  return result.images[0]
 
54
  except Exception as e:
55
  print(f"Fehler in text_to_image: {e}")
 
56
  return None
57
 
58
  def img_to_image(image, prompt="", neg_prompt="", strength=0.75):
59
  try:
 
 
 
60
  pipe = load_img2img()
61
  img = image.convert("RGB").resize((IMG_SIZE, IMG_SIZE))
 
62
  result = pipe(
63
  prompt=prompt or "snowy winter landscape",
64
  negative_prompt=neg_prompt or "blur, furniture, grass, table",
@@ -67,36 +103,73 @@ def img_to_image(image, prompt="", neg_prompt="", strength=0.75):
67
  guidance_scale=7.5,
68
  num_inference_steps=STEPS
69
  )
 
70
  return result.images[0]
 
71
  except Exception as e:
72
  print(f"Fehler in img_to_image: {e}")
 
73
  return None
74
 
75
  # === UI ===
76
  with gr.Blocks() as app:
77
- gr.Markdown("# Text-to-Image + Img2Img\n**Optimierte Version für bessere Ergebnisse**")
78
 
79
  with gr.Tab("Text to Image"):
80
- gr.Markdown("### Tipp: Verwende englische Prompts für beste Ergebnisse")
81
- txt_in = gr.Textbox(
82
- placeholder="z.B. 'a snowman in a forest, winter, snowy trees, highly detailed'",
83
- lines=2,
84
- value="a snowman in a forest, winter, snowy trees, highly detailed"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85
  )
86
- txt_out = gr.Image(label="Ergebnis")
87
- txt_btn = gr.Button("Generieren")
88
- txt_btn.click(text_to_image, txt_in, txt_out)
89
 
90
  with gr.Tab("Bild zu Schneelandschaft"):
91
- img_in = gr.Image(type="pil")
92
- prompt = gr.Textbox(
93
- value="girl hugging snowman in snowy landscape, realistic, winter",
94
- lines=2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
95
  )
96
- neg = gr.Textbox(value="table, tarp, grass, blur, furniture", lines=2)
97
- strength = gr.Slider(0.6, 0.9, 0.75)
98
- img_out = gr.Image(label="Ergebnis")
99
- img_btn = gr.Button("Generieren")
100
- img_btn.click(img_to_image, [img_in, prompt, neg, strength], img_out)
101
 
102
- app.launch(share=True)
 
 
 
 
 
 
 
3
  from diffusers import StableDiffusionPipeline, StableDiffusionImg2ImgPipeline
4
  import torch
5
  from PIL import Image
6
+ import traceback
7
+ import logging
8
+
9
+ # Logging aktivieren
10
+ logging.basicConfig(level=logging.INFO)
11
+ logger = logging.getLogger(__name__)
12
 
13
  # === AUTO HARDWARE ===
14
  device = "cuda" if torch.cuda.is_available() else "cpu"
15
  torch_dtype = torch.float16 if device == "cuda" else torch.float32
16
+ IMG_SIZE = 384 # Konservative Größe für Stabilität
17
+ STEPS = 30 if device == "cuda" else 25
18
+
19
+ print(f"Device: {device}, Torch dtype: {torch_dtype}")
20
 
21
  # === GLOBALE VARIABLEN ===
22
  pipe_txt2img = None
 
25
  def load_txt2img():
26
  global pipe_txt2img
27
  if pipe_txt2img is None:
28
+ try:
29
+ print("Lade Text-to-Image Pipeline...")
30
+ pipe_txt2img = StableDiffusionPipeline.from_pretrained(
31
+ "runwayml/stable-diffusion-v1-5",
32
+ torch_dtype=torch_dtype,
33
+ use_safetensors=True,
34
+ variant="fp16" if device == "cuda" else None
35
+ )
36
+ pipe_txt2img = pipe_txt2img.to(device)
37
+ pipe_txt2img.enable_attention_slicing()
38
+ print("Text-to-Image Pipeline erfolgreich geladen")
39
+ except Exception as e:
40
+ print(f"Fehler beim Laden der Pipeline: {e}")
41
+ traceback.print_exc()
42
+ raise
43
  return pipe_txt2img
44
 
45
  def load_img2img():
46
  global pipe_img2img
47
  if pipe_img2img is None:
48
+ try:
49
+ print("Lade Img2Img Pipeline...")
50
+ pipe_img2img = StableDiffusionImg2ImgPipeline.from_pretrained(
51
+ "runwayml/stable-diffusion-v1-5",
52
+ torch_dtype=torch_dtype,
53
+ use_safetensors=True,
54
+ variant="fp16" if device == "cuda" else None
55
+ )
56
+ pipe_img2img = pipe_img2img.to(device)
57
+ pipe_img2img.enable_attention_slicing()
58
+ print("Img2Img Pipeline erfolgreich geladen")
59
+ except Exception as e:
60
+ print(f"Fehler beim Laden der Img2Img Pipeline: {e}")
61
+ traceback.print_exc()
62
+ raise
63
  return pipe_img2img
64
 
65
  # === FUNKTIONEN ===
66
  def text_to_image(prompt):
67
  try:
68
+ if not prompt or prompt.strip() == "":
69
+ return None
70
+
71
+ print(f"Generiere Bild für Prompt: {prompt}")
72
  pipe = load_txt2img()
73
+
74
  result = pipe(
75
  prompt=prompt,
76
  height=IMG_SIZE,
 
78
  num_inference_steps=STEPS,
79
  guidance_scale=7.5,
80
  )
81
+
82
+ print("Bild erfolgreich generiert")
83
  return result.images[0]
84
+
85
  except Exception as e:
86
  print(f"Fehler in text_to_image: {e}")
87
+ traceback.print_exc()
88
  return None
89
 
90
  def img_to_image(image, prompt="", neg_prompt="", strength=0.75):
91
  try:
92
+ if image is None:
93
+ return None
94
+
95
  pipe = load_img2img()
96
  img = image.convert("RGB").resize((IMG_SIZE, IMG_SIZE))
97
+
98
  result = pipe(
99
  prompt=prompt or "snowy winter landscape",
100
  negative_prompt=neg_prompt or "blur, furniture, grass, table",
 
103
  guidance_scale=7.5,
104
  num_inference_steps=STEPS
105
  )
106
+
107
  return result.images[0]
108
+
109
  except Exception as e:
110
  print(f"Fehler in img_to_image: {e}")
111
+ traceback.print_exc()
112
  return None
113
 
114
  # === UI ===
115
  with gr.Blocks() as app:
116
+ gr.Markdown("# Text-to-Image + Img2Img\n**Stabile Version für dein Environment**")
117
 
118
  with gr.Tab("Text to Image"):
119
+ gr.Markdown("### Wichtige Tipps:")
120
+ gr.Markdown("- Verwende englische Prompts")
121
+ gr.Markdown("- Erste Generation kann länger dauern (Modell-Download)")
122
+
123
+ with gr.Row():
124
+ txt_in = gr.Textbox(
125
+ label="Prompt (englisch)",
126
+ placeholder="a snowman in a forest, winter, snowy trees, highly detailed",
127
+ lines=2,
128
+ value="a snowman in a forest, winter, snowy trees, highly detailed"
129
+ )
130
+ with gr.Row():
131
+ txt_out = gr.Image(label="Ergebnis", height=400)
132
+ with gr.Row():
133
+ txt_btn = gr.Button("✨ Bild generieren", variant="primary")
134
+
135
+ txt_btn.click(
136
+ fn=text_to_image,
137
+ inputs=txt_in,
138
+ outputs=txt_out
139
  )
 
 
 
140
 
141
  with gr.Tab("Bild zu Schneelandschaft"):
142
+ with gr.Row():
143
+ img_in = gr.Image(type="pil", label="Eingabebild")
144
+ with gr.Row():
145
+ prompt = gr.Textbox(
146
+ label="Prompt",
147
+ value="girl hugging snowman in snowy landscape, realistic, winter",
148
+ lines=2
149
+ )
150
+ with gr.Row():
151
+ neg = gr.Textbox(
152
+ label="Negative Prompts",
153
+ value="table, tarp, grass, blur, furniture",
154
+ lines=2
155
+ )
156
+ with gr.Row():
157
+ strength = gr.Slider(0.6, 0.9, 0.75, label="Strength")
158
+ with gr.Row():
159
+ img_out = gr.Image(label="Ergebnis", height=400)
160
+ with gr.Row():
161
+ img_btn = gr.Button("✨ Bild transformieren", variant="primary")
162
+
163
+ img_btn.click(
164
+ fn=img_to_image,
165
+ inputs=[img_in, prompt, neg, strength],
166
+ outputs=img_out
167
  )
 
 
 
 
 
168
 
169
+ # Server-Konfiguration für bessere Stabilität
170
+ app.launch(
171
+ share=True,
172
+ server_name="0.0.0.0",
173
+ server_port=7860,
174
+ show_error=True
175
+ )