Spaces:
Sleeping
Sleeping
Update space
Browse files- app.py +19 -54
- requirements.txt +5 -6
app.py
CHANGED
|
@@ -1,61 +1,26 @@
|
|
| 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 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
if __name__ == "__main__":
|
| 56 |
-
|
| 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)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import torch
|
| 2 |
from diffusers import StableDiffusionPipeline
|
| 3 |
+
import gradio as gr
|
| 4 |
|
| 5 |
+
def generate_image(prompt):
|
|
|
|
| 6 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 7 |
model_id = "CompVis/stable-diffusion-v1-4"
|
| 8 |
pipe = StableDiffusionPipeline.from_pretrained(model_id).to(device)
|
| 9 |
+
|
| 10 |
+
with torch.no_grad():
|
| 11 |
+
image = pipe(prompt).images[0]
|
| 12 |
+
|
| 13 |
+
return image
|
| 14 |
+
|
| 15 |
+
# Define the Gradio interface
|
| 16 |
+
interface = gr.Interface(
|
| 17 |
+
fn=generate_image,
|
| 18 |
+
inputs="text",
|
| 19 |
+
outputs="image",
|
| 20 |
+
title="Stable Diffusion Image Generator",
|
| 21 |
+
description="Generate images from text prompts using Stable Diffusion."
|
| 22 |
+
)
|
| 23 |
+
|
| 24 |
+
# Launch the app
|
| 25 |
if __name__ == "__main__":
|
| 26 |
+
interface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
requirements.txt
CHANGED
|
@@ -1,6 +1,5 @@
|
|
| 1 |
-
|
| 2 |
-
diffusers
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
xformers
|
|
|
|
| 1 |
+
torch==2.0.1
|
| 2 |
+
diffusers==0.21.1
|
| 3 |
+
transformers==4.32.1
|
| 4 |
+
gradio==3.32.0
|
| 5 |
+
accelerate==0.22.0
|
|
|