-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
36 lines (26 loc) · 1007 Bytes
/
app.py
File metadata and controls
36 lines (26 loc) · 1007 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#!/usr/bin/env python3
import os
from flask import Flask, request, jsonify
from sentence_transformers import SentenceTransformer
# 🔐 Fix permission error by redirecting HF cache to a safe location
os.environ['HF_HOME'] = '/tmp/huggingface'
# Initialize Flask and the sentence embedding model
app = Flask(__name__)
model = SentenceTransformer('sentence-transformers/all-MiniLM-L6-v2', cache_folder='/tmp/huggingface')
@app.route("/")
def health():
return jsonify({"status": "ok", "message": "Embedding service is running."})
@app.route("/embed", methods=["POST"])
def embed_text():
data = request.json
if not data or "text" not in data:
return jsonify({"error": "No text provided"}), 400
text = data["text"]
embedding = model.encode(text).tolist()
return jsonify({
"text": text,
"embedding": embedding
})
if __name__ == "__main__":
port = int(os.getenv("PORT", 7840))
app.run(host="0.0.0.0", port=port, debug=True, use_reloader=False)