Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
|
3 |
+
from diffusers import UniDiffuserPipeline
|
4 |
+
from diffusers.utils import load_image
|
5 |
+
|
6 |
+
device = "cuda"
|
7 |
+
model_id_or_path = "thu-ml/unidiffuser-v1"
|
8 |
+
pipe = UniDiffuserPipeline.from_pretrained(model_id_or_path, torch_dtype=torch.float16)
|
9 |
+
pipe.to(device)
|
10 |
+
|
11 |
+
# Image variation can be performed with an image-to-text generation followed by a text-to-image generation:
|
12 |
+
# 1. Image-to-text generation
|
13 |
+
image_url = "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/unidiffuser/unidiffuser_example_image.jpg"
|
14 |
+
init_image = load_image(image_url).resize((512, 512))
|
15 |
+
|
16 |
+
sample = pipe(image=init_image, num_inference_steps=20, guidance_scale=8.0)
|
17 |
+
i2t_text = sample.text[0]
|
18 |
+
print(i2t_text)
|
19 |
+
|
20 |
+
# 2. Text-to-image generation
|
21 |
+
sample = pipe(prompt=i2t_text, num_inference_steps=20, guidance_scale=8.0)
|
22 |
+
final_image = sample.images[0]
|
23 |
+
final_image.save("unidiffuser_image_variation_sample.png")
|