Update app.py
Browse files
app.py
CHANGED
|
@@ -1,26 +1,29 @@
|
|
| 1 |
-
# Installer les dépendances nécessaires
|
| 2 |
-
!pip install torch torchvision transformers diffusers
|
| 3 |
-
|
| 4 |
import torch
|
| 5 |
-
from transformers import
|
| 6 |
-
from diffusers import StableDiffusionPipeline
|
| 7 |
import matplotlib.pyplot as plt
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
-
#
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
image = pipe(prompt).images[0]
|
| 18 |
-
image.save(output_path)
|
| 19 |
-
plt.imshow(image)
|
| 20 |
-
plt.axis('off')
|
| 21 |
-
plt.show()
|
| 22 |
-
print(f"Generated image saved as '{output_path}'")
|
| 23 |
|
| 24 |
-
#
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import torch
|
| 2 |
+
from transformers import StableDiffusionPipeline
|
|
|
|
| 3 |
import matplotlib.pyplot as plt
|
| 4 |
|
| 5 |
+
# Vérifiez si CUDA est disponible
|
| 6 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 7 |
+
|
| 8 |
+
# Chargez le modèle Stable Diffusion
|
| 9 |
+
model_id = "CompVis/stable-diffusion-v1-4" # Modèle à partir de Hugging Face
|
| 10 |
+
pipe = StableDiffusionPipeline.from_pretrained(model_id).to(device)
|
| 11 |
+
|
| 12 |
+
# Fonction pour générer des images
|
| 13 |
+
def generate_image(prompt, num_images=1):
|
| 14 |
+
images = []
|
| 15 |
+
for _ in range(num_images):
|
| 16 |
+
with torch.no_grad():
|
| 17 |
+
image = pipe(prompt).images[0]
|
| 18 |
+
images.append(image)
|
| 19 |
+
return images
|
| 20 |
|
| 21 |
+
# Génération d'images à partir d'un prompt
|
| 22 |
+
prompt = "a fantasy landscape with mountains and a river"
|
| 23 |
+
generated_images = generate_image(prompt, num_images=1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
+
# Affichage de l'image générée
|
| 26 |
+
for img in generated_images:
|
| 27 |
+
plt.imshow(img)
|
| 28 |
+
plt.axis('off')
|
| 29 |
+
plt.show()
|