Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,7 +2,6 @@ import gradio as gr
|
|
| 2 |
import torch
|
| 3 |
import random
|
| 4 |
import time
|
| 5 |
-
import io
|
| 6 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 7 |
from diffusers import DiffusionPipeline, LCMScheduler
|
| 8 |
from PIL import Image, ImageFilter
|
|
@@ -41,22 +40,31 @@ pipe.enable_vae_slicing()
|
|
| 41 |
pipe.set_progress_bar_config(disable=True)
|
| 42 |
|
| 43 |
# ===============================
|
| 44 |
-
# ULTRA MODE (
|
| 45 |
# ===============================
|
| 46 |
-
def call_ultra_api(prompt,
|
| 47 |
try:
|
|
|
|
| 48 |
client = Client("mrfakename/Z-Image-Turbo")
|
|
|
|
|
|
|
|
|
|
| 49 |
result = client.predict(
|
| 50 |
prompt=prompt,
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
|
|
|
|
|
|
| 54 |
api_name="/predict"
|
| 55 |
)
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
| 57 |
return Image.open(result)
|
| 58 |
except Exception as e:
|
| 59 |
-
print(f"API Error: {e}")
|
| 60 |
return None
|
| 61 |
|
| 62 |
# ===============================
|
|
@@ -83,12 +91,17 @@ def generate(prompt, user_neg, res, step_val, is_ultra):
|
|
| 83 |
|
| 84 |
if is_ultra:
|
| 85 |
yield (None, "🎨 Generating Image...", gr.update(interactive=False))
|
| 86 |
-
# Ultra mode uses
|
| 87 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
if image:
|
| 89 |
yield (image, f"✅ Done in Ultra Mode.", gr.update(interactive=True))
|
| 90 |
else:
|
| 91 |
-
yield (None, "❌ API Error
|
| 92 |
else:
|
| 93 |
yield (None, "🧠 Analysing Prompt", gr.update(interactive=False))
|
| 94 |
enhanced = enhance_prompt(prompt)
|
|
@@ -97,7 +110,6 @@ def generate(prompt, user_neg, res, step_val, is_ultra):
|
|
| 97 |
generator = torch.Generator("cpu").manual_seed(seed)
|
| 98 |
start = time.time()
|
| 99 |
|
| 100 |
-
# Normal mode uses default_neg
|
| 101 |
image = pipe(
|
| 102 |
prompt=enhanced,
|
| 103 |
negative_prompt=default_neg,
|
|
@@ -146,7 +158,6 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue="green"), css=custom_css) as dem
|
|
| 146 |
output_img = gr.Image(label="Result", interactive=False)
|
| 147 |
status = gr.Markdown("🟢 Ready", elem_classes="status-box")
|
| 148 |
|
| 149 |
-
# Connect the UI logic
|
| 150 |
ultra_check.change(
|
| 151 |
toggle_ultra,
|
| 152 |
inputs=[ultra_check],
|
|
|
|
| 2 |
import torch
|
| 3 |
import random
|
| 4 |
import time
|
|
|
|
| 5 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 6 |
from diffusers import DiffusionPipeline, LCMScheduler
|
| 7 |
from PIL import Image, ImageFilter
|
|
|
|
| 40 |
pipe.set_progress_bar_config(disable=True)
|
| 41 |
|
| 42 |
# ===============================
|
| 43 |
+
# ULTRA MODE (FIXED API CALL)
|
| 44 |
# ===============================
|
| 45 |
+
def call_ultra_api(prompt, steps, resolution, seed):
|
| 46 |
try:
|
| 47 |
+
# Connect specifically to the Z-Image-Turbo Space
|
| 48 |
client = Client("mrfakename/Z-Image-Turbo")
|
| 49 |
+
|
| 50 |
+
# Based on your documentation, the parameters are:
|
| 51 |
+
# prompt, height, width, num_inference_steps, seed, randomize_seed
|
| 52 |
result = client.predict(
|
| 53 |
prompt=prompt,
|
| 54 |
+
height=int(resolution),
|
| 55 |
+
width=int(resolution),
|
| 56 |
+
num_inference_steps=int(steps),
|
| 57 |
+
seed=int(seed),
|
| 58 |
+
randomize_seed=False,
|
| 59 |
api_name="/predict"
|
| 60 |
)
|
| 61 |
+
|
| 62 |
+
# result[0] is usually the image path in this specific Space
|
| 63 |
+
if isinstance(result, tuple) or isinstance(result, list):
|
| 64 |
+
return Image.open(result[0])
|
| 65 |
return Image.open(result)
|
| 66 |
except Exception as e:
|
| 67 |
+
print(f"Detailed API Error: {e}")
|
| 68 |
return None
|
| 69 |
|
| 70 |
# ===============================
|
|
|
|
| 91 |
|
| 92 |
if is_ultra:
|
| 93 |
yield (None, "🎨 Generating Image...", gr.update(interactive=False))
|
| 94 |
+
# Ultra mode uses the direct prompt + user negative (if API supports neg,
|
| 95 |
+
# but Z-Turbo usually ignores neg prompt to stay 'Turbo')
|
| 96 |
+
# We pass the prompt combined with negative for best results if the API is simple
|
| 97 |
+
full_prompt = f"{prompt} | Negative: {user_neg}" if user_neg else prompt
|
| 98 |
+
|
| 99 |
+
image = call_ultra_api(full_prompt, step_val, size, seed)
|
| 100 |
+
|
| 101 |
if image:
|
| 102 |
yield (image, f"✅ Done in Ultra Mode.", gr.update(interactive=True))
|
| 103 |
else:
|
| 104 |
+
yield (None, "❌ API Error: The external GPU server is busy.", gr.update(interactive=True))
|
| 105 |
else:
|
| 106 |
yield (None, "🧠 Analysing Prompt", gr.update(interactive=False))
|
| 107 |
enhanced = enhance_prompt(prompt)
|
|
|
|
| 110 |
generator = torch.Generator("cpu").manual_seed(seed)
|
| 111 |
start = time.time()
|
| 112 |
|
|
|
|
| 113 |
image = pipe(
|
| 114 |
prompt=enhanced,
|
| 115 |
negative_prompt=default_neg,
|
|
|
|
| 158 |
output_img = gr.Image(label="Result", interactive=False)
|
| 159 |
status = gr.Markdown("🟢 Ready", elem_classes="status-box")
|
| 160 |
|
|
|
|
| 161 |
ultra_check.change(
|
| 162 |
toggle_ultra,
|
| 163 |
inputs=[ultra_check],
|