Spaces:
Sleeping
Sleeping
Update space with app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# import os
|
| 2 |
+
# import sys
|
| 3 |
+
|
| 4 |
+
# import torch
|
| 5 |
+
# import torchvision.utils as vutils
|
| 6 |
+
|
| 7 |
+
# from models.clip_encoder import CLIPModel
|
| 8 |
+
# from models.denoising_network import DenoisingNetwork
|
| 9 |
+
# from utils.sde_functions import SDE, add_noise # Import SDE class
|
| 10 |
+
|
| 11 |
+
# sys.path.append(os.path.abspath(os.path.join(os.path.dirname(_file_), '..')))
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
# def sample(text, checkpoint_path='checkpoints/model_epoch_50.pth', output_path='generated_images/sample.png', num_steps=1000, guidance_scale=3.5):
|
| 15 |
+
# device = 'cuda' if torch.cuda.is_available() else 'cpu'
|
| 16 |
+
# model = DenoisingNetwork().to(device)
|
| 17 |
+
# model.load_state_dict(torch.load(checkpoint_path))
|
| 18 |
+
# model.eval()
|
| 19 |
+
|
| 20 |
+
# clip_model = CLIPModel()
|
| 21 |
+
# text_embedding = clip_model.encode_text(text).to(device)
|
| 22 |
+
|
| 23 |
+
# # Initialize SDE instance
|
| 24 |
+
# sde = SDE(noise_type="additive", noise_scale=0.1, guidance_scale=guidance_scale)
|
| 25 |
+
|
| 26 |
+
# with torch.no_grad():
|
| 27 |
+
# # Start with random noise
|
| 28 |
+
# x = torch.randn(1, 3, 32, 32).to(device)
|
| 29 |
+
# for t in reversed(range(1, num_steps + 1)):
|
| 30 |
+
# time_factor = t / num_steps
|
| 31 |
+
# x = add_noise(x, time_factor) # Adding noise following the new schedule
|
| 32 |
+
# x = sde.reverse_sde(x, time_factor, model) # Call reverse_sde as a method of the SDE instance
|
| 33 |
+
|
| 34 |
+
# vutils.save_image(x, output_path, normalize=True)
|
| 35 |
+
# print(f"Generated image saved at {output_path}")
|
| 36 |
+
|
| 37 |
+
# if _name_ == "_main_":
|
| 38 |
+
# sample("A cat riding a skateboard")
|
| 39 |
+
# scripts/sample.py
|
| 40 |
+
import torch
|
| 41 |
+
from diffusers import StableDiffusionPipeline
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
def generate_images(text_prompts, output_dir='generated_images/'):
|
| 45 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 46 |
+
model_id = "CompVis/stable-diffusion-v1-4"
|
| 47 |
+
pipe = StableDiffusionPipeline.from_pretrained(model_id).to(device)
|
| 48 |
+
|
| 49 |
+
for i, prompt in enumerate(text_prompts):
|
| 50 |
+
with torch.no_grad():
|
| 51 |
+
image = pipe(prompt).images[0]
|
| 52 |
+
image.save(f"{output_dir}/sample_{i}.png")
|
| 53 |
+
print(f"Generated image {i+1} saved at {output_dir}/sample_{i}.png")
|
| 54 |
+
|
| 55 |
+
if __name__ == "__main__":
|
| 56 |
+
prompts = [
|
| 57 |
+
"A cat riding a skateboard",
|
| 58 |
+
"A futuristic city skyline at sunset",
|
| 59 |
+
"A serene mountain landscape in spring"
|
| 60 |
+
]
|
| 61 |
+
generate_images(prompts)
|