CoolFace
Apppublic

Sadashiv/c

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
utils.py57 linesDownload Raw Back to root
1from pymongo import MongoClient2from dotenv import load_dotenv3import gridfs4import pickle5import os6 7# Load environment variables from .env file8load_dotenv()9 10def load_model_and_encoders(model_path, transformer_path, target_encoder_path):11    with open(model_path, 'rb') as f:12        model = pickle.load(f)13 14    with open(transformer_path, 'rb') as f:15        pipeline_encoder = pickle.load(f)16 17    with open(target_encoder_path, 'rb') as f:18        label_encoder = pickle.load(f)19    20    return model, pipeline_encoder, label_encoder21 22 23def retrieve_image_by_name_from_mongodb(file_name, database_name, collection_name):24    # Establish a connection to MongoDB25    client = MongoClient(os.getenv("MONGO_URL"))26 27    # Access the specified database28    db = client[database_name]29 30    # Create a new GridFS object (a specification for storing and retrieving large binary objects)31    fs = gridfs.GridFS(db, collection=collection_name)32 33    # Find the image data using the filename in the metadata34    image_data = fs.find_one({"filename": file_name})35 36    try:37        if image_data is None:38            raise ValueError("image_data is None")39        40        return image_data.read()41    except Exception as e:42        print(f"An error occurred: {e}")43        raise  # Re-raise the caught exception44 45 46def retrieve_data(database_name, collection_name, search_query):47    # Connect to MongoDB48    client = MongoClient(os.getenv("MONGO_URL"))49    database = client[database_name]50    collection = database[collection_name]51 52    # Search for the document based on the provided query53    result = collection.find_one(search_query)54 55    client.close()56    return result['data_info']57