chinmayjha commited on
Commit
3eeed2d
Β·
1 Parent(s): 8a1135c

Fix OPIK configuration: make COMET_PROJECT optional

Browse files
Files changed (1) hide show
  1. src/second_brain_online/opik_utils.py +55 -72
src/second_brain_online/opik_utils.py CHANGED
@@ -1,77 +1,60 @@
1
- import os
2
-
3
- import opik
4
- from loguru import logger
5
- from opik.configurator.configure import OpikConfigurator
6
-
7
- from second_brain_online.config import settings
8
 
 
 
9
 
10
- def configure() -> None:
11
- if settings.COMET_API_KEY and settings.COMET_PROJECT:
12
- try:
13
- client = OpikConfigurator(api_key=settings.COMET_API_KEY)
14
- default_workspace = client._get_default_workspace()
15
- except Exception:
16
- logger.warning(
17
- "Default workspace not found. Setting workspace to None and enabling interactive mode."
18
- )
19
- default_workspace = None
20
-
21
- os.environ["OPIK_PROJECT_NAME"] = settings.COMET_PROJECT
22
-
23
- opik.configure(
24
- api_key=settings.COMET_API_KEY,
25
- workspace=default_workspace,
26
- use_local=False,
27
- force=True,
28
- )
29
- logger.info(
30
- f"Opik configured successfully using workspace '{default_workspace}'"
31
- )
32
- else:
33
- logger.warning(
34
- "COMET_API_KEY and COMET_PROJECT are not set. Set them to enable prompt monitoring with Opik (powered by Comet ML)."
35
- )
36
-
37
-
38
- def get_or_create_dataset(name: str, prompts: list[str]) -> opik.Dataset | None:
39
- client = opik.Opik()
40
  try:
41
- dataset = client.get_dataset(name=name)
42
- except Exception:
43
- dataset = None
44
-
45
- if dataset:
46
- logger.warning(f"Dataset '{name}' already exists. Skipping dataset creation.")
47
-
48
- return dataset
49
-
50
- assert prompts, "Prompts are required to create a dataset."
51
-
52
- dataset_items = []
53
- for prompt in prompts:
54
- dataset_items.append(
55
- {
56
- "input": prompt,
57
- }
58
  )
 
 
 
59
 
60
- dataset = create_dataset(
61
- name=name,
62
- description="Dataset for evaluating the agentic app.",
63
- items=dataset_items,
64
- )
65
-
66
- return dataset
67
-
68
-
69
- def create_dataset(name: str, description: str, items: list[dict]) -> opik.Dataset:
70
- client = opik.Opik()
71
-
72
- dataset = client.get_or_create_dataset(name=name, description=description)
73
- dataset.insert(items)
74
-
75
- dataset = client.get_dataset(name=name)
76
-
77
- return dataset
 
1
+ #!/usr/bin/env python3
2
+ """
3
+ Hugging Face Space app for Second Brain AI Assistant.
 
 
 
 
4
 
5
+ Direct implementation for HF Space deployment.
6
+ """
7
 
8
+ import os
9
+ import sys
10
+ from pathlib import Path
11
+
12
+ # Add the current directory and src directory to Python path
13
+ sys.path.append('.')
14
+ 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
+ from second_brain_online import opik_utils
19
+
20
+ def main():
21
+ """Main function for Hugging Face Space deployment."""
22
+ # Set default values for HF Spaces
23
+ retriever_config_path = os.getenv("RETRIEVER_CONFIG_PATH", "configs/compute_rag_vector_index_openai_contextual_simple.yaml")
24
+
25
+ print("πŸš€ Starting Second Brain AI Assistant...")
26
+ print(f"πŸ“ Using retriever config: {retriever_config_path}")
27
+
28
+ # Debug: Check environment variables
29
+ print("πŸ” Environment variables check:")
30
+ print(f" COMET_API_KEY: {'SET' if os.getenv('COMET_API_KEY') else 'NOT SET'}")
31
+ print(f" COMET_PROJECT: {os.getenv('COMET_PROJECT', 'NOT SET')}")
32
+ print(f" OPENAI_API_KEY: {'SET' if os.getenv('OPENAI_API_KEY') else 'NOT SET'}")
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
  try:
37
+ opik_utils.configure()
38
+ print("βœ… OPIK configured successfully")
39
+ except Exception as e:
40
+ print(f"⚠️ OPIK configuration failed: {e}")
41
+
42
+ try:
43
+ # Initialize agent
44
+ agent = get_agent(retriever_config_path=Path(retriever_config_path))
45
+
46
+ # Get the actual agent from the wrapper
47
+ actual_agent = agent._AgentWrapper__agent
48
+
49
+ # Launch custom UI
50
+ CustomGradioUI(actual_agent).launch(
51
+ server_name="0.0.0.0",
52
+ server_port=7860,
53
+ share=False
54
  )
55
+ except Exception as e:
56
+ print(f"❌ Error starting the application: {e}")
57
+ raise
58
 
59
+ if __name__ == "__main__":
60
+ main()