Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Flask, request, jsonify
|
2 |
+
from flask_cors import CORS
|
3 |
+
from chromadb.errors import ChromaError
|
4 |
+
|
5 |
+
from your_chromedb_module import (
|
6 |
+
add_document,
|
7 |
+
search,
|
8 |
+
delete_document,
|
9 |
+
delete_collection,
|
10 |
+
add_image_to_chroma
|
11 |
+
)
|
12 |
+
|
13 |
+
app = Flask(__name__)
|
14 |
+
CORS(app) # Permettre les requêtes cross-origin
|
15 |
+
|
16 |
+
@app.route("/delete", methods=["GET"])
|
17 |
+
def delete():
|
18 |
+
try:
|
19 |
+
delete_collection("D24F9DF2BB1C815C9A267C7DB5E3C9B9")
|
20 |
+
return "Collection supprimée avec succès !", 200
|
21 |
+
except ChromaError as e:
|
22 |
+
return jsonify({"error": str(e)}), 500
|
23 |
+
|
24 |
+
@app.route("/embedding", methods=["POST"])
|
25 |
+
def embedding():
|
26 |
+
data = request.json
|
27 |
+
collection_name = data.get("collectionName")
|
28 |
+
doc_id = data.get("id")
|
29 |
+
text = data.get("text")
|
30 |
+
metadata = data.get("metadata")
|
31 |
+
try:
|
32 |
+
add_document(collection_name, doc_id, text, metadata)
|
33 |
+
return jsonify({"message": "Document ajouté avec succès"}), 200
|
34 |
+
except Exception as e:
|
35 |
+
return jsonify({"error": str(e)}), 500
|
36 |
+
|
37 |
+
@app.route("/image-embedding", methods=["POST"])
|
38 |
+
def image_embedding():
|
39 |
+
data = request.json
|
40 |
+
collection_name = data.get("collectionName")
|
41 |
+
doc_id = data.get("id")
|
42 |
+
image_path = data.get("imagePath")
|
43 |
+
metadata = data.get("metadata")
|
44 |
+
try:
|
45 |
+
add_image_to_chroma(collection_name, doc_id, image_path, metadata)
|
46 |
+
return jsonify({"message": "Image ajoutée avec succès"}), 200
|
47 |
+
except Exception as e:
|
48 |
+
return jsonify({"error": str(e)}), 500
|
49 |
+
|
50 |
+
@app.route("/search", methods=["POST"])
|
51 |
+
def search_query():
|
52 |
+
data = request.json
|
53 |
+
query = data.get("query")
|
54 |
+
collection_name = data.get("collectionName")
|
55 |
+
n_results = data.get("nResults", 10)
|
56 |
+
try:
|
57 |
+
results = search(collection_name, query, n_results)
|
58 |
+
return jsonify({"results": results}), 200
|
59 |
+
except Exception as e:
|
60 |
+
return jsonify({"error": str(e)}), 500
|
61 |
+
|
62 |
+
if __name__ == "__main__":
|
63 |
+
app.run(host="0.0.0.0",debug=True)
|