Spaces:
Running
Running
File size: 2,021 Bytes
4785556 b21bc91 4785556 321371d 4785556 321371d 4785556 65ec8b4 |
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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
from flask import Flask, request, jsonify
from flask_cors import CORS
from chromadb.errors import ChromaError
from chromedb_service import (
add_document,
search,
delete_document,
delete_collection,
add_image_to_chroma
)
app = Flask(__name__)
CORS(app) # Permettre les requêtes cross-origin
@app.route("/delete", methods=["GET"])
def delete():
try:
delete_collection("D24F9DF2BB1C815C9A267C7DB5E3C9B9")
return "Collection supprimée avec succès !", 200
except ChromaError as e:
return jsonify({"error": str(e)}), 500
@app.route("/embedding", methods=["POST"])
def embedding():
data = request.json
collection_name = data.get("collectionName")
doc_id = data.get("id")
text = data.get("text")
metadata = data.get("metadata")
try:
add_document(collection_name, doc_id, text, metadata)
return jsonify({"message": "Document ajouté avec succès"}), 200
except Exception as e:
return jsonify({"error": str(e)}), 500
@app.route("/image-embedding", methods=["POST"])
def image_embedding():
data = request.json
collection_name = data.get("collectionName")
doc_id = data.get("id")
image_path = data.get("imagePath")
metadata = data.get("metadata")
try:
add_image_to_chroma(collection_name, doc_id, image_path, metadata)
return jsonify({"message": "Image ajoutée avec succès"}), 200
except Exception as e:
return jsonify({"error": str(e)}), 500
@app.route("/search", methods=["POST"])
def search_query():
data = request.json
query = data.get("query")
collection_name = data.get("collectionName")
metadata = data.get("metadata")
n_results = data.get("nResults", 10)
try:
results = search(collection_name, query, metadata, n_results)
return jsonify({"results": results}), 200
except Exception as e:
return jsonify({"error": str(e)}), 500
if __name__ == "__main__":
app.run(host="0.0.0.0",debug=True, port=7860)
|