CoolFace
Apppublic

ProsegurPruebas/embeddings-api

sourceHugging Faceupdated 3mo agoView on Hugging Face
0likes
app.py67 linesDownload Raw Back to root
1#!/usr/bin/env python32"""3API MINI DE EMBEDDINGS4Solo genera embeddings de texto (usado por el flujo de consulta en n8n).5La extracción de PDF/DOCX/XLSX/CSV se sigue haciendo localmente.6"""7 8import os9from flask import Flask, request, jsonify10from sentence_transformers import SentenceTransformer11 12app = Flask(__name__)13 14print("📄 Cargando modelo de embeddings (intfloat/e5-large)...")15model = SentenceTransformer('intfloat/e5-large')16print("✅ Modelo cargado!")17 18 19@app.route('/embeddings', methods=['POST'])20def get_embeddings():21    """Genera el embedding de un texto (compatible con el nodo HTTP Request5 de n8n)."""22    try:23        data = request.get_json()24        text_input = data.get('input', data.get('inputs', ''))25 26        if isinstance(text_input, list):27            text_input = text_input[0] if text_input else ''28 29        if not text_input:30            return jsonify({'error': 'No input provided'}), 40031 32        embedding = model.encode(text_input).tolist()33 34        print(f"📊 Embedding generado para: '{text_input[:50]}...'")35 36        response = {37            'data': [{38                'embedding': embedding,39                'index': 0,40                'object': 'embedding'41            }],42            'model': 'intfloat/e5-large',43            'object': 'list'44        }45 46        return jsonify(response)47 48    except Exception as e:49        print(f"❌ Error en embeddings: {str(e)}")50        return jsonify({'error': str(e)}), 50051 52 53@app.route('/health', methods=['GET'])54def health():55    """Health check endpoint"""56    return jsonify({57        'status': 'ok',58        'model': 'intfloat/e5-large',59        'ready': True60    })61 62 63if __name__ == '__main__':64    port = int(os.environ.get('PORT', 5000))65    print(f"🌐 Server running on http://0.0.0.0:{port}")66    app.run(host='0.0.0.0', port=port, debug=False)67