Testk / lk.py
MoeZilla's picture
Create lk.py
1578456 verified
# Step 1: Install necessary packages
# !pip install stable-diffusion-cpp-python requests
# Step 2: Import required libraries
import os
import requests
from stable_diffusion_cpp import StableDiffusion
# Function to download a file and print status
def download_file(url, save_path):
print(f"Starting download for: {save_path}")
response = requests.get(url, stream=True)
response.raise_for_status() # Check for request errors
# Get the total file size for progress indication
total_size = int(response.headers.get('content-length', 0))
downloaded_size = 0
with open(save_path, 'wb') as f:
for data in response.iter_content(chunk_size=8192):
f.write(data)
downloaded_size += len(data)
# Print progress
print(f"Downloading {save_path}: {downloaded_size}/{total_size} bytes downloaded", end='\r')
print(f"\nDownloaded {save_path} successfully.")
# Step 3: Define model URLs
vae_url = "https://huggingface.co/pranavajay/flowgram/resolve/main/vae/diffusion_pytorch_model.safetensors"
model_url = "https://huggingface.co/pranavajay/flowgram/resolve/main/flowgram.safetensors"
clip_url = "https://huggingface.co/pranavajay/flowgram/resolve/main/text_encoder/clip_l.safetensors"
t5xxl_url = "https://huggingface.co/pranavajay/flowgram/resolve/main/text_encoder/t5xxl_fp16.safetensors"
# Step 4: Define local save paths
vae_path = "vae/diffusion_pytorch_model.safetensors"
model_path = "flowgram.safetensors"
clip_path = "text_encoder/clip_l.safetensors"
t5xxl_path = "text_encoder/t5xxl_fp16.safetensors"
# Step 5: Create directories if they don't exist
os.makedirs("vae", exist_ok=True)
os.makedirs("text_encoder", exist_ok=True)
# Step 6: Download the models if they do not exist
if not os.path.exists(vae_path):
download_file(vae_url, vae_path)
if not os.path.exists(model_path):
download_file(model_url, model_path)
if not os.path.exists(clip_path):
download_file(clip_url, clip_path)
if not os.path.exists(t5xxl_path):
download_file(t5xxl_url, t5xxl_path)
# Step 7: Initialize the StableDiffusion model
flowgram_diffusion = StableDiffusion(
diffusion_model_path=model_path,
clip_l_path=clip_path,
t5xxl_path=t5xxl_path,
vae_path=vae_path,
)
# Step 8: Function to generate an image from text
def generate_image(prompt, num_images=1, guidance_scale=7.5):
# Generate images
images = flowgram_diffusion.generate(prompt, num_images=num_images, guidance_scale=guidance_scale)
# Return the generated images
return images
# Step 9: Example usage
if __name__ == "__main__":
prompt = "A beautiful landscape with mountains and a river"
generated_images = generate_image(prompt)
# Save or display the images
for i, img in enumerate(generated_images):
img.save(f"generated_image_{i}.png") # Save each generated image
print(f"Saved generated_image_{i}.png")