93 lines
3.3 KiB
Python
93 lines
3.3 KiB
Python
#!/usr/bin/env python3
|
|
"""EmbeddingGemma HTTP server - Metal GPU (MPS) accelerated, compatible with M4/M5."""
|
|
|
|
import argparse, json, time, torch
|
|
from flask import Flask, request, jsonify
|
|
from transformers import AutoModel, AutoTokenizer
|
|
import numpy as np
|
|
|
|
app = Flask(__name__)
|
|
|
|
MODEL = None
|
|
TOKENIZER = None
|
|
DEVICE = None
|
|
|
|
def load_model(model_path: str = ""):
|
|
global MODEL, TOKENIZER, DEVICE
|
|
if MODEL is not None:
|
|
return
|
|
import os
|
|
# 一律使用本地模型目錄,絕不上網
|
|
local = os.path.expanduser("~/momentry/models/embeddinggemma-300m")
|
|
if not (os.path.isdir(local) and os.path.isfile(os.path.join(local, "config.json"))):
|
|
raise RuntimeError(
|
|
f"[EmbeddingGemma] Local model not found at {local}. "
|
|
"Please copy the model files (config.json, model.safetensors, tokenizer*) to this directory."
|
|
)
|
|
DEVICE = "mps" if torch.backends.mps.is_available() else "cpu"
|
|
dtype = torch.float32
|
|
print(f"[EmbeddingGemma] Loading model from local: {local} on {DEVICE} (dtype={dtype})...")
|
|
t0 = time.time()
|
|
MODEL = AutoModel.from_pretrained(
|
|
local,
|
|
torch_dtype=dtype,
|
|
trust_remote_code=True,
|
|
).eval().to(DEVICE)
|
|
TOKENIZER = AutoTokenizer.from_pretrained(local, trust_remote_code=True)
|
|
print(f"[EmbeddingGemma] Loaded in {time.time()-t0:.1f}s on {DEVICE}")
|
|
|
|
def embed(texts: list[str]) -> list[list[float]]:
|
|
inputs = TOKENIZER(texts, padding=True, truncation=True, return_tensors="pt").to(DEVICE)
|
|
with torch.no_grad():
|
|
outputs = MODEL(**inputs)
|
|
mask = inputs["attention_mask"].unsqueeze(-1).to(outputs.last_hidden_state.dtype)
|
|
pooled = (outputs.last_hidden_state * mask).sum(dim=1) / (mask.sum(dim=1) + 1e-9)
|
|
pooled = torch.nn.functional.normalize(pooled, p=2, dim=1)
|
|
return pooled.cpu().numpy().tolist()
|
|
|
|
@app.route("/v1/embeddings", methods=["POST"])
|
|
def embeddings():
|
|
start_time = time.time()
|
|
data = request.get_json()
|
|
texts = data.get("input", [])
|
|
if isinstance(texts, str):
|
|
texts = [texts]
|
|
if not texts:
|
|
return jsonify({"error": "empty input"}), 400
|
|
try:
|
|
# Tokenize to get token count
|
|
inputs = TOKENIZER(texts, padding=True, truncation=True, return_tensors="pt")
|
|
total_tokens = inputs["input_ids"].shape[1] * len(texts)
|
|
|
|
# Generate embeddings
|
|
emb = embed(texts)
|
|
|
|
duration_ms = int((time.time() - start_time) * 1000)
|
|
|
|
result = {
|
|
"data": [{"embedding": e, "index": i} for i, e in enumerate(emb)],
|
|
"model": "embeddinggemma-300m",
|
|
"usage": {
|
|
"prompt_tokens": total_tokens,
|
|
"completion_tokens": 0,
|
|
"total_tokens": total_tokens
|
|
},
|
|
"timings": {
|
|
"duration_ms": duration_ms
|
|
}
|
|
}
|
|
return jsonify(result)
|
|
except Exception as e:
|
|
return jsonify({"error": str(e)}), 500
|
|
|
|
@app.route("/health", methods=["GET"])
|
|
def health():
|
|
return jsonify({"status": "ok", "device": str(DEVICE)})
|
|
|
|
if __name__ == "__main__":
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument("--port", type=int, default=11436)
|
|
args = parser.parse_args()
|
|
load_model() # 一律使用 ~/momentry/models/embeddinggemma-300m,絕不上網
|
|
app.run(host="0.0.0.0", port=args.port, threaded=True)
|