akhaliq HF Staff commited on
Commit
ed5b2a8
·
1 Parent(s): 99bd7fb

feat: enable HF_TRANSFER, improve model loading logs, and increase GPU duration to 120s

Browse files
Files changed (2) hide show
  1. app.py +13 -7
  2. requirements.txt +1 -0
app.py CHANGED
@@ -1,4 +1,5 @@
1
  import os
 
2
  import uuid
3
  import torch
4
  from diffusers import ErnieImagePipeline
@@ -10,17 +11,22 @@ import spaces
10
  torch.set_float32_matmul_precision("high")
11
 
12
  # Initialize Pipeline
13
- pipe = ErnieImagePipeline.from_pretrained(
14
- "Baidu/ERNIE-Image-Turbo",
15
- torch_dtype=torch.bfloat16,
16
- )
17
-
18
- if torch.cuda.is_available():
 
19
  pipe = pipe.to("cuda")
 
 
 
 
20
 
21
  app = Server()
22
 
23
- @spaces.GPU
24
  @app.api()
25
  def generate_image(prompt: str, width: int = 1024, height: int = 1024, guidance_scale: float = 1.0, num_inference_steps: int = 8, use_pe: bool = True) -> FileData:
26
  """Generate an image using ERNIE-Image-Turbo."""
 
1
  import os
2
+ os.environ["HF_HUB_ENABLE_HF_TRANSFER"] = "1"
3
  import uuid
4
  import torch
5
  from diffusers import ErnieImagePipeline
 
11
  torch.set_float32_matmul_precision("high")
12
 
13
  # Initialize Pipeline
14
+ print("Loading model Baidu/ERNIE-Image-Turbo... this may take a few minutes!", flush=True)
15
+ try:
16
+ pipe = ErnieImagePipeline.from_pretrained(
17
+ "Baidu/ERNIE-Image-Turbo",
18
+ torch_dtype=torch.bfloat16,
19
+ )
20
+ print("Model loaded successfully. Moving to CUDA...", flush=True)
21
  pipe = pipe.to("cuda")
22
+ print("Model is on CUDA. Initializing Server...", flush=True)
23
+ except Exception as e:
24
+ print(f"Error during model loading: {e}", flush=True)
25
+ raise
26
 
27
  app = Server()
28
 
29
+ @spaces.GPU(duration=120)
30
  @app.api()
31
  def generate_image(prompt: str, width: int = 1024, height: int = 1024, guidance_scale: float = 1.0, num_inference_steps: int = 8, use_pe: bool = True) -> FileData:
32
  """Generate an image using ERNIE-Image-Turbo."""
requirements.txt CHANGED
@@ -5,3 +5,4 @@ transformers
5
  accelerate
6
  safetensors
7
  git+https://github.com/huggingface/diffusers.git
 
 
5
  accelerate
6
  safetensors
7
  git+https://github.com/huggingface/diffusers.git
8
+ hf_transfer