Update app.py
Browse files
app.py
CHANGED
|
@@ -1,11 +1,10 @@
|
|
|
|
|
| 1 |
import torch
|
| 2 |
-
import spaces
|
| 3 |
import gradio as gr
|
| 4 |
from diffusers import FluxPipeline
|
| 5 |
from huggingface_hub import hf_hub_download
|
| 6 |
import random
|
| 7 |
|
| 8 |
-
|
| 9 |
# Chargement du modèle Flux.1-schnell
|
| 10 |
model_id = "black-forest-labs/FLUX.1-schnell"
|
| 11 |
|
|
@@ -13,21 +12,40 @@ model_id = "black-forest-labs/FLUX.1-schnell"
|
|
| 13 |
lora_repo = None
|
| 14 |
lora_path = None
|
| 15 |
|
| 16 |
-
def load_lora(repo_id):
|
| 17 |
global lora_repo, lora_path
|
| 18 |
try:
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
except Exception as e:
|
| 23 |
return f"❌ Erreur LoRA : {str(e)}"
|
| 24 |
|
| 25 |
-
@spaces.GPU(duration=120)
|
| 26 |
def generate(prompt, negative_prompt, width=1024, height=1024, steps=4, seed=-1, lora_scale=0.8):
|
| 27 |
try:
|
| 28 |
-
# Chargement du pipe
|
| 29 |
pipe = FluxPipeline.from_pretrained(model_id, torch_dtype=torch.bfloat16)
|
| 30 |
-
pipe.
|
| 31 |
|
| 32 |
# Chargement LoRA si disponible
|
| 33 |
if lora_repo and lora_path:
|
|
@@ -54,14 +72,19 @@ def generate(prompt, negative_prompt, width=1024, height=1024, steps=4, seed=-1,
|
|
| 54 |
return None
|
| 55 |
|
| 56 |
# Interface Gradio
|
| 57 |
-
with gr.Blocks(title="Flux Schnell + LoRA"
|
| 58 |
gr.Markdown("# 🎨 Flux.1 Schnell + LoRA\nGénérateur rapide (4 steps) avec support LoRA personnalisé")
|
| 59 |
|
| 60 |
with gr.Row():
|
| 61 |
with gr.Column(scale=1):
|
| 62 |
lora_input = gr.Textbox(
|
| 63 |
label="Repo HuggingFace LoRA",
|
| 64 |
-
placeholder="ex:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
value=""
|
| 66 |
)
|
| 67 |
load_btn = gr.Button("Charger LoRA", variant="secondary")
|
|
@@ -89,7 +112,7 @@ with gr.Blocks(title="Flux Schnell + LoRA", theme=gr.themes.Soft()) as demo:
|
|
| 89 |
generate_btn = gr.Button("🚀 Générer Image", variant="primary", size="lg")
|
| 90 |
output = gr.Image(label="Résultat", type="pil")
|
| 91 |
|
| 92 |
-
load_btn.click(load_lora, inputs=lora_input, outputs=status)
|
| 93 |
generate_btn.click(
|
| 94 |
generate,
|
| 95 |
inputs=[prompt, neg_prompt, width, height, steps, seed, lora_scale_slider],
|
|
@@ -97,4 +120,4 @@ with gr.Blocks(title="Flux Schnell + LoRA", theme=gr.themes.Soft()) as demo:
|
|
| 97 |
)
|
| 98 |
|
| 99 |
if __name__ == "__main__":
|
| 100 |
-
demo.launch()
|
|
|
|
| 1 |
+
import spaces # Pour Zero GPU gratuit
|
| 2 |
import torch
|
|
|
|
| 3 |
import gradio as gr
|
| 4 |
from diffusers import FluxPipeline
|
| 5 |
from huggingface_hub import hf_hub_download
|
| 6 |
import random
|
| 7 |
|
|
|
|
| 8 |
# Chargement du modèle Flux.1-schnell
|
| 9 |
model_id = "black-forest-labs/FLUX.1-schnell"
|
| 10 |
|
|
|
|
| 12 |
lora_repo = None
|
| 13 |
lora_path = None
|
| 14 |
|
| 15 |
+
def load_lora(repo_id, subfolder=""):
|
| 16 |
global lora_repo, lora_path
|
| 17 |
try:
|
| 18 |
+
# Essayer plusieurs noms de fichiers courants
|
| 19 |
+
possible_files = [
|
| 20 |
+
"lora.safetensors",
|
| 21 |
+
"flux_lora.safetensors",
|
| 22 |
+
"flux-lora.safetensors",
|
| 23 |
+
"pytorch_lora_weights.safetensors"
|
| 24 |
+
]
|
| 25 |
+
|
| 26 |
+
# Si un subfolder est spécifié (ex: "anime")
|
| 27 |
+
kwargs = {"repo_id": repo_id}
|
| 28 |
+
if subfolder:
|
| 29 |
+
kwargs["subfolder"] = subfolder
|
| 30 |
+
|
| 31 |
+
for filename in possible_files:
|
| 32 |
+
try:
|
| 33 |
+
lora_path = hf_hub_download(filename=filename, **kwargs)
|
| 34 |
+
lora_repo = repo_id
|
| 35 |
+
return f"✅ LoRA chargé : {repo_id}/{subfolder if subfolder else ''} ({filename})"
|
| 36 |
+
except:
|
| 37 |
+
continue
|
| 38 |
+
|
| 39 |
+
return f"❌ Aucun fichier LoRA trouvé. Essayez avec un chemin spécifique."
|
| 40 |
except Exception as e:
|
| 41 |
return f"❌ Erreur LoRA : {str(e)}"
|
| 42 |
|
| 43 |
+
@spaces.GPU(duration=120) # Allocation GPU pour 120s
|
| 44 |
def generate(prompt, negative_prompt, width=1024, height=1024, steps=4, seed=-1, lora_scale=0.8):
|
| 45 |
try:
|
| 46 |
+
# Chargement du pipe dans la fonction (obligatoire pour Zero GPU)
|
| 47 |
pipe = FluxPipeline.from_pretrained(model_id, torch_dtype=torch.bfloat16)
|
| 48 |
+
pipe.to("cuda")
|
| 49 |
|
| 50 |
# Chargement LoRA si disponible
|
| 51 |
if lora_repo and lora_path:
|
|
|
|
| 72 |
return None
|
| 73 |
|
| 74 |
# Interface Gradio
|
| 75 |
+
with gr.Blocks(title="Flux Schnell + LoRA") as demo:
|
| 76 |
gr.Markdown("# 🎨 Flux.1 Schnell + LoRA\nGénérateur rapide (4 steps) avec support LoRA personnalisé")
|
| 77 |
|
| 78 |
with gr.Row():
|
| 79 |
with gr.Column(scale=1):
|
| 80 |
lora_input = gr.Textbox(
|
| 81 |
label="Repo HuggingFace LoRA",
|
| 82 |
+
placeholder="ex: stabilityai/stable-diffusion-xl-base-1.0",
|
| 83 |
+
value=""
|
| 84 |
+
)
|
| 85 |
+
subfolder_input = gr.Textbox(
|
| 86 |
+
label="Sous-dossier (optionnel)",
|
| 87 |
+
placeholder="ex: anime (pour XLabs-AI)",
|
| 88 |
value=""
|
| 89 |
)
|
| 90 |
load_btn = gr.Button("Charger LoRA", variant="secondary")
|
|
|
|
| 112 |
generate_btn = gr.Button("🚀 Générer Image", variant="primary", size="lg")
|
| 113 |
output = gr.Image(label="Résultat", type="pil")
|
| 114 |
|
| 115 |
+
load_btn.click(load_lora, inputs=[lora_input, subfolder_input], outputs=status)
|
| 116 |
generate_btn.click(
|
| 117 |
generate,
|
| 118 |
inputs=[prompt, neg_prompt, width, height, steps, seed, lora_scale_slider],
|
|
|
|
| 120 |
)
|
| 121 |
|
| 122 |
if __name__ == "__main__":
|
| 123 |
+
demo.launch(theme=gr.themes.Soft())
|