Upload 327 files
Browse files- database/__init__.py +57 -6
- requirements.txt +19 -6
database/__init__.py
CHANGED
|
@@ -10,12 +10,18 @@ new manager if that module is unavailable.
|
|
| 10 |
|
| 11 |
from importlib import util as _importlib_util
|
| 12 |
from pathlib import Path as _Path
|
| 13 |
-
from typing import Optional as _Optional
|
| 14 |
|
| 15 |
from .db_manager import DatabaseManager
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
legacy_path = _Path(__file__).resolve().parent.parent / "database.py"
|
| 20 |
if not legacy_path.exists():
|
| 21 |
return None
|
|
@@ -26,19 +32,64 @@ def _load_legacy_database() -> _Optional[type]:
|
|
| 26 |
|
| 27 |
module = _importlib_util.module_from_spec(spec)
|
| 28 |
try:
|
| 29 |
-
spec.loader.exec_module(module)
|
| 30 |
except Exception:
|
| 31 |
# If loading the legacy module fails we silently fall back to DatabaseManager
|
| 32 |
return None
|
| 33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
return getattr(module, "Database", None)
|
| 35 |
|
| 36 |
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
if _LegacyDatabase is not None:
|
| 40 |
Database = _LegacyDatabase
|
| 41 |
else:
|
| 42 |
Database = DatabaseManager
|
| 43 |
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
from importlib import util as _importlib_util
|
| 12 |
from pathlib import Path as _Path
|
| 13 |
+
from typing import Optional as _Optional, Any as _Any
|
| 14 |
|
| 15 |
from .db_manager import DatabaseManager
|
| 16 |
|
| 17 |
+
|
| 18 |
+
def _load_legacy_module():
|
| 19 |
+
"""Load the legacy root-level ``database.py`` module if it exists.
|
| 20 |
+
|
| 21 |
+
This is used to support older entry points like ``get_database`` and the
|
| 22 |
+
``Database`` class that live in the legacy file.
|
| 23 |
+
"""
|
| 24 |
+
|
| 25 |
legacy_path = _Path(__file__).resolve().parent.parent / "database.py"
|
| 26 |
if not legacy_path.exists():
|
| 27 |
return None
|
|
|
|
| 32 |
|
| 33 |
module = _importlib_util.module_from_spec(spec)
|
| 34 |
try:
|
| 35 |
+
spec.loader.exec_module(module) # type: ignore[union-attr]
|
| 36 |
except Exception:
|
| 37 |
# If loading the legacy module fails we silently fall back to DatabaseManager
|
| 38 |
return None
|
| 39 |
|
| 40 |
+
return module
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
def _load_legacy_database_class() -> _Optional[type]:
|
| 44 |
+
"""Load the legacy ``Database`` class from ``database.py`` if available."""
|
| 45 |
+
|
| 46 |
+
module = _load_legacy_module()
|
| 47 |
+
if module is None:
|
| 48 |
+
return None
|
| 49 |
return getattr(module, "Database", None)
|
| 50 |
|
| 51 |
|
| 52 |
+
def _load_legacy_get_database() -> _Optional[callable]:
|
| 53 |
+
"""Load the legacy ``get_database`` function from ``database.py`` if available."""
|
| 54 |
+
|
| 55 |
+
module = _load_legacy_module()
|
| 56 |
+
if module is None:
|
| 57 |
+
return None
|
| 58 |
+
return getattr(module, "get_database", None)
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
_LegacyDatabase = _load_legacy_database_class()
|
| 62 |
+
_LegacyGetDatabase = _load_legacy_get_database()
|
| 63 |
+
_db_manager_instance: _Optional[DatabaseManager] = None
|
| 64 |
+
|
| 65 |
|
| 66 |
if _LegacyDatabase is not None:
|
| 67 |
Database = _LegacyDatabase
|
| 68 |
else:
|
| 69 |
Database = DatabaseManager
|
| 70 |
|
| 71 |
+
|
| 72 |
+
def get_database(*args: _Any, **kwargs: _Any) -> _Any:
|
| 73 |
+
"""Return a database instance compatible with legacy callers.
|
| 74 |
+
|
| 75 |
+
The resolution order is:
|
| 76 |
+
|
| 77 |
+
1. If the legacy ``database.py`` file exists and exposes ``get_database``,
|
| 78 |
+
use that function (this returns the legacy singleton used by the
|
| 79 |
+
Gradio crypto dashboard and other older modules).
|
| 80 |
+
2. Otherwise, return a singleton instance of ``DatabaseManager`` from the
|
| 81 |
+
new SQLAlchemy-backed implementation.
|
| 82 |
+
"""
|
| 83 |
+
|
| 84 |
+
if _LegacyGetDatabase is not None:
|
| 85 |
+
return _LegacyGetDatabase(*args, **kwargs)
|
| 86 |
+
|
| 87 |
+
global _db_manager_instance
|
| 88 |
+
if _db_manager_instance is None:
|
| 89 |
+
_db_manager_instance = DatabaseManager()
|
| 90 |
+
# Ensure tables are created for the monitoring schema
|
| 91 |
+
_db_manager_instance.init_database()
|
| 92 |
+
return _db_manager_instance
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
__all__ = ["DatabaseManager", "Database", "get_database"]
|
requirements.txt
CHANGED
|
@@ -1,9 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
# Core API Server Requirements
|
| 2 |
fastapi==0.109.0
|
| 3 |
uvicorn[standard]==0.27.0
|
| 4 |
pydantic==2.5.3
|
| 5 |
sqlalchemy==2.0.25
|
| 6 |
-
httpx
|
| 7 |
websockets>=12.0
|
| 8 |
python-dotenv
|
| 9 |
python-multipart
|
|
@@ -13,8 +22,12 @@ aiohttp>=3.8.0
|
|
| 13 |
# Data Processing
|
| 14 |
pandas>=2.1.0
|
| 15 |
|
| 16 |
-
#
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Unified dependencies for API server + Gradio dashboards (Hugging Face Spaces).
|
| 2 |
+
|
| 3 |
+
This single file is what Hugging Face Spaces will install. It includes:
|
| 4 |
+
- Core API server stack
|
| 5 |
+
- Database/SQLAlchemy layer
|
| 6 |
+
- Gradio dashboard + Plotly charts
|
| 7 |
+
- Optional Transformers-based AI features
|
| 8 |
+
"""
|
| 9 |
+
|
| 10 |
# Core API Server Requirements
|
| 11 |
fastapi==0.109.0
|
| 12 |
uvicorn[standard]==0.27.0
|
| 13 |
pydantic==2.5.3
|
| 14 |
sqlalchemy==2.0.25
|
| 15 |
+
httpx==0.26.0
|
| 16 |
websockets>=12.0
|
| 17 |
python-dotenv
|
| 18 |
python-multipart
|
|
|
|
| 22 |
# Data Processing
|
| 23 |
pandas>=2.1.0
|
| 24 |
|
| 25 |
+
# Gradio Dashboard & UI
|
| 26 |
+
gradio==4.12.0
|
| 27 |
+
plotly==5.18.0 # Enables chart features in the dashboard
|
| 28 |
+
psutil==5.9.6 # Optional, used for system monitoring widgets
|
| 29 |
+
|
| 30 |
+
# AI/ML Libraries (optional but recommended for AI features)
|
| 31 |
+
transformers>=4.36.0 # Used by ai_models and HF integration
|
| 32 |
+
torch>=2.0.0 # Backend for transformers (CPU build on HF Spaces)
|
| 33 |
+
sentencepiece>=0.1.99 # Required by some Transformer text models
|