Astridkraft commited on
Commit
3eb3bc8
·
verified ·
1 Parent(s): 4ef6a50

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -77
app.py CHANGED
@@ -1,26 +1,25 @@
1
- # app.py - FIX FÜR BILD-ANZEIGE
2
  import gradio as gr
3
  from diffusers import StableDiffusionPipeline, StableDiffusionImg2ImgPipeline
4
  import torch
5
  from PIL import Image
6
- import io
7
 
8
  # === OPTIMIERTE EINSTELLUNGEN ===
9
  device = "cuda" if torch.cuda.is_available() else "cpu"
10
  torch_dtype = torch.float16 if device == "cuda" else torch.float32
11
- IMG_SIZE = 320
12
- STEPS = 20
13
 
14
  print(f"Running on: {device}")
15
 
16
  # === PIPELINES ===
17
  pipe_txt2img = None
18
- pipe_img2img = None
19
 
20
- def load_txt2img():
21
  global pipe_txt2img
22
  if pipe_txt2img is None:
23
- print("Loading Text-to-Image model...")
24
  pipe_txt2img = StableDiffusionPipeline.from_pretrained(
25
  "runwayml/stable-diffusion-v1-5",
26
  torch_dtype=torch_dtype,
@@ -31,108 +30,68 @@ def load_txt2img():
31
  pipe_txt2img.enable_attention_slicing()
32
  return pipe_txt2img
33
 
34
- def load_img2img():
35
- global pipe_img2img
36
- if pipe_img2img is None:
37
- print("Loading Img2Img model...")
38
- pipe_img2img = StableDiffusionImg2ImgPipeline.from_pretrained(
39
- "runwayml/stable-diffusion-v1-5",
40
- torch_dtype=torch_dtype,
41
- use_safetensors=True,
42
- safety_checker=None,
43
- requires_safety_checker=False
44
- ).to(device)
45
- pipe_img2img.enable_attention_slicing()
46
- return pipe_img2img
47
-
48
  # === FUNKTIONEN ===
49
  def text_to_image(prompt):
50
  try:
51
- if not prompt.strip():
52
  return None
53
 
54
- print(f"Generating: {prompt}")
55
- pipe = load_txt2img()
56
 
57
- # Direkte Generierung ohne komplexe Parameter
 
 
58
  image = pipe(
59
  prompt=prompt,
60
  height=IMG_SIZE,
61
  width=IMG_SIZE,
62
  num_inference_steps=STEPS,
63
- guidance_scale=7.5
64
  ).images[0]
65
 
66
- print("✅ Bild erfolgreich generiert")
67
- return image
68
-
69
- except Exception as e:
70
- print(f"❌ Fehler: {e}")
71
- return None
72
-
73
- def img_to_image(image, prompt="", strength=0.75):
74
- try:
75
- if image is None:
76
- return None
77
-
78
- pipe = load_img2img()
79
- img_resized = image.convert("RGB").resize((IMG_SIZE, IMG_SIZE))
80
-
81
- image = pipe(
82
- prompt=prompt or "beautiful landscape",
83
- image=img_resized,
84
- strength=strength,
85
- num_inference_steps=STEPS,
86
- guidance_scale=7.5
87
- ).images[0]
88
 
 
89
  return image
90
 
91
  except Exception as e:
92
  print(f"❌ Fehler: {e}")
 
 
93
  return None
94
 
95
- # === UI ===
96
- with gr.Blocks(title="AI Bild Generator") as app:
97
  gr.Markdown("# 🎨 AI Bild Generator")
98
 
99
  with gr.Tab("Text zu Bild"):
100
- gr.Markdown("**Eingabe:**")
101
  txt_input = gr.Textbox(
102
- label="Beschreibung",
103
- placeholder="Beschreibe dein Bild auf Englisch...",
104
  lines=2
105
  )
106
- generate_btn = gr.Button("🎨 Bild generieren", variant="primary")
107
- txt_output = gr.Image(label="Ergebnis", show_label=True)
108
 
109
  generate_btn.click(
110
  fn=text_to_image,
111
  inputs=txt_input,
112
- outputs=txt_output
113
- )
114
-
115
- with gr.Tab("Bild zu Bild"):
116
- gr.Markdown("**Bild hochladen und verändern:**")
117
- img_input = gr.Image(type="pil", label="Eingabebild")
118
- img_prompt = gr.Textbox(
119
- label="Transformations-Prompt",
120
- placeholder="Wie soll das Bild verändert werden?",
121
- lines=2
122
- )
123
- strength_slider = gr.Slider(0.6, 0.9, 0.75, label="Stärke der Veränderung")
124
- transform_btn = gr.Button("🔄 Bild transformieren", variant="primary")
125
- img_output = gr.Image(label="Transformiertes Bild", show_label=True)
126
-
127
- transform_btn.click(
128
- fn=img_to_image,
129
- inputs=[img_input, img_prompt, strength_slider],
130
- outputs=img_output
131
  )
132
 
133
- # SIMPLER APP START
134
  app.launch(
135
  share=True,
136
- server_name="0.0.0.0",
137
- server_port=7860
 
 
 
 
 
 
138
  )
 
1
+ # app.py - FIX FÜR TIMEOUT PROBLEM
2
  import gradio as gr
3
  from diffusers import StableDiffusionPipeline, StableDiffusionImg2ImgPipeline
4
  import torch
5
  from PIL import Image
6
+ import time
7
 
8
  # === OPTIMIERTE EINSTELLUNGEN ===
9
  device = "cuda" if torch.cuda.is_available() else "cpu"
10
  torch_dtype = torch.float16 if device == "cuda" else torch.float32
11
+ IMG_SIZE = 256 # Noch kleiner für schnellere Generierung
12
+ STEPS = 15 # Noch weniger Schritte
13
 
14
  print(f"Running on: {device}")
15
 
16
  # === PIPELINES ===
17
  pipe_txt2img = None
 
18
 
19
+ def load_pipeline():
20
  global pipe_txt2img
21
  if pipe_txt2img is None:
22
+ print("Loading model...")
23
  pipe_txt2img = StableDiffusionPipeline.from_pretrained(
24
  "runwayml/stable-diffusion-v1-5",
25
  torch_dtype=torch_dtype,
 
30
  pipe_txt2img.enable_attention_slicing()
31
  return pipe_txt2img
32
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
  # === FUNKTIONEN ===
34
  def text_to_image(prompt):
35
  try:
36
+ if not prompt or not prompt.strip():
37
  return None
38
 
39
+ print(f"Starting generation for: {prompt}")
40
+ start_time = time.time()
41
 
42
+ pipe = load_pipeline()
43
+
44
+ # Schnellste mögliche Generierung
45
  image = pipe(
46
  prompt=prompt,
47
  height=IMG_SIZE,
48
  width=IMG_SIZE,
49
  num_inference_steps=STEPS,
50
+ guidance_scale=7.0, # Niedriger für schnellere Konvergenz
51
  ).images[0]
52
 
53
+ end_time = time.time()
54
+ print(f"✅ Bild generiert in {end_time - start_time:.2f} Sekunden")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55
 
56
+ # Sofortige Rückgabe
57
  return image
58
 
59
  except Exception as e:
60
  print(f"❌ Fehler: {e}")
61
+ import traceback
62
+ traceback.print_exc()
63
  return None
64
 
65
+ # === UI MIT EXPLIZITER QUEUE ===
66
+ with gr.Blocks() as app:
67
  gr.Markdown("# 🎨 AI Bild Generator")
68
 
69
  with gr.Tab("Text zu Bild"):
70
+ gr.Markdown("**Eingabe (Englisch):**")
71
  txt_input = gr.Textbox(
72
+ placeholder="z.B. a red apple on a table",
 
73
  lines=2
74
  )
75
+ generate_btn = gr.Button("🎨 Generieren", variant="primary")
76
+ txt_output = gr.Image(label="Ergebnis")
77
 
78
  generate_btn.click(
79
  fn=text_to_image,
80
  inputs=txt_input,
81
+ outputs=txt_output,
82
+ # Wichtig: Queue konfigurieren
83
+ concurrency_limit=1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
84
  )
85
 
86
+ # === LAUNCH MIT OPTIMIERTEN TIMEOUTS ===
87
  app.launch(
88
  share=True,
89
+ server_name="0.0.0.0",
90
+ server_port=7860,
91
+ # Wichtige Timeout-Einstellungen:
92
+ max_file_size="10MB",
93
+ # Verhindert "Connection re-established":
94
+ ssl_verify=False,
95
+ quiet=True,
96
+ show_error=True
97
  )