VectorSearch / api /app.py
Jindra Snobl
Added all files from first attempt
633d9b3
from flask import Flask, request, jsonify, Blueprint
import os
from dotenv import load_dotenv
from api.embed import get_embeddings
# from huggingface_hub import login
load_dotenv()
app = Flask(__name__)
hf_token = os.environ.get("HF_TOKEN", "")
# login(hf_token)
HASURA_SECRET_HEADER_VALUE = os.getenv("HASURA_SECRET_HEADER_VALUE", "secret")
HASURA_EVENTS_HEADER = os.getenv("HASURA_EVENTS_HEADER", "x-hasura-event-secret")
@app.route("/health", methods=["GET"])
def health_check():
return jsonify({"status": "OK"}), 200
# Create a blueprint for the routes that require middleware
api_bp = Blueprint("api", __name__)
# @api_bp.before_request
# def verify_hasura_secret():
# secret = request.headers.get(HASURA_EVENTS_HEADER)
# if not secret or secret != HASURA_SECRET_HEADER_VALUE:
# return jsonify({"message": "Invalid secret"}), 401
@api_bp.route("/get_embedding", methods=["post"])
def get_embedding():
try:
data = request.get_json()
query = data.get("query")
if not query:
return jsonify({"message": "Query is required"}), 400
embeddings = get_embeddings(query)
return (
jsonify(
{
"query": query,
"embeddings": embeddings,
}
),
200,
)
except Exception as e:
return jsonify({"message": f"Error: {str(e)}"}), 500
app.register_blueprint(api_bp)
if __name__ == "__main__":
app.run(debug=True)