Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from diffusers import DDPMPipeline
|
| 3 |
import torch
|
| 4 |
-
from PIL import Image
|
| 5 |
import os
|
| 6 |
|
| 7 |
# --- CONFIGURATION ---
|
|
@@ -11,6 +11,7 @@ DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
|
| 11 |
print(f"π Initializing Stable-Lime Protocol on {DEVICE}...")
|
| 12 |
|
| 13 |
# --- LOAD MODEL ---
|
|
|
|
| 14 |
try:
|
| 15 |
# Load the pipeline directly from your Hub repo
|
| 16 |
pipeline = DDPMPipeline.from_pretrained(MODEL_ID)
|
|
@@ -18,26 +19,40 @@ try:
|
|
| 18 |
print("β
Lime Status: ONLINE")
|
| 19 |
except Exception as e:
|
| 20 |
print(f"β CRITICAL FAILURE: Could not load the Lime. Error: {e}")
|
| 21 |
-
# Fallback to avoid crashing the space immediately, allows debugging
|
| 22 |
pipeline = None
|
| 23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
def generate_lime():
|
| 25 |
"""
|
| 26 |
The core inference function.
|
| 27 |
Summons a lime from the latent void.
|
| 28 |
"""
|
| 29 |
if pipeline is None:
|
| 30 |
-
|
|
|
|
|
|
|
| 31 |
|
| 32 |
print("π Generating new specimen...")
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
# --- CUSTOM CSS ---
|
| 40 |
-
# Giving it that dark, research-lab vibe from your screenshots
|
| 41 |
custom_css = """
|
| 42 |
body { background-color: #0d0d0d; color: #e0e0e0; font-family: 'Courier New', monospace; }
|
| 43 |
.gradio-container { max-width: 700px !important; margin-top: 40px !important; }
|
|
@@ -53,9 +68,14 @@ with gr.Blocks(css=custom_css, title="Stable-Lime v1.1") as demo:
|
|
| 53 |
|
| 54 |
with gr.Row():
|
| 55 |
with gr.Column():
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
-
# Button is now directly below image, no status text
|
| 59 |
generate_btn = gr.Button("INITIALIZE GENERATION", elem_classes="lime-btn")
|
| 60 |
|
| 61 |
gr.HTML("<div class='footer'>Running on Unconditional U-Net Architecture | Powered by Limes</div>")
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from diffusers import DDPMPipeline
|
| 3 |
import torch
|
| 4 |
+
from PIL import Image, ImageDraw
|
| 5 |
import os
|
| 6 |
|
| 7 |
# --- CONFIGURATION ---
|
|
|
|
| 11 |
print(f"π Initializing Stable-Lime Protocol on {DEVICE}...")
|
| 12 |
|
| 13 |
# --- LOAD MODEL ---
|
| 14 |
+
pipeline = None
|
| 15 |
try:
|
| 16 |
# Load the pipeline directly from your Hub repo
|
| 17 |
pipeline = DDPMPipeline.from_pretrained(MODEL_ID)
|
|
|
|
| 19 |
print("β
Lime Status: ONLINE")
|
| 20 |
except Exception as e:
|
| 21 |
print(f"β CRITICAL FAILURE: Could not load the Lime. Error: {e}")
|
|
|
|
| 22 |
pipeline = None
|
| 23 |
|
| 24 |
+
def create_error_image(message):
|
| 25 |
+
"""Generates a fallback image containing the error message to debug API issues."""
|
| 26 |
+
img = Image.new('RGB', (512, 512), color=(20, 0, 0))
|
| 27 |
+
d = ImageDraw.Draw(img)
|
| 28 |
+
try:
|
| 29 |
+
# Basic text drawing if font loading fails
|
| 30 |
+
d.text((20, 250), f"SYSTEM FAILURE:\n{message}", fill=(255, 50, 50))
|
| 31 |
+
except:
|
| 32 |
+
pass
|
| 33 |
+
return img
|
| 34 |
+
|
| 35 |
def generate_lime():
|
| 36 |
"""
|
| 37 |
The core inference function.
|
| 38 |
Summons a lime from the latent void.
|
| 39 |
"""
|
| 40 |
if pipeline is None:
|
| 41 |
+
# Return a generated error image so the frontend has something to show
|
| 42 |
+
# instead of a broken link/null response
|
| 43 |
+
return create_error_image("MODEL_LOAD_FAIL")
|
| 44 |
|
| 45 |
print("π Generating new specimen...")
|
| 46 |
+
try:
|
| 47 |
+
# Reduced steps slightly for better responsiveness if on CPU
|
| 48 |
+
steps = 50 if DEVICE == "cpu" else 100
|
| 49 |
+
image = pipeline(num_inference_steps=steps).images[0]
|
| 50 |
+
return image
|
| 51 |
+
except Exception as e:
|
| 52 |
+
print(f"Generation Error: {e}")
|
| 53 |
+
return create_error_image("INFERENCE_ERR")
|
| 54 |
|
| 55 |
# --- CUSTOM CSS ---
|
|
|
|
| 56 |
custom_css = """
|
| 57 |
body { background-color: #0d0d0d; color: #e0e0e0; font-family: 'Courier New', monospace; }
|
| 58 |
.gradio-container { max-width: 700px !important; margin-top: 40px !important; }
|
|
|
|
| 68 |
|
| 69 |
with gr.Row():
|
| 70 |
with gr.Column():
|
| 71 |
+
# KEY CHANGE: type="filepath" ensures the API receives a string path
|
| 72 |
+
# instead of a complex object or base64 string.
|
| 73 |
+
lime_output = gr.Image(
|
| 74 |
+
label="Generated Artifact",
|
| 75 |
+
type="filepath",
|
| 76 |
+
elem_id="lime-out"
|
| 77 |
+
)
|
| 78 |
|
|
|
|
| 79 |
generate_btn = gr.Button("INITIALIZE GENERATION", elem_classes="lime-btn")
|
| 80 |
|
| 81 |
gr.HTML("<div class='footer'>Running on Unconditional U-Net Architecture | Powered by Limes</div>")
|