Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,14 +3,12 @@ import random
|
|
| 3 |
import uuid
|
| 4 |
import gradio as gr
|
| 5 |
import numpy as np
|
| 6 |
-
import torch
|
| 7 |
-
import concurrent.futures
|
| 8 |
from PIL import Image
|
|
|
|
|
|
|
| 9 |
from diffusers import StableDiffusionXLPipeline, EulerAncestralDiscreteScheduler
|
| 10 |
from typing import Tuple
|
| 11 |
|
| 12 |
-
|
| 13 |
-
# Function to apply styles
|
| 14 |
def apply_style(style_name: str, positive: str, negative: str = "") -> Tuple[str, str]:
|
| 15 |
styles = {
|
| 16 |
"3840 x 2160": (
|
|
@@ -24,8 +22,6 @@ def apply_style(style_name: str, positive: str, negative: str = "") -> Tuple[str
|
|
| 24 |
p, n = styles.get(style_name, styles[DEFAULT_STYLE_NAME])
|
| 25 |
return p.replace("{prompt}", positive), n + negative
|
| 26 |
|
| 27 |
-
|
| 28 |
-
# Function to load and prepare the model
|
| 29 |
def load_and_prepare_model():
|
| 30 |
model_id = "SG161222/RealVisXL_V5.0_Lightning"
|
| 31 |
pipe = StableDiffusionXLPipeline.from_pretrained(
|
|
@@ -37,25 +33,19 @@ def load_and_prepare_model():
|
|
| 37 |
pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config)
|
| 38 |
return pipe
|
| 39 |
|
| 40 |
-
|
| 41 |
model = load_and_prepare_model()
|
| 42 |
|
| 43 |
-
|
| 44 |
-
# Function to randomize or apply a fixed seed
|
| 45 |
def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
|
| 46 |
if randomize_seed:
|
| 47 |
seed = random.randint(0, np.iinfo(np.int32).max)
|
| 48 |
return seed
|
| 49 |
|
| 50 |
-
|
| 51 |
-
# Function to save generated images
|
| 52 |
def save_image(img):
|
| 53 |
unique_name = str(uuid.uuid4()) + ".png"
|
| 54 |
img.save(unique_name)
|
| 55 |
return unique_name
|
| 56 |
|
| 57 |
-
|
| 58 |
-
# Main generation function with timeout
|
| 59 |
def generate(
|
| 60 |
prompt: str,
|
| 61 |
seed: int = 1,
|
|
@@ -67,37 +57,26 @@ def generate(
|
|
| 67 |
):
|
| 68 |
global model
|
| 69 |
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
# Use a timeout for CPU-bound operations
|
| 91 |
-
with concurrent.futures.ThreadPoolExecutor() as executor:
|
| 92 |
-
future = executor.submit(run_model)
|
| 93 |
-
try:
|
| 94 |
-
image_path = future.result(timeout=180) # Set timeout to 180 seconds
|
| 95 |
-
return image_path
|
| 96 |
-
except concurrent.futures.TimeoutError:
|
| 97 |
-
return "Error: Image generation timed out. Please try again with different settings or on a GPU-enabled setup."
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
# Gradio interface setup
|
| 101 |
with gr.Blocks(theme="soft") as demo:
|
| 102 |
# Block for "SNAPSCRIBE" centered at the top
|
| 103 |
with gr.Row():
|
|
@@ -106,34 +85,38 @@ with gr.Blocks(theme="soft") as demo:
|
|
| 106 |
gr.Markdown("<h2 style='text-align:center; color:white; font-weight:bold; text-decoration:underline;'>Developed using RealVisXL_V5.0_Lightning model with ❤ by Aklavya</h2>")
|
| 107 |
|
| 108 |
with gr.Row():
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 137 |
|
| 138 |
run_button.click(
|
| 139 |
fn=generate,
|
|
@@ -141,4 +124,25 @@ with gr.Blocks(theme="soft") as demo:
|
|
| 141 |
outputs=[result_image],
|
| 142 |
)
|
| 143 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
demo.launch()
|
|
|
|
| 3 |
import uuid
|
| 4 |
import gradio as gr
|
| 5 |
import numpy as np
|
|
|
|
|
|
|
| 6 |
from PIL import Image
|
| 7 |
+
import spaces
|
| 8 |
+
import torch
|
| 9 |
from diffusers import StableDiffusionXLPipeline, EulerAncestralDiscreteScheduler
|
| 10 |
from typing import Tuple
|
| 11 |
|
|
|
|
|
|
|
| 12 |
def apply_style(style_name: str, positive: str, negative: str = "") -> Tuple[str, str]:
|
| 13 |
styles = {
|
| 14 |
"3840 x 2160": (
|
|
|
|
| 22 |
p, n = styles.get(style_name, styles[DEFAULT_STYLE_NAME])
|
| 23 |
return p.replace("{prompt}", positive), n + negative
|
| 24 |
|
|
|
|
|
|
|
| 25 |
def load_and_prepare_model():
|
| 26 |
model_id = "SG161222/RealVisXL_V5.0_Lightning"
|
| 27 |
pipe = StableDiffusionXLPipeline.from_pretrained(
|
|
|
|
| 33 |
pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config)
|
| 34 |
return pipe
|
| 35 |
|
|
|
|
| 36 |
model = load_and_prepare_model()
|
| 37 |
|
|
|
|
|
|
|
| 38 |
def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
|
| 39 |
if randomize_seed:
|
| 40 |
seed = random.randint(0, np.iinfo(np.int32).max)
|
| 41 |
return seed
|
| 42 |
|
|
|
|
|
|
|
| 43 |
def save_image(img):
|
| 44 |
unique_name = str(uuid.uuid4()) + ".png"
|
| 45 |
img.save(unique_name)
|
| 46 |
return unique_name
|
| 47 |
|
| 48 |
+
@spaces.GPU(duration=60, enable_queue=True)
|
|
|
|
| 49 |
def generate(
|
| 50 |
prompt: str,
|
| 51 |
seed: int = 1,
|
|
|
|
| 57 |
):
|
| 58 |
global model
|
| 59 |
|
| 60 |
+
seed = int(randomize_seed_fn(seed, randomize_seed))
|
| 61 |
+
generator = torch.Generator(device=model.device).manual_seed(seed)
|
| 62 |
+
|
| 63 |
+
positive_prompt, negative_prompt = apply_style("3840 x 2160", prompt)
|
| 64 |
+
|
| 65 |
+
options = {
|
| 66 |
+
"prompt": [positive_prompt],
|
| 67 |
+
"negative_prompt": [negative_prompt],
|
| 68 |
+
"width": width,
|
| 69 |
+
"height": height,
|
| 70 |
+
"guidance_scale": guidance_scale,
|
| 71 |
+
"num_inference_steps": num_inference_steps,
|
| 72 |
+
"generator": generator,
|
| 73 |
+
"output_type": "pil",
|
| 74 |
+
}
|
| 75 |
+
|
| 76 |
+
images = model(**options).images
|
| 77 |
+
image_path = save_image(images[0]) # Saving the first generated image
|
| 78 |
+
return image_path
|
| 79 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
with gr.Blocks(theme="soft") as demo:
|
| 81 |
# Block for "SNAPSCRIBE" centered at the top
|
| 82 |
with gr.Row():
|
|
|
|
| 85 |
gr.Markdown("<h2 style='text-align:center; color:white; font-weight:bold; text-decoration:underline;'>Developed using RealVisXL_V5.0_Lightning model with ❤ by Aklavya</h2>")
|
| 86 |
|
| 87 |
with gr.Row():
|
| 88 |
+
with gr.Column(scale=3):
|
| 89 |
+
prompt = gr.Textbox(
|
| 90 |
+
label="Input Prompt",
|
| 91 |
+
placeholder="Describe the image you want to create",
|
| 92 |
+
lines=2,
|
| 93 |
+
)
|
| 94 |
+
run_button = gr.Button("Generate Image")
|
| 95 |
+
|
| 96 |
+
# Example prompts box with Copy to Clipboard button
|
| 97 |
+
example_prompts_text = (
|
| 98 |
+
"Dew-covered spider web in morning sunlight, with blurred greenery\n"
|
| 99 |
+
"--------------------------------------------\n"
|
| 100 |
+
"Glass of cold water with ice cubes and condensation on a wooden table\n"
|
| 101 |
+
"--------------------------------------------\n"
|
| 102 |
+
"Coffee cup with latte art, steam rising, and morning sunlight\n"
|
| 103 |
+
"--------------------------------------------\n"
|
| 104 |
+
"Autumn forest with golden leaves, sunlight through trees, and a breeze"
|
| 105 |
+
)
|
| 106 |
+
|
| 107 |
+
example_prompts = gr.Textbox(
|
| 108 |
+
value=example_prompts_text,
|
| 109 |
+
lines=5,
|
| 110 |
+
label="Sample Inputs",
|
| 111 |
+
interactive=False,
|
| 112 |
+
)
|
| 113 |
+
with gr.Column(scale=7):
|
| 114 |
+
result_image = gr.Image(
|
| 115 |
+
label="Generated Image",
|
| 116 |
+
type="filepath",
|
| 117 |
+
elem_id="output_image",
|
| 118 |
+
height=1024 # Increased the height by 100%
|
| 119 |
+
)
|
| 120 |
|
| 121 |
run_button.click(
|
| 122 |
fn=generate,
|
|
|
|
| 124 |
outputs=[result_image],
|
| 125 |
)
|
| 126 |
|
| 127 |
+
# # Footer with custom style and text
|
| 128 |
+
# gr.Markdown("""
|
| 129 |
+
# <style>
|
| 130 |
+
# .footer {
|
| 131 |
+
# position: relative;
|
| 132 |
+
# left: 0;
|
| 133 |
+
# bottom: 0;
|
| 134 |
+
# width: 100%;
|
| 135 |
+
# background-color: white;
|
| 136 |
+
# color: black;
|
| 137 |
+
# text-align: center;
|
| 138 |
+
# }
|
| 139 |
+
# #output_image {
|
| 140 |
+
# height: calc(100% + 512px); /* Dynamically increases height by 100% */
|
| 141 |
+
# }
|
| 142 |
+
# </style>
|
| 143 |
+
# <div class="footer">
|
| 144 |
+
# <p>Developed with ❤ by Aklavya(Bucky)</p>
|
| 145 |
+
# </div>
|
| 146 |
+
# """)
|
| 147 |
+
|
| 148 |
demo.launch()
|