created backend/database.py
Browse files- backend/database.py +65 -0
- backend/main.py +0 -0
- backend/ml_model.py +0 -0
backend/database.py
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import cloudinary
|
| 3 |
+
import cloudinary.uploader
|
| 4 |
+
import cloudinary.api
|
| 5 |
+
from motor.motor.asyncio import AsyncIOMotorClient
|
| 6 |
+
from dotenv import load_dotenv
|
| 7 |
+
import logging
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
# Load environment variables from .env file
|
| 11 |
+
load_dotenv()
|
| 12 |
+
|
| 13 |
+
# Configure logging
|
| 14 |
+
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
| 15 |
+
logging.info('Environment variables loaded successfully.')
|
| 16 |
+
|
| 17 |
+
# MongoDB configuration
|
| 18 |
+
MONGODB_URI = os.getenv('MONGODB_URI')
|
| 19 |
+
DB_NAME = os.getenv('DATABASE_NAME')
|
| 20 |
+
|
| 21 |
+
# Cloudinary Configuration
|
| 22 |
+
cloudinary.config(
|
| 23 |
+
cloud_name=os.getenv('CLOUDINARY_CLOUD_NAME'),
|
| 24 |
+
api_key=os.getenv('CLOUDINARY_API_KEY'),
|
| 25 |
+
api_secret=os.getenv('CLOUDINARY_API_SECRET'),
|
| 26 |
+
secure=True
|
| 27 |
+
)
|
| 28 |
+
|
| 29 |
+
def upload_image_to_cloud(file_byte: bytes, folder_name: str='xrays') -> str:
|
| 30 |
+
"""
|
| 31 |
+
Uploads an image to Cloudinary and returns the URL of the uploaded image.
|
| 32 |
+
|
| 33 |
+
Args:
|
| 34 |
+
file_byte (bytes): The byte content of the image to be uploaded.
|
| 35 |
+
folder_name (str): The folder in Cloudinary where the image will be stored.
|
| 36 |
+
|
| 37 |
+
Returns:
|
| 38 |
+
str: The URL of the uploaded image.
|
| 39 |
+
|
| 40 |
+
Raises:
|
| 41 |
+
Exception: If there is an error during the upload process.
|
| 42 |
+
|
| 43 |
+
"""
|
| 44 |
+
try:
|
| 45 |
+
logging.info('Uploading image to Cloudinary...')
|
| 46 |
+
response = cloudinary.uploader.upload(file_byte,
|
| 47 |
+
folder=f'precision_diagnostics/{folder_name}')
|
| 48 |
+
logging.info('Image uploaded successfully.')
|
| 49 |
+
return response.get('secure_url')
|
| 50 |
+
except Exception as e:
|
| 51 |
+
logging.error(f'Error uploading image: {e}')
|
| 52 |
+
raise e
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
# Initialize MongoDB client
|
| 57 |
+
try:
|
| 58 |
+
client = AsyncIOMotorClient(MONGODB_URI)
|
| 59 |
+
db = client[DB_NAME]
|
| 60 |
+
logging.info('Connected to MongoDB successfully.')
|
| 61 |
+
except Exception as e:
|
| 62 |
+
logging.error(f'Error connecting to MongoDB: {e}')
|
| 63 |
+
raise e
|
| 64 |
+
|
| 65 |
+
diagnostic_records = db['records']
|
backend/main.py
ADDED
|
File without changes
|
backend/ml_model.py
ADDED
|
File without changes
|