Spaces:
Sleeping
Sleeping
Fix OPIK import errors for Hugging Face Spaces deployment
Browse files- Make OPIK imports optional with graceful error handling
- Update opik_utils.py to check OPIK availability before use
- Update app.py to handle missing OPIK dependencies
- Fix README.md app_file configuration
- Ensure app runs without OPIK dependencies on HF Spaces
- app.py +17 -6
- src/second_brain_online/opik_utils.py +20 -3
app.py
CHANGED
|
@@ -15,7 +15,15 @@ sys.path.append('src')
|
|
| 15 |
|
| 16 |
from second_brain_online.application.agents import get_agent
|
| 17 |
from second_brain_online.application.ui import CustomGradioUI
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
def main():
|
| 21 |
"""Main function for Hugging Face Space deployment."""
|
|
@@ -33,11 +41,14 @@ def main():
|
|
| 33 |
print(f" MONGODB_URI: {'SET' if os.getenv('MONGODB_URI') else 'NOT SET'}")
|
| 34 |
|
| 35 |
# Configure OPIK (optional - will use default project if COMET_PROJECT not set)
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
try:
|
| 43 |
# Initialize agent
|
|
|
|
| 15 |
|
| 16 |
from second_brain_online.application.agents import get_agent
|
| 17 |
from second_brain_online.application.ui import CustomGradioUI
|
| 18 |
+
|
| 19 |
+
# Try to import opik_utils, but handle import errors gracefully
|
| 20 |
+
try:
|
| 21 |
+
from second_brain_online import opik_utils
|
| 22 |
+
OPIK_UTILS_AVAILABLE = True
|
| 23 |
+
except ImportError as e:
|
| 24 |
+
print(f"⚠️ OPIK utils not available: {e}")
|
| 25 |
+
opik_utils = None
|
| 26 |
+
OPIK_UTILS_AVAILABLE = False
|
| 27 |
|
| 28 |
def main():
|
| 29 |
"""Main function for Hugging Face Space deployment."""
|
|
|
|
| 41 |
print(f" MONGODB_URI: {'SET' if os.getenv('MONGODB_URI') else 'NOT SET'}")
|
| 42 |
|
| 43 |
# Configure OPIK (optional - will use default project if COMET_PROJECT not set)
|
| 44 |
+
if OPIK_UTILS_AVAILABLE:
|
| 45 |
+
try:
|
| 46 |
+
opik_utils.configure()
|
| 47 |
+
print("✅ OPIK configured successfully")
|
| 48 |
+
except Exception as e:
|
| 49 |
+
print(f"⚠️ OPIK configuration failed: {e}")
|
| 50 |
+
else:
|
| 51 |
+
print("⚠️ OPIK utils not available, skipping configuration")
|
| 52 |
|
| 53 |
try:
|
| 54 |
# Initialize agent
|
src/second_brain_online/opik_utils.py
CHANGED
|
@@ -5,13 +5,26 @@ OPIK utilities for Second Brain AI Assistant.
|
|
| 5 |
|
| 6 |
import os
|
| 7 |
from loguru import logger
|
| 8 |
-
import opik
|
| 9 |
-
from opik import OpikConfigurator
|
| 10 |
|
| 11 |
from second_brain_online.config import settings
|
| 12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
def configure() -> None:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
if settings.COMET_API_KEY:
|
| 16 |
try:
|
| 17 |
client = OpikConfigurator(api_key=settings.COMET_API_KEY)
|
|
@@ -39,7 +52,11 @@ def configure() -> None:
|
|
| 39 |
)
|
| 40 |
|
| 41 |
|
| 42 |
-
def get_or_create_dataset(name: str, prompts: list[str])
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
client = opik.Opik()
|
| 44 |
try:
|
| 45 |
dataset = client.get_dataset(name)
|
|
|
|
| 5 |
|
| 6 |
import os
|
| 7 |
from loguru import logger
|
|
|
|
|
|
|
| 8 |
|
| 9 |
from second_brain_online.config import settings
|
| 10 |
|
| 11 |
+
# Try to import opik, but handle import errors gracefully
|
| 12 |
+
try:
|
| 13 |
+
import opik
|
| 14 |
+
from opik import OpikConfigurator
|
| 15 |
+
OPIK_AVAILABLE = True
|
| 16 |
+
except ImportError as e:
|
| 17 |
+
logger.warning(f"OPIK not available: {e}")
|
| 18 |
+
opik = None
|
| 19 |
+
OpikConfigurator = None
|
| 20 |
+
OPIK_AVAILABLE = False
|
| 21 |
+
|
| 22 |
|
| 23 |
def configure() -> None:
|
| 24 |
+
if not OPIK_AVAILABLE:
|
| 25 |
+
logger.warning("OPIK is not available. Skipping OPIK configuration.")
|
| 26 |
+
return
|
| 27 |
+
|
| 28 |
if settings.COMET_API_KEY:
|
| 29 |
try:
|
| 30 |
client = OpikConfigurator(api_key=settings.COMET_API_KEY)
|
|
|
|
| 52 |
)
|
| 53 |
|
| 54 |
|
| 55 |
+
def get_or_create_dataset(name: str, prompts: list[str]):
|
| 56 |
+
if not OPIK_AVAILABLE:
|
| 57 |
+
logger.warning("OPIK is not available. Cannot create dataset.")
|
| 58 |
+
return None
|
| 59 |
+
|
| 60 |
client = opik.Opik()
|
| 61 |
try:
|
| 62 |
dataset = client.get_dataset(name)
|