-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.py
157 lines (123 loc) · 4.91 KB
/
config.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# Copyright 2025 Dawood Thouseef
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os
import logging
import sys
from pathlib import Path
import shutil
from pydantic import BaseModel
import threading
stop_event = threading.Event()
VERSION="0.0.1"
class Model(BaseModel):
name:str
type:str
url:str=None
api_key:str=None
####################################
# Load .env file
####################################
JARVIS_DIR = Path(__file__).parent # the path containing this file
SESSION_PATH=os.path.join(Path.home(),".cache","jarvis")
PLUGIN_DIR=os.path.join(SESSION_PATH,"components","alert_plugin")
if not os.path.exists(PLUGIN_DIR):
os.makedirs(PLUGIN_DIR,exist_ok=True)
TOOLS=[]
ALLOW_DANGEROUS_REQUEST = True
if not os.path.exists(SESSION_PATH):
os.makedirs(SESSION_PATH,exist_ok=True)
try:
from dotenv import find_dotenv, load_dotenv
load_dotenv(find_dotenv(str(JARVIS_DIR / ".env")))
except ImportError:
print("dotenv not installed, skipping...")
####################################
# LOGGING
####################################
LOG_DIR = os.path.join(JARVIS_DIR, "logs")
os.makedirs(LOG_DIR, exist_ok=True)
LOG_FILE = os.path.join(LOG_DIR, "app.log")
log_levels = ["CRITICAL", "ERROR", "WARNING", "INFO", "DEBUG"]
GLOBAL_LOG_LEVEL = os.environ.get("GLOBAL_LOG_LEVEL", "INFO").upper()
if GLOBAL_LOG_LEVEL not in log_levels:
GLOBAL_LOG_LEVEL = "INFO"
# Remove all handlers associated with the root logger
for handler in logging.root.handlers[:]:
logging.root.removeHandler(handler)
# Configure root logger manually
root_logger = logging.getLogger()
root_logger.setLevel(getattr(logging, GLOBAL_LOG_LEVEL))
formatter = logging.Formatter("%(asctime)s - %(levelname)s - [%(name)s] - %(threadName)s - %(message)s")
file_handler = logging.FileHandler(LOG_FILE, mode='a', encoding='utf-8')
file_handler.setFormatter(formatter)
stream_handler = logging.StreamHandler(sys.stdout)
stream_handler.setFormatter(formatter)
root_logger.addHandler(file_handler)
root_logger.addHandler(stream_handler)
log = logging.getLogger("MainLogger")
log.setLevel(getattr(logging, GLOBAL_LOG_LEVEL))
log.propagate = False
log.info(f"GLOBAL_LOG_LEVEL: {GLOBAL_LOG_LEVEL}")
log_sources = ["DB", "MAIN", "AGENTS", "GUI", "AUDIO", "VISION","MEMORY"]
SRC_LOG_LEVELS = {}
loggers = {}
for source in log_sources:
log_env_var = source + "_LOG_LEVEL"
source_log_level = os.environ.get(log_env_var, GLOBAL_LOG_LEVEL).upper()
if source_log_level not in log_levels:
source_log_level = GLOBAL_LOG_LEVEL
source_logger = logging.getLogger(source)
source_logger.setLevel(getattr(logging, source_log_level))
source_logger.propagate = True
SRC_LOG_LEVELS[source] = source_log_level
loggers[source] = source_logger
log.info(f"{log_env_var}: {source_log_level}")
###########################
# ENV (dev,test,prod)
####################################
ENV = os.environ.get("ENV", "dev")
####################################
# DATA/FRONTEND BUILD DIR
####################################
DATA_DIR = Path(os.getenv("DATA_DIR", JARVIS_DIR / "data")).resolve()
NEW_DATA_DIR = Path(os.getenv("DATA_DIR", JARVIS_DIR / "data")).resolve()
NEW_DATA_DIR.mkdir(parents=True, exist_ok=True)
# Check if the data directory exists in the package directory
if DATA_DIR.exists() and DATA_DIR != NEW_DATA_DIR:
log.info(f"Moving {DATA_DIR} to {NEW_DATA_DIR}")
for item in DATA_DIR.iterdir():
dest = NEW_DATA_DIR / item.name
if item.is_dir():
shutil.copytree(item, dest, dirs_exist_ok=True)
else:
shutil.copy2(item, dest)
DATA_DIR = Path(os.getenv("DATA_DIR", JARVIS_DIR / "data"))
####################################
# Database
####################################
# Check if the file exists
if os.path.exists(f"{DATA_DIR}/ollama.db"):
# Rename the file
os.rename(f"{DATA_DIR}/ollama.db", f"{DATA_DIR}/jarvis.db")
log.info("Database migrated from Ollama-WebUI successfully.")
else:
pass
DATABASE_URL = os.environ.get("DATABASE_URL", f"sqlite:///{DATA_DIR}/jarvis.db")
# Replace the postgres:// with postgresql://
if "postgres://" in DATABASE_URL:
DATABASE_URL = DATABASE_URL.replace("postgres://", "postgresql://")
SCREENSHOT_PATH=DATA_DIR / "screenshot" / "screenshot_with_text.png"
MIC_RECORD_LOCATION = DATA_DIR / "cache"/ "audio" / "mic_record.wav"
SYSTEM_SOUND_LOCATION = DATA_DIR / "system_sound.wav"
JUST_SCREENSHOT_PATH = DATA_DIR / "screenshot" /"screenshot.png"