Spaces:
Sleeping
Sleeping
add: stablediffusion pipeline (#1)
Browse files- add: stablediffusion pipeline (fb43b6746d6a7f5bf3c64b6b63ebe6e125393e52)
Co-authored-by: Mehdi Merabet <[email protected]>
app.py
CHANGED
@@ -1,41 +1,30 @@
|
|
1 |
import gradio as gr
|
2 |
from PIL import Image
|
3 |
import torch
|
4 |
-
from
|
5 |
|
6 |
-
# Charger le
|
7 |
model_name = "Yaquv/rickthenpc"
|
8 |
-
|
9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
# Fonction de génération et de post-traitement
|
12 |
def generate_image(prompt):
|
13 |
"""
|
14 |
Génère une image à partir du prompt en utilisant le modèle Hugging Face.
|
15 |
"""
|
16 |
-
|
17 |
-
|
18 |
-
inputs = tokenizer(prompt, return_tensors="pt")
|
19 |
-
|
20 |
-
# Si GPU disponible, envoyer le modèle et les entrées sur le GPU
|
21 |
-
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
22 |
-
model.to(device)
|
23 |
-
inputs = {key: val.to(device) for key, val in inputs.items()}
|
24 |
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
# Si la sortie est déjà une image (ex : tableau NumPy), la convertir en image
|
29 |
-
if isinstance(outputs, torch.Tensor):
|
30 |
-
image_data = outputs.detach().cpu().numpy()
|
31 |
-
else:
|
32 |
-
raise ValueError("Le modèle a retourné un format inattendu.")
|
33 |
-
|
34 |
-
# Assurer que la sortie est une image au format (H, W, C)
|
35 |
-
if len(image_data.shape) == 3: # (Hauteur, Largeur, Canaux)
|
36 |
-
image = Image.fromarray((image_data * 255).astype("uint8"))
|
37 |
-
else:
|
38 |
-
raise ValueError("Les données générées ne sont pas au format image attendu.")
|
39 |
|
40 |
return image
|
41 |
|
@@ -54,4 +43,4 @@ iface = gr.Interface(
|
|
54 |
|
55 |
# Lancer l'application
|
56 |
if __name__ == "__main__":
|
57 |
-
iface.launch()
|
|
|
1 |
import gradio as gr
|
2 |
from PIL import Image
|
3 |
import torch
|
4 |
+
from diffusers import StableDiffusionPipeline
|
5 |
|
6 |
+
# Charger le pipeline de diffusion depuis Hugging Face
|
7 |
model_name = "Yaquv/rickthenpc"
|
8 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
9 |
+
|
10 |
+
try:
|
11 |
+
pipe = StableDiffusionPipeline.from_pretrained(model_name)
|
12 |
+
pipe = pipe.to(device)
|
13 |
+
except Exception as e:
|
14 |
+
print(f"Erreur lors du chargement du modèle : {e}")
|
15 |
+
pipe = None
|
16 |
|
17 |
# Fonction de génération et de post-traitement
|
18 |
def generate_image(prompt):
|
19 |
"""
|
20 |
Génère une image à partir du prompt en utilisant le modèle Hugging Face.
|
21 |
"""
|
22 |
+
if pipe is None:
|
23 |
+
return "Le modèle n'a pas pu être chargé."
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
|
25 |
+
try:
|
26 |
+
# Générer l'image
|
27 |
+
image = pipe(prompt).images[0]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
|
29 |
return image
|
30 |
|
|
|
43 |
|
44 |
# Lancer l'application
|
45 |
if __name__ == "__main__":
|
46 |
+
iface.launch()
|