Spaces:
Runtime error
Runtime error
Download backend/database.py from Mayank11207/scamdetect-backend: direct link, hf CLI and curl.
- Browser
- Download file 958 Bytes
-
https://huggingface.co/spaces/Mayank11207/scamdetect-backend/resolve/main/backend/database.py
- Command line
-
hf download hf://spaces/Mayank11207/scamdetect-backend/backend/database.py
-
curl -L -o database.py https://huggingface.co/spaces/Mayank11207/scamdetect-backend/resolve/main/backend/database.py
958 Bytes
| import os | |
| from sqlalchemy import create_engine | |
| from sqlalchemy.orm import sessionmaker | |
| from sqlalchemy.orm import declarative_base | |
| # Use environment variable for production (PostgreSQL on Render) | |
| # Falls back to SQLite for local development | |
| DATABASE_URL = os.getenv("DATABASE_URL", "sqlite:///./scamdetect.db") | |
| # Render provides postgres:// but SQLAlchemy needs postgresql:// | |
| if DATABASE_URL.startswith("postgres://"): | |
| DATABASE_URL = DATABASE_URL.replace("postgres://", "postgresql://", 1) | |
| # check_same_thread is needed only for SQLite | |
| connect_args = {} | |
| if "sqlite" in DATABASE_URL: | |
| connect_args = {"check_same_thread": False} | |
| engine = create_engine(DATABASE_URL, connect_args=connect_args) | |
| SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine) | |
| Base = declarative_base() | |
| # Dependency for API endpoints to get the DB session | |
| def get_db(): | |
| db = SessionLocal() | |
| try: | |
| yield db | |
| finally: | |
| db.close() | |