codermert commited on
Commit
5c2cac2
·
verified ·
1 Parent(s): 1438e9f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +119 -31
app.py CHANGED
@@ -1,39 +1,127 @@
1
  import gradio as gr
 
 
 
2
  import torch
3
- from diffusers import StableDiffusionPipeline, DPMSolverMultistepScheduler
4
- from safetensors.torch import load_file
5
 
6
- model_id = "runwayml/stable-diffusion-v1-5"
7
- lora_path = "https://huggingface.co/codermert/model_malika/resolve/main/sarah-lora.safetensors"
 
 
 
8
 
9
- pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16)
10
- pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
11
- pipe = pipe.to("cuda")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
 
13
- # LoRA dosyasını yükle
14
- state_dict = load_file(lora_path)
15
- pipe.unet.load_attn_procs(state_dict)
 
 
16
 
17
- def generate_image(prompt, negative_prompt, guidance_scale, num_inference_steps):
18
- image = pipe(
19
- prompt=prompt,
20
- negative_prompt=negative_prompt,
21
- guidance_scale=guidance_scale,
22
- num_inference_steps=num_inference_steps
23
- ).images[0]
24
- return image
 
 
 
25
 
26
- iface = gr.Interface(
27
- fn=generate_image,
28
- inputs=[
29
- gr.Textbox(label="Prompt"),
30
- gr.Textbox(label="Negative Prompt"),
31
- gr.Slider(minimum=1, maximum=20, step=0.5, label="Guidance Scale", value=7.5),
32
- gr.Slider(minimum=1, maximum=100, step=1, label="Number of Inference Steps", value=50)
33
- ],
34
- outputs=gr.Image(label="Generated Image"),
35
- title="Stable Diffusion with LoRA",
36
- description="Generate images using Stable Diffusion v1.5 with a custom LoRA model."
37
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
 
39
- iface.launch()
 
1
  import gradio as gr
2
+ import numpy as np
3
+ import random
4
+ import spaces
5
  import torch
6
+ from diffusers import DiffusionPipeline
 
7
 
8
+ dtype = torch.bfloat16
9
+ device = "cuda" if torch.cuda.is_available() else "cpu"
10
+ pipe = DiffusionPipeline.from_pretrained("codermert/zehra_flux", torch_dtype=dtype).to(device)
11
+ MAX_SEED = np.iinfo(np.int32).max
12
+ MAX_IMAGE_SIZE = 2048
13
 
14
+ @spaces.GPU()
15
+ def infer(prompt, seed=42, randomize_seed=False, width=1024, height=1024, num_inference_steps=4, num_images=4, progress=gr.Progress(track_tqdm=True)):
16
+ if randomize_seed:
17
+ seed = random.randint(0, MAX_SEED)
18
+
19
+ generator = torch.Generator().manual_seed(seed)
20
+ images = []
21
+
22
+ for _ in range(num_images):
23
+ image = pipe(
24
+ prompt=prompt,
25
+ width=width,
26
+ height=height,
27
+ num_inference_steps=num_inference_steps,
28
+ generator=generator,
29
+ guidance_scale=0.0
30
+ ).images[0]
31
+ images.append(image)
32
+ # Her görsel için farklı seed kullan
33
+ seed = random.randint(0, MAX_SEED)
34
+ generator = torch.Generator().manual_seed(seed)
35
+
36
+ return images, seed
37
 
38
+ examples = [
39
+ "a tiny astronaut hatching from an egg on the moon",
40
+ "a cat holding a sign that says hello world",
41
+ "an anime illustration of a wiener schnitzel",
42
+ ]
43
 
44
+ css = """
45
+ #col-container {
46
+ margin: 0 auto;
47
+ max-width: 900px;
48
+ }
49
+ .generated-images {
50
+ display: grid;
51
+ grid-template-columns: repeat(2, 1fr);
52
+ gap: 10px;
53
+ }
54
+ """
55
 
56
+ with gr.Blocks(css=css) as demo:
57
+ with gr.Column(elem_id="col-container"):
58
+ gr.Markdown("""# Zehra Flux Image Generator
59
+ 4 farklı görsel üreten AI görsel oluşturucu
60
+ """)
61
+
62
+ with gr.Row():
63
+ prompt = gr.Text(
64
+ label="Prompt",
65
+ show_label=False,
66
+ max_lines=1,
67
+ placeholder="Görseliniz için prompt girin",
68
+ container=False,
69
+ )
70
+ run_button = gr.Button("Oluştur", scale=0)
71
+
72
+ # 4 görsel için grid layout
73
+ with gr.Row(elem_classes="generated-images"):
74
+ results = [gr.Image(label=f"Sonuç {i+1}", show_label=True) for i in range(4)]
75
+
76
+ with gr.Accordion("Gelişmiş Ayarlar", open=False):
77
+ seed = gr.Slider(
78
+ label="Seed",
79
+ minimum=0,
80
+ maximum=MAX_SEED,
81
+ step=1,
82
+ value=0,
83
+ )
84
+
85
+ randomize_seed = gr.Checkbox(label="Rastgele seed", value=True)
86
+
87
+ with gr.Row():
88
+ width = gr.Slider(
89
+ label="Genişlik",
90
+ minimum=256,
91
+ maximum=MAX_IMAGE_SIZE,
92
+ step=32,
93
+ value=1024,
94
+ )
95
+
96
+ height = gr.Slider(
97
+ label="Yükseklik",
98
+ minimum=256,
99
+ maximum=MAX_IMAGE_SIZE,
100
+ step=32,
101
+ value=1024,
102
+ )
103
+
104
+ num_inference_steps = gr.Slider(
105
+ label="Inference adım sayısı",
106
+ minimum=1,
107
+ maximum=50,
108
+ step=1,
109
+ value=4,
110
+ )
111
+
112
+ gr.Examples(
113
+ examples=examples,
114
+ fn=infer,
115
+ inputs=[prompt],
116
+ outputs=[*results, seed],
117
+ cache_examples="lazy"
118
+ )
119
+
120
+ gr.on(
121
+ triggers=[run_button.click, prompt.submit],
122
+ fn=infer,
123
+ inputs=[prompt, seed, randomize_seed, width, height, num_inference_steps],
124
+ outputs=[*results, seed]
125
+ )
126
 
127
+ demo.launch()