Download api/populate_db.py from iBrokeTheCode/Image_Classifier_with_CNN: direct link, hf CLI and curl.
- Browser
- Download file 1.97 kB
-
https://huggingface.co/spaces/iBrokeTheCode/Image_Classifier_with_CNN/resolve/main/api/populate_db.py
- Command line
-
hf download hf://spaces/iBrokeTheCode/Image_Classifier_with_CNN/api/populate_db.py
-
curl -L -o populate_db.py https://huggingface.co/spaces/iBrokeTheCode/Image_Classifier_with_CNN/resolve/main/api/populate_db.py
1.97 kB
| import psycopg2 | |
| from app import settings as config | |
| from app.db import Base | |
| from app.user.models import User | |
| from psycopg2.errors import DuplicateDatabase | |
| from sqlalchemy import create_engine | |
| from sqlalchemy.orm import sessionmaker | |
| # Database configuration | |
| DATABASE_USERNAME = config.DATABASE_USERNAME | |
| DATABASE_PASSWORD = config.DATABASE_PASSWORD | |
| DATABASE_HOST = config.DATABASE_HOST | |
| DATABASE_NAME = config.DATABASE_NAME | |
| # Create the initial connection URL to PostgreSQL (without specifying the database) | |
| initial_connection_url = ( | |
| f"postgresql://{DATABASE_USERNAME}:{DATABASE_PASSWORD}@{DATABASE_HOST}/postgres" | |
| ) | |
| print(initial_connection_url) | |
| conn = None | |
| # Connect to PostgreSQL to create the database if it doesn't exist | |
| try: | |
| conn = psycopg2.connect(initial_connection_url) | |
| conn.autocommit = True | |
| cursor = conn.cursor() | |
| # Create the database | |
| cursor.execute(f"CREATE DATABASE {DATABASE_NAME}") | |
| print(f"Database '{DATABASE_NAME}' created successfully") | |
| except DuplicateDatabase as e: | |
| if "already exists" in str(e): | |
| print(f"Database '{DATABASE_NAME}' already exists.") | |
| else: | |
| print(f"Error creating database: {e}") | |
| finally: | |
| if conn: | |
| cursor.close() | |
| conn.close() | |
| # Database connection URL to the newly created database | |
| SQLALCHEMY_DATABASE_URL = f"postgresql://{DATABASE_USERNAME}:{DATABASE_PASSWORD}@{DATABASE_HOST}/{DATABASE_NAME}" | |
| print(SQLALCHEMY_DATABASE_URL) | |
| # Create engine | |
| engine = create_engine(SQLALCHEMY_DATABASE_URL) | |
| # Drop all tables if they exist | |
| Base.metadata.drop_all(engine) | |
| print("Tables dropped") | |
| # Create all tables | |
| Base.metadata.create_all(engine) | |
| print("Tables created") | |
| # Populate database with a default user | |
| print("Populating database with default user") | |
| Session = sessionmaker(bind=engine) | |
| session = Session() | |
| user = User( | |
| name="Admin User", | |
| password="admin", | |
| email="admin@example.com", | |
| ) | |
| session.add(user) | |
| session.commit() | |
| print("Default user added") | |