m0ksh commited on
Commit
5100cb5
·
verified ·
1 Parent(s): e46e6e3

Sync from GitHub (preserve manual model files)

Browse files
Files changed (2) hide show
  1. StreamlitApp/utils/predict.py +6 -72
  2. requirements.txt +1 -2
StreamlitApp/utils/predict.py CHANGED
@@ -1,12 +1,8 @@
1
- import os
2
  import pathlib
3
- import requests
4
  import numpy as np
5
  import torch
6
  import streamlit as st
7
  from torch import nn
8
- from typing import Optional
9
- import shutil
10
 
11
  # Model Definition
12
  class FastMLP(nn.Module):
@@ -24,21 +20,6 @@ class FastMLP(nn.Module):
24
  def forward(self, x):
25
  return self.layers(x)
26
 
27
- # Utility: download file from URL to local path (streaming)
28
- def _download_file(url: str, dest_path: str):
29
- dest = pathlib.Path(dest_path)
30
- dest.parent.mkdir(parents=True, exist_ok=True)
31
- with requests.get(url, stream=True) as r:
32
- r.raise_for_status()
33
- with open(dest, 'wb') as f:
34
- for chunk in r.iter_content(chunk_size=8192):
35
- if chunk:
36
- f.write(chunk)
37
-
38
- def _get_env(key: str) -> Optional[str]:
39
- v = os.environ.get(key)
40
- return v if v else None
41
-
42
  # Model Loader
43
  @st.cache_resource
44
  def load_model():
@@ -53,60 +34,13 @@ def load_model():
53
  ]
54
  model_path = next((p for p in candidates if p.exists()), candidates[0])
55
 
56
- # If the model file doesn't exist, try to download it from a configured URL
57
  if not model_path.exists():
58
-
59
- model_url = _get_env("MODEL_URL")
60
-
61
- if model_url:
62
- try:
63
- _download_file(model_url, str(model_path))
64
- except Exception as e:
65
- st.error(f"Failed to download model from MODEL_URL: {e}")
66
- raise
67
- else:
68
- # Fall back to Hugging Face Hub model repo download.
69
- # Configure these in HF Space secrets/vars, or locally in env:
70
- # - MODEL_REPO_ID (e.g. "m0ksh/peptideai-models")
71
- # - MODEL_FILENAME (default: "ampMLModel.pt")
72
- model_repo_id = _get_env("MODEL_REPO_ID")
73
- model_filename = _get_env("MODEL_FILENAME") or "ampMLModel.pt"
74
-
75
- if not model_repo_id:
76
- raise FileNotFoundError(
77
- "Model file './models/ampMLModel.pt' not found.\n"
78
- "Set one of:\n"
79
- "- MODEL_URL (direct download URL), or\n"
80
- "- MODEL_REPO_ID (Hugging Face model repo id) and optional MODEL_FILENAME.\n"
81
- "\n"
82
- "Debug (env vars detected): "
83
- f"MODEL_URL={'set' if _get_env('MODEL_URL') else 'missing'}, "
84
- f"MODEL_REPO_ID={'set' if _get_env('MODEL_REPO_ID') else 'missing'}, "
85
- f"MODEL_FILENAME={'set' if _get_env('MODEL_FILENAME') else 'missing'}\n"
86
- )
87
-
88
- try:
89
- from huggingface_hub import hf_hub_download
90
- except Exception as e:
91
- raise RuntimeError(
92
- "Missing dependency 'huggingface_hub'. Add it to requirements.txt.\n"
93
- f"Import error: {e}"
94
- ) from e
95
-
96
- token = _get_env("HF_TOKEN") or _get_env("HUGGINGFACE_TOKEN")
97
- downloaded_path = hf_hub_download(
98
- repo_id=model_repo_id,
99
- filename=model_filename,
100
- token=token,
101
- )
102
- model_path.parent.mkdir(parents=True, exist_ok=True)
103
- shutil.copyfile(downloaded_path, model_path)
104
-
105
- if not model_path.exists():
106
- raise FileNotFoundError(
107
- f"Model download did not produce file at: {model_path}\n"
108
- "Check MODEL_URL or MODEL_REPO_ID/MODEL_FILENAME configuration."
109
- )
110
 
111
  # Build model and load weights
112
  model = FastMLP(input_dim=1024)
 
 
1
  import pathlib
 
2
  import numpy as np
3
  import torch
4
  import streamlit as st
5
  from torch import nn
 
 
6
 
7
  # Model Definition
8
  class FastMLP(nn.Module):
 
20
  def forward(self, x):
21
  return self.layers(x)
22
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
  # Model Loader
24
  @st.cache_resource
25
  def load_model():
 
34
  ]
35
  model_path = next((p for p in candidates if p.exists()), candidates[0])
36
 
 
37
  if not model_path.exists():
38
+ raise FileNotFoundError(
39
+ "Model file 'ampMLModel.pt' not found in any of:\n"
40
+ f"- {repo_root / 'MLModels' / 'ampMLModel.pt'}\n"
41
+ f"- {repo_root / 'models' / 'ampMLModel.pt'}\n"
42
+ f"- {streamlitapp_dir / 'models' / 'ampMLModel.pt'}\n"
43
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44
 
45
  # Build model and load weights
46
  model = FastMLP(input_dim=1024)
requirements.txt CHANGED
@@ -5,5 +5,4 @@ torch
5
  scikit-learn
6
  matplotlib
7
  plotly
8
- requests
9
- huggingface_hub
 
5
  scikit-learn
6
  matplotlib
7
  plotly
8
+ requests