Update app.py
Browse files
app.py
CHANGED
@@ -1,36 +1,83 @@
|
|
1 |
import gradio as gr
|
2 |
import torch
|
|
|
3 |
from diffusers import AutoPipelineForText2Image
|
|
|
4 |
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
|
|
|
|
|
|
|
|
16 |
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
|
|
|
|
21 |
)
|
22 |
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
|
29 |
# Gradio arayüzü
|
30 |
with gr.Blocks(title="Malika - FLUX Text-to-Image") as demo:
|
31 |
gr.Markdown("# Malika - FLUX.1 Text-to-Image Modeliyle Görüntü Oluşturucu")
|
32 |
gr.Markdown("Bu uygulama, codermert/malikafinal modelini kullanarak metinden görüntü oluşturur.")
|
33 |
|
|
|
|
|
|
|
|
|
34 |
with gr.Row():
|
35 |
with gr.Column():
|
36 |
prompt_input = gr.Textbox(
|
@@ -49,14 +96,20 @@ with gr.Blocks(title="Malika - FLUX Text-to-Image") as demo:
|
|
49 |
image_output = gr.Image(label="Oluşturulan Görüntü")
|
50 |
prompt_used = gr.Textbox(label="Kullanılan Prompt")
|
51 |
|
|
|
|
|
|
|
|
|
|
|
52 |
generate_btn.click(
|
53 |
-
fn=generate_image,
|
54 |
inputs=[prompt_input, tok_checkbox],
|
55 |
outputs=[image_output, prompt_used]
|
56 |
)
|
57 |
|
58 |
gr.Markdown("""
|
59 |
## Kullanım Tavsiyeleri
|
|
|
60 |
- Eğer model için özel bir trigger sözcüğü gerekliyse 'TOK' seçeneğini işaretli bırakın
|
61 |
- Daha gerçekçi sonuçlar için "photorealistic, 8K, detailed" gibi ifadeler ekleyebilirsiniz
|
62 |
- Örnek: "portrait of a woman with blue eyes, photorealistic, 8K"
|
|
|
1 |
import gradio as gr
|
2 |
import torch
|
3 |
+
import os
|
4 |
from diffusers import AutoPipelineForText2Image
|
5 |
+
from huggingface_hub import snapshot_download
|
6 |
|
7 |
+
class ModelHandler:
|
8 |
+
def __init__(self):
|
9 |
+
self.pipeline = None
|
10 |
+
|
11 |
+
def load_model(self, progress=gr.Progress()):
|
12 |
+
if self.pipeline is not None:
|
13 |
+
return "Model zaten yüklü."
|
14 |
+
|
15 |
+
progress(0, desc="Base model indiriliyor...")
|
16 |
+
# Base modeli indir
|
17 |
+
base_model_path = snapshot_download(
|
18 |
+
repo_id="black-forest-labs/FLUX.1-dev",
|
19 |
+
local_dir="./models/base_model",
|
20 |
+
ignore_patterns=["*.bin", "*.onnx"] if os.path.exists("./models/base_model") else None
|
21 |
+
)
|
22 |
|
23 |
+
progress(0.5, desc="LoRA modeli indiriliyor...")
|
24 |
+
# LoRA modelini indir
|
25 |
+
lora_model_path = snapshot_download(
|
26 |
+
repo_id="codermert/malikafinal",
|
27 |
+
local_dir="./models/lora_model",
|
28 |
+
ignore_patterns=["*.bin", "*.onnx"] if os.path.exists("./models/lora_model") else None
|
29 |
)
|
30 |
|
31 |
+
progress(0.7, desc="Pipeline oluşturuluyor...")
|
32 |
+
# Pipeline'ı oluştur
|
33 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
34 |
+
dtype = torch.float16 if device == "cuda" else torch.float32
|
35 |
+
|
36 |
+
self.pipeline = AutoPipelineForText2Image.from_pretrained(
|
37 |
+
base_model_path,
|
38 |
+
torch_dtype=dtype
|
39 |
+
).to(device)
|
40 |
+
|
41 |
+
progress(0.9, desc="LoRA yükleniyor...")
|
42 |
+
# LoRA'yı yükle
|
43 |
+
lora_path = os.path.join(lora_model_path, "lora.safetensors")
|
44 |
+
if os.path.exists(lora_path):
|
45 |
+
self.pipeline.load_lora_weights(lora_path)
|
46 |
+
else:
|
47 |
+
return "LoRA dosyası bulunamadı!"
|
48 |
+
|
49 |
+
progress(1.0, desc="Tamamlandı!")
|
50 |
+
return "Model başarıyla yüklendi! Artık görüntü oluşturmaya hazırsınız."
|
51 |
+
|
52 |
+
def generate_image(self, prompt, use_tok=True, progress=gr.Progress()):
|
53 |
+
if self.pipeline is None:
|
54 |
+
return None, "Lütfen önce modeli yükleyin!"
|
55 |
+
|
56 |
+
# Eğer use_tok seçeneği işaretlendiyse, prompt'a TOK ekle
|
57 |
+
if use_tok and "TOK" not in prompt:
|
58 |
+
prompt = f"TOK {prompt}"
|
59 |
+
|
60 |
+
try:
|
61 |
+
progress(0.2, desc="Görüntü oluşturuluyor...")
|
62 |
+
# Görüntü oluştur
|
63 |
+
image = self.pipeline(prompt).images[0]
|
64 |
+
progress(1.0, desc="Tamamlandı!")
|
65 |
+
return image, f"Oluşturulan prompt: {prompt}"
|
66 |
+
except Exception as e:
|
67 |
+
return None, f"Hata oluştu: {str(e)}"
|
68 |
+
|
69 |
+
# Model işleyiciyi oluştur
|
70 |
+
model_handler = ModelHandler()
|
71 |
|
72 |
# Gradio arayüzü
|
73 |
with gr.Blocks(title="Malika - FLUX Text-to-Image") as demo:
|
74 |
gr.Markdown("# Malika - FLUX.1 Text-to-Image Modeliyle Görüntü Oluşturucu")
|
75 |
gr.Markdown("Bu uygulama, codermert/malikafinal modelini kullanarak metinden görüntü oluşturur.")
|
76 |
|
77 |
+
with gr.Row():
|
78 |
+
load_model_btn = gr.Button("Modeli Yükle", variant="primary")
|
79 |
+
model_status = gr.Textbox(label="Model Durumu", value="Model henüz yüklenmedi")
|
80 |
+
|
81 |
with gr.Row():
|
82 |
with gr.Column():
|
83 |
prompt_input = gr.Textbox(
|
|
|
96 |
image_output = gr.Image(label="Oluşturulan Görüntü")
|
97 |
prompt_used = gr.Textbox(label="Kullanılan Prompt")
|
98 |
|
99 |
+
load_model_btn.click(
|
100 |
+
fn=model_handler.load_model,
|
101 |
+
outputs=model_status
|
102 |
+
)
|
103 |
+
|
104 |
generate_btn.click(
|
105 |
+
fn=model_handler.generate_image,
|
106 |
inputs=[prompt_input, tok_checkbox],
|
107 |
outputs=[image_output, prompt_used]
|
108 |
)
|
109 |
|
110 |
gr.Markdown("""
|
111 |
## Kullanım Tavsiyeleri
|
112 |
+
- İlk olarak "Modeli Yükle" düğmesine tıklayın (bu işlem biraz zaman alabilir)
|
113 |
- Eğer model için özel bir trigger sözcüğü gerekliyse 'TOK' seçeneğini işaretli bırakın
|
114 |
- Daha gerçekçi sonuçlar için "photorealistic, 8K, detailed" gibi ifadeler ekleyebilirsiniz
|
115 |
- Örnek: "portrait of a woman with blue eyes, photorealistic, 8K"
|