Spaces:
Sleeping
Sleeping
File size: 2,945 Bytes
887671e 6cc882d c8b47c2 6cc882d 887671e 783a463 4a98482 4cdcfd2 887671e 3a8eb6a 6cc882d 323789b 783a463 6cc882d 4a98482 28a9a2e 783a463 8b8a717 6cc882d 4cdcfd2 783a463 3a8eb6a 783a463 3a8eb6a 4cdcfd2 783a463 28a9a2e 4cdcfd2 3a8eb6a 783a463 3a8eb6a 4cdcfd2 3a8eb6a da1bc5c 323789b 783a463 4cdcfd2 323789b 4cdcfd2 783a463 4cdcfd2 783a463 6cc882d 3a8eb6a 4cdcfd2 6cc882d 887671e | 1 2 3 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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 | import gradio as gr
import numpy as np
import random
import spaces
import torch
import os
from diffusers import Flux2KleinPipeline
from huggingface_hub import hf_hub_download, InferenceClient
from safetensors import safe_open
from safetensors.torch import load_file
dtype = torch.float16
device = "cuda" if torch.cuda.is_available() else "cpu"
hf_token = os.getenv("HF_TOKEN")
# Pipeline yükle
pipe = Flux2KleinPipeline.from_pretrained(
"black-forest-labs/FLUX.2-klein-9B",
torch_dtype=dtype,
token=hf_token
).to(device)
# LoRA yükle
LORA_PATH = "safetensors/IlgaCengizFLUX.2-klein-base-9b.safetensors"
pipe.load_lora_weights(LORA_PATH)
pipe.fuse_lora(lora_scale=0.75)
MAX_SEED = np.iinfo(np.int32).max
@spaces.GPU() # ZeroGPU'yu tetikleyen dekoratör
def infer(prompt, seed=42, randomize_seed=False, width=1024, height=1024,
num_inference_steps=28, guidance_scale=3.5,
progress=gr.Progress(track_tqdm=True)):
if randomize_seed:
seed = random.randint(0, MAX_SEED)
# FLUX'ta textual inversion yok; trigger word'ü düz metne ekle
trigger_word = "IlgaCengiz, " # ← LoRA'nı nasıl eğittiysen o trigger word
enhanced_prompt = trigger_word + prompt
generator = torch.Generator(device="cpu").manual_seed(seed)
image = pipe(
prompt=enhanced_prompt,
width=width,
height=height,
num_inference_steps=num_inference_steps,
generator=generator,
guidance_scale=guidance_scale,
).images[0]
return image, seed
with gr.Blocks() as demo:
with gr.Column():
gr.Markdown("# Ilga Character Generator\nFLUX.2-klein + Custom LoRA")
with gr.Row():
prompt = gr.Text(
label="Prompt",
show_label=False,
max_lines=1,
placeholder="Enter your prompt",
container=False,
)
run_button = gr.Button("Run", scale=0)
result = gr.Image(label="Result", show_label=False)
with gr.Accordion("Advanced Settings", open=False):
seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0)
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
with gr.Row():
width = gr.Slider(label="Width", minimum=512, maximum=1360, step=64, value=1024)
height = gr.Slider(label="Height", minimum=512, maximum=1360, step=64, value=1024)
with gr.Row():
num_inference_steps = gr.Slider(label="Steps", minimum=4, maximum=50, step=1, value=28)
guidance_scale = gr.Slider(label="Guidance Scale", minimum=1.0, maximum=10.0, step=0.5, value=3.5)
gr.on(
triggers=[run_button.click, prompt.submit],
fn=infer,
inputs=[prompt, seed, randomize_seed, width, height, num_inference_steps, guidance_scale],
outputs=[result, seed]
)
demo.launch() |