akhaliq HF Staff commited on
Commit
85df522
Β·
verified Β·
1 Parent(s): d6c9b79

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -50
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 os
10
 
11
- # Global variables for model
12
- pipe = None
13
- device = None
14
 
15
- def initialize_model():
16
- """Initialize the model on first use"""
17
- global pipe, device
18
-
19
- if pipe is not None:
20
- return
21
-
22
- try:
23
- device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
24
- model_id = 'meituan-longcat/LongCat-Image-Edit'
25
-
26
- print(f"πŸ”„ Loading model from {model_id}...")
27
-
28
- # Load text processor
29
- text_processor = AutoProcessor.from_pretrained(
30
- model_id,
31
- subfolder='tokenizer'
32
- )
33
-
34
- # Load transformer
35
- transformer = LongCatImageTransformer2DModel.from_pretrained(
36
- model_id,
37
- subfolder='transformer',
38
- torch_dtype=torch.bfloat16,
39
- use_safetensors=True
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
- First run may take longer as the model loads from Hugging Face Hub.
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
- api_name="edit_image"
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