Spaces:
Runtime error
Runtime error
Download src/database/db.py from Zok213/road-damage: direct link, hf CLI and curl.
- Browser
- Download file 1.07 kB
-
https://huggingface.co/spaces/Zok213/road-damage/resolve/main/src/database/db.py
- Command line
-
hf download hf://spaces/Zok213/road-damage/src/database/db.py
-
curl -L -o db.py https://huggingface.co/spaces/Zok213/road-damage/resolve/main/src/database/db.py
1.07 kB
| from sqlalchemy import create_engine | |
| from sqlalchemy.ext.declarative import declarative_base | |
| from sqlalchemy.orm import sessionmaker | |
| import os | |
| from dotenv import load_dotenv | |
| load_dotenv() | |
| # Get DATABASE_URL from environment, fallback to SQLite for local/HuggingFace deployment | |
| DATABASE_URL = os.getenv("DATABASE_URL") | |
| if DATABASE_URL: | |
| # Fix Heroku-style postgres:// URLs | |
| if DATABASE_URL.startswith("postgres://"): | |
| DATABASE_URL = DATABASE_URL.replace("postgres://", "postgresql://", 1) | |
| else: | |
| # Fallback to SQLite for HuggingFace Spaces / local development without database | |
| DATABASE_URL = "sqlite:///./data/road_damage.db" | |
| print(f"[DB] No DATABASE_URL set, using SQLite: {DATABASE_URL}") | |
| # Create engine | |
| engine = create_engine( | |
| DATABASE_URL, | |
| connect_args={"check_same_thread": False} if DATABASE_URL.startswith("sqlite") else {} | |
| ) | |
| SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine) | |
| Base = declarative_base() | |
| def get_db(): | |
| db = SessionLocal() | |
| try: | |
| yield db | |
| finally: | |
| db.close() | |