hostin commited on
Commit
2e2639d
1 Parent(s): 8be9ab9

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +79 -0
app.py ADDED
@@ -0,0 +1,79 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # @title 📸 Image Generation (multimodel + Gradio Web Interface)
2
+
3
+ # Install Dependencies
4
+ !pip install transformers torch diffusers accelerate invisible_watermark safetensors huggingface-hub gradio --quiet
5
+
6
+ from diffusers import DiffusionPipeline, StableDiffusionPipeline
7
+ import torch
8
+ from gradio.components import Gallery
9
+ from PIL import Image
10
+ from IPython.display import display
11
+ import os
12
+ from datetime import datetime
13
+ import gradio as gr
14
+
15
+ # Function to generate and display images
16
+ def generate_and_display_images(model_selection, scenery, style, height, width, num_images=2, n_steps=50, high_noise_frac=0.5, guidance_scale=2.6, negative_prompt="", seed=None):
17
+ if seed is None or seed == '':
18
+ seed = torch.randint(low=0, high=2**32, size=(1,)).item()
19
+ else:
20
+ try:
21
+ seed = int(seed)
22
+ except ValueError:
23
+ return "Invalid seed value. Seed must be an integer."
24
+ torch.manual_seed(seed)
25
+
26
+ prompt = f"Scenery: {scenery}; Style: {style}"
27
+
28
+ generated_images = []
29
+ if model_selection == "dreamlike-art/dreamlike-photoreal-2.0":
30
+ model = StableDiffusionPipeline.from_pretrained(model_selection, torch_dtype=torch.float16).to("cuda")
31
+ for _ in range(num_images):
32
+ image = model(prompt=prompt, num_inference_steps=n_steps, guidance_scale=guidance_scale, negative_prompt=negative_prompt, height=height, width=width).images[0]
33
+ generated_images.append(image)
34
+ else:
35
+ base = DiffusionPipeline.from_pretrained(model_selection, torch_dtype=torch.float16, use_auth_token=True).to("cuda")
36
+ for _ in range(num_images):
37
+ if "refiner" in model_selection:
38
+ image_latent = base(prompt=prompt, num_inference_steps=n_steps, denoising_end=high_noise_frac, output_type="latent").images
39
+ image = image_latent[0] # Placeholder for actual refiner step
40
+ else:
41
+ image = base(prompt=prompt, num_inference_steps=n_steps, guidance_scale=guidance_scale, negative_prompt=negative_prompt, height=height, width=width).images[0]
42
+ generated_images.append(image)
43
+
44
+ # Save images and return file paths for Gradio display
45
+ file_paths = []
46
+ for i, image in enumerate(generated_images):
47
+ timestamp = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
48
+ filename = f"{seed}_{timestamp}_{i}.jpg"
49
+ image.save(filename)
50
+ file_paths.append(filename)
51
+
52
+ return file_paths
53
+
54
+
55
+
56
+ # Define Gradio interface
57
+ iface = gr.Interface(
58
+ fn=generate_and_display_images,
59
+ inputs=[
60
+ gr.components.Dropdown(choices=["stabilityai/sdxl-turbo", "stabilityai/stable-diffusion-xl-base-1.0", "runwayml/stable-diffusion-v1-5", "dreamlike-art/dreamlike-photoreal-2.0", "Kardbord/stable-diffusion-v1-5-unsafe"], label="Model Selection"),
61
+ gr.components.Textbox(label="Scenery", placeholder="Describe the scenery you want in the image"),
62
+ gr.components.Textbox(label="Style", placeholder="Describe the style of the image (e.g., photorealistic, liminal, dark)"),
63
+ gr.components.Slider(minimum=1, maximum=2048, step=1, value=1024, label="Height"),
64
+ gr.components.Slider(minimum=1, maximum=2048, step=1, value=576, label="Width"),
65
+ gr.components.Number(value=10, label="Number of Images"),
66
+ gr.components.Slider(minimum=0, maximum=60, step=1, value=30, label="Number of Inference Steps"),
67
+ gr.components.Slider(minimum=0.0, maximum=1.0, step=0.01, value=0.16, label="High Noise Fraction"),
68
+ gr.components.Slider(minimum=0.0, maximum=10.0, step=0.1, value=8, label="Guidance Scale"),
69
+ gr.components.Textbox(value="", label="Negative Prompt"),
70
+ gr.components.Textbox(value=None, label="Seed (Optional)")
71
+ ],
72
+ outputs=Gallery(label="Generated Images"),
73
+ examples=[["dreamlike-art/dreamlike-photoreal-2.0", "scenery : melting flesh", "style : (((photorealistic))), liminal, cryptic, cinematic, highly detailed, sharp focus, dark, creepy, weirdcore", 1024, 576, 10, 30, 0.16, 8, "2D || naked || Low Quality || text logos || watermarks || signatures || out of frame || jpeg artifacts || ugly || poorly drawn || extra limbs || extra hands || extra feet || backwards limbs || extra fingers || extra toes || unrealistic, incorrect, bad anatomy || cut off body pieces || strange body positions || impossible body positioning || Mismatched eyes || cross eyed || crooked face || crooked lips || unclear || undefined || mutations || deformities || off center || poor_composition || duplicate faces, blurry, blurred, unclear, deformed anatomy, deformed face, crazy eyes, bad hands, deformed body", None]],
74
+ title="Image Generation Tool",
75
+ description="Generate images using various diffusion models."
76
+ )
77
+
78
+ # Launch the interface
79
+ iface.launch(share=True, debug=True)