infinity1096 commited on
Commit
7f9c5ee
·
1 Parent(s): b91a473

fix spaces

Browse files
Files changed (1) hide show
  1. app.py +11 -14
app.py CHANGED
@@ -11,6 +11,7 @@ import subprocess
11
  import sys
12
  import subprocess
13
  import importlib
 
14
  import spaces
15
 
16
  try:
@@ -36,31 +37,28 @@ from uniflowmatch.models.ufm import (
36
  from uniflowmatch.utils.viz import warp_image_with_flow
37
 
38
  # Global model variable
39
- model = None
40
  USE_REFINEMENT_MODEL = False
41
 
 
42
  def initialize_model(use_refinement: bool = False):
43
  """Initialize the model - call this once at startup"""
44
- global model, USE_REFINEMENT_MODEL
45
- USE_REFINEMENT_MODEL = use_refinement
46
-
47
  try:
48
- if USE_REFINEMENT_MODEL:
49
  print("Loading UFM Refinement model from infinity1096/UFM-Refine...")
50
- model = UniFlowMatchClassificationRefinement.from_pretrained("infinity1096/UFM-Refine")
51
  else:
52
  print("Loading UFM Base model from infinity1096/UFM-Base...")
53
- model = UniFlowMatchConfidence.from_pretrained("infinity1096/UFM-Base")
54
 
55
  # Set model to evaluation mode
56
- if hasattr(model, "eval"):
57
- model.eval()
58
 
59
  print("Model loaded successfully!")
60
- return True
61
  except Exception as e:
62
  print(f"Error loading model: {e}")
63
- return False
64
 
65
  @spaces.GPU
66
  def process_images(source_image, target_image, model_type_choice):
@@ -72,9 +70,8 @@ def process_images(source_image, target_image, model_type_choice):
72
 
73
  # Reinitialize model if type has changed
74
  current_refinement = model_type_choice == "Refinement Model"
75
- if current_refinement != USE_REFINEMENT_MODEL:
76
- print(f"Switching to {model_type_choice}...")
77
- initialize_model(current_refinement)
78
 
79
  if model is None:
80
  return None, None, None, "Model not loaded. Please restart the application."
 
11
  import sys
12
  import subprocess
13
  import importlib
14
+ from functools import lru_cache
15
  import spaces
16
 
17
  try:
 
37
  from uniflowmatch.utils.viz import warp_image_with_flow
38
 
39
  # Global model variable
 
40
  USE_REFINEMENT_MODEL = False
41
 
42
+ @lru_cache(maxsize=2)
43
  def initialize_model(use_refinement: bool = False):
44
  """Initialize the model - call this once at startup"""
 
 
 
45
  try:
46
+ if use_refinement:
47
  print("Loading UFM Refinement model from infinity1096/UFM-Refine...")
48
+ model_obj = UniFlowMatchClassificationRefinement.from_pretrained("infinity1096/UFM-Refine")
49
  else:
50
  print("Loading UFM Base model from infinity1096/UFM-Base...")
51
+ model_obj = UniFlowMatchConfidence.from_pretrained("infinity1096/UFM-Base")
52
 
53
  # Set model to evaluation mode
54
+ if hasattr(model_obj, "eval"):
55
+ model_obj.eval()
56
 
57
  print("Model loaded successfully!")
58
+ return True, model_obj
59
  except Exception as e:
60
  print(f"Error loading model: {e}")
61
+ return False, None
62
 
63
  @spaces.GPU
64
  def process_images(source_image, target_image, model_type_choice):
 
70
 
71
  # Reinitialize model if type has changed
72
  current_refinement = model_type_choice == "Refinement Model"
73
+ print(f"Switching to {model_type_choice}...")
74
+ ret, model = initialize_model(current_refinement)
 
75
 
76
  if model is None:
77
  return None, None, None, "Model not loaded. Please restart the application."