PunchNFIT
commited on
Commit
Β·
5c20eb3
1
Parent(s):
57f4afd
Simplified: Flask direct run, no Gunicorn
Browse files- Dockerfile +2 -1
- api_inference.py +21 -28
Dockerfile
CHANGED
|
@@ -27,4 +27,5 @@ RUN touch /.gitconfig && chmod 666 /.gitconfig
|
|
| 27 |
USER appuser
|
| 28 |
|
| 29 |
# 8) Start server (Gunicorn)
|
| 30 |
-
CMD ["
|
|
|
|
|
|
| 27 |
USER appuser
|
| 28 |
|
| 29 |
# 8) Start server (Gunicorn)
|
| 30 |
+
CMD ["python", "api_inference.py"]
|
| 31 |
+
|
api_inference.py
CHANGED
|
@@ -3,19 +3,16 @@ import torch
|
|
| 3 |
import torch.nn as nn
|
| 4 |
from flask import Flask, request, jsonify
|
| 5 |
from transformers import (
|
| 6 |
-
AutoTokenizer,
|
| 7 |
-
AutoModel,
|
| 8 |
AutoConfig,
|
|
|
|
| 9 |
PretrainedConfig,
|
| 10 |
PreTrainedModel,
|
| 11 |
-
TOKENIZER_MAPPING,
|
| 12 |
-
RobertaTokenizer
|
| 13 |
)
|
|
|
|
| 14 |
|
| 15 |
# ============================================================
|
| 16 |
-
#
|
| 17 |
# ============================================================
|
| 18 |
-
|
| 19 |
class CustomSNPConfig(PretrainedConfig):
|
| 20 |
model_type = "custom_snp"
|
| 21 |
|
|
@@ -40,47 +37,42 @@ class CustomSNPModel(PreTrainedModel):
|
|
| 40 |
return self.projection(x)
|
| 41 |
|
| 42 |
|
| 43 |
-
# --- Force registration order ---
|
| 44 |
-
TOKENIZER_MAPPING[CustomSNPConfig] = (RobertaTokenizer, RobertaTokenizer)
|
| 45 |
-
AutoConfig.register("custom_snp", CustomSNPConfig)
|
| 46 |
-
AutoModel.register(CustomSNPConfig, CustomSNPModel)
|
| 47 |
-
|
| 48 |
# ============================================================
|
| 49 |
-
# Environment
|
| 50 |
# ============================================================
|
| 51 |
os.environ["HF_HOME"] = "/tmp/huggingface"
|
| 52 |
MODEL_DIR = "./"
|
| 53 |
PORT = int(os.environ.get("PORT", 7860))
|
| 54 |
-
|
| 55 |
app = Flask(__name__)
|
| 56 |
|
| 57 |
# ============================================================
|
| 58 |
-
# Load Model & Tokenizer
|
| 59 |
# ============================================================
|
| 60 |
try:
|
| 61 |
print("Loading model from:", MODEL_DIR)
|
| 62 |
config = AutoConfig.from_pretrained(MODEL_DIR, trust_remote_code=True)
|
| 63 |
-
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
model.eval()
|
| 66 |
print("β
Custom SNP model loaded successfully.")
|
| 67 |
except Exception as e:
|
| 68 |
print("β Error loading custom model:", e)
|
| 69 |
raise e
|
| 70 |
|
| 71 |
-
# ============================================================
|
| 72 |
-
# Initialize weights (optional)
|
| 73 |
-
# ============================================================
|
| 74 |
-
def initialize_weights_if_missing(model):
|
| 75 |
-
for name, param in model.named_parameters():
|
| 76 |
-
if param.requires_grad and (torch.isnan(param).any() or torch.all(param == 0)):
|
| 77 |
-
nn.init.xavier_uniform_(param)
|
| 78 |
-
print(f"π§ Initialized missing weights: {name}")
|
| 79 |
-
|
| 80 |
-
initialize_weights_if_missing(model)
|
| 81 |
|
| 82 |
# ============================================================
|
| 83 |
-
#
|
| 84 |
# ============================================================
|
| 85 |
@app.route("/", methods=["GET"])
|
| 86 |
def home():
|
|
@@ -135,8 +127,9 @@ def test():
|
|
| 135 |
"embedding_preview": vector[0][:6]
|
| 136 |
})
|
| 137 |
|
|
|
|
| 138 |
# ============================================================
|
| 139 |
-
# Run
|
| 140 |
# ============================================================
|
| 141 |
if __name__ == "__main__":
|
| 142 |
print(f"π Starting SNP Universal Embedding API on port {PORT}")
|
|
|
|
| 3 |
import torch.nn as nn
|
| 4 |
from flask import Flask, request, jsonify
|
| 5 |
from transformers import (
|
|
|
|
|
|
|
| 6 |
AutoConfig,
|
| 7 |
+
AutoModel,
|
| 8 |
PretrainedConfig,
|
| 9 |
PreTrainedModel,
|
|
|
|
|
|
|
| 10 |
)
|
| 11 |
+
from transformers import RobertaTokenizerFast as RobertaTokenizer
|
| 12 |
|
| 13 |
# ============================================================
|
| 14 |
+
# Custom SNP Architecture (no Gunicorn complications)
|
| 15 |
# ============================================================
|
|
|
|
| 16 |
class CustomSNPConfig(PretrainedConfig):
|
| 17 |
model_type = "custom_snp"
|
| 18 |
|
|
|
|
| 37 |
return self.projection(x)
|
| 38 |
|
| 39 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
# ============================================================
|
| 41 |
+
# Environment
|
| 42 |
# ============================================================
|
| 43 |
os.environ["HF_HOME"] = "/tmp/huggingface"
|
| 44 |
MODEL_DIR = "./"
|
| 45 |
PORT = int(os.environ.get("PORT", 7860))
|
|
|
|
| 46 |
app = Flask(__name__)
|
| 47 |
|
| 48 |
# ============================================================
|
| 49 |
+
# Load Model & Tokenizer (direct tokenizer, no AutoTokenizer)
|
| 50 |
# ============================================================
|
| 51 |
try:
|
| 52 |
print("Loading model from:", MODEL_DIR)
|
| 53 |
config = AutoConfig.from_pretrained(MODEL_DIR, trust_remote_code=True)
|
| 54 |
+
|
| 55 |
+
# Use concrete tokenizer to avoid mapping issues
|
| 56 |
+
try:
|
| 57 |
+
tokenizer = RobertaTokenizer.from_pretrained(MODEL_DIR)
|
| 58 |
+
print("β
Loaded tokenizer from model directory.")
|
| 59 |
+
except Exception:
|
| 60 |
+
print("β οΈ Falling back to default roberta-base tokenizer.")
|
| 61 |
+
tokenizer = RobertaTokenizer.from_pretrained("roberta-base")
|
| 62 |
+
|
| 63 |
+
model = CustomSNPModel(config)
|
| 64 |
+
if os.path.exists(os.path.join(MODEL_DIR, "pytorch_model.bin")):
|
| 65 |
+
state = torch.load(os.path.join(MODEL_DIR, "pytorch_model.bin"), map_location="cpu")
|
| 66 |
+
model.load_state_dict(state, strict=False)
|
| 67 |
model.eval()
|
| 68 |
print("β
Custom SNP model loaded successfully.")
|
| 69 |
except Exception as e:
|
| 70 |
print("β Error loading custom model:", e)
|
| 71 |
raise e
|
| 72 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
# ============================================================
|
| 75 |
+
# Routes
|
| 76 |
# ============================================================
|
| 77 |
@app.route("/", methods=["GET"])
|
| 78 |
def home():
|
|
|
|
| 127 |
"embedding_preview": vector[0][:6]
|
| 128 |
})
|
| 129 |
|
| 130 |
+
|
| 131 |
# ============================================================
|
| 132 |
+
# Run Flask directly (no Gunicorn)
|
| 133 |
# ============================================================
|
| 134 |
if __name__ == "__main__":
|
| 135 |
print(f"π Starting SNP Universal Embedding API on port {PORT}")
|