Spaces:
Runtime error
Runtime error
Nikhil Pravin Pise
feat: production upgrade β agentic RAG, OpenSearch, Redis, Langfuse, Docker, Gradio, Telegram
1e732dd Download src/database.py from T0X1N/Agentic-RagBot: direct link, hf CLI and curl.
- Browser
- Download file 1.16 kB
-
https://huggingface.co/spaces/T0X1N/Agentic-RagBot/resolve/9699bea9d646520bb68c26ef02b87ec4bcedcfbf/src/database.py
- Command line
-
hf download hf://spaces/T0X1N/Agentic-RagBot@9699bea9d646520bb68c26ef02b87ec4bcedcfbf/src/database.py
-
curl -L -o database.py https://huggingface.co/spaces/T0X1N/Agentic-RagBot/resolve/9699bea9d646520bb68c26ef02b87ec4bcedcfbf/src/database.py
1.16 kB
| """ | |
| MediGuard AI β Database layer | |
| Provides SQLAlchemy engine/session factories and the declarative Base. | |
| """ | |
| from __future__ import annotations | |
| from functools import lru_cache | |
| from typing import Generator | |
| from sqlalchemy import create_engine | |
| from sqlalchemy.orm import Session, sessionmaker, DeclarativeBase | |
| from src.settings import get_settings | |
| class Base(DeclarativeBase): | |
| """Shared declarative base for all ORM models.""" | |
| pass | |
| def _engine(): | |
| settings = get_settings() | |
| return create_engine( | |
| settings.postgres.database_url, | |
| pool_pre_ping=True, | |
| pool_size=5, | |
| max_overflow=10, | |
| echo=settings.debug, | |
| ) | |
| def _session_factory() -> sessionmaker[Session]: | |
| return sessionmaker(bind=_engine(), autocommit=False, autoflush=False) | |
| def get_db() -> Generator[Session, None, None]: | |
| """FastAPI dependency β yields a DB session and commits/rolls back.""" | |
| session = _session_factory()() | |
| try: | |
| yield session | |
| session.commit() | |
| except Exception: | |
| session.rollback() | |
| raise | |
| finally: | |
| session.close() | |