Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,3 @@
|
|
| 1 |
-
import spaces
|
| 2 |
import gradio as gr
|
| 3 |
import torch
|
| 4 |
from PIL import Image
|
|
@@ -6,52 +5,37 @@ from transformers import AutoProcessor
|
|
| 6 |
from longcat_image.models import LongCatImageTransformer2DModel
|
| 7 |
from longcat_image.pipelines import LongCatImageEditPipeline
|
| 8 |
import numpy as np
|
| 9 |
-
import
|
| 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 |
-
).to(device)
|
| 41 |
-
|
| 42 |
-
# Load pipeline
|
| 43 |
-
pipe = LongCatImageEditPipeline.from_pretrained(
|
| 44 |
-
model_id,
|
| 45 |
-
transformer=transformer,
|
| 46 |
-
text_processor=text_processor,
|
| 47 |
-
)
|
| 48 |
-
pipe.to(device, torch.bfloat16)
|
| 49 |
-
|
| 50 |
-
print(f"β
Model loaded successfully on {device}")
|
| 51 |
-
|
| 52 |
-
except Exception as e:
|
| 53 |
-
print(f"β Error loading model: {e}")
|
| 54 |
-
raise
|
| 55 |
|
| 56 |
@spaces.GPU(duration=120)
|
| 57 |
def edit_image(
|
|
@@ -72,9 +56,6 @@ def edit_image(
|
|
| 72 |
raise gr.Error("Please enter an edit instruction")
|
| 73 |
|
| 74 |
try:
|
| 75 |
-
# Initialize model if not already loaded
|
| 76 |
-
initialize_model()
|
| 77 |
-
|
| 78 |
progress(0.1, desc="Preparing image...")
|
| 79 |
|
| 80 |
# Convert to RGB if needed
|
|
@@ -189,7 +170,7 @@ with gr.Blocks(fill_height=True) as demo:
|
|
| 189 |
<div style="padding: 10px; background-color: #f0f7ff; border-radius: 8px; margin-top: 10px;">
|
| 190 |
<p style="margin: 0; font-size: 12px; color: #555;">
|
| 191 |
β±οΈ <strong>Note:</strong> Zero-GPU provides 120 seconds of GPU time per request.
|
| 192 |
-
|
| 193 |
Processing typically takes 30-60 seconds depending on settings.
|
| 194 |
</p>
|
| 195 |
</div>
|
|
@@ -235,7 +216,7 @@ with gr.Blocks(fill_height=True) as demo:
|
|
| 235 |
seed
|
| 236 |
],
|
| 237 |
outputs=output_image,
|
| 238 |
-
|
| 239 |
)
|
| 240 |
|
| 241 |
# Footer
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
from PIL import Image
|
|
|
|
| 5 |
from longcat_image.models import LongCatImageTransformer2DModel
|
| 6 |
from longcat_image.pipelines import LongCatImageEditPipeline
|
| 7 |
import numpy as np
|
| 8 |
+
import spaces
|
| 9 |
|
| 10 |
+
# Load model directly at startup
|
| 11 |
+
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
| 12 |
+
model_id = 'meituan-longcat/LongCat-Image-Edit'
|
| 13 |
|
| 14 |
+
print(f"π Loading model from {model_id}...")
|
| 15 |
+
|
| 16 |
+
# Load text processor
|
| 17 |
+
text_processor = AutoProcessor.from_pretrained(
|
| 18 |
+
model_id,
|
| 19 |
+
subfolder='tokenizer'
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
+
# Load transformer
|
| 23 |
+
transformer = LongCatImageTransformer2DModel.from_pretrained(
|
| 24 |
+
model_id,
|
| 25 |
+
subfolder='transformer',
|
| 26 |
+
torch_dtype=torch.bfloat16,
|
| 27 |
+
use_safetensors=True
|
| 28 |
+
).to(device)
|
| 29 |
+
|
| 30 |
+
# Load pipeline
|
| 31 |
+
pipe = LongCatImageEditPipeline.from_pretrained(
|
| 32 |
+
model_id,
|
| 33 |
+
transformer=transformer,
|
| 34 |
+
text_processor=text_processor,
|
| 35 |
+
)
|
| 36 |
+
pipe.to(device, torch.bfloat16)
|
| 37 |
+
|
| 38 |
+
print(f"β
Model loaded successfully on {device}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
@spaces.GPU(duration=120)
|
| 41 |
def edit_image(
|
|
|
|
| 56 |
raise gr.Error("Please enter an edit instruction")
|
| 57 |
|
| 58 |
try:
|
|
|
|
|
|
|
|
|
|
| 59 |
progress(0.1, desc="Preparing image...")
|
| 60 |
|
| 61 |
# Convert to RGB if needed
|
|
|
|
| 170 |
<div style="padding: 10px; background-color: #f0f7ff; border-radius: 8px; margin-top: 10px;">
|
| 171 |
<p style="margin: 0; font-size: 12px; color: #555;">
|
| 172 |
β±οΈ <strong>Note:</strong> Zero-GPU provides 120 seconds of GPU time per request.
|
| 173 |
+
Model is loaded at startup from Hugging Face Hub.
|
| 174 |
Processing typically takes 30-60 seconds depending on settings.
|
| 175 |
</p>
|
| 176 |
</div>
|
|
|
|
| 216 |
seed
|
| 217 |
],
|
| 218 |
outputs=output_image,
|
| 219 |
+
api_visibility="public"
|
| 220 |
)
|
| 221 |
|
| 222 |
# Footer
|