re-type commited on
Commit
3f97919
·
verified ·
1 Parent(s): 79b76f1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +110 -41
app.py CHANGED
@@ -48,53 +48,122 @@ keras_model = None
48
  kmer_to_index = None
49
  analyzer = None
50
 
51
- # --- Load Models ---
52
- def load_models():
53
- global boundary_model, keras_model, kmer_to_index
54
- hf_token = os.getenv("HF_TOKEN", None)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55
 
56
- # Load boundary model
57
- if GenePredictor:
58
- try:
59
- boundary_path = hf_hub_download(
60
- repo_id=MODEL_REPO,
61
- filename="best_boundary_aware_model.pth",
62
- token=hf_token,
63
- cache_dir="/data/models"
64
- )
65
- boundary_model = GenePredictor(boundary_path)
66
- logging.info("Boundary model loaded successfully.")
67
- except Exception as e:
68
- logging.warning(f"Failed to load boundary model: {e}")
69
- boundary_model = None
70
  else:
71
- logging.warning("GenePredictor not available.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72
 
73
- # Load Keras model
74
- if load_model:
 
75
  try:
76
- keras_path = hf_hub_download(
77
- repo_id=MODEL_REPO,
78
- filename="best_model.keras",
79
- token=hf_token,
80
- cache_dir="/data/models"
81
- )
82
- kmer_path = hf_hub_download(
83
- repo_id=MODEL_REPO,
84
- filename="kmer_to_index.pkl",
85
- token=hf_token,
86
- cache_dir="/data/models"
87
- )
88
- keras_model = load_model(keras_path)
89
- with open(kmer_path, "rb") as f:
90
- kmer_to_index = pickle.load(f)
91
- logging.info("Keras model and k-mer index loaded successfully.")
 
 
 
92
  except Exception as e:
93
- logging.warning(f"Failed to load Keras model or k-mer index: {e}")
94
- keras_model = None
95
- kmer_to_index = None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
96
  else:
97
- logging.warning("Keras/TensorFlow not available.")
 
 
 
 
98
 
99
  # --- Initialize Tree Analyzer ---
100
  def init_tree_analyzer():
 
48
  kmer_to_index = None
49
  analyzer = None
50
 
51
+ --- Load Models ---
52
+ boundary_model = None
53
+ keras_model = None
54
+ kmer_to_index = None
55
+
56
+ # Try to load boundary model from Hugging Face Hub
57
+ try:
58
+ boundary_path = hf_hub_download(
59
+ repo_id=model_repo,
60
+ filename="best_boundary_aware_model.pth",
61
+ token=hf_token
62
+ )
63
+ if os.path.exists(boundary_path):
64
+ boundary_model = GenePredictor(boundary_path)
65
+ logging.info("Boundary model loaded successfully from Hugging Face Hub.")
66
+ else:
67
+ logging.warning(f"Boundary model file not found after download")
68
+ except Exception as e:
69
+ logging.error(f"Failed to load boundary model from HF Hub: {e}")
70
+
71
+ # Try to load Keras model from Hugging Face Hub
72
+ try:
73
+ keras_path = hf_hub_download(
74
+ repo_id=model_repo,
75
+ filename="best_model.keras",
76
+ token=hf_token
77
+ )
78
+ kmer_path = hf_hub_download(
79
+ repo_id=model_repo,
80
+ filename="kmer_to_index.pkl",
81
+ token=hf_token
82
+ )
83
 
84
+ if os.path.exists(keras_path) and os.path.exists(kmer_path):
85
+ keras_model = load_model(keras_path)
86
+ with open(kmer_path, "rb") as f:
87
+ kmer_to_index = pickle.load(f)
88
+ logging.info("Keras model and k-mer index loaded successfully from Hugging Face Hub.")
 
 
 
 
 
 
 
 
 
89
  else:
90
+ logging.warning(f"Keras model or kmer files not found after download")
91
+ except Exception as e:
92
+ logging.error(f"Failed to load Keras model from HF Hub: {e}")
93
+
94
+ # --- Load Verification Models from models directory ---
95
+ verification_models = {}
96
+
97
+ def load_verification_models():
98
+ """Load all verification models from the models directory"""
99
+ global verification_models
100
+ models_dir = "models"
101
+
102
+ if not os.path.exists(models_dir):
103
+ logging.warning(f"Models directory not found: {models_dir}")
104
+ return
105
+
106
+ # Load different types of verification models
107
+ model_files = {
108
+ "boundary_model": "best_boundary_aware_model.pth",
109
+ "keras_model": "best_model.keras",
110
+ "kmer_index": "kmer_to_index.pkl",
111
+ "additional_model_1": "verification_model_1.pth", # Add your model names here
112
+ "additional_model_2": "verification_model_2.keras",
113
+ # Add more models as needed
114
+ }
115
 
116
+ for model_name, filename in model_files.items():
117
+ model_path = os.path.join(models_dir, filename)
118
+
119
  try:
120
+ if os.path.exists(model_path):
121
+ if filename.endswith('.pth'):
122
+ # PyTorch model
123
+ if model_name == "boundary_model":
124
+ verification_models[model_name] = GenePredictor(model_path)
125
+ else:
126
+ verification_models[model_name] = torch.load(model_path, map_location='cpu')
127
+
128
+ elif filename.endswith('.keras'):
129
+ # Keras model
130
+ verification_models[model_name] = load_model(model_path)
131
+
132
+ elif filename.endswith('.pkl'):
133
+ # Pickle file
134
+ with open(model_path, 'rb') as f:
135
+ verification_models[model_name] = pickle.load(f)
136
+
137
+ logging.info(f"Loaded verification model: {model_name}")
138
+
139
  except Exception as e:
140
+ logging.error(f"Failed to load {model_name} from {model_path}: {e}")
141
+
142
+ # Load verification models at startup
143
+ load_verification_models()
144
+
145
+ # --- Initialize Tree Analyzer ---
146
+ analyzer = None
147
+ try:
148
+ analyzer = ml_simplified_tree.PhylogeneticTreeAnalyzer()
149
+ if os.path.exists(csv_path):
150
+ if analyzer.load_data(csv_path):
151
+ logging.info("Tree analyzer initialized successfully")
152
+ # Try to train AI model (optional)
153
+ try:
154
+ if not analyzer.train_ai_model():
155
+ logging.warning("AI model training failed; proceeding with basic analysis.")
156
+ except Exception as e:
157
+ logging.warning(f"AI model training failed: {e}")
158
+ else:
159
+ logging.error("Failed to load CSV data for tree analyzer")
160
+ analyzer = None
161
  else:
162
+ logging.error(f"CSV file not found: {csv_path}")
163
+ analyzer = None
164
+ except Exception as e:
165
+ logging.error(f"Failed to initialize tree analyzer: {e}")
166
+ analyzer = None
167
 
168
  # --- Initialize Tree Analyzer ---
169
  def init_tree_analyzer():