Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,32 +1,41 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import pickle
|
| 3 |
import numpy as np
|
| 4 |
import torch
|
| 5 |
-
import dnnlib
|
| 6 |
-
import legacy
|
| 7 |
from PIL import Image
|
| 8 |
-
import requests
|
| 9 |
-
import os
|
| 10 |
|
| 11 |
-
# --- Model Adı ve Yükleme ---
|
| 12 |
# !! DEĞİŞTİRİLECEK YER !!
|
| 13 |
-
# Hugging Face'e
|
| 14 |
-
MODEL_FILENAME = "network-snapshot-
|
| 15 |
|
| 16 |
-
# Modelin, Hugging Face deposunun kök dizininde olduğunu varsayıyoruz.
|
| 17 |
MODEL_PATH = MODEL_FILENAME
|
| 18 |
-
device = torch.device('cpu') # Ücretsiz CPU donanımı için 'cpu'
|
| 19 |
-
|
| 20 |
-
# StyleGAN2 kütüphanelerini indirmek ve kurmak için yardımcı fonksiyon
|
| 21 |
-
def setup_stylegan2_libs():
|
| 22 |
-
if not os.path.exists('stylegan2-ada-pytorch'):
|
| 23 |
-
print("StyleGAN2 deposu indiriliyor...")
|
| 24 |
-
os.system('git clone https://github.com/NVlabs/stylegan2-ada-pytorch.git')
|
| 25 |
-
import sys
|
| 26 |
-
sys.path.insert(0, "/app/stylegan2-ada-pytorch") # HF Spaces'te yol genellikle /app olur
|
| 27 |
-
|
| 28 |
-
print("StyleGAN2 kütüphaneleri hazırlanıyor...")
|
| 29 |
-
setup_stylegan2_libs()
|
| 30 |
|
| 31 |
# Modelin yüklenmesi
|
| 32 |
G = None
|
|
@@ -37,34 +46,25 @@ try:
|
|
| 37 |
print("Model başarıyla yüklendi.")
|
| 38 |
except Exception as e:
|
| 39 |
print(f"Model yüklenemedi: {e}")
|
| 40 |
-
|
| 41 |
-
gr.Warning(f"Model Yüklenemedi! Lütfen model dosyasının ({MODEL_FILENAME}) repoda olduğundan ve adının doğru yazıldığından emin olun. Hata: {e}")
|
| 42 |
|
| 43 |
-
|
| 44 |
-
# --- Görüntü Üretme Fonksiyonu ---
|
| 45 |
def generate_image(seed, truncation_psi):
|
| 46 |
if G is None:
|
| 47 |
raise gr.Error("Model yüklenemediği için görüntü üretilemiyor. Lütfen Space loglarını kontrol edin.")
|
| 48 |
|
| 49 |
-
# Eğer seed -1 ise (rastgele butonuna basıldıysa), rastgele bir seed üret
|
| 50 |
if seed == -1:
|
| 51 |
seed = np.random.randint(0, 2**32 - 1)
|
| 52 |
|
| 53 |
print(f"Görüntü üretiliyor... Seed: {seed}, Truncation: {truncation_psi}")
|
| 54 |
|
| 55 |
-
# Gürültü vektörünü (z) oluştur
|
| 56 |
z = torch.from_numpy(np.random.RandomState(seed).randn(1, G.z_dim)).to(device)
|
| 57 |
-
|
| 58 |
-
# Gürültü vektörünü modele vererek görüntüyü üret
|
| 59 |
img = G(z, None, truncation_psi=truncation_psi, noise_mode='const')
|
| 60 |
-
|
| 61 |
-
# Çıktıyı 0-255 aralığına getirip bir PIL Image'e dönüştür
|
| 62 |
img = (img.permute(0, 2, 3, 1) * 127.5 + 128).clamp(0, 255).to(torch.uint8)
|
| 63 |
|
| 64 |
return Image.fromarray(img[0].cpu().numpy(), 'RGB'), seed
|
| 65 |
|
| 66 |
-
|
| 67 |
-
# --- Gradio Arayüzü ---
|
| 68 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 69 |
gr.Markdown("# 🎨 StyleGAN2 ile Eğitilmiş Anime Karakter Üreticisi")
|
| 70 |
gr.Markdown("Bu demo, özel olarak eğitilmiş bir StyleGAN2 modeli kullanarak yeni anime karakterleri üretir.")
|
|
@@ -82,20 +82,12 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 82 |
image_output = gr.Image(label="Sonuç", type="pil")
|
| 83 |
used_seed_output = gr.Number(label="Kullanılan Seed", interactive=False)
|
| 84 |
|
| 85 |
-
# Butonlara tıklandığında hangi fonksiyonun hangi girdilerle çalışacağını belirle
|
| 86 |
generate_btn.click(fn=generate_image, inputs=[seed_input, truncation_input], outputs=[image_output, used_seed_output])
|
| 87 |
random_btn.click(fn=lambda: -1, inputs=None, outputs=seed_input).then(
|
| 88 |
fn=generate_image, inputs=[seed_input, truncation_input], outputs=[image_output, used_seed_output]
|
| 89 |
)
|
| 90 |
|
| 91 |
-
gr.Examples(
|
| 92 |
-
examples=[
|
| 93 |
-
[100, 0.7],
|
| 94 |
-
[42, 0.8],
|
| 95 |
-
[12345, 1.0]
|
| 96 |
-
],
|
| 97 |
-
inputs=[seed_input, truncation_input]
|
| 98 |
-
)
|
| 99 |
|
| 100 |
# Uygulamayı başlat
|
| 101 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
import sys
|
| 4 |
+
import subprocess
|
| 5 |
+
|
| 6 |
+
# --- 1. StyleGAN2 Kütüphanelerini Hazırlama (En Önemli Adım) ---
|
| 7 |
+
# dnnlib ve legacy gibi özel kütüphaneleri bulabilmek için
|
| 8 |
+
# stylegan2-ada-pytorch deposunu klonlamamız gerekiyor.
|
| 9 |
+
|
| 10 |
+
# Klonlanacak repo ve hedef klasör adı
|
| 11 |
+
repo_url = "https://github.com/NVlabs/stylegan2-ada-pytorch.git"
|
| 12 |
+
repo_dir = "stylegan2-ada-pytorch"
|
| 13 |
+
|
| 14 |
+
# Eğer klasör zaten yoksa, git clone komutunu çalıştır
|
| 15 |
+
if not os.path.exists(repo_dir):
|
| 16 |
+
print(f"'{repo_dir}' deposu indiriliyor...")
|
| 17 |
+
subprocess.run(["git", "clone", repo_url])
|
| 18 |
+
print("Depo başarıyla indirildi.")
|
| 19 |
+
|
| 20 |
+
# Klonlanan klasörün yolunu Python'un arama yollarına ekle
|
| 21 |
+
# Bu sayede `import dnnlib` ve `import legacy` komutları çalışır.
|
| 22 |
+
sys.path.insert(0, os.path.join(os.getcwd(), repo_dir))
|
| 23 |
+
|
| 24 |
+
# Artık import edebiliriz
|
| 25 |
import pickle
|
| 26 |
import numpy as np
|
| 27 |
import torch
|
| 28 |
+
import dnnlib
|
| 29 |
+
import legacy
|
| 30 |
from PIL import Image
|
|
|
|
|
|
|
| 31 |
|
| 32 |
+
# --- 2. Model Adı ve Yükleme ---
|
| 33 |
# !! DEĞİŞTİRİLECEK YER !!
|
| 34 |
+
# Hugging Face'e yüklediğiniz .pkl model dosyasının tam adını buraya yazın.
|
| 35 |
+
MODEL_FILENAME = "network-snapshot-006000.pkl"
|
| 36 |
|
|
|
|
| 37 |
MODEL_PATH = MODEL_FILENAME
|
| 38 |
+
device = torch.device('cpu') # Ücretsiz CPU donanımı için 'cpu'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
# Modelin yüklenmesi
|
| 41 |
G = None
|
|
|
|
| 46 |
print("Model başarıyla yüklendi.")
|
| 47 |
except Exception as e:
|
| 48 |
print(f"Model yüklenemedi: {e}")
|
| 49 |
+
gr.Warning(f"Model Yüklenemedi! Dosya adının ({MODEL_FILENAME}) doğru olduğundan emin olun. Hata: {e}")
|
|
|
|
| 50 |
|
| 51 |
+
# --- 3. Görüntü Üretme Fonksiyonu ---
|
|
|
|
| 52 |
def generate_image(seed, truncation_psi):
|
| 53 |
if G is None:
|
| 54 |
raise gr.Error("Model yüklenemediği için görüntü üretilemiyor. Lütfen Space loglarını kontrol edin.")
|
| 55 |
|
|
|
|
| 56 |
if seed == -1:
|
| 57 |
seed = np.random.randint(0, 2**32 - 1)
|
| 58 |
|
| 59 |
print(f"Görüntü üretiliyor... Seed: {seed}, Truncation: {truncation_psi}")
|
| 60 |
|
|
|
|
| 61 |
z = torch.from_numpy(np.random.RandomState(seed).randn(1, G.z_dim)).to(device)
|
|
|
|
|
|
|
| 62 |
img = G(z, None, truncation_psi=truncation_psi, noise_mode='const')
|
|
|
|
|
|
|
| 63 |
img = (img.permute(0, 2, 3, 1) * 127.5 + 128).clamp(0, 255).to(torch.uint8)
|
| 64 |
|
| 65 |
return Image.fromarray(img[0].cpu().numpy(), 'RGB'), seed
|
| 66 |
|
| 67 |
+
# --- 4. Gradio Arayüzü ---
|
|
|
|
| 68 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 69 |
gr.Markdown("# 🎨 StyleGAN2 ile Eğitilmiş Anime Karakter Üreticisi")
|
| 70 |
gr.Markdown("Bu demo, özel olarak eğitilmiş bir StyleGAN2 modeli kullanarak yeni anime karakterleri üretir.")
|
|
|
|
| 82 |
image_output = gr.Image(label="Sonuç", type="pil")
|
| 83 |
used_seed_output = gr.Number(label="Kullanılan Seed", interactive=False)
|
| 84 |
|
|
|
|
| 85 |
generate_btn.click(fn=generate_image, inputs=[seed_input, truncation_input], outputs=[image_output, used_seed_output])
|
| 86 |
random_btn.click(fn=lambda: -1, inputs=None, outputs=seed_input).then(
|
| 87 |
fn=generate_image, inputs=[seed_input, truncation_input], outputs=[image_output, used_seed_output]
|
| 88 |
)
|
| 89 |
|
| 90 |
+
gr.Examples(examples=[[100, 0.7], [42, 0.8]], inputs=[seed_input, truncation_input])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
|
| 92 |
# Uygulamayı başlat
|
| 93 |
demo.launch()
|