Update app.py
Browse files
app.py
CHANGED
@@ -1,157 +1,31 @@
|
|
1 |
import gradio as gr
|
2 |
import torch
|
3 |
-
import os
|
4 |
from diffusers import AutoPipelineForText2Image
|
5 |
-
from huggingface_hub import hf_hub_download
|
6 |
-
import logging
|
7 |
-
from pathlib import Path
|
8 |
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
MODEL_CACHE = Path("./model_cache")
|
15 |
-
BASE_MODEL_PATH = MODEL_CACHE / "base_model"
|
16 |
-
LORA_MODEL_PATH = MODEL_CACHE / "lora_model"
|
17 |
-
|
18 |
-
class ModelHandler:
|
19 |
-
def __init__(self):
|
20 |
-
self.pipeline = None
|
21 |
-
self.device = "cuda" if torch.cuda.is_available() else "cpu"
|
22 |
-
self.dtype = torch.float16 if self.device == "cuda" else torch.float32
|
23 |
-
|
24 |
-
def load_model(self, progress=gr.Progress()):
|
25 |
-
try:
|
26 |
-
if self.pipeline is not None:
|
27 |
-
return "Model zaten yüklü."
|
28 |
-
|
29 |
-
# Model cache dizinlerini oluştur
|
30 |
-
MODEL_CACHE.mkdir(exist_ok=True)
|
31 |
-
BASE_MODEL_PATH.mkdir(exist_ok=True)
|
32 |
-
LORA_MODEL_PATH.mkdir(exist_ok=True)
|
33 |
-
|
34 |
-
progress(0, desc="Base model indiriliyor...")
|
35 |
-
# Base modeli indir
|
36 |
-
if not (BASE_MODEL_PATH / "model_index.json").exists():
|
37 |
-
hf_hub_download(
|
38 |
-
repo_id="black-forest-labs/FLUX.1-dev",
|
39 |
-
filename="model_index.json",
|
40 |
-
local_dir=BASE_MODEL_PATH,
|
41 |
-
token=os.getenv("HF_TOKEN")
|
42 |
-
)
|
43 |
-
|
44 |
-
progress(0.5, desc="LoRA modeli indiriliyor...")
|
45 |
-
# LoRA modelini indir
|
46 |
-
if not (LORA_MODEL_PATH / "lora.safetensors").exists():
|
47 |
-
hf_hub_download(
|
48 |
-
repo_id="codermert/ezelll_flux",
|
49 |
-
filename="lora.safetensors",
|
50 |
-
local_dir=LORA_MODEL_PATH,
|
51 |
-
token=os.getenv("HF_TOKEN")
|
52 |
-
)
|
53 |
-
|
54 |
-
progress(0.7, desc="Pipeline oluşturuluyor...")
|
55 |
-
# Pipeline'ı oluştur
|
56 |
-
self.pipeline = AutoPipelineForText2Image.from_pretrained(
|
57 |
-
str(BASE_MODEL_PATH),
|
58 |
-
torch_dtype=self.dtype,
|
59 |
-
use_safetensors=True,
|
60 |
-
cache_dir=MODEL_CACHE
|
61 |
-
).to(self.device)
|
62 |
-
|
63 |
-
progress(0.9, desc="LoRA yükleniyor...")
|
64 |
-
# LoRA'yı yükle
|
65 |
-
lora_path = LORA_MODEL_PATH / "lora.safetensors"
|
66 |
-
if lora_path.exists():
|
67 |
-
self.pipeline.load_lora_weights(str(lora_path))
|
68 |
-
else:
|
69 |
-
return "LoRA dosyası bulunamadı!"
|
70 |
-
|
71 |
-
progress(1.0, desc="Tamamlandı!")
|
72 |
-
return "Model başarıyla yüklendi! Artık görüntü oluşturmaya hazırsınız."
|
73 |
-
except Exception as e:
|
74 |
-
logger.error(f"Model yükleme hatası: {str(e)}")
|
75 |
-
return f"Model yüklenirken hata oluştu: {str(e)}"
|
76 |
-
|
77 |
-
def generate_image(self, prompt, use_tok=True, progress=gr.Progress()):
|
78 |
-
try:
|
79 |
-
if self.pipeline is None:
|
80 |
-
return None, "Lütfen önce modeli yükleyin!"
|
81 |
-
|
82 |
-
# Eğer use_tok seçeneği işaretlendiyse, prompt'a TOK ekle
|
83 |
-
if use_tok and "TOK" not in prompt:
|
84 |
-
prompt = f"TOK {prompt}"
|
85 |
-
|
86 |
-
progress(0.2, desc="Görüntü oluşturuluyor...")
|
87 |
-
# Görüntü oluştur
|
88 |
-
image = self.pipeline(
|
89 |
-
prompt,
|
90 |
-
num_inference_steps=30,
|
91 |
-
guidance_scale=7.5,
|
92 |
-
width=512,
|
93 |
-
height=512
|
94 |
-
).images[0]
|
95 |
-
|
96 |
-
progress(1.0, desc="Tamamlandı!")
|
97 |
-
return image, f"Oluşturulan prompt: {prompt}"
|
98 |
-
except Exception as e:
|
99 |
-
logger.error(f"Görüntü oluşturma hatası: {str(e)}")
|
100 |
-
return None, f"Hata oluştu: {str(e)}"
|
101 |
-
|
102 |
-
# Model işleyiciyi oluştur
|
103 |
-
model_handler = ModelHandler()
|
104 |
-
|
105 |
-
# Gradio arayüzü
|
106 |
-
with gr.Blocks(title="Malika - FLUX Text-to-Image") as demo:
|
107 |
-
gr.Markdown("# Malika - FLUX.1 Text-to-Image Modeliyle Görüntü Oluşturucu")
|
108 |
-
gr.Markdown("Bu uygulama, codermert/malikafinal modelini kullanarak metinden görüntü oluşturur.")
|
109 |
-
|
110 |
-
with gr.Row():
|
111 |
-
load_model_btn = gr.Button("Modeli Yükle", variant="primary")
|
112 |
-
model_status = gr.Textbox(label="Model Durumu", value="Model henüz yüklenmedi")
|
113 |
-
|
114 |
-
with gr.Row():
|
115 |
-
with gr.Column():
|
116 |
-
prompt_input = gr.Textbox(
|
117 |
-
label="Prompt",
|
118 |
-
placeholder="Görüntü için prompt yazın...",
|
119 |
-
lines=3
|
120 |
-
)
|
121 |
-
tok_checkbox = gr.Checkbox(
|
122 |
-
label="Otomatik TOK Ekle",
|
123 |
-
value=True,
|
124 |
-
info="İşaretliyse prompt'a otomatik olarak TOK ekler"
|
125 |
-
)
|
126 |
-
generate_btn = gr.Button("Görüntü Oluştur", variant="primary")
|
127 |
-
|
128 |
-
with gr.Column():
|
129 |
-
image_output = gr.Image(label="Oluşturulan Görüntü")
|
130 |
-
prompt_used = gr.Textbox(label="Kullanılan Prompt")
|
131 |
-
|
132 |
-
load_model_btn.click(
|
133 |
-
fn=model_handler.load_model,
|
134 |
-
outputs=model_status
|
135 |
)
|
136 |
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
)
|
142 |
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
""
|
|
|
154 |
|
155 |
-
|
156 |
-
if __name__ == "__main__":
|
157 |
-
demo.launch(share=True)
|
|
|
1 |
import gradio as gr
|
2 |
import torch
|
|
|
3 |
from diffusers import AutoPipelineForText2Image
|
|
|
|
|
|
|
4 |
|
5 |
+
def generate_image(prompt):
|
6 |
+
# Pipeline'ı başlat
|
7 |
+
pipeline = AutoPipelineForText2Image.from_pretrained(
|
8 |
+
'prithivMLmods/Canopus-LoRA-Flux-UltraRealism-2.0',
|
9 |
+
torch_dtype=torch.float16
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
)
|
11 |
|
12 |
+
# LoRA modelini yükle
|
13 |
+
pipeline.load_lora_weights(
|
14 |
+
'codermert/ezelll_flux',
|
15 |
+
weight_name='flux_train_replicate.safetensors'
|
16 |
)
|
17 |
|
18 |
+
# Görsel oluştur
|
19 |
+
image = pipeline(prompt).images[0]
|
20 |
+
return image
|
21 |
+
|
22 |
+
# Gradio arayüzü
|
23 |
+
iface = gr.Interface(
|
24 |
+
fn=generate_image,
|
25 |
+
inputs=gr.Textbox(label="Prompt'unuzu girin (zehra kelimesini kullanmayı unutmayın)"),
|
26 |
+
outputs=gr.Image(label="Oluşturulan Görsel"),
|
27 |
+
title="Ezel Flux Görsel Oluşturucu",
|
28 |
+
description="Bu model 'zehra' kelimesi ile en iyi sonucu verir."
|
29 |
+
)
|
30 |
|
31 |
+
iface.launch()
|
|
|
|