Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,18 +3,19 @@ import random
|
|
| 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": (
|
| 15 |
-
"hyper-realistic image of {prompt}.
|
| 16 |
-
"
|
| 17 |
-
|
| 18 |
),
|
| 19 |
"Style Zero": ("{prompt}", ""),
|
| 20 |
}
|
|
@@ -23,6 +24,8 @@ def apply_style(style_name: str, positive: str, negative: str = "") -> Tuple[str
|
|
| 23 |
p, n = styles.get(style_name, styles[DEFAULT_STYLE_NAME])
|
| 24 |
return p.replace("{prompt}", positive), n + negative
|
| 25 |
|
|
|
|
|
|
|
| 26 |
def load_and_prepare_model():
|
| 27 |
model_id = "SG161222/RealVisXL_V5.0_Lightning"
|
| 28 |
pipe = StableDiffusionXLPipeline.from_pretrained(
|
|
@@ -34,19 +37,25 @@ def load_and_prepare_model():
|
|
| 34 |
pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config)
|
| 35 |
return pipe
|
| 36 |
|
|
|
|
| 37 |
model = load_and_prepare_model()
|
| 38 |
|
|
|
|
|
|
|
| 39 |
def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
|
| 40 |
if randomize_seed:
|
| 41 |
seed = random.randint(0, np.iinfo(np.int32).max)
|
| 42 |
return seed
|
| 43 |
|
|
|
|
|
|
|
| 44 |
def save_image(img):
|
| 45 |
unique_name = str(uuid.uuid4()) + ".png"
|
| 46 |
img.save(unique_name)
|
| 47 |
return unique_name
|
| 48 |
|
| 49 |
-
|
|
|
|
| 50 |
def generate(
|
| 51 |
prompt: str,
|
| 52 |
seed: int = 1,
|
|
@@ -58,26 +67,37 @@ def generate(
|
|
| 58 |
):
|
| 59 |
global model
|
| 60 |
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
with gr.Blocks(theme="soft") as demo:
|
| 82 |
# Block for "SNAPSCRIBE" centered at the top
|
| 83 |
with gr.Row():
|
|
@@ -85,6 +105,7 @@ with gr.Blocks(theme="soft") as demo:
|
|
| 85 |
gr.Markdown("<h1 style='text-align:center; color:white; font-weight:bold; text-decoration:underline;'>SNAPSCRIBE</h1>")
|
| 86 |
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>")
|
| 87 |
|
|
|
|
| 88 |
with gr.Column(scale=3):
|
| 89 |
prompt = gr.Textbox(
|
| 90 |
label="Input Prompt",
|
|
@@ -92,25 +113,20 @@ with gr.Blocks(theme="soft") as demo:
|
|
| 92 |
lines=2,
|
| 93 |
)
|
| 94 |
run_button = gr.Button("Generate Image")
|
| 95 |
-
|
| 96 |
-
# Example prompts box
|
| 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=
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
lines=5,
|
| 110 |
label="Sample Inputs",
|
| 111 |
interactive=False,
|
| 112 |
)
|
| 113 |
-
|
| 114 |
with gr.Column(scale=7):
|
| 115 |
result_image = gr.Image(
|
| 116 |
label="Generated Image",
|
|
@@ -118,10 +134,6 @@ with gr.Blocks(theme="soft") as demo:
|
|
| 118 |
elem_id="output_image",
|
| 119 |
height=600 # Increased the height by 100%
|
| 120 |
)
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
# with gr.Column(scale=12):
|
| 124 |
-
# 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</h1>")
|
| 125 |
|
| 126 |
run_button.click(
|
| 127 |
fn=generate,
|
|
@@ -129,25 +141,4 @@ with gr.Blocks(theme="soft") as demo:
|
|
| 129 |
outputs=[result_image],
|
| 130 |
)
|
| 131 |
|
| 132 |
-
|
| 133 |
-
# gr.HTML("""
|
| 134 |
-
# <style>
|
| 135 |
-
# .footer {
|
| 136 |
-
# position: relative;
|
| 137 |
-
# left: 0;
|
| 138 |
-
# bottom: 0;
|
| 139 |
-
# width: 100%;
|
| 140 |
-
# background-color: white;
|
| 141 |
-
# color: black;
|
| 142 |
-
# text-align: center;
|
| 143 |
-
# padding: 10px;
|
| 144 |
-
# margin-top: 20px;
|
| 145 |
-
# font-weight: bold;
|
| 146 |
-
# }
|
| 147 |
-
# </style>
|
| 148 |
-
# <div class="footer">
|
| 149 |
-
# <p>Developed using RealVisXL_V5.0_Lightning model with ❤ by Aklavya (Bucky)</p>
|
| 150 |
-
# </div>
|
| 151 |
-
# """)
|
| 152 |
-
|
| 153 |
-
demo.launch()
|
|
|
|
| 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": (
|
| 17 |
+
"hyper-realistic 8K image of {prompt}. ultra-detailed, lifelike, high-resolution, sharp, vibrant colors, photorealistic",
|
| 18 |
+
"cartoonish, low resolution, blurry, simplistic, abstract, deformed, ugly",
|
|
|
|
| 19 |
),
|
| 20 |
"Style Zero": ("{prompt}", ""),
|
| 21 |
}
|
|
|
|
| 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 |
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 |
):
|
| 68 |
global model
|
| 69 |
|
| 70 |
+
def run_model():
|
| 71 |
+
seed = int(randomize_seed_fn(seed, randomize_seed))
|
| 72 |
+
generator = torch.Generator(device=model.device).manual_seed(seed)
|
| 73 |
+
|
| 74 |
+
positive_prompt, negative_prompt = apply_style("3840 x 2160", prompt)
|
| 75 |
+
|
| 76 |
+
options = {
|
| 77 |
+
"prompt": [positive_prompt],
|
| 78 |
+
"negative_prompt": [negative_prompt],
|
| 79 |
+
"width": width,
|
| 80 |
+
"height": height,
|
| 81 |
+
"guidance_scale": guidance_scale,
|
| 82 |
+
"num_inference_steps": num_inference_steps,
|
| 83 |
+
"generator": generator,
|
| 84 |
+
"output_type": "pil",
|
| 85 |
+
}
|
| 86 |
+
|
| 87 |
+
images = model(**options).images
|
| 88 |
+
return save_image(images[0]) # Save and return the image path
|
| 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():
|
|
|
|
| 105 |
gr.Markdown("<h1 style='text-align:center; color:white; font-weight:bold; text-decoration:underline;'>SNAPSCRIBE</h1>")
|
| 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 |
with gr.Column(scale=3):
|
| 110 |
prompt = gr.Textbox(
|
| 111 |
label="Input Prompt",
|
|
|
|
| 113 |
lines=2,
|
| 114 |
)
|
| 115 |
run_button = gr.Button("Generate Image")
|
| 116 |
+
|
| 117 |
+
# Example prompts box
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
example_prompts = gr.Textbox(
|
| 119 |
+
value="Dew-covered spider web in morning sunlight, with blurred greenery\n"
|
| 120 |
+
"--------------------------------------------\n"
|
| 121 |
+
"Glass of cold water with ice cubes and condensation on a wooden table\n"
|
| 122 |
+
"--------------------------------------------\n"
|
| 123 |
+
"Coffee cup with latte art, steam rising, and morning sunlight\n"
|
| 124 |
+
"--------------------------------------------\n"
|
| 125 |
+
"Autumn forest with golden leaves, sunlight through trees, and a breeze",
|
| 126 |
lines=5,
|
| 127 |
label="Sample Inputs",
|
| 128 |
interactive=False,
|
| 129 |
)
|
|
|
|
| 130 |
with gr.Column(scale=7):
|
| 131 |
result_image = gr.Image(
|
| 132 |
label="Generated Image",
|
|
|
|
| 134 |
elem_id="output_image",
|
| 135 |
height=600 # Increased the height by 100%
|
| 136 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 137 |
|
| 138 |
run_button.click(
|
| 139 |
fn=generate,
|
|
|
|
| 141 |
outputs=[result_image],
|
| 142 |
)
|
| 143 |
|
| 144 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|