Robys01 commited on
Commit
3b679af
·
1 Parent(s): 028e260

Pre-load example images for the face aging demo to solve gradio move issue.

Browse files
Files changed (1) hide show
  1. app.py +6 -5
app.py CHANGED
@@ -5,7 +5,6 @@ from models import UNet
5
  from test_functions import process_image
6
  from PIL import Image
7
  import gradio as gr
8
-
9
  from huggingface_hub import hf_hub_download
10
 
11
  MODEL_PATH = "model/best_unet_model.pth"
@@ -22,14 +21,16 @@ model.load_state_dict(torch.load(MODEL_PATH, map_location=torch.device("cpu"), w
22
  model.eval()
23
 
24
  def age_image(image: Image.Image, source_age: int, target_age: int) -> Image.Image:
25
- # Ensure the image is in RGB or grayscale; if not, convert it.
26
  if image.mode not in ["RGB", "L"]:
27
  print(f"Converting image from {image.mode} to RGB")
28
  image = image.convert("RGB")
29
-
30
  processed_image = process_image(model, image, source_age, target_age)
31
  return processed_image
32
 
 
 
 
 
33
  iface = gr.Interface(
34
  fn=age_image,
35
  inputs=[
@@ -39,8 +40,8 @@ iface = gr.Interface(
39
  ],
40
  outputs=gr.Image(type="pil", label="Aged Image"),
41
  examples=[
42
- ["examples/girl.jpg", 14, 50],
43
- ["examples/trump.jpg", 74, 30],
44
  ],
45
  title="Face Aging Demo",
46
  description="Upload an image along with a source age approximation and a target age to generate an aged version of the face."
 
5
  from test_functions import process_image
6
  from PIL import Image
7
  import gradio as gr
 
8
  from huggingface_hub import hf_hub_download
9
 
10
  MODEL_PATH = "model/best_unet_model.pth"
 
21
  model.eval()
22
 
23
  def age_image(image: Image.Image, source_age: int, target_age: int) -> Image.Image:
 
24
  if image.mode not in ["RGB", "L"]:
25
  print(f"Converting image from {image.mode} to RGB")
26
  image = image.convert("RGB")
 
27
  processed_image = process_image(model, image, source_age, target_age)
28
  return processed_image
29
 
30
+ # Pre-load the example images as PIL objects
31
+ example1 = Image.open("examples/girl.jpg")
32
+ example2 = Image.open("examples/trump.jpg")
33
+
34
  iface = gr.Interface(
35
  fn=age_image,
36
  inputs=[
 
40
  ],
41
  outputs=gr.Image(type="pil", label="Aged Image"),
42
  examples=[
43
+ [example1, 14, 50],
44
+ [example2, 74, 30],
45
  ],
46
  title="Face Aging Demo",
47
  description="Upload an image along with a source age approximation and a target age to generate an aged version of the face."