Spaces:
Runtime error
Runtime error
Create chromedb_service.py
Browse files- chromedb_service.py +88 -0
chromedb_service.py
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
from PIL import Image
|
| 3 |
+
import numpy as np
|
| 4 |
+
import tensorflow as tf
|
| 5 |
+
import chromadb
|
| 6 |
+
from chromadb.config import Settings
|
| 7 |
+
|
| 8 |
+
# Fonction pour télécharger une image depuis une URL
|
| 9 |
+
def download_image(url):
|
| 10 |
+
response = requests.get(url)
|
| 11 |
+
if response.status_code != 200:
|
| 12 |
+
raise Exception(f"Erreur lors du téléchargement de l'image : {response.status_code}")
|
| 13 |
+
return Image.open(io.BytesIO(response.content))
|
| 14 |
+
|
| 15 |
+
# Fonction pour encoder une image en vecteurs à partir d'une URL
|
| 16 |
+
def encode_image_from_url(image_url):
|
| 17 |
+
image = download_image(image_url)
|
| 18 |
+
image = image.resize((224, 224)) # Redimensionner à 224x224
|
| 19 |
+
image_array = np.array(image) / 255.0 # Normaliser les valeurs des pixels
|
| 20 |
+
image_tensor = tf.convert_to_tensor(image_array, dtype=tf.float32)
|
| 21 |
+
image_tensor = tf.expand_dims(image_tensor, axis=0) # Ajouter une dimension pour le batch
|
| 22 |
+
|
| 23 |
+
# Charger le modèle MobileNet
|
| 24 |
+
model = tf.keras.applications.MobileNetV2(weights='imagenet', include_top=False, pooling='avg')
|
| 25 |
+
embeddings = model(image_tensor)
|
| 26 |
+
return embeddings.numpy()[0] # Retourner les vecteurs sous forme de tableau
|
| 27 |
+
|
| 28 |
+
# Ajouter une image dans ChromaDB
|
| 29 |
+
def add_image_to_chroma(collection_name, id, image_url, metadata):
|
| 30 |
+
vector = encode_image_from_url(image_url)
|
| 31 |
+
chroma_client = chromadb.HttpClient(host='https://stable-diffusion-engine.oneiro-lego.com')
|
| 32 |
+
collection = chroma_client.get_or_create_collection(
|
| 33 |
+
name=collection_name, dimension=len(vector)
|
| 34 |
+
)
|
| 35 |
+
collection.add(
|
| 36 |
+
ids=[id],
|
| 37 |
+
embeddings=[vector],
|
| 38 |
+
metadatas=[metadata]
|
| 39 |
+
)
|
| 40 |
+
print(f"Image {image_url} ajoutée avec succès !")
|
| 41 |
+
|
| 42 |
+
# Ajouter un document dans ChromaDB
|
| 43 |
+
def add_document(collection_name, id, text, metadata):
|
| 44 |
+
chroma_client = chromadb.HttpClient(host='https://stable-diffusion-engine.oneiro-lego.com')
|
| 45 |
+
collection = chroma_client.get_or_create_collection(name=collection_name)
|
| 46 |
+
collection.upsert(
|
| 47 |
+
documents=[text],
|
| 48 |
+
ids=[id],
|
| 49 |
+
metadatas=[metadata]
|
| 50 |
+
)
|
| 51 |
+
print(f"Document {id} ajouté avec succès !")
|
| 52 |
+
|
| 53 |
+
# Supprimer un document dans ChromaDB
|
| 54 |
+
def delete_document(collection_name, id):
|
| 55 |
+
chroma_client = chromadb.HttpClient(host='https://stable-diffusion-engine.oneiro-lego.com')
|
| 56 |
+
collection = chroma_client.get_or_create_collection(name=collection_name)
|
| 57 |
+
collection.delete(ids=[id])
|
| 58 |
+
print(f"Document {id} supprimé avec succès !")
|
| 59 |
+
|
| 60 |
+
# Supprimer une collection dans ChromaDB
|
| 61 |
+
def delete_collection(collection_name):
|
| 62 |
+
chroma_client = chromadb.HttpClient(host='https://stable-diffusion-engine.oneiro-lego.com')
|
| 63 |
+
chroma_client.delete_collection(name=collection_name)
|
| 64 |
+
print(f"Collection {collection_name} supprimée avec succès !")
|
| 65 |
+
|
| 66 |
+
# Recherche dans une collection
|
| 67 |
+
def search(collection_name, query, metadata, n_results):
|
| 68 |
+
chroma_client = chromadb.HttpClient(host='https://stable-diffusion-engine.oneiro-lego.com')
|
| 69 |
+
collection = chroma_client.get_or_create_collection(name=collection_name)
|
| 70 |
+
results = collection.query(
|
| 71 |
+
query_texts=query,
|
| 72 |
+
where=metadata,
|
| 73 |
+
n_results=n_results
|
| 74 |
+
)
|
| 75 |
+
return parse_chromadb_response(results)
|
| 76 |
+
|
| 77 |
+
# Analyse des réponses de ChromaDB
|
| 78 |
+
def parse_chromadb_response(response):
|
| 79 |
+
results = [
|
| 80 |
+
{
|
| 81 |
+
"id": response["ids"][0][i],
|
| 82 |
+
"distance": response["distances"][0][i],
|
| 83 |
+
"document": response["documents"][0][i],
|
| 84 |
+
"metadata": response["metadatas"][0][i] if response["metadatas"] and len(response["metadatas"][0]) > i else None
|
| 85 |
+
}
|
| 86 |
+
for i in range(len(response["ids"][0]))
|
| 87 |
+
]
|
| 88 |
+
return results
|