chromadb-api / app.py
Saad0KH's picture
Create app.py
4785556 verified
raw
history blame
1.97 kB
from flask import Flask, request, jsonify
from flask_cors import CORS
from chromadb.errors import ChromaError
from your_chromedb_module 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")
n_results = data.get("nResults", 10)
try:
results = search(collection_name, query, 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)