Create hk.py
Browse files
hk.py
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import requests
|
| 3 |
+
from stable_diffusion_cpp import StableDiffusion
|
| 4 |
+
|
| 5 |
+
# Function to download a file
|
| 6 |
+
def download_file(url, save_path):
|
| 7 |
+
response = requests.get(url)
|
| 8 |
+
response.raise_for_status() # Check for request errors
|
| 9 |
+
with open(save_path, 'wb') as f:
|
| 10 |
+
f.write(response.content)
|
| 11 |
+
|
| 12 |
+
# Define model URLs
|
| 13 |
+
vae_url = "https://huggingface.co/pranavajay/flowgram/resolve/main/vae/diffusion_pytorch_model.safetensors"
|
| 14 |
+
model_url = "https://huggingface.co/pranavajay/flowgram/resolve/main/flowgram.safetensors"
|
| 15 |
+
clip_url = "https://huggingface.co/pranavajay/flowgram/resolve/main/text_encoder/clip_l.safetensors"
|
| 16 |
+
t5xxl_url = "https://huggingface.co/pranavajay/flowgram/resolve/main/text_encoder/t5xxl_fp16.safetensors"
|
| 17 |
+
|
| 18 |
+
# Define local save paths
|
| 19 |
+
vae_path = "vae/diffusion_pytorch_model.safetensors"
|
| 20 |
+
model_path = "flowgram.safetensors"
|
| 21 |
+
clip_path = "text_encoder/clip_l.safetensors"
|
| 22 |
+
t5xxl_path = "text_encoder/t5xxl_fp16.safetensors"
|
| 23 |
+
|
| 24 |
+
# Create directories if they don't exist
|
| 25 |
+
os.makedirs("vae", exist_ok=True)
|
| 26 |
+
os.makedirs("text_encoder", exist_ok=True)
|
| 27 |
+
|
| 28 |
+
# Download the models if they do not exist
|
| 29 |
+
if not os.path.exists(vae_path):
|
| 30 |
+
download_file(vae_url, vae_path)
|
| 31 |
+
if not os.path.exists(model_path):
|
| 32 |
+
download_file(model_url, model_path)
|
| 33 |
+
if not os.path.exists(clip_path):
|
| 34 |
+
download_file(clip_url, clip_path)
|
| 35 |
+
if not os.path.exists(t5xxl_path):
|
| 36 |
+
download_file(t5xxl_url, t5xxl_path)
|
| 37 |
+
|
| 38 |
+
# Initialize the StableDiffusion model
|
| 39 |
+
flowgram_diffusion = StableDiffusion(
|
| 40 |
+
diffusion_model_path=model_path,
|
| 41 |
+
clip_l_path=clip_path,
|
| 42 |
+
t5xxl_path=t5xxl_path,
|
| 43 |
+
vae_path=vae_path,
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
# Function to generate an image from text
|
| 47 |
+
def generate_image(prompt, num_images=1, guidance_scale=7.5):
|
| 48 |
+
# Generate images
|
| 49 |
+
images = flowgram_diffusion.generate(prompt, num_images=num_images, guidance_scale=guidance_scale)
|
| 50 |
+
|
| 51 |
+
# Return the generated images
|
| 52 |
+
return images
|
| 53 |
+
|
| 54 |
+
# Example usage
|
| 55 |
+
if __name__ == "__main__":
|
| 56 |
+
prompt = "A beautiful landscape with mountains and a river"
|
| 57 |
+
generated_images = generate_image(prompt)
|
| 58 |
+
|
| 59 |
+
# Save or display the images
|
| 60 |
+
for i, img in enumerate(generated_images):
|
| 61 |
+
img.save(f"generated_image_{i}.png") # Save each generated image
|