tugrulkaya commited on
Commit
6391a9f
·
verified ·
1 Parent(s): 8be9f21

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +146 -0
app.py ADDED
@@ -0,0 +1,146 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import gradio as gr
3
+ from diffusers import StableDiffusionPipeline, DPMSolverMultistepScheduler
4
+
5
+ # Model yükleme
6
+ print("Model yükleniyor...")
7
+ model_id = "runwayml/stable-diffusion-v1-5"
8
+
9
+ pipe = StableDiffusionPipeline.from_pretrained(
10
+ model_id,
11
+ torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
12
+ safety_checker=None
13
+ )
14
+
15
+ pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
16
+
17
+ if torch.cuda.is_available():
18
+ pipe = pipe.to("cuda")
19
+ pipe.enable_attention_slicing()
20
+ print("GPU kullanılıyor ✅")
21
+ else:
22
+ print("CPU kullanılıyor (yavaş olabilir)")
23
+
24
+ def generate_image(prompt, negative_prompt, num_steps, guidance_scale, width, height, seed):
25
+ """Görüntü üretme fonksiyonu"""
26
+ device = "cuda" if torch.cuda.is_available() else "cpu"
27
+ generator = torch.Generator(device).manual_seed(int(seed))
28
+
29
+ image = pipe(
30
+ prompt=prompt,
31
+ negative_prompt=negative_prompt,
32
+ num_inference_steps=int(num_steps),
33
+ guidance_scale=guidance_scale,
34
+ width=int(width),
35
+ height=int(height),
36
+ generator=generator
37
+ ).images[0]
38
+
39
+ return image
40
+
41
+ # Gradio UI
42
+ with gr.Blocks(title="🎨 Stable Diffusion Text-to-Image", theme=gr.themes.Soft()) as demo:
43
+ gr.Markdown("""
44
+ # 🎨 Stable Diffusion Görüntü Üretici
45
+ Text-to-Image modeli ile hayal gücünüzü görüntüye dönüştürün!
46
+
47
+ **Model:** Stable Diffusion v1.5
48
+ **Geliştirici:** Mehmet Tuğrul Kaya
49
+ """)
50
+
51
+ with gr.Row():
52
+ with gr.Column(scale=1):
53
+ prompt_input = gr.Textbox(
54
+ label="✍️ Prompt (Ne görmek istiyorsun?)",
55
+ placeholder="A beautiful sunset over mountains, digital art, highly detailed",
56
+ lines=3
57
+ )
58
+
59
+ negative_prompt_input = gr.Textbox(
60
+ label="🚫 Negative Prompt",
61
+ placeholder="blurry, low quality, distorted",
62
+ lines=2,
63
+ value="blurry, low quality, ugly, distorted, deformed"
64
+ )
65
+
66
+ with gr.Accordion("⚙️ Gelişmiş Ayarlar", open=False):
67
+ steps_slider = gr.Slider(
68
+ minimum=10,
69
+ maximum=50,
70
+ value=25,
71
+ step=1,
72
+ label="🔄 Inference Steps"
73
+ )
74
+
75
+ guidance_slider = gr.Slider(
76
+ minimum=1,
77
+ maximum=20,
78
+ value=7.5,
79
+ step=0.5,
80
+ label="🎯 Guidance Scale"
81
+ )
82
+
83
+ with gr.Row():
84
+ width_slider = gr.Slider(
85
+ minimum=256,
86
+ maximum=768,
87
+ value=512,
88
+ step=64,
89
+ label="📐 Width"
90
+ )
91
+
92
+ height_slider = gr.Slider(
93
+ minimum=256,
94
+ maximum=768,
95
+ value=512,
96
+ step=64,
97
+ label="📏 Height"
98
+ )
99
+
100
+ seed_input = gr.Number(
101
+ label="🌱 Seed",
102
+ value=42,
103
+ precision=0
104
+ )
105
+
106
+ generate_btn = gr.Button("🎨 Görüntü Üret", variant="primary", size="lg")
107
+
108
+ with gr.Column(scale=1):
109
+ output_image = gr.Image(label="🖼️ Üretilen Görüntü", type="pil")
110
+
111
+ gr.Markdown("### 💡 Örnek Promptlar:")
112
+ gr.Examples(
113
+ examples=[
114
+ ["A majestic lion in the savanna at sunset, photorealistic, 8k, detailed", "blurry, low quality", 25, 7.5, 512, 512, 42],
115
+ ["A futuristic cyberpunk city with neon lights, night scene, highly detailed", "blurry, dark, low quality", 30, 8.0, 512, 512, 123],
116
+ ["A cute cat wearing a wizard hat, digital art, kawaii style, colorful", "scary, ugly, dark", 25, 7.5, 512, 512, 456],
117
+ ["Beautiful Turkish tea glass on traditional table, Istanbul Bosphorus view, sunset", "blurry, low quality", 25, 7.5, 512, 512, 789],
118
+ ["Ancient library with magical books floating, fantasy art, detailed", "dark, blurry", 28, 7.5, 512, 512, 999],
119
+ ],
120
+ inputs=[prompt_input, negative_prompt_input, steps_slider, guidance_slider, width_slider, height_slider, seed_input],
121
+ outputs=output_image,
122
+ fn=generate_image,
123
+ cache_examples=False
124
+ )
125
+
126
+ generate_btn.click(
127
+ fn=generate_image,
128
+ inputs=[prompt_input, negative_prompt_input, steps_slider, guidance_slider, width_slider, height_slider, seed_input],
129
+ outputs=output_image
130
+ )
131
+
132
+ gr.Markdown("""
133
+ ---
134
+ ### 📝 Kullanım İpuçları:
135
+ - **Prompt**: Detaylı ve açıklayıcı olun (örn: "photorealistic", "8k", "detailed" ekleyin)
136
+ - **Negative Prompt**: İstemediğiniz özellikleri ekleyin
137
+ - **Steps**: 25-30 arası optimal (daha fazla = daha yavaş)
138
+ - **Guidance Scale**: 7-8 arası genelde en iyi sonuçları verir
139
+
140
+ ### 🔗 Bağlantılar:
141
+ - [GitHub](https://github.com/mtkaya)
142
+ - [Hugging Face](https://huggingface.co/tugrulkaya)
143
+ """)
144
+
145
+ if __name__ == "__main__":
146
+ demo.launch()