codermert commited on
Commit
031ae3b
·
verified ·
1 Parent(s): c34ea9b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +66 -48
app.py CHANGED
@@ -3,67 +3,84 @@ 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/ezelll_flux",
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
@@ -120,4 +137,5 @@ with gr.Blocks(title="Malika - FLUX Text-to-Image") as demo:
120
  """)
121
 
122
  # Arayüzü başlat
123
- demo.launch()
 
 
3
  import os
4
  from diffusers import AutoPipelineForText2Image
5
  from huggingface_hub import snapshot_download
6
+ import logging
7
+
8
+ # Logging ayarları
9
+ logging.basicConfig(level=logging.INFO)
10
+ logger = logging.getLogger(__name__)
11
 
12
  class ModelHandler:
13
  def __init__(self):
14
  self.pipeline = None
15
+ self.device = "cuda" if torch.cuda.is_available() else "cpu"
16
+ self.dtype = torch.float16 if self.device == "cuda" else torch.float32
17
 
18
  def load_model(self, progress=gr.Progress()):
19
+ try:
20
+ if self.pipeline is not None:
21
+ return "Model zaten yüklü."
22
+
23
+ progress(0, desc="Base model indiriliyor...")
24
+ # Base modeli indir
25
+ base_model_path = snapshot_download(
26
+ repo_id="black-forest-labs/FLUX.1-dev",
27
+ local_dir="./models/base_model",
28
+ ignore_patterns=["*.bin", "*.onnx"] if os.path.exists("./models/base_model") else None,
29
+ token=os.getenv("HF_TOKEN")
30
+ )
31
+
32
+ progress(0.5, desc="LoRA modeli indiriliyor...")
33
+ # LoRA modelini indir
34
+ lora_model_path = snapshot_download(
35
+ repo_id="codermert/ezelll_flux",
36
+ local_dir="./models/lora_model",
37
+ ignore_patterns=["*.bin", "*.onnx"] if os.path.exists("./models/lora_model") else None,
38
+ token=os.getenv("HF_TOKEN")
39
+ )
40
+
41
+ progress(0.7, desc="Pipeline oluşturuluyor...")
42
+ # Pipeline'ı oluştur
43
+ self.pipeline = AutoPipelineForText2Image.from_pretrained(
44
+ base_model_path,
45
+ torch_dtype=self.dtype,
46
+ use_safetensors=True
47
+ ).to(self.device)
48
+
49
+ progress(0.9, desc="LoRA yükleniyor...")
50
+ # LoRA'yı yükle
51
+ lora_path = os.path.join(lora_model_path, "lora.safetensors")
52
+ if os.path.exists(lora_path):
53
+ self.pipeline.load_lora_weights(lora_path)
54
+ else:
55
+ return "LoRA dosyası bulunamadı!"
56
+
57
+ progress(1.0, desc="Tamamlandı!")
58
+ return "Model başarıyla yüklendi! Artık görüntü oluşturmaya hazırsınız."
59
+ except Exception as e:
60
+ logger.error(f"Model yükleme hatası: {str(e)}")
61
+ return f"Model yüklenirken hata oluştu: {str(e)}"
62
 
63
  def generate_image(self, prompt, use_tok=True, progress=gr.Progress()):
 
 
 
 
 
 
 
64
  try:
65
+ if self.pipeline is None:
66
+ return None, "Lütfen önce modeli yükleyin!"
67
+
68
+ # Eğer use_tok seçeneği işaretlendiyse, prompt'a TOK ekle
69
+ if use_tok and "TOK" not in prompt:
70
+ prompt = f"TOK {prompt}"
71
+
72
  progress(0.2, desc="Görüntü oluşturuluyor...")
73
  # Görüntü oluştur
74
+ image = self.pipeline(
75
+ prompt,
76
+ num_inference_steps=30,
77
+ guidance_scale=7.5
78
+ ).images[0]
79
+
80
  progress(1.0, desc="Tamamlandı!")
81
  return image, f"Oluşturulan prompt: {prompt}"
82
  except Exception as e:
83
+ logger.error(f"Görüntü oluşturma hatası: {str(e)}")
84
  return None, f"Hata oluştu: {str(e)}"
85
 
86
  # Model işleyiciyi oluştur
 
137
  """)
138
 
139
  # Arayüzü başlat
140
+ if __name__ == "__main__":
141
+ demo.launch(share=True)