FlameF0X commited on
Commit
ac813ec
Β·
verified Β·
1 Parent(s): ca75acb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -11
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
- return None
 
 
31
 
32
  print("πŸ‹ Generating new specimen...")
33
- # Generate the image
34
- # num_inference_steps=50 is a good balance for speed/quality on CPU Spaces
35
- image = pipeline(num_inference_steps=100).images[0]
36
-
37
- return image
 
 
 
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
- lime_output = gr.Image(label="Generated Artifact", type="pil", elem_id="lime-out")
 
 
 
 
 
 
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>")