Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,139 +1,104 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import numpy as np
|
| 3 |
-
import random
|
| 4 |
-
import spaces
|
| 5 |
import torch
|
| 6 |
-
from diffusers import
|
| 7 |
-
from
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
#
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
""")
|
| 61 |
-
|
| 62 |
with gr.Row():
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
step=1,
|
| 83 |
-
value=0,
|
| 84 |
-
)
|
| 85 |
-
|
| 86 |
-
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
| 87 |
-
|
| 88 |
-
with gr.Row():
|
| 89 |
-
|
| 90 |
-
width = gr.Slider(
|
| 91 |
-
label="Width",
|
| 92 |
-
minimum=256,
|
| 93 |
-
maximum=MAX_IMAGE_SIZE,
|
| 94 |
-
step=32,
|
| 95 |
-
value=1024,
|
| 96 |
-
)
|
| 97 |
-
|
| 98 |
-
height = gr.Slider(
|
| 99 |
-
label="Height",
|
| 100 |
-
minimum=256,
|
| 101 |
-
maximum=MAX_IMAGE_SIZE,
|
| 102 |
-
step=32,
|
| 103 |
-
value=1024,
|
| 104 |
-
)
|
| 105 |
-
|
| 106 |
-
with gr.Row():
|
| 107 |
-
|
| 108 |
-
guidance_scale = gr.Slider(
|
| 109 |
-
label="Guidance Scale",
|
| 110 |
-
minimum=1,
|
| 111 |
-
maximum=15,
|
| 112 |
-
step=0.1,
|
| 113 |
-
value=3.5,
|
| 114 |
-
)
|
| 115 |
-
|
| 116 |
-
num_inference_steps = gr.Slider(
|
| 117 |
-
label="Number of inference steps",
|
| 118 |
-
minimum=1,
|
| 119 |
-
maximum=50,
|
| 120 |
-
step=1,
|
| 121 |
-
value=28,
|
| 122 |
-
)
|
| 123 |
-
|
| 124 |
-
gr.Examples(
|
| 125 |
-
examples = examples,
|
| 126 |
-
fn = infer,
|
| 127 |
-
inputs = [prompt],
|
| 128 |
-
outputs = [result, seed],
|
| 129 |
-
cache_examples="lazy"
|
| 130 |
-
)
|
| 131 |
-
|
| 132 |
-
gr.on(
|
| 133 |
-
triggers=[run_button.click, prompt.submit],
|
| 134 |
-
fn = infer,
|
| 135 |
-
inputs = [prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
|
| 136 |
-
outputs = [result, seed]
|
| 137 |
-
)
|
| 138 |
-
|
| 139 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
| 2 |
import torch
|
| 3 |
+
from diffusers import StableDiffusionXLPipeline
|
| 4 |
+
from PIL import Image as PILImage
|
| 5 |
+
import concurrent.futures
|
| 6 |
+
|
| 7 |
+
# Model cache to avoid reloading the model multiple times
|
| 8 |
+
model_cache = {}
|
| 9 |
+
|
| 10 |
+
def load_model(model_name):
|
| 11 |
+
# Check if the model is already cached to avoid reloading every time
|
| 12 |
+
if model_name in model_cache:
|
| 13 |
+
return model_cache[model_name]
|
| 14 |
+
|
| 15 |
+
print(f"Loading model: {model_name}")
|
| 16 |
+
try:
|
| 17 |
+
# Select device (CPU only for ZeroGPU plan)
|
| 18 |
+
device = "cpu" # Set to CPU, as you don't have GPU access
|
| 19 |
+
|
| 20 |
+
# Load the model with float32 (since float16 is not supported on CPU)
|
| 21 |
+
model = StableDiffusionXLPipeline.from_pretrained(model_name, torch_dtype=torch.float32)
|
| 22 |
+
model.to(device)
|
| 23 |
+
|
| 24 |
+
# Cache the model for future use
|
| 25 |
+
model_cache[model_name] = model
|
| 26 |
+
print("Model loaded successfully.")
|
| 27 |
+
return model
|
| 28 |
+
except Exception as e:
|
| 29 |
+
print(f"Error loading model: {e}")
|
| 30 |
+
return None
|
| 31 |
+
|
| 32 |
+
# Function to generate the image with a timeout
|
| 33 |
+
def generate_image_with_timeout(prompt):
|
| 34 |
+
timeout = 180 # Timeout after 180 seconds
|
| 35 |
+
|
| 36 |
+
try:
|
| 37 |
+
# Use ThreadPoolExecutor to handle the timeout
|
| 38 |
+
with concurrent.futures.ThreadPoolExecutor() as executor:
|
| 39 |
+
future = executor.submit(generate_image, prompt)
|
| 40 |
+
return future.result(timeout=timeout) # Will raise TimeoutError if the process exceeds timeout
|
| 41 |
+
|
| 42 |
+
except concurrent.futures.TimeoutError:
|
| 43 |
+
return "Error: The image generation timed out. Please try again."
|
| 44 |
+
|
| 45 |
+
# Function to generate the image
|
| 46 |
+
def generate_image(prompt):
|
| 47 |
+
model_name = "SG161222/RealVisXL_V5.0_Lightning"
|
| 48 |
+
model = load_model(model_name)
|
| 49 |
+
|
| 50 |
+
if model is None:
|
| 51 |
+
return "Error loading the model."
|
| 52 |
+
|
| 53 |
+
try:
|
| 54 |
+
# Generate the image from the prompt
|
| 55 |
+
with torch.no_grad():
|
| 56 |
+
output = model(prompt)
|
| 57 |
+
image = output.images[0] # Assuming the first image is the one we need
|
| 58 |
+
image = PILImage.fromarray(image) # Convert to PIL image format for Gradio
|
| 59 |
+
return image
|
| 60 |
+
except Exception as e:
|
| 61 |
+
print(f"Error generating image: {e}")
|
| 62 |
+
return "Error generating the image."
|
| 63 |
+
|
| 64 |
+
# Define the Gradio interface using gr.Blocks
|
| 65 |
+
def create_gradio_interface():
|
| 66 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 67 |
+
gr.Markdown("""
|
| 68 |
+
<h1 style="
|
| 69 |
+
text-align: center;
|
| 70 |
+
color: white;
|
| 71 |
+
font-weight: bold;
|
| 72 |
+
text-transform: uppercase;
|
| 73 |
+
text-decoration: underline;
|
| 74 |
+
margin-top: 30px;
|
| 75 |
+
font-family: 'Arial', sans-serif;
|
| 76 |
+
background: linear-gradient(45deg, #ff6b6b, #f06595);
|
| 77 |
+
padding: 10px 20px;
|
| 78 |
+
border-radius: 15px;
|
| 79 |
+
box-shadow: 0px 4px 8px rgba(0, 0, 0, 0.3);
|
| 80 |
+
">
|
| 81 |
+
SNAPSCRIBE
|
| 82 |
+
</h1>
|
| 83 |
""")
|
| 84 |
+
|
| 85 |
with gr.Row():
|
| 86 |
+
with gr.Column(scale=3, min_width=300): # Changed scale to integer
|
| 87 |
+
prompt_input = gr.Textbox(label="Enter your prompt here", placeholder="e.g., A futuristic city skyline")
|
| 88 |
+
submit_button = gr.Button("Generate Image")
|
| 89 |
+
|
| 90 |
+
with gr.Column(scale=7, min_width=600): # Changed scale to integer
|
| 91 |
+
output_image = gr.Image(label="Generated Image", height=640)
|
| 92 |
+
|
| 93 |
+
submit_button.click(fn=generate_image_with_timeout, inputs=[prompt_input], outputs=output_image)
|
| 94 |
+
|
| 95 |
+
gr.Markdown("""
|
| 96 |
+
<div style="position: relative; left: 0; bottom: 0; width: 100%; background-color: #0B0F19; color: white; text-align: center; padding: 10px 0;">
|
| 97 |
+
<p>Developed with ❤ by Aklavya (Bucky)</p>
|
| 98 |
+
</div>
|
| 99 |
+
""")
|
| 100 |
+
|
| 101 |
+
demo.launch() # Removed `share=True`
|
| 102 |
+
|
| 103 |
+
# Launch the Gradio interface
|
| 104 |
+
create_gradio_interface()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|