|
|
from flask import Flask, request, jsonify |
|
|
from sentence_transformers import SentenceTransformer |
|
|
import os |
|
|
import numpy as np |
|
|
|
|
|
|
|
|
model = SentenceTransformer('naver/splade_v2_distil') |
|
|
|
|
|
app = Flask(__name__) |
|
|
|
|
|
@app.route('/vectorize', methods=['POST']) |
|
|
def vectorize(): |
|
|
try: |
|
|
json_data = request.get_json() |
|
|
texts = json_data.get('inputs') |
|
|
|
|
|
if not texts: |
|
|
return jsonify({"error": "No se encontró el campo 'inputs' en la solicitud."}), 400 |
|
|
|
|
|
|
|
|
embedding_array = model.encode(texts) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
indices = np.where(embedding_array > 0)[0] |
|
|
values = embedding_array[indices] |
|
|
|
|
|
|
|
|
serializable_result = { |
|
|
'indices': indices.tolist(), |
|
|
'values': values.tolist() |
|
|
} |
|
|
|
|
|
return jsonify(serializable_result) |
|
|
|
|
|
except Exception as e: |
|
|
return jsonify({"error": str(e)}), 500 |
|
|
|
|
|
@app.route('/', methods=['GET']) |
|
|
def health_check(): |
|
|
return "API para SPLADE está funcionando!" |
|
|
|
|
|
if __name__ == '__main__': |
|
|
app.run(host='0.0.0.0', port=7860) |