Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Installer les dépendances nécessaires
|
| 2 |
+
!pip install torch torchvision transformers diffusers
|
| 3 |
+
|
| 4 |
+
import torch
|
| 5 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 6 |
+
from diffusers import StableDiffusionPipeline
|
| 7 |
+
import matplotlib.pyplot as plt
|
| 8 |
+
|
| 9 |
+
# Chargement du modèle de génération d'images Stable Diffusion depuis Hugging Face
|
| 10 |
+
model_id = "CompVis/stable-diffusion-v1-4" # Stable Diffusion v1.4
|
| 11 |
+
pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16)
|
| 12 |
+
pipe = pipe.to("cuda") # Utiliser le GPU si disponible
|
| 13 |
+
|
| 14 |
+
# Fonction pour générer et sauvegarder une image d'animal
|
| 15 |
+
def generate_animal_image(prompt, output_path="animal_image.png"):
|
| 16 |
+
with torch.no_grad():
|
| 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 |
+
# Exemple d'utilisation
|
| 25 |
+
prompt = "a cute animal in a forest"
|
| 26 |
+
generate_animal_image(prompt)
|