Update app.py
Browse files
app.py
CHANGED
|
@@ -3,53 +3,50 @@ import gradio as gr
|
|
| 3 |
from diffusers import Flux2Pipeline
|
| 4 |
|
| 5 |
# -----------------------------
|
| 6 |
-
#
|
| 7 |
# -----------------------------
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
feature_extractor=None, # inutile
|
| 21 |
-
).to("cpu")
|
| 22 |
-
|
| 23 |
-
with torch.inference_mode():
|
| 24 |
-
prompt_embeds = pipe.encode_prompt(prompt)
|
| 25 |
-
|
| 26 |
-
return prompt_embeds
|
| 27 |
-
|
| 28 |
|
| 29 |
# -----------------------------
|
| 30 |
-
#
|
| 31 |
# -----------------------------
|
| 32 |
-
def
|
| 33 |
-
|
|
|
|
| 34 |
|
| 35 |
-
|
| 36 |
-
|
| 37 |
|
| 38 |
-
#
|
| 39 |
-
|
|
|
|
| 40 |
|
| 41 |
-
return f"Embeddings générés :
|
| 42 |
|
| 43 |
|
| 44 |
# -----------------------------
|
| 45 |
# INTERFACE GRADIO
|
| 46 |
# -----------------------------
|
| 47 |
demo = gr.Interface(
|
| 48 |
-
fn=
|
| 49 |
inputs=gr.Textbox(label="Prompt", placeholder="Écris ton texte ici..."),
|
| 50 |
-
outputs=
|
| 51 |
-
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
| 53 |
)
|
| 54 |
|
| 55 |
demo.launch()
|
|
|
|
| 3 |
from diffusers import Flux2Pipeline
|
| 4 |
|
| 5 |
# -----------------------------
|
| 6 |
+
# CHARGEMENT MINIMAL DU TEXT ENCODER FLUX
|
| 7 |
# -----------------------------
|
| 8 |
+
pipe = Flux2Pipeline.from_pretrained(
|
| 9 |
+
"black-forest-labs/FLUX.2-klein-4B",
|
| 10 |
+
torch_dtype=torch.float32,
|
| 11 |
+
low_cpu_mem_usage=True,
|
| 12 |
+
|
| 13 |
+
# On désactive tout ce qui n'est pas utile pour encode_prompt()
|
| 14 |
+
transformer=None,
|
| 15 |
+
vae=None,
|
| 16 |
+
scheduler=None,
|
| 17 |
+
safety_checker=None,
|
| 18 |
+
feature_extractor=None,
|
| 19 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
# -----------------------------
|
| 22 |
+
# ENCODEUR
|
| 23 |
# -----------------------------
|
| 24 |
+
def encode_text(prompt: str):
|
| 25 |
+
if not prompt.strip():
|
| 26 |
+
return "Prompt vide", None
|
| 27 |
|
| 28 |
+
with torch.inference_mode():
|
| 29 |
+
embeds = pipe.encode_prompt(prompt)
|
| 30 |
|
| 31 |
+
# Sauvegarde dans un fichier temporaire
|
| 32 |
+
file_path = "embeds.pt"
|
| 33 |
+
torch.save(embeds.cpu(), file_path)
|
| 34 |
|
| 35 |
+
return f"Embeddings générés : {tuple(embeds.shape)}", file_path
|
| 36 |
|
| 37 |
|
| 38 |
# -----------------------------
|
| 39 |
# INTERFACE GRADIO
|
| 40 |
# -----------------------------
|
| 41 |
demo = gr.Interface(
|
| 42 |
+
fn=encode,
|
| 43 |
inputs=gr.Textbox(label="Prompt", placeholder="Écris ton texte ici..."),
|
| 44 |
+
outputs=[
|
| 45 |
+
gr.Textbox(label="Infos"),
|
| 46 |
+
gr.File(label="Fichier .pt des embeddings")
|
| 47 |
+
],
|
| 48 |
+
title="Encodeur Texte FLUX.2 — Minimal",
|
| 49 |
+
description="Encodeur officiel FLUX.2 (Mistral-3-Small). Génère des embeddings compatibles avec Flux2Pipeline.",
|
| 50 |
)
|
| 51 |
|
| 52 |
demo.launch()
|