Update app.py
Browse files
app.py
CHANGED
|
@@ -1,65 +1,79 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import sys
|
| 3 |
-
import subprocess
|
| 4 |
-
|
| 5 |
-
# --- Installation automatique des paquets manquants lors du démarrage du Space ---
|
| 6 |
-
try:
|
| 7 |
-
from mage_flow import MageFlowPipeline
|
| 8 |
-
except ImportError:
|
| 9 |
-
print("⏳ Installation du pipeline officiel de Microsoft depuis GitHub...")
|
| 10 |
-
# Installation des dépendances requises
|
| 11 |
-
subprocess.check_call([sys.executable, "-m", "pip", "install", "diffusers==0.38.0", "transformers>=5.3", "einops", "pydantic", "loguru", "accelerate", "gradio"])
|
| 12 |
-
# Installation directe du sous-dossier mage_flow
|
| 13 |
-
subprocess.check_call([sys.executable, "-m", "pip", "install", "git+https://github.com/microsoft/Mage.git#subdirectory=mage_flow", "--no-deps"])
|
| 14 |
-
from mage_flow import MageFlowPipeline
|
| 15 |
-
|
| 16 |
import torch
|
|
|
|
|
|
|
| 17 |
import gradio as gr
|
| 18 |
from PIL import Image
|
|
|
|
| 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 |
with gr.Row():
|
|
|
|
| 50 |
with gr.Column():
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
with gr.Column():
|
| 59 |
-
image_output = gr.Image(label="Image Générée", type="pil")
|
| 60 |
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
-
# Lancement
|
| 64 |
if __name__ == "__main__":
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import torch
|
| 2 |
+
import traceback
|
| 3 |
+
import io
|
| 4 |
import gradio as gr
|
| 5 |
from PIL import Image
|
| 6 |
+
from diffusers import Flux2KleinPipeline
|
| 7 |
|
| 8 |
+
# 1. Chargement initial du modèle (exécuté une seule fois au démarrage)
|
| 9 |
+
print("Loading model...")
|
| 10 |
+
pipe = Flux2KleinPipeline.from_pretrained(
|
| 11 |
+
"black-forest-labs/FLUX.2-klein-base-4B",
|
| 12 |
+
torch_dtype=torch.bfloat16,
|
| 13 |
+
)
|
| 14 |
+
pipe.to("cuda")
|
| 15 |
+
pipe.load_lora_weights("./sking_v73_flux_4b_000027000.safetensors")
|
| 16 |
+
print("Model loaded successfully.")
|
| 17 |
|
| 18 |
+
# 2. Fonction principale appelée par l'interface Gradio
|
| 19 |
+
def generate_minecraft_image(input_image, prompt, guidance, seed, n_step):
|
| 20 |
+
if input_image is None:
|
| 21 |
+
raise gr.Error("Veuillez téléverser une image de départ.")
|
| 22 |
+
|
| 23 |
+
try:
|
| 24 |
+
# Prétraitement similaire à votre API (Conversion RGBA)
|
| 25 |
+
img = input_image.convert("RGBA")
|
| 26 |
+
|
| 27 |
+
# Exécution du pipeline Flux 2 Klein
|
| 28 |
+
pipeline_output = pipe(
|
| 29 |
+
image=img,
|
| 30 |
+
prompt=prompt,
|
| 31 |
+
height=768,
|
| 32 |
+
width=768,
|
| 33 |
+
num_inference_steps=int(n_step),
|
| 34 |
+
guidance_scale=float(guidance),
|
| 35 |
+
num_images_per_prompt=1,
|
| 36 |
+
generator=torch.Generator("cuda").manual_seed(int(seed))
|
| 37 |
)
|
| 38 |
+
|
| 39 |
+
# Retourne la liste des images générées à la galerie Gradio
|
| 40 |
+
return pipeline_output.images
|
| 41 |
|
| 42 |
+
except Exception as e:
|
| 43 |
+
traceback.print_exc()
|
| 44 |
+
print(f"Error during generation: {e}")
|
| 45 |
+
raise gr.Error(f"Erreur lors de la génération : {str(e)}")
|
| 46 |
+
|
| 47 |
+
# 3. Construction de l'interface graphique (Gradio)
|
| 48 |
+
with gr.Blocks(title="Minecraft IA - Flux.2 Klein", theme=gr.themes.Soft()) as demo:
|
| 49 |
+
gr.Markdown("# 🎮 Minecraft Pixel-to-Pixel IA Generator")
|
| 50 |
+
gr.Markdown("Transformez vos images ou appliquez votre style LoRA avec FLUX.2 Klein [4B].")
|
| 51 |
|
| 52 |
with gr.Row():
|
| 53 |
+
# Colonne de gauche : Entrées et paramètres
|
| 54 |
with gr.Column():
|
| 55 |
+
input_img = gr.Image(label="Image source (Image Input)", type="pil")
|
| 56 |
+
prompt_txt = gr.Textbox(label="Prompt (Mot-clé / Description)", value="", placeholder="Ex: minecraft skin, isometric block...")
|
| 57 |
+
|
| 58 |
+
with gr.Accordion("Paramètres avancés", open=True):
|
| 59 |
+
guidance_slider = gr.Slider(minimum=1.0, maximum=20.0, value=4.0, step=0.5, label="Guidance Scale")
|
| 60 |
+
steps_slider = gr.Slider(minimum=1, maximum=150, value=100, step=1, label="Nombre de pas (Inference Steps)")
|
| 61 |
+
seed_number = gr.Number(value=42, label="Seed", precision=0)
|
|
|
|
|
|
|
| 62 |
|
| 63 |
+
submit_btn = gr.Button("🚀 Générer l'image", variant="primary")
|
| 64 |
+
|
| 65 |
+
# Colonne de droite : Résultats
|
| 66 |
+
with gr.Column():
|
| 67 |
+
output_gallery = gr.Gallery(label="Images Générées", columns=1, height="auto", object_fit="contain")
|
| 68 |
+
|
| 69 |
+
# Liaison du bouton à la fonction de génération
|
| 70 |
+
submit_btn.click(
|
| 71 |
+
fn=generate_minecraft_image,
|
| 72 |
+
inputs=[input_img, prompt_txt, guidance_slider, seed_number, steps_slider],
|
| 73 |
+
outputs=output_gallery
|
| 74 |
+
)
|
| 75 |
|
| 76 |
+
# 4. Lancement du serveur Web local
|
| 77 |
if __name__ == "__main__":
|
| 78 |
+
# Lance l'interface sur le port 10012 similaire à votre configuration uvicorn
|
| 79 |
+
demo.launch(server_name="0.0.0.0", server_port=10012)
|