akhaliq HF Staff commited on
Commit
462c8e0
·
verified ·
1 Parent(s): 0bea800

Update app.py from anycoder

Browse files
Files changed (1) hide show
  1. app.py +14 -4
app.py CHANGED
@@ -1,13 +1,14 @@
1
  from PIL import Image
2
  import torch
3
  import gradio as gr
 
4
 
5
  # Load models
6
  model2 = torch.hub.load(
7
  "AK391/animegan2-pytorch:main",
8
  "generator",
9
  pretrained=True,
10
- device="cuda",
11
  progress=False
12
  )
13
 
@@ -15,23 +16,29 @@ model1 = torch.hub.load(
15
  "AK391/animegan2-pytorch:main",
16
  "generator",
17
  pretrained="face_paint_512_v1",
18
- device="cuda"
19
  )
20
 
21
  face2paint = torch.hub.load(
22
  'AK391/animegan2-pytorch:main',
23
  'face2paint',
24
  size=512,
25
- device="cuda",
26
  side_by_side=False
27
  )
28
 
 
29
  def inference(img, ver):
30
  """Convert portrait to anime style"""
 
31
  if ver == 'Version 2':
 
32
  out = face2paint(model2, img)
 
33
  else:
 
34
  out = face2paint(model1, img)
 
35
  return out
36
 
37
  # Custom CSS for modern, mobile-friendly design
@@ -151,7 +158,7 @@ with gr.Blocks(fill_height=True) as demo:
151
  # Header
152
  with gr.Column(elem_id="header"):
153
  gr.Markdown("# ⚡ AnimeGAN v2")
154
- gr.Markdown("Transform your portraits into stunning anime-style artwork")
155
 
156
  # Main content
157
  with gr.Column(elem_id="main-content"):
@@ -232,6 +239,9 @@ with gr.Blocks(fill_height=True) as demo:
232
  <p style='margin-top: 1rem; font-size: 0.9rem; color: rgba(255,255,255,0.6);'>
233
  💡 <strong>Tip:</strong> Use a cropped portrait photo for best results
234
  </p>
 
 
 
235
  """
236
  )
237
 
 
1
  from PIL import Image
2
  import torch
3
  import gradio as gr
4
+ import spaces
5
 
6
  # Load models
7
  model2 = torch.hub.load(
8
  "AK391/animegan2-pytorch:main",
9
  "generator",
10
  pretrained=True,
11
+ device="cpu", # Load on CPU initially
12
  progress=False
13
  )
14
 
 
16
  "AK391/animegan2-pytorch:main",
17
  "generator",
18
  pretrained="face_paint_512_v1",
19
+ device="cpu" # Load on CPU initially
20
  )
21
 
22
  face2paint = torch.hub.load(
23
  'AK391/animegan2-pytorch:main',
24
  'face2paint',
25
  size=512,
26
+ device="cpu", # Load on CPU initially
27
  side_by_side=False
28
  )
29
 
30
+ @spaces.GPU # Zero GPU decorator - moves to GPU only when function runs
31
  def inference(img, ver):
32
  """Convert portrait to anime style"""
33
+ # Move models to GPU when function is called
34
  if ver == 'Version 2':
35
+ model2.to('cuda')
36
  out = face2paint(model2, img)
37
+ model2.to('cpu') # Move back to CPU to free GPU
38
  else:
39
+ model1.to('cuda')
40
  out = face2paint(model1, img)
41
+ model1.to('cpu') # Move back to CPU to free GPU
42
  return out
43
 
44
  # Custom CSS for modern, mobile-friendly design
 
158
  # Header
159
  with gr.Column(elem_id="header"):
160
  gr.Markdown("# ⚡ AnimeGAN v2")
161
+ gr.Markdown("Transform your portraits into stunning anime-style artwork using Zero GPU")
162
 
163
  # Main content
164
  with gr.Column(elem_id="main-content"):
 
239
  <p style='margin-top: 1rem; font-size: 0.9rem; color: rgba(255,255,255,0.6);'>
240
  💡 <strong>Tip:</strong> Use a cropped portrait photo for best results
241
  </p>
242
+ <p style='margin-top: 0.5rem; font-size: 0.85rem; color: rgba(255,255,255,0.5);'>
243
+ ⚡ Powered by Zero GPU - efficient GPU usage on Hugging Face Spaces
244
+ </p>
245
  """
246
  )
247