Nad54 commited on
Commit
c24f8a7
·
verified ·
1 Parent(s): c186368

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +5 -109
app.py CHANGED
@@ -1,109 +1,5 @@
1
- import torch, gradio as gr
2
- from PIL import Image
3
- from diffusers import StableDiffusionImg2ImgPipeline, DPMSolverMultistepScheduler
4
-
5
- # --- Configuration ---
6
- BASE_MODEL_ID = "sd-legacy/stable-diffusion-v1-5" # modèle de base
7
- LORA_PATH = "./wanostyle_2_offset.safetensors" # LoRA que tu as uploadé à la racine du Space
8
-
9
- device = "cuda"
10
- dtype = torch.float16
11
-
12
- # --- Chargement pipeline SD1.5 ---
13
- print("🔹 Chargement du modèle de base...")
14
- pipe = StableDiffusionImg2ImgPipeline.from_pretrained(
15
- BASE_MODEL_ID,
16
- torch_dtype=dtype,
17
- safety_checker=None,
18
- use_safetensors=True,
19
- )
20
- pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
21
-
22
- # Optimisations mémoire stables (GPU T4)
23
- pipe.enable_attention_slicing()
24
- pipe.enable_vae_slicing()
25
- pipe.enable_vae_tiling()
26
- pipe.to(device)
27
-
28
- # Chargement du LoRA
29
- print("🔹 Chargement du LoRA...")
30
- pipe.load_lora_weights(LORA_PATH, adapter_name="onepiece")
31
-
32
- # Negative prompt par défaut
33
- DEFAULT_NEG = "low quality, worst quality, extra fingers, deformed, blurry, text, watermark, logo"
34
-
35
- def stylize(
36
- image,
37
- prompt,
38
- lora_scale=0.9,
39
- strength=0.45,
40
- cfg=7.0,
41
- steps=30,
42
- width=640,
43
- height=640,
44
- seed=-1
45
- ):
46
- if image is None:
47
- return None
48
-
49
- # Générateur de seed
50
- generator = None
51
- if seed is not None and int(seed) >= 0:
52
- generator = torch.Generator(device=device).manual_seed(int(seed))
53
-
54
- # Redimensionne l'image d'entrée
55
- image = image.convert("RGB").resize((int(width), int(height)), Image.Resampling.LANCZOS)
56
-
57
- # Active le LoRA
58
- pipe.set_adapters(["onepiece"], adapter_weights=[float(lora_scale)])
59
-
60
- # Génération
61
- result = pipe(
62
- prompt=prompt.strip(),
63
- image=image,
64
- strength=float(strength),
65
- guidance_scale=float(cfg),
66
- num_inference_steps=int(steps),
67
- negative_prompt=DEFAULT_NEG,
68
- generator=generator
69
- )
70
-
71
- return result.images[0]
72
-
73
-
74
- # --- Interface Gradio ---
75
- EX_PROMPT = (
76
- "portrait buste, pirate stylisé One Piece, cel shading, ligne claire, "
77
- "détails anime, expression confiante, fond simple"
78
- )
79
-
80
- with gr.Blocks(css="footer{display:none !important}") as demo:
81
- gr.Markdown("# 🏴‍☠️ One Piece (LoRA) – Génération d'image stylisée")
82
-
83
- with gr.Row():
84
- with gr.Column():
85
- in_image = gr.Image(type="pil", label="Photo de la personne")
86
- prompt = gr.Textbox(label="Prompt", value=EX_PROMPT)
87
- lora_scale = gr.Slider(0.0, 1.5, value=0.9, step=0.05, label="Force du LoRA (style One Piece)")
88
- strength = gr.Slider(0.1, 0.9, value=0.45, step=0.05, label="Denoise strength (garde du visage)")
89
- cfg = gr.Slider(1, 12, value=7, step=0.5, label="CFG (guidance scale)")
90
- steps = gr.Slider(10, 60, value=30, step=1, label="Steps")
91
- with gr.Row():
92
- width = gr.Dropdown(choices=[512, 640, 768], value=640, label="Largeur")
93
- height = gr.Dropdown(choices=[512, 640, 768], value=640, label="Hauteur")
94
- seed = gr.Number(value=-1, label="Seed (-1 aléatoire)")
95
- btn = gr.Button("🎨 Générer l'image", variant="primary")
96
- with gr.Column():
97
- out_image = gr.Image(label="Résultat", interactive=False)
98
-
99
- btn.click(
100
- stylize,
101
- inputs=[in_image, prompt, lora_scale, strength, cfg, steps, width, height, seed],
102
- outputs=out_image
103
- )
104
-
105
- # Empêche les OOM liés aux multiples requêtes
106
- demo.queue(concurrency_count=1, max_size=4)
107
-
108
- if __name__ == "__main__":
109
- demo.launch()
 
1
+ import torch
2
+ print(f"Is CUDA available: {torch.cuda.is_available()}")
3
+ # True
4
+ print(f"CUDA device: {torch.cuda.get_device_name(torch.cuda.current_device())}")
5
+ # Tesla T4