Update api_inference.py
Browse files- api_inference.py +20 -10
api_inference.py
CHANGED
|
@@ -6,7 +6,7 @@ from transformers import (
|
|
| 6 |
AutoTokenizer,
|
| 7 |
AutoModel,
|
| 8 |
AutoConfig,
|
| 9 |
-
PretrainedConfig,
|
| 10 |
PreTrainedModel,
|
| 11 |
)
|
| 12 |
|
|
@@ -25,18 +25,23 @@ PORT = int(os.environ.get("PORT", 7860))
|
|
| 25 |
app = Flask(__name__)
|
| 26 |
|
| 27 |
# ============================================================
|
| 28 |
-
# Register Custom SNP Architecture
|
| 29 |
# ============================================================
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
| 32 |
model_type = "custom_snp"
|
| 33 |
|
|
|
|
|
|
|
|
|
|
| 34 |
class CustomSNPModel(PreTrainedModel):
|
| 35 |
config_class = CustomSNPConfig
|
| 36 |
|
| 37 |
def __init__(self, config):
|
| 38 |
super().__init__(config)
|
| 39 |
-
# This
|
| 40 |
self.shared_encoder = AutoModel.from_config(config)
|
| 41 |
|
| 42 |
hidden_size = self.shared_encoder.config.hidden_size
|
|
@@ -63,6 +68,7 @@ class CustomSNPModel(PreTrainedModel):
|
|
| 63 |
proj = self.projection(x)
|
| 64 |
return proj # Return the final projection
|
| 65 |
|
|
|
|
| 66 |
# Register model so AutoModel recognizes it
|
| 67 |
AutoConfig.register("custom_snp", CustomSNPConfig)
|
| 68 |
AutoModel.register(CustomSNPConfig, CustomSNPModel)
|
|
@@ -75,8 +81,12 @@ try:
|
|
| 75 |
print("Loading model from:", MODEL_DIR)
|
| 76 |
|
| 77 |
# trust_remote_code=True is essential for this to work
|
|
|
|
| 78 |
config = AutoConfig.from_pretrained(MODEL_DIR, trust_remote_code=True)
|
|
|
|
| 79 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_DIR, trust_remote_code=True)
|
|
|
|
|
|
|
| 80 |
model = AutoModel.from_pretrained(MODEL_DIR, config=config, trust_remote_code=True)
|
| 81 |
|
| 82 |
model.eval()
|
|
@@ -89,7 +99,7 @@ except Exception as e:
|
|
| 89 |
|
| 90 |
|
| 91 |
# ============================================================
|
| 92 |
-
# Flask API Routes
|
| 93 |
# ============================================================
|
| 94 |
@app.route("/", methods=["GET"])
|
| 95 |
def home():
|
|
@@ -105,19 +115,19 @@ def health():
|
|
| 105 |
def embed():
|
| 106 |
try:
|
| 107 |
data = request.get_json(force=True)
|
| 108 |
-
text = data.get("text", "")
|
| 109 |
if not text:
|
| 110 |
return jsonify({"error": "Text is required"}), 400
|
| 111 |
|
| 112 |
# Tokenize the text
|
| 113 |
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
|
| 114 |
|
| 115 |
-
# Run inference
|
| 116 |
with torch.no_grad():
|
| 117 |
embeddings = model(**inputs)
|
| 118 |
|
| 119 |
-
#
|
| 120 |
-
return jsonify({"embedding": embeddings.tolist()})
|
| 121 |
|
| 122 |
except Exception as e:
|
| 123 |
print(f"ERROR in /embed: {e}")
|
|
|
|
| 6 |
AutoTokenizer,
|
| 7 |
AutoModel,
|
| 8 |
AutoConfig,
|
| 9 |
+
PretrainedConfig, # <-- 1. YOURS: Using PretrainedConfig (This is correct)
|
| 10 |
PreTrainedModel,
|
| 11 |
)
|
| 12 |
|
|
|
|
| 25 |
app = Flask(__name__)
|
| 26 |
|
| 27 |
# ============================================================
|
| 28 |
+
# Register Custom SNP Architecture
|
| 29 |
# ============================================================
|
| 30 |
+
|
| 31 |
+
# This is from YOUR original file. It is correct because
|
| 32 |
+
# PretrainedConfig has the .from_dict() method.
|
| 33 |
+
class CustomSNPConfig(PretrainedConfig):
|
| 34 |
model_type = "custom_snp"
|
| 35 |
|
| 36 |
+
|
| 37 |
+
# This is from MY file. This is the fix for your 500 error,
|
| 38 |
+
# as it loads the real transformer model.
|
| 39 |
class CustomSNPModel(PreTrainedModel):
|
| 40 |
config_class = CustomSNPConfig
|
| 41 |
|
| 42 |
def __init__(self, config):
|
| 43 |
super().__init__(config)
|
| 44 |
+
# This correctly loads the base transformer
|
| 45 |
self.shared_encoder = AutoModel.from_config(config)
|
| 46 |
|
| 47 |
hidden_size = self.shared_encoder.config.hidden_size
|
|
|
|
| 68 |
proj = self.projection(x)
|
| 69 |
return proj # Return the final projection
|
| 70 |
|
| 71 |
+
|
| 72 |
# Register model so AutoModel recognizes it
|
| 73 |
AutoConfig.register("custom_snp", CustomSNPConfig)
|
| 74 |
AutoModel.register(CustomSNPConfig, CustomSNPModel)
|
|
|
|
| 81 |
print("Loading model from:", MODEL_DIR)
|
| 82 |
|
| 83 |
# trust_remote_code=True is essential for this to work
|
| 84 |
+
# This will now succeed because CustomSNPConfig has .from_dict()
|
| 85 |
config = AutoConfig.from_pretrained(MODEL_DIR, trust_remote_code=True)
|
| 86 |
+
|
| 87 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_DIR, trust_remote_code=True)
|
| 88 |
+
|
| 89 |
+
# This will load your custom model architecture
|
| 90 |
model = AutoModel.from_pretrained(MODEL_DIR, config=config, trust_remote_code=True)
|
| 91 |
|
| 92 |
model.eval()
|
|
|
|
| 99 |
|
| 100 |
|
| 101 |
# ============================================================
|
| 102 |
+
# Flask API Routes
|
| 103 |
# ============================================================
|
| 104 |
@app.route("/", methods=["GET"])
|
| 105 |
def home():
|
|
|
|
| 115 |
def embed():
|
| 116 |
try:
|
| 117 |
data = request.get_json(force=True)
|
| 118 |
+
text = data.get("text", "") # Your Colab script is correct, it sends "text"
|
| 119 |
if not text:
|
| 120 |
return jsonify({"error": "Text is required"}), 400
|
| 121 |
|
| 122 |
# Tokenize the text
|
| 123 |
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
|
| 124 |
|
| 125 |
+
# Run inference (no .float() conversion needed)
|
| 126 |
with torch.no_grad():
|
| 127 |
embeddings = model(**inputs)
|
| 128 |
|
| 129 |
+
# Return the projection
|
| 130 |
+
return jsonify({"embedding": embeddings.tolist()}) # Your Colab script is correct, it expects "embedding"
|
| 131 |
|
| 132 |
except Exception as e:
|
| 133 |
print(f"ERROR in /embed: {e}")
|