Alibrown commited on
Commit
8b69324
·
verified ·
1 Parent(s): ebb6216

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -44
app.py CHANGED
@@ -1,68 +1,78 @@
1
- import torch
2
- from diffusers import FluxPipeline
3
  import gradio as gr
 
 
 
 
 
 
 
 
 
 
 
4
 
5
- def generate_flux_image(
6
- prompt,
7
- width=512,
8
- height=512,
9
  num_inference_steps=50,
10
- guidance_scale=7.5,
11
- seed=None
12
  ):
13
- # Zufallsgenerator-Initialisierung
14
- if seed is None:
15
- generator = torch.Generator(device="cuda" if torch.cuda.is_available() else "cpu")
16
- else:
17
- generator = torch.Generator(device="cuda" if torch.cuda.is_available() else "cpu").manual_seed(seed)
18
-
19
- # Pipeline laden
20
- pipeline = FluxPipeline.from_pretrained(
21
- "models/enhanceaiteam/Flux-uncensored", # Korrigierter Pfad
22
- torch_dtype=torch.float16
23
- )
24
-
25
- # Überprüfen und ggf. auf GPU verschieben
26
- if torch.cuda.is_available():
27
- pipeline = pipeline.to("cuda")
28
-
29
- # Bildgenerierung
30
- image = pipeline(
31
- prompt=prompt,
32
- width=width,
33
- height=height,
34
- num_inference_steps=num_inference_steps,
35
- guidance_scale=guidance_scale,
36
- generator=generator
37
- ).images[0]
38
-
39
- return image
40
 
41
- # Gradio-Interface
42
  def create_gradio_interface():
43
  with gr.Blocks() as demo:
 
 
44
  with gr.Row():
45
- prompt = gr.Textbox(label="Bildprompt")
46
  width = gr.Slider(minimum=64, maximum=2048, value=512, label="Breite")
47
  height = gr.Slider(minimum=64, maximum=2048, value=512, label="Höhe")
48
 
49
  with gr.Row():
50
  steps = gr.Slider(minimum=10, maximum=100, value=50, label="Inference Steps")
51
  guidance = gr.Slider(minimum=1, maximum=15, value=7.5, label="Guidance Scale")
52
- seed = gr.Number(label="Seed (optional)", precision=0)
53
 
54
- generate_btn = gr.Button("Bild generieren")
55
  output_image = gr.Image(label="Generiertes Bild")
56
 
57
  generate_btn.click(
58
- fn=generate_flux_image,
59
- inputs=[prompt, width, height, steps, guidance, seed],
60
  outputs=output_image
61
  )
 
 
 
 
 
 
 
 
 
 
62
 
63
  return demo
64
 
65
- # Interface starten
66
- if __name__ == "__main__":
67
  interface = create_gradio_interface()
68
- interface.launch(share=True)
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ from diffusers import DiffusionPipeline
3
+ import torch
4
+
5
+ # Pipeline global vorinitialisieren für bessere Performance
6
+ pipe = DiffusionPipeline.from_pretrained("black-forest-labs/FLUX.1-dev")
7
+ pipe.load_lora_weights("enhanceaiteam/Flux-uncensored")
8
+
9
+ # GPU-Optimierung
10
+ if torch.cuda.is_available():
11
+ pipe = pipe.to("cuda")
12
+ pipe.enable_model_cpu_offload() # Zusätzliche Speicheroptimierung
13
 
14
+ def generate_image(
15
+ prompt,
16
+ width=512,
17
+ height=512,
18
  num_inference_steps=50,
19
+ guidance_scale=7.5
 
20
  ):
21
+ try:
22
+ image = pipe(
23
+ prompt=prompt,
24
+ width=width,
25
+ height=height,
26
+ num_inference_steps=num_inference_steps,
27
+ guidance_scale=guidance_scale
28
+ ).images[0]
29
+ return image
30
+ except Exception as e:
31
+ print(f"Fehler bei Bildgenerierung: {e}")
32
+ return None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
 
34
+ # Gradio-Interface mit erweiterten Optionen
35
  def create_gradio_interface():
36
  with gr.Blocks() as demo:
37
+ gr.Markdown("# Flux Bildgenerator")
38
+
39
  with gr.Row():
40
+ prompt = gr.Textbox(label="Bildprompt", lines=2)
41
  width = gr.Slider(minimum=64, maximum=2048, value=512, label="Breite")
42
  height = gr.Slider(minimum=64, maximum=2048, value=512, label="Höhe")
43
 
44
  with gr.Row():
45
  steps = gr.Slider(minimum=10, maximum=100, value=50, label="Inference Steps")
46
  guidance = gr.Slider(minimum=1, maximum=15, value=7.5, label="Guidance Scale")
 
47
 
48
+ generate_btn = gr.Button("Bild generieren", variant="primary")
49
  output_image = gr.Image(label="Generiertes Bild")
50
 
51
  generate_btn.click(
52
+ fn=generate_image,
53
+ inputs=[prompt, width, height, steps, guidance],
54
  outputs=output_image
55
  )
56
+
57
+ # Beispiel-Prompts
58
+ gr.Examples(
59
+ examples=[
60
+ ["Astronaut in a jungle, cold color palette, muted colors, detailed, 8k"],
61
+ ["Cyberpunk city landscape at night"],
62
+ ["Realistic portrait of a wolf in mountain terrain"]
63
+ ],
64
+ inputs=[prompt]
65
+ )
66
 
67
  return demo
68
 
69
+ # Hauptausführung
70
+ def main():
71
  interface = create_gradio_interface()
72
+ interface.launch(
73
+ share=True, # Für öffentlichen Zugang
74
+ debug=True # Detaillierte Fehlermeldungen
75
+ )
76
+
77
+ if __name__ == "__main__":
78
+ main()