multi_model_detection / mongo_lib.py
andythebest's picture
add mongodb log
37e6c30 verified
#python -m pip install "pymongo[srv]==3.11"
from pymongo.mongo_client import MongoClient
from datetime import datetime
import os
def insert_mongodb_log(model_name,document:dict):
mongo_uri = os.getenv('mongo_uri')
client = MongoClient(mongo_uri)
try:
#client.admin.command('ping')
# 選擇數據庫,如果不存在會自動創建
db = client["huggingface-space"] # 替換成你想要的數據庫名稱
# 選擇集合,如果不存在會自動創建
collection = db["space-log"] # 替換成你想要的集合名稱
# 將文檔插入到集合中
document.update({"model_name":model_name,"process_time": str(datetime.now())})
result = collection.insert_one(document)
# 打印插入結果
#print(f"Document inserted with ID: {result.inserted_id}")
except Exception as e:
print(e)
if __name__ == "__main__":
# 創建JSON文檔
document = {
"msg": "hello world",
"status": "success",
}
insert_mongodb_log("test_client",document)