Compare commits
No commits in common. "39a937564950780cf1958d9ffd301e6f28efe331" and "49737075961962895981431c7a7571f7da241a73" have entirely different histories.
39a9375649
...
4973707596
24 changed files with 1186 additions and 1032 deletions
|
@ -1,63 +1,53 @@
|
|||
# __init__.py
|
||||
import os
|
||||
import json
|
||||
import yaml
|
||||
from pathlib import Path
|
||||
import ipaddress
|
||||
import multiprocessing
|
||||
from dotenv import load_dotenv
|
||||
from dateutil import tz
|
||||
from pathlib import Path
|
||||
from pydantic import BaseModel
|
||||
from typing import List, Optional
|
||||
from .classes import AutoResponder, IMAPConfig, SMTPConfig, EmailAccount, EmailContact, IncomingEmail, Database, Geocoder, APIConfig, Configuration
|
||||
from .logs import Logger
|
||||
from .classes import Database, Geocoder, APIConfig, Configuration, EmailConfiguration, Dir
|
||||
|
||||
# INITIALization
|
||||
BASE_DIR = Path(__file__).resolve().parent
|
||||
CONFIG_DIR = BASE_DIR / "config"
|
||||
ENV_PATH = CONFIG_DIR / ".env"
|
||||
load_dotenv(ENV_PATH)
|
||||
LOGS_DIR = BASE_DIR / "logs"
|
||||
os.makedirs(LOGS_DIR, exist_ok=True)
|
||||
L = Logger("Central", LOGS_DIR)
|
||||
|
||||
# API essentials
|
||||
### Initial initialization
|
||||
API = APIConfig.load('api', 'secrets')
|
||||
Dir = Configuration.load('dirs')
|
||||
HOST = f"{API.BIND}:{API.PORT}"
|
||||
LOCAL_HOSTS = [ipaddress.ip_address(localhost.strip()) for localhost in os.getenv('LOCAL_HOSTS', '127.0.0.1').split(',')] + ['localhost']
|
||||
SUBNET_BROADCAST = os.getenv("SUBNET_BROADCAST", '10.255.255.255')
|
||||
MAX_CPU_CORES = min(int(os.getenv("MAX_CPU_CORES", int(multiprocessing.cpu_count()/2))), multiprocessing.cpu_count())
|
||||
Dir = Dir()
|
||||
ENV_PATH = Dir.CONFIG / ".env"
|
||||
LOGS_DIR = Dir.LOGS
|
||||
L = Logger("Central", LOGS_DIR)
|
||||
os.makedirs(LOGS_DIR, exist_ok=True)
|
||||
load_dotenv(ENV_PATH)
|
||||
|
||||
DB = Database.from_env()
|
||||
### API essentials
|
||||
DB = Database.from_yaml('db.yaml')
|
||||
|
||||
ASR = Configuration.load('asr')
|
||||
IMG = Configuration.load('img')
|
||||
Cal = Configuration.load('cal', 'secrets')
|
||||
print(f"Cal configuration: {Cal.__dict__}")
|
||||
Email = EmailConfiguration.load('email', 'secrets')
|
||||
LLM = Configuration.load('llm', 'secrets')
|
||||
News = Configuration.load('news', 'secrets')
|
||||
IMG = Configuration.load('img', 'secrets')
|
||||
Obsidian = Configuration.load('obsidian')
|
||||
TTS = Configuration.load('tts', 'secrets')
|
||||
CourtListener = Configuration.load('courtlistener', 'secrets')
|
||||
Tailscale = Configuration.load('tailscale', 'secrets')
|
||||
Cloudflare = Configuration.load('cloudflare', 'secrets')
|
||||
|
||||
# Directories & general paths
|
||||
ROUTER_DIR = BASE_DIR / "routers"
|
||||
DATA_DIR = BASE_DIR / "data"
|
||||
os.makedirs(DATA_DIR, exist_ok=True)
|
||||
ALERTS_DIR = DATA_DIR / "alerts"
|
||||
os.makedirs(ALERTS_DIR, exist_ok=True)
|
||||
|
||||
### Directories & general paths
|
||||
REQUESTS_DIR = LOGS_DIR / "requests"
|
||||
os.makedirs(REQUESTS_DIR, exist_ok=True)
|
||||
REQUESTS_LOG_PATH = LOGS_DIR / "requests.log"
|
||||
|
||||
# LOCATE AND WEATHER LOCALIZATIONS
|
||||
USER_FULLNAME = os.getenv('USER_FULLNAME')
|
||||
USER_BIO = os.getenv('USER_BIO')
|
||||
NAMED_LOCATIONS = CONFIG_DIR / "named-locations.yaml"
|
||||
### LOCATE AND WEATHER LOCALIZATIONS
|
||||
# DB = DATA_DIR / "weatherlocate.db" # deprecated
|
||||
VISUALCROSSING_BASE_URL = os.getenv("VISUALCROSSING_BASE_URL", "https://weather.visualcrossing.com/VisualCrossingWebServices/rest/services/timeline")
|
||||
VISUALCROSSING_API_KEY = os.getenv("VISUALCROSSING_API_KEY")
|
||||
GEONAMES_TXT = DATA_DIR / "geonames.txt"
|
||||
LOCATIONS_CSV = DATA_DIR / "US.csv"
|
||||
TZ = tz.gettz(os.getenv("TZ", "America/Los_Angeles"))
|
||||
TZ_CACHE = DATA_DIR / "tzcache.json"
|
||||
GEO = Geocoder(NAMED_LOCATIONS, TZ_CACHE)
|
||||
TZ_CACHE = Dir.DATA / "tzcache.json"
|
||||
GEO = Geocoder(Dir.config.locations, TZ_CACHE)
|
||||
|
||||
# Obsidian & notes
|
||||
### Obsidian & notes
|
||||
ALLOWED_FILENAME_CHARS = r'[^\w \.-]'
|
||||
MAX_PATH_LENGTH = 254
|
||||
OBSIDIAN_VAULT_DIR = Path(os.getenv("OBSIDIAN_BASE_DIR") or Path(Dir.HOME) / "Nextcloud" / "notes")
|
||||
|
@ -69,135 +59,22 @@ OBSIDIAN_BANNER_SCENE = os.getenv("OBSIDIAN_BANNER_SCENE", "wallpaper")
|
|||
OBSIDIAN_CHROMADB_COLLECTION = os.getenv("OBSIDIAN_CHROMADB_COLLECTION", "obsidian")
|
||||
ARCHIVE_DIR = Path(os.getenv("ARCHIVE_DIR", OBSIDIAN_VAULT_DIR / "archive"))
|
||||
os.makedirs(ARCHIVE_DIR, exist_ok=True)
|
||||
DOC_DIR = DATA_DIR / "docs"
|
||||
os.makedirs(DOC_DIR, exist_ok=True)
|
||||
|
||||
# DATETIME SCHEMA FOR DAILY NOTE FOLDER HIERARCHY FORMATTING ###
|
||||
### DATETIME SCHEMA FOR DAILY NOTE FOLDER HIERARCHY FORMATTING ###
|
||||
YEAR_FMT = os.getenv("YEAR_FMT")
|
||||
MONTH_FMT = os.getenv("MONTH_FMT")
|
||||
DAY_FMT = os.getenv("DAY_FMT")
|
||||
DAY_SHORT_FMT = os.getenv("DAY_SHORT_FMT")
|
||||
|
||||
# Large language model
|
||||
LLM_URL = os.getenv("LLM_URL", "http://localhost:11434")
|
||||
LLM_SYS_MSG = os.getenv("SYSTEM_MSG", "You are a helpful AI assistant.")
|
||||
DEFAULT_LLM = os.getenv("DEFAULT_LLM", "llama3")
|
||||
DEFAULT_VISION = os.getenv("DEFAULT_VISION", "llava")
|
||||
DEFAULT_VOICE = os.getenv("DEFAULT_VOICE", "Luna")
|
||||
DEFAULT_11L_VOICE = os.getenv("DEFAULT_11L_VOICE", "Victoria")
|
||||
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
||||
|
||||
# Summarization
|
||||
SUMMARY_CHUNK_SIZE = int(os.getenv("SUMMARY_CHUNK_SIZE", 16384)) # measured in tokens
|
||||
SUMMARY_CHUNK_OVERLAP = int(os.getenv("SUMMARY_CHUNK_OVERLAP", 256)) # measured in tokens
|
||||
SUMMARY_TPW = float(os.getenv("SUMMARY_TPW", 1.3)) # measured in tokens
|
||||
SUMMARY_LENGTH_RATIO = int(os.getenv("SUMMARY_LENGTH_RATIO", 4)) # measured as original to length ratio
|
||||
SUMMARY_MIN_LENGTH = int(os.getenv("SUMMARY_MIN_LENGTH", 150)) # measured in tokens
|
||||
SUMMARY_MODEL = os.getenv("SUMMARY_MODEL", "dolphin-llama3:8b-256k")
|
||||
SUMMARY_TOKEN_LIMIT = int(os.getenv("SUMMARY_TOKEN_LIMIT", 16384))
|
||||
SUMMARY_INSTRUCT = os.getenv('SUMMARY_INSTRUCT', "You are an AI assistant that provides accurate summaries of text -- nothing more and nothing less. You must not include ANY extraneous text other than the sumary. Do not include comments apart from the summary, do not preface the summary, and do not provide any form of postscript. Do not add paragraph breaks. Do not add any kind of formatting. Your response should begin with, consist of, and end with an accurate plaintext summary.")
|
||||
SUMMARY_INSTRUCT_TTS = os.getenv('SUMMARY_INSTRUCT_TTS', "You are an AI assistant that provides email summaries for Sanjay. Your response will undergo Text-To-Speech conversion and added to Sanjay's private podcast. Providing adequate context (Sanjay did not send this question to you, he will only hear your response) but aiming for conciseness and precision, and bearing in mind the Text-To-Speech conversion (avoiding acronyms and formalities), summarize the following email.")
|
||||
|
||||
|
||||
# Stable diffusion
|
||||
IMG_DIR = DATA_DIR / "img" / "images"
|
||||
os.makedirs(IMG_DIR, exist_ok=True)
|
||||
IMG_WORKFLOWS_DIR = DATA_DIR / "img" / "workflows"
|
||||
os.makedirs(IMG_WORKFLOWS_DIR, exist_ok=True)
|
||||
COMFYUI_URL = os.getenv('COMFYUI_URL', "http://localhost:8188")
|
||||
COMFYUI_DIR = Path(os.getenv('COMFYUI_DIR'))
|
||||
COMFYUI_OUTPUT_DIR = COMFYUI_DIR / 'output'
|
||||
COMFYUI_LAUNCH_CMD = os.getenv('COMFYUI_LAUNCH_CMD', 'mamba activate comfyui && python main.py')
|
||||
IMG_CONFIG_PATH = CONFIG_DIR / 'img.yaml'
|
||||
|
||||
# ASR
|
||||
ASR_DIR = DATA_DIR / "asr"
|
||||
os.makedirs(ASR_DIR, exist_ok=True)
|
||||
WHISPER_CPP_DIR = Path(Dir.HOME) / str(os.getenv("WHISPER_CPP_DIR"))
|
||||
WHISPER_CPP_MODELS = os.getenv('WHISPER_CPP_MODELS', 'NULL,VOID').split(',')
|
||||
|
||||
# TTS
|
||||
PREFERRED_TTS = os.getenv("PREFERRED_TTS", "None")
|
||||
TTS_DIR = DATA_DIR / "tts"
|
||||
os.makedirs(TTS_DIR, exist_ok=True)
|
||||
VOICE_DIR = TTS_DIR / 'voices'
|
||||
os.makedirs(VOICE_DIR, exist_ok=True)
|
||||
PODCAST_DIR = os.getenv("PODCAST_DIR", TTS_DIR / "sideloads")
|
||||
os.makedirs(PODCAST_DIR, exist_ok=True)
|
||||
TTS_OUTPUT_DIR = TTS_DIR / 'outputs'
|
||||
os.makedirs(TTS_OUTPUT_DIR, exist_ok=True)
|
||||
TTS_SEGMENTS_DIR = TTS_DIR / 'segments'
|
||||
os.makedirs(TTS_SEGMENTS_DIR, exist_ok=True)
|
||||
ELEVENLABS_API_KEY = os.getenv("ELEVENLABS_API_KEY")
|
||||
|
||||
|
||||
# Calendar & email account
|
||||
MS365_TOGGLE = True if os.getenv("MS365_TOGGLE") == "True" else False
|
||||
ICAL_TOGGLE = True if os.getenv("ICAL_TOGGLE") == "True" else False
|
||||
ICS_PATH = DATA_DIR / 'calendar.ics' # deprecated now, but maybe revive?
|
||||
ICALENDARS = os.getenv('ICALENDARS', 'NULL,VOID').split(',')
|
||||
|
||||
EMAIL_CONFIG = CONFIG_DIR / "email.yaml"
|
||||
EMAIL_LOGS = LOGS_DIR / "email"
|
||||
os.makedirs(EMAIL_LOGS, exist_ok = True)
|
||||
|
||||
# Courtlistener & other webhooks
|
||||
COURTLISTENER_DOCKETS_DIR = DATA_DIR / "courtlistener" / "dockets"
|
||||
os.makedirs(COURTLISTENER_DOCKETS_DIR, exist_ok=True)
|
||||
COURTLISTENER_SEARCH_DIR = DATA_DIR / "courtlistener" / "cases"
|
||||
os.makedirs(COURTLISTENER_SEARCH_DIR, exist_ok=True)
|
||||
CASETABLE_PATH = DATA_DIR / "courtlistener" / "cases.json"
|
||||
COURTLISTENER_API_KEY = os.getenv("COURTLISTENER_API_KEY")
|
||||
COURTLISTENER_BASE_URL = os.getenv("COURTLISTENER_BASE_URL", "https://www.courtlistener.com")
|
||||
COURTLISTENER_DOCKETS_URL = "https://www.courtlistener.com/api/rest/v3/dockets/"
|
||||
|
||||
# Keys & passwords
|
||||
PUBLIC_KEY_FILE = os.getenv("PUBLIC_KEY_FILE", 'you_public_key.asc')
|
||||
PUBLIC_KEY = (BASE_DIR.parent / PUBLIC_KEY_FILE).read_text()
|
||||
### Keys & passwords
|
||||
MAC_ID = os.getenv("MAC_ID")
|
||||
MAC_UN = os.getenv("MAC_UN")
|
||||
MAC_PW = os.getenv("MAC_PW")
|
||||
TIMING_API_KEY = os.getenv("TIMING_API_KEY")
|
||||
TIMING_API_URL = os.getenv("TIMING_API_URL", "https://web.timingapp.com/api/v1")
|
||||
PHOTOPRISM_URL = os.getenv("PHOTOPRISM_URL")
|
||||
PHOTOPRISM_USER = os.getenv("PHOTOPRISM_USER")
|
||||
PHOTOPRISM_PASS = os.getenv("PHOTOPRISM_PASS")
|
||||
|
||||
# Tailscale
|
||||
TS_IP = ipaddress.ip_address(os.getenv("TS_IP", "NULL"))
|
||||
TS_SUBNET = ipaddress.ip_network(os.getenv("TS_SUBNET")) if os.getenv("TS_SUBNET") else None
|
||||
TS_ID = os.getenv("TS_ID", "NULL")
|
||||
TS_TAILNET = os.getenv("TS_TAILNET", "NULL")
|
||||
TS_ADDRESS = f"http://{TS_ID}.{TS_TAILNET}.ts.net"
|
||||
|
||||
# Cloudflare
|
||||
CF_API_BASE_URL = os.getenv("CF_API_BASE_URL")
|
||||
CF_TOKEN = os.getenv("CF_TOKEN")
|
||||
CF_IP = DATA_DIR / "cf_ip.txt" # to be deprecated soon
|
||||
CF_DOMAINS_PATH = DATA_DIR / "cf_domains.json" # to be deprecated soon
|
||||
|
||||
# Caddy - not fully implemented
|
||||
### Caddy - not fully implemented
|
||||
API.URL = os.getenv("API.URL")
|
||||
CADDY_SERVER = os.getenv('CADDY_SERVER', None)
|
||||
CADDYFILE_PATH = os.getenv("CADDYFILE_PATH", "") if CADDY_SERVER is not None else None
|
||||
CADDY_API_KEY = os.getenv("CADDY_API_KEY")
|
||||
|
||||
# Microsoft Graph
|
||||
MS365_CLIENT_ID = os.getenv('MS365_CLIENT_ID')
|
||||
MS365_SECRET = os.getenv('MS365_SECRET')
|
||||
MS365_TENANT_ID = os.getenv('MS365_TENANT_ID')
|
||||
MS365_CERT_PATH = CONFIG_DIR / 'MS365' / '.cert.pem' # deprecated
|
||||
MS365_KEY_PATH = CONFIG_DIR / 'MS365' / '.cert.key' # deprecated
|
||||
MS365_KEY = MS365_KEY_PATH.read_text()
|
||||
MS365_TOKEN_PATH = CONFIG_DIR / 'MS365' / '.token.txt'
|
||||
MS365_THUMBPRINT = os.getenv('MS365_THUMBPRINT')
|
||||
|
||||
MS365_LOGIN_URL = os.getenv("MS365_LOGIN_URL", "https://login.microsoftonline.com")
|
||||
MS365_AUTHORITY_URL = f"{MS365_LOGIN_URL}/{MS365_TENANT_ID}"
|
||||
MS365_REDIRECT_PATH = os.getenv("MS365_REDIRECT_PATH", "https://api.sij.ai/o365/oauth_redirect")
|
||||
MS365_SCOPE = os.getenv("MS365_SCOPE", 'Calendars.Read,Calendars.ReadWrite,offline_access').split(',')
|
||||
|
||||
|
||||
# Maintenance
|
||||
GARBAGE_COLLECTION_INTERVAL = 60 * 60 # Run cleanup every hour
|
||||
GARBAGE_TTL = 60 * 60 * 24 # Delete files older than 24 hours
|
||||
CADDY_API_KEY = os.getenv("CADDY_API_KEY")
|
|
@ -4,34 +4,25 @@ from fastapi import FastAPI, Request, HTTPException, Response
|
|||
from fastapi.responses import JSONResponse
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
from starlette.middleware.base import BaseHTTPMiddleware
|
||||
from starlette.middleware.base import BaseHTTPMiddleware
|
||||
from starlette.requests import ClientDisconnect
|
||||
from hypercorn.asyncio import serve
|
||||
from hypercorn.config import Config as HypercornConfig
|
||||
import sys
|
||||
import asyncio
|
||||
import httpx
|
||||
import argparse
|
||||
import json
|
||||
import ipaddress
|
||||
import importlib
|
||||
from dotenv import load_dotenv
|
||||
from pathlib import Path
|
||||
from datetime import datetime
|
||||
import argparse
|
||||
from . import L, API, ROUTER_DIR
|
||||
|
||||
parser = argparse.ArgumentParser(description='Personal API.')
|
||||
parser.add_argument('--log', type=str, default='INFO', help='Set overall log level (e.g., DEBUG, INFO, WARNING)')
|
||||
parser.add_argument('--debug', nargs='+', default=[], help='Set DEBUG log level for specific modules')
|
||||
parser.add_argument('--debug', action='store_true', help='Set log level to L.INFO')
|
||||
parser.add_argument('--test', type=str, help='Load only the specified module.')
|
||||
args = parser.parse_args()
|
||||
|
||||
from . import L, API, Dir
|
||||
L.setup_from_args(args)
|
||||
print(f"Debug modules after setup: {L.debug_modules}") # Debug print
|
||||
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
# CORSMiddleware
|
||||
app.add_middleware(
|
||||
CORSMiddleware,
|
||||
allow_origins=['*'],
|
||||
|
@ -60,10 +51,8 @@ class SimpleAPIKeyMiddleware(BaseHTTPMiddleware):
|
|||
content={"detail": "Invalid or missing API key"}
|
||||
)
|
||||
response = await call_next(request)
|
||||
# L.DEBUG(f"Request from {client_ip} is complete")
|
||||
return response
|
||||
|
||||
# Add the middleware to your FastAPI app
|
||||
app.add_middleware(SimpleAPIKeyMiddleware)
|
||||
|
||||
@app.exception_handler(HTTPException)
|
||||
|
@ -72,14 +61,12 @@ async def http_exception_handler(request: Request, exc: HTTPException):
|
|||
L.ERR(f"Request: {request.method} {request.url}")
|
||||
return JSONResponse(status_code=exc.status_code, content={"detail": exc.detail})
|
||||
|
||||
|
||||
@app.middleware("http")
|
||||
async def handle_exception_middleware(request: Request, call_next):
|
||||
try:
|
||||
response = await call_next(request)
|
||||
except RuntimeError as exc:
|
||||
if str(exc) == "Response content longer than Content-Length":
|
||||
# Update the Content-Length header to match the actual response content length
|
||||
response.headers["Content-Length"] = str(len(response.body))
|
||||
else:
|
||||
raise
|
||||
|
@ -87,33 +74,32 @@ async def handle_exception_middleware(request: Request, call_next):
|
|||
|
||||
|
||||
def load_router(router_name):
|
||||
router_file = ROUTER_DIR / f'{router_name}.py'
|
||||
module_logger = L.get_module_logger(router_name)
|
||||
module_logger.debug(f"Attempting to load {router_name.capitalize()}...")
|
||||
router_file = Dir.ROUTERS / f'{router_name}.py'
|
||||
L.DEBUG(f"Attempting to load {router_name.capitalize()}...")
|
||||
if router_file.exists():
|
||||
module_path = f'sijapi.routers.{router_name}'
|
||||
try:
|
||||
module = importlib.import_module(module_path)
|
||||
router = getattr(module, router_name)
|
||||
app.include_router(router)
|
||||
# module_logger.info(f"{router_name.capitalize()} router loaded.")
|
||||
L.INFO(f"{router_name.capitalize()} router loaded.")
|
||||
except (ImportError, AttributeError) as e:
|
||||
module_logger.critical(f"Failed to load router {router_name}: {e}")
|
||||
L.CRIT(f"Failed to load router {router_name}: {e}")
|
||||
else:
|
||||
module_logger.error(f"Router file for {router_name} does not exist.")
|
||||
L.ERR(f"Router file for {router_name} does not exist.")
|
||||
|
||||
def main(argv):
|
||||
if args.test:
|
||||
load_router(args.test)
|
||||
else:
|
||||
L.logger.critical(f"sijapi launched")
|
||||
L.logger.critical(f"Arguments: {args}")
|
||||
L.CRIT(f"sijapi launched")
|
||||
L.CRIT(f"{args._get_args}")
|
||||
for module_name in API.MODULES.__fields__:
|
||||
if getattr(API.MODULES, module_name):
|
||||
load_router(module_name)
|
||||
|
||||
config = HypercornConfig()
|
||||
config.bind = [API.BIND]
|
||||
config.bind = [API.BIND] # Use the resolved BIND value
|
||||
asyncio.run(serve(app, config))
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
import asyncio
|
||||
import json
|
||||
import math
|
||||
import multiprocessing
|
||||
import os
|
||||
import re
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
|
@ -13,38 +14,199 @@ from zoneinfo import ZoneInfo
|
|||
import aiofiles
|
||||
import aiohttp
|
||||
import asyncpg
|
||||
from typing import Union, Any
|
||||
from pydantic import BaseModel, Field, ConfigDict
|
||||
import reverse_geocoder as rg
|
||||
import yaml
|
||||
from dotenv import load_dotenv
|
||||
from pydantic import BaseModel, Field, create_model
|
||||
from pydantic import BaseModel, Field, create_model, ConfigDict, validator
|
||||
from srtm import get_data
|
||||
from timezonefinder import TimezoneFinder
|
||||
from pathlib import Path
|
||||
from typing import Any, Dict, List, Optional, Union, TypeVar, Type
|
||||
import yaml
|
||||
from typing import List, Optional
|
||||
from dotenv import load_dotenv
|
||||
|
||||
T = TypeVar('T', bound='Configuration')
|
||||
|
||||
class HierarchicalPath(os.PathLike):
|
||||
def __init__(self, path=None, base=None, home=None):
|
||||
self.home = Path(home).expanduser() if home else Path.home()
|
||||
self.base = Path(base).resolve() if base else self._find_base()
|
||||
self.path = self._resolve_path(path) if path else self.base
|
||||
|
||||
def _find_base(self):
|
||||
current = Path(__file__).resolve().parent
|
||||
while current.name != 'sijapi' and current != current.parent:
|
||||
current = current.parent
|
||||
return current
|
||||
|
||||
def _resolve_path(self, path):
|
||||
if isinstance(path, HierarchicalPath):
|
||||
return path.path
|
||||
if isinstance(path, Path):
|
||||
return path
|
||||
path = self._resolve_placeholders(path)
|
||||
if path.startswith(('~', 'HOME')):
|
||||
return self.home / path.lstrip('~').lstrip('HOME').lstrip('/')
|
||||
if path.startswith('/'):
|
||||
return Path(path)
|
||||
return self._resolve_relative_path(self.base / path)
|
||||
|
||||
def _resolve_placeholders(self, path):
|
||||
placeholders = {
|
||||
'HOME': str(self.home),
|
||||
'BASE': str(self.base),
|
||||
}
|
||||
pattern = r'\{\{\s*([^}]+)\s*\}\}'
|
||||
return re.sub(pattern, lambda m: placeholders.get(m.group(1).strip(), m.group(0)), path)
|
||||
|
||||
def _resolve_relative_path(self, path):
|
||||
if path.is_file():
|
||||
return path
|
||||
if path.is_dir():
|
||||
return path
|
||||
yaml_path = path.with_suffix('.yaml')
|
||||
if yaml_path.is_file():
|
||||
return yaml_path
|
||||
return path
|
||||
|
||||
def __truediv__(self, other):
|
||||
return HierarchicalPath(self.path / other, base=self.base, home=self.home)
|
||||
|
||||
def __getattr__(self, name):
|
||||
return HierarchicalPath(self.path / name, base=self.base, home=self.home)
|
||||
|
||||
def __str__(self):
|
||||
return str(self.path)
|
||||
|
||||
def __repr__(self):
|
||||
return f"HierarchicalPath('{self.path}')"
|
||||
|
||||
def __fspath__(self):
|
||||
return os.fspath(self.path)
|
||||
|
||||
def __eq__(self, other):
|
||||
if isinstance(other, (HierarchicalPath, Path, str)):
|
||||
return str(self.path) == str(other)
|
||||
return False
|
||||
|
||||
def __lt__(self, other):
|
||||
if isinstance(other, (HierarchicalPath, Path, str)):
|
||||
return str(self.path) < str(other)
|
||||
return False
|
||||
|
||||
def __le__(self, other):
|
||||
if isinstance(other, (HierarchicalPath, Path, str)):
|
||||
return str(self.path) <= str(other)
|
||||
return False
|
||||
|
||||
def __gt__(self, other):
|
||||
if isinstance(other, (HierarchicalPath, Path, str)):
|
||||
return str(self.path) > str(other)
|
||||
return False
|
||||
|
||||
def __ge__(self, other):
|
||||
if isinstance(other, (HierarchicalPath, Path, str)):
|
||||
return str(self.path) >= str(other)
|
||||
return False
|
||||
|
||||
def __hash__(self):
|
||||
return hash(self.path)
|
||||
|
||||
def __getattribute__(self, name):
|
||||
try:
|
||||
return super().__getattribute__(name)
|
||||
except AttributeError:
|
||||
return getattr(self.path, name)
|
||||
|
||||
|
||||
class Dir(BaseModel):
|
||||
HOME: HierarchicalPath = Field(default_factory=lambda: HierarchicalPath(Path.home()))
|
||||
BASE: HierarchicalPath | None = None
|
||||
|
||||
model_config = ConfigDict(arbitrary_types_allowed=True)
|
||||
|
||||
@classmethod
|
||||
def determine_base(cls) -> HierarchicalPath:
|
||||
return HierarchicalPath(HierarchicalPath()._find_base())
|
||||
|
||||
def __init__(self, **data):
|
||||
super().__init__(**data)
|
||||
if self.BASE is None:
|
||||
self.BASE = self.determine_base()
|
||||
|
||||
@classmethod
|
||||
def load(cls, yaml_path: Union[str, Path] = None) -> 'Dir':
|
||||
yaml_path = cls._resolve_path(yaml_path) if yaml_path else None
|
||||
if yaml_path:
|
||||
with open(yaml_path, 'r') as file:
|
||||
config_data = yaml.safe_load(file)
|
||||
print(f"Loaded directory configuration from {yaml_path}")
|
||||
resolved_data = cls.resolve_placeholders(config_data)
|
||||
else:
|
||||
resolved_data = {}
|
||||
return cls(**resolved_data)
|
||||
|
||||
@classmethod
|
||||
def _resolve_path(cls, path: Union[str, Path]) -> Path:
|
||||
base_path = cls.determine_base().path.parent
|
||||
path = Path(path)
|
||||
if not path.suffix:
|
||||
path = base_path / 'sijapi' / 'config' / f"{path.name}.yaml"
|
||||
elif not path.is_absolute():
|
||||
path = base_path / path
|
||||
return path
|
||||
|
||||
@classmethod
|
||||
def resolve_placeholders(cls, data: Any) -> Any:
|
||||
if isinstance(data, dict):
|
||||
return {k: cls.resolve_placeholders(v) for k, v in data.items()}
|
||||
elif isinstance(data, list):
|
||||
return [cls.resolve_placeholders(v) for v in data]
|
||||
elif isinstance(data, str):
|
||||
return cls.resolve_string_placeholders(data)
|
||||
return data
|
||||
|
||||
@classmethod
|
||||
def resolve_string_placeholders(cls, value: str) -> Any:
|
||||
if value.startswith('{{') and value.endswith('}}'):
|
||||
parts = value.strip('{}').strip().split('.')
|
||||
result = cls.HOME
|
||||
for part in parts:
|
||||
result = getattr(result, part)
|
||||
return result
|
||||
elif value == '*~*':
|
||||
return cls.HOME
|
||||
return HierarchicalPath(value)
|
||||
|
||||
def __getattr__(self, name):
|
||||
return HierarchicalPath(self.BASE / name.lower(), base=self.BASE.path, home=self.HOME.path)
|
||||
|
||||
def model_dump(self, *args, **kwargs):
|
||||
d = super().model_dump(*args, **kwargs)
|
||||
return {k: str(v) for k, v in d.items()}
|
||||
|
||||
|
||||
import os
|
||||
from pathlib import Path
|
||||
from typing import Union, Optional, Any, Dict, List
|
||||
import yaml
|
||||
import re
|
||||
from pydantic import BaseModel, create_model
|
||||
from dotenv import load_dotenv
|
||||
|
||||
class Configuration(BaseModel):
|
||||
HOME: Path = Path.home()
|
||||
HOME: Path = Field(default_factory=Path.home)
|
||||
_dir_config: Optional['Configuration'] = None
|
||||
dir: Dir = Field(default_factory=Dir)
|
||||
|
||||
class Config:
|
||||
arbitrary_types_allowed = True
|
||||
extra = "allow" # This allows extra fields
|
||||
|
||||
@classmethod
|
||||
def load(cls, yaml_path: Union[str, Path], secrets_path: Optional[Union[str, Path]] = None, dir_config: Optional['Configuration'] = None) -> 'Configuration':
|
||||
yaml_path = cls._resolve_path(yaml_path, 'config')
|
||||
if secrets_path:
|
||||
secrets_path = cls._resolve_path(secrets_path, 'config')
|
||||
|
||||
|
||||
try:
|
||||
with yaml_path.open('r') as file:
|
||||
config_data = yaml.safe_load(file)
|
||||
|
||||
print(f"Loaded configuration data from {yaml_path}")
|
||||
|
||||
if secrets_path:
|
||||
|
@ -53,25 +215,32 @@ class Configuration(BaseModel):
|
|||
print(f"Loaded secrets data from {secrets_path}")
|
||||
config_data.update(secrets_data)
|
||||
|
||||
# Ensure HOME is set
|
||||
if config_data.get('HOME') is None:
|
||||
config_data['HOME'] = str(Path.home())
|
||||
print(f"HOME was None in config, set to default: {config_data['HOME']}")
|
||||
|
||||
load_dotenv()
|
||||
|
||||
instance = cls.create_dynamic_model(**config_data)
|
||||
instance = cls(**config_data)
|
||||
instance._dir_config = dir_config or instance
|
||||
|
||||
|
||||
resolved_data = instance.resolve_placeholders(config_data)
|
||||
instance = cls.create_dynamic_model(**resolved_data)
|
||||
instance._dir_config = dir_config or instance
|
||||
|
||||
return instance
|
||||
return cls._create_nested_config(resolved_data)
|
||||
except Exception as e:
|
||||
print(f"Error loading configuration: {str(e)}")
|
||||
raise
|
||||
|
||||
@classmethod
|
||||
def _create_nested_config(cls, data):
|
||||
if isinstance(data, dict):
|
||||
print(f"Creating nested config for: {cls.__name__}")
|
||||
print(f"Data: {data}")
|
||||
return cls(**{k: cls._create_nested_config(v) for k, v in data.items()})
|
||||
elif isinstance(data, list):
|
||||
return [cls._create_nested_config(item) for item in data]
|
||||
else:
|
||||
return data
|
||||
|
||||
def __getattr__(self, name):
|
||||
value = self.__dict__.get(name)
|
||||
if isinstance(value, dict):
|
||||
return Configuration(**value)
|
||||
return value
|
||||
|
||||
@classmethod
|
||||
def _resolve_path(cls, path: Union[str, Path], default_dir: str) -> Path:
|
||||
base_path = Path(__file__).parent.parent # This will be two levels up from this file
|
||||
|
@ -98,15 +267,7 @@ class Configuration(BaseModel):
|
|||
|
||||
for match in matches:
|
||||
parts = match.split('.')
|
||||
if len(parts) == 1: # Internal reference
|
||||
replacement = getattr(self._dir_config, parts[0], str(Path.home() / parts[0].lower()))
|
||||
elif len(parts) == 2 and parts[0] == 'Dir':
|
||||
replacement = getattr(self._dir_config, parts[1], str(Path.home() / parts[1].lower()))
|
||||
elif len(parts) == 2 and parts[0] == 'ENV':
|
||||
replacement = os.getenv(parts[1], '')
|
||||
else:
|
||||
replacement = value # Keep original if not recognized
|
||||
|
||||
replacement = self._resolve_nested_placeholder(parts)
|
||||
value = value.replace('{{' + match + '}}', str(replacement))
|
||||
|
||||
# Convert to Path if it looks like a file path
|
||||
|
@ -114,25 +275,16 @@ class Configuration(BaseModel):
|
|||
return Path(value).expanduser()
|
||||
return value
|
||||
|
||||
@classmethod
|
||||
def create_dynamic_model(cls, **data):
|
||||
for key, value in data.items():
|
||||
if isinstance(value, dict):
|
||||
data[key] = cls.create_dynamic_model(**value)
|
||||
elif isinstance(value, list) and all(isinstance(item, dict) for item in value):
|
||||
data[key] = [cls.create_dynamic_model(**item) for item in value]
|
||||
|
||||
DynamicModel = create_model(
|
||||
f'Dynamic{cls.__name__}',
|
||||
__base__=cls,
|
||||
**{k: (Any, v) for k, v in data.items()}
|
||||
)
|
||||
return DynamicModel(**data)
|
||||
|
||||
class Config:
|
||||
extra = "allow"
|
||||
arbitrary_types_allowed = True
|
||||
|
||||
def _resolve_nested_placeholder(self, parts: List[str]) -> Any:
|
||||
current = self._dir_config
|
||||
for part in parts:
|
||||
if part == 'ENV':
|
||||
return os.getenv(parts[-1], '')
|
||||
elif hasattr(current, part):
|
||||
current = getattr(current, part)
|
||||
else:
|
||||
return str(Path.home() / part.lower())
|
||||
return current
|
||||
|
||||
|
||||
class APIConfig(BaseModel):
|
||||
|
@ -145,23 +297,25 @@ class APIConfig(BaseModel):
|
|||
MODULES: Any # This will be replaced with a dynamic model
|
||||
TZ: str
|
||||
KEYS: List[str]
|
||||
MAX_CPU_CORES: int = Field(default_factory=lambda: min(
|
||||
int(os.getenv("MAX_CPU_CORES", multiprocessing.cpu_count() // 2)), multiprocessing.cpu_count()
|
||||
))
|
||||
|
||||
@classmethod
|
||||
def load(cls, config_path: Union[str, Path], secrets_path: Union[str, Path]):
|
||||
config_path = cls._resolve_path(config_path, 'config')
|
||||
secrets_path = cls._resolve_path(secrets_path, 'config')
|
||||
|
||||
# Load main configuration
|
||||
with open(config_path, 'r') as file:
|
||||
config_data = yaml.safe_load(file)
|
||||
|
||||
print(f"Loaded main config: {config_data}") # Debug print
|
||||
print(f"Loaded main config: {config_data}")
|
||||
|
||||
# Load secrets
|
||||
try:
|
||||
with open(secrets_path, 'r') as file:
|
||||
secrets_data = yaml.safe_load(file)
|
||||
print(f"Loaded secrets: {secrets_data}") # Debug print
|
||||
print(f"Loaded secrets: {secrets_data}")
|
||||
except FileNotFoundError:
|
||||
print(f"Secrets file not found: {secrets_path}")
|
||||
secrets_data = {}
|
||||
|
@ -172,7 +326,7 @@ class APIConfig(BaseModel):
|
|||
# Resolve internal placeholders
|
||||
config_data = cls.resolve_placeholders(config_data)
|
||||
|
||||
print(f"Resolved config: {config_data}") # Debug print
|
||||
print(f"Resolved config: {config_data}")
|
||||
|
||||
# Handle KEYS placeholder
|
||||
if isinstance(config_data.get('KEYS'), list) and len(config_data['KEYS']) == 1:
|
||||
|
@ -184,7 +338,7 @@ class APIConfig(BaseModel):
|
|||
secret_key = parts[1]
|
||||
if secret_key in secrets_data:
|
||||
config_data['KEYS'] = secrets_data[secret_key]
|
||||
print(f"Replaced KEYS with secret: {config_data['KEYS']}") # Debug print
|
||||
print(f"Replaced KEYS with secret: {config_data['KEYS']}")
|
||||
else:
|
||||
print(f"Secret key '{secret_key}' not found in secrets file")
|
||||
else:
|
||||
|
@ -200,10 +354,10 @@ class APIConfig(BaseModel):
|
|||
modules_fields[key] = (bool, value)
|
||||
else:
|
||||
raise ValueError(f"Invalid value for module {key}: {value}. Must be 'on', 'off', True, or False.")
|
||||
|
||||
|
||||
DynamicModulesConfig = create_model('DynamicModulesConfig', **modules_fields)
|
||||
config_data['MODULES'] = DynamicModulesConfig(**modules_data)
|
||||
|
||||
|
||||
return cls(**config_data)
|
||||
|
||||
@classmethod
|
||||
|
@ -235,12 +389,12 @@ class APIConfig(BaseModel):
|
|||
resolved_data[key] = [resolve_value(item) for item in value]
|
||||
else:
|
||||
resolved_data[key] = resolve_value(value)
|
||||
|
||||
|
||||
# Resolve BIND separately to ensure HOST and PORT are used
|
||||
if 'BIND' in resolved_data:
|
||||
resolved_data['BIND'] = resolved_data['BIND'].replace('{{ HOST }}', str(resolved_data['HOST']))
|
||||
resolved_data['BIND'] = resolved_data['BIND'].replace('{{ PORT }}', str(resolved_data['PORT']))
|
||||
|
||||
|
||||
return resolved_data
|
||||
|
||||
def __getattr__(self, name: str) -> Any:
|
||||
|
@ -252,8 +406,6 @@ class APIConfig(BaseModel):
|
|||
def active_modules(self) -> List[str]:
|
||||
return [module for module, is_active in self.MODULES.__dict__.items() if is_active]
|
||||
|
||||
|
||||
|
||||
class Location(BaseModel):
|
||||
latitude: float
|
||||
longitude: float
|
||||
|
@ -481,7 +633,6 @@ class Geocoder:
|
|||
timezone=await self.timezone(latitude, longitude)
|
||||
)
|
||||
|
||||
|
||||
def round_coords(self, lat: float, lon: float, decimal_places: int = 2) -> Tuple[float, float]:
|
||||
return (round(lat, decimal_places), round(lon, decimal_places))
|
||||
|
||||
|
@ -582,55 +733,103 @@ class Database(BaseModel):
|
|||
await conn.close()
|
||||
|
||||
@classmethod
|
||||
def from_env(cls):
|
||||
import os
|
||||
return cls(
|
||||
host=os.getenv("DB_HOST", "localhost"),
|
||||
port=int(os.getenv("DB_PORT", 5432)),
|
||||
user=os.getenv("DB_USER"),
|
||||
password=os.getenv("DB_PASSWORD"),
|
||||
database=os.getenv("DB_NAME"),
|
||||
db_schema=os.getenv("DB_SCHEMA")
|
||||
)
|
||||
def from_yaml(cls, yaml_path: Union[str, Path]):
|
||||
yaml_path = Path(yaml_path)
|
||||
if not yaml_path.is_absolute():
|
||||
yaml_path = Path(__file__).parent / 'config' / yaml_path
|
||||
|
||||
with open(yaml_path, 'r') as file:
|
||||
config = yaml.safe_load(file)
|
||||
return cls(**config)
|
||||
|
||||
def to_dict(self):
|
||||
return self.dict(exclude_none=True)
|
||||
|
||||
|
||||
|
||||
class IMAPConfig(BaseModel):
|
||||
username: str
|
||||
password: str
|
||||
host: str
|
||||
port: int
|
||||
encryption: str = None
|
||||
encryption: Optional[str]
|
||||
|
||||
class SMTPConfig(BaseModel):
|
||||
username: str
|
||||
password: str
|
||||
host: str
|
||||
port: int
|
||||
encryption: str = None
|
||||
encryption: Optional[str]
|
||||
|
||||
class AutoResponder(BaseModel):
|
||||
name: str
|
||||
style: str
|
||||
context: str
|
||||
ollama_model: str = "llama3"
|
||||
image_prompt: Optional[str] = None
|
||||
image_scene: Optional[str] = None
|
||||
|
||||
class AccountAutoResponder(BaseModel):
|
||||
name: str
|
||||
smtp: str
|
||||
whitelist: List[str]
|
||||
blacklist: List[str]
|
||||
image_prompt: Optional[str] = None
|
||||
image_scene: Optional[str] = None
|
||||
smtp: SMTPConfig
|
||||
|
||||
|
||||
class EmailAccount(BaseModel):
|
||||
name: str
|
||||
refresh: int
|
||||
fullname: Optional[str]
|
||||
bio: Optional[str]
|
||||
refresh: int
|
||||
summarize: bool = False
|
||||
podcast: bool = False
|
||||
imap: IMAPConfig
|
||||
autoresponders: Optional[List[AutoResponder]]
|
||||
imap: str
|
||||
autoresponders: List[AccountAutoResponder]
|
||||
|
||||
class EmailConfiguration(Configuration):
|
||||
imaps: List[IMAPConfig]
|
||||
smtps: List[SMTPConfig]
|
||||
autoresponders: List[AutoResponder]
|
||||
accounts: List[EmailAccount]
|
||||
|
||||
@classmethod
|
||||
def _create_nested_config(cls, data):
|
||||
if isinstance(data, dict):
|
||||
if 'imaps' in data:
|
||||
return cls(
|
||||
imaps=[IMAPConfig(**imap) for imap in data['imaps']],
|
||||
smtps=[SMTPConfig(**smtp) for smtp in data['smtps']],
|
||||
autoresponders=[AutoResponder(**ar) for ar in data['autoresponders']],
|
||||
accounts=[EmailAccount(**account) for account in data['accounts']],
|
||||
**{k: v for k, v in data.items() if k not in ['imaps', 'smtps', 'autoresponders', 'accounts']}
|
||||
)
|
||||
else:
|
||||
return data # Return the dict as-is for nested structures
|
||||
elif isinstance(data, list):
|
||||
return [cls._create_nested_config(item) for item in data]
|
||||
else:
|
||||
return data
|
||||
|
||||
@classmethod
|
||||
def load(cls, yaml_path: Union[str, Path], secrets_path: Optional[Union[str, Path]] = None, dir_config: Optional['Configuration'] = None) -> 'EmailConfiguration':
|
||||
config_data = super().load(yaml_path, secrets_path, dir_config)
|
||||
return cls._create_nested_config(config_data)
|
||||
|
||||
# ... (rest of the methods remain the same)
|
||||
|
||||
def get_imap(self, username: str) -> Optional[IMAPConfig]:
|
||||
return next((imap for imap in self.imaps if imap.username == username), None)
|
||||
|
||||
def get_smtp(self, username: str) -> Optional[SMTPConfig]:
|
||||
return next((smtp for smtp in self.smtps if smtp.username == username), None)
|
||||
|
||||
def get_autoresponder(self, name: str) -> Optional[AutoResponder]:
|
||||
return next((ar for ar in self.autoresponders if ar.name == name), None)
|
||||
|
||||
def get_account(self, name: str) -> Optional[EmailAccount]:
|
||||
return next((account for account in self.accounts if account.name == name), None)
|
||||
|
||||
def get_email_accounts(self) -> List[EmailAccount]:
|
||||
return self.accounts
|
||||
|
||||
class EmailContact(BaseModel):
|
||||
email: str
|
||||
|
@ -642,4 +841,4 @@ class IncomingEmail(BaseModel):
|
|||
recipients: List[EmailContact]
|
||||
subject: str
|
||||
body: str
|
||||
attachments: List[dict] = []
|
||||
attachments: List[dict] = []
|
113
sijapi/logs.py
113
sijapi/logs.py
|
@ -1,42 +1,99 @@
|
|||
# logs.py
|
||||
import os
|
||||
import sys
|
||||
from loguru import logger
|
||||
import traceback
|
||||
|
||||
class Logger:
|
||||
def __init__(self, name, logs_dir):
|
||||
self.logs_dir = logs_dir
|
||||
self.name = name
|
||||
self.logger = logger
|
||||
self.debug_modules = set()
|
||||
self.logger = logger.bind(name=name)
|
||||
|
||||
def setup_from_args(self, args):
|
||||
if not os.path.exists(self.logs_dir):
|
||||
os.makedirs(self.logs_dir)
|
||||
|
||||
self.logger.remove()
|
||||
|
||||
log_format = "{time:YYYY-MM-DD HH:mm:ss} - {name} - <level>{level: <8}</level> - <level>{message}</level>"
|
||||
|
||||
# File handler
|
||||
self.logger.add(os.path.join(self.logs_dir, 'app.log'), rotation="2 MB", level="DEBUG", format=log_format)
|
||||
|
||||
# Set debug modules
|
||||
self.debug_modules = set(args.debug)
|
||||
|
||||
# Console handler with custom filter
|
||||
def module_filter(record):
|
||||
return (record["level"].no >= logger.level(args.log.upper()).no or
|
||||
record["name"] in self.debug_modules)
|
||||
|
||||
self.logger.add(sys.stdout, level="DEBUG", format=log_format, filter=module_filter, colorize=True)
|
||||
|
||||
# Custom color and style mappings
|
||||
self.logger.level("CRITICAL", color="<yellow><bold><MAGENTA>")
|
||||
self.logger.level("ERROR", color="<red><bold>")
|
||||
self.logger.level("WARNING", color="<yellow><bold>")
|
||||
self.logger.level("DEBUG", color="<green><bold>")
|
||||
|
||||
self.logger.info(f"Debug modules: {self.debug_modules}")
|
||||
# Remove default logger
|
||||
logger.remove()
|
||||
|
||||
def get_module_logger(self, module_name):
|
||||
return self.logger.bind(name=module_name)
|
||||
# File handler
|
||||
handler_path = os.path.join(self.logs_dir, 'app.log')
|
||||
logger.add(handler_path, rotation="2 MB", compression="zip", level="DEBUG", format="{time:YYYY-MM-DD HH:mm:ss} - {name} - {level} - {message}")
|
||||
|
||||
# Console handler
|
||||
log_format = (
|
||||
"{time:YYYY-MM-DD HH:mm:ss} - "
|
||||
"{name} - "
|
||||
"<level>{level: <8}</level> - "
|
||||
"<level>"
|
||||
"{message}"
|
||||
"</level>"
|
||||
)
|
||||
console_level = "DEBUG" if args.debug else "INFO"
|
||||
logger.add(
|
||||
sys.stdout,
|
||||
format=log_format,
|
||||
level=console_level,
|
||||
colorize=True,
|
||||
filter=lambda record: record["level"].name != "INFO", # Apply colors to all levels except INFO
|
||||
)
|
||||
|
||||
# Add a separate handler for INFO level without colors
|
||||
logger.add(
|
||||
sys.stdout,
|
||||
format="{time:YYYY-MM-DD HH:mm:ss} - {name} - {level: <8} - {message}",
|
||||
level="INFO",
|
||||
filter=lambda record: record["level"].name == "INFO",
|
||||
)
|
||||
|
||||
|
||||
# Custom color and style mappings
|
||||
logger.level("CRITICAL", color="<yellow><bold><MAGENTA>")
|
||||
logger.level("ERROR", color="<red><bold>")
|
||||
logger.level("WARNING", color="<yellow><bold>")
|
||||
logger.level("DEBUG", color="<green><bold>")
|
||||
|
||||
# Test color output
|
||||
self.logger.debug("Debug message (should be italic green)")
|
||||
self.logger.info("Info message (should be uncolored)")
|
||||
self.logger.warning("Warning message (should be bold orange/yellow)")
|
||||
self.logger.error("Error message (should be bold red)")
|
||||
self.logger.critical("Critical message (should be bold yellow on magenta)")
|
||||
|
||||
|
||||
def DEBUG(self, log_message): self.logger.debug(log_message)
|
||||
def INFO(self, log_message): self.logger.info(log_message)
|
||||
def WARN(self, log_message): self.logger.warning(log_message)
|
||||
def ERR(self, log_message):
|
||||
self.logger.error(log_message)
|
||||
self.logger.error(traceback.format_exc())
|
||||
def CRIT(self, log_message):
|
||||
self.logger.critical(log_message)
|
||||
self.logger.critical(traceback.format_exc())
|
||||
|
||||
def get_logger(self):
|
||||
return self
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import argparse
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument('--debug', action='store_true')
|
||||
args = parser.parse_args()
|
||||
|
||||
logger_instance = Logger("test", "logs")
|
||||
logger_instance.setup_from_args(args)
|
||||
test_logger = logger_instance.get_logger()
|
||||
|
||||
print("FORCE_COLOR:", os.environ.get('FORCE_COLOR'))
|
||||
print("NO_COLOR:", os.environ.get('NO_COLOR'))
|
||||
print("TERM:", os.environ.get('TERM'))
|
||||
print("PYCHARM_HOSTED:", os.environ.get('PYCHARM_HOSTED'))
|
||||
print("PYTHONIOENCODING:", os.environ.get('PYTHONIOENCODING'))
|
||||
|
||||
test_logger.debug("This is a debug message")
|
||||
test_logger.info("This is an info message")
|
||||
test_logger.warning("This is a warning message")
|
||||
test_logger.error("This is an error message")
|
||||
test_logger.critical("This is a critical message")
|
||||
|
|
|
@ -14,10 +14,10 @@ from fastapi import APIRouter, HTTPException, Form, UploadFile, File, Background
|
|||
from fastapi.responses import JSONResponse
|
||||
from pydantic import BaseModel, Field
|
||||
from typing import Optional
|
||||
from sijapi import L, ASR_DIR, WHISPER_CPP_MODELS, GARBAGE_COLLECTION_INTERVAL, GARBAGE_TTL, WHISPER_CPP_DIR, MAX_CPU_CORES
|
||||
|
||||
from sijapi import L, API, Dir, ASR
|
||||
|
||||
asr = APIRouter()
|
||||
logger = L.get_module_logger("asr")
|
||||
|
||||
class TranscribeParams(BaseModel):
|
||||
model: str = Field(default="small")
|
||||
|
@ -81,13 +81,13 @@ async def transcribe_endpoint(
|
|||
return JSONResponse(content={"status": "timeout", "message": "Transcription is taking longer than expected. Please check back later."}, status_code=202)
|
||||
|
||||
async def transcribe_audio(file_path, params: TranscribeParams):
|
||||
logger.debug(f"Transcribing audio file from {file_path}...")
|
||||
L.DEBUG(f"Transcribing audio file from {file_path}...")
|
||||
file_path = await convert_to_wav(file_path)
|
||||
model = params.model if params.model in WHISPER_CPP_MODELS else 'small'
|
||||
model_path = WHISPER_CPP_DIR / 'models' / f'ggml-{model}.bin'
|
||||
command = [str(WHISPER_CPP_DIR / 'build' / 'bin' / 'main')]
|
||||
model = params.model if params.model in ASR.MODELS else 'small'
|
||||
model_path = ASR.WHISPER_DIR.models / f'ggml-{model}.bin'
|
||||
command = [str(ASR.WHISPER_DIR.build.bin.main)]
|
||||
command.extend(['-m', str(model_path)])
|
||||
command.extend(['-t', str(max(1, min(params.threads or MAX_CPU_CORES, MAX_CPU_CORES)))])
|
||||
command.extend(['-t', str(max(1, min(params.threads or API.MAX_CPU_CORES, API.MAX_CPU_CORES)))])
|
||||
command.extend(['-np']) # Always enable no-prints
|
||||
|
||||
|
||||
|
@ -119,11 +119,11 @@ async def transcribe_audio(file_path, params: TranscribeParams):
|
|||
command.extend(['--dtw', params.dtw])
|
||||
|
||||
command.extend(['-f', file_path])
|
||||
logger.debug(f"Command: {command}")
|
||||
L.DEBUG(f"Command: {command}")
|
||||
|
||||
# Create a unique ID for this transcription job
|
||||
job_id = str(uuid.uuid4())
|
||||
logger.debug(f"Created job ID: {job_id}")
|
||||
L.DEBUG(f"Created job ID: {job_id}")
|
||||
|
||||
# Store the job status
|
||||
transcription_results[job_id] = {"status": "processing", "result": None}
|
||||
|
@ -135,20 +135,20 @@ async def transcribe_audio(file_path, params: TranscribeParams):
|
|||
poll_interval = 1 # 1 second
|
||||
start_time = asyncio.get_event_loop().time()
|
||||
|
||||
logger.debug(f"Starting to poll for job {job_id}")
|
||||
L.DEBUG(f"Starting to poll for job {job_id}")
|
||||
try:
|
||||
while asyncio.get_event_loop().time() - start_time < max_wait_time:
|
||||
job_status = transcription_results.get(job_id, {})
|
||||
logger.debug(f"Current status for job {job_id}: {job_status['status']}")
|
||||
L.DEBUG(f"Current status for job {job_id}: {job_status['status']}")
|
||||
if job_status["status"] == "completed":
|
||||
logger.info(f"Transcription completed for job {job_id}")
|
||||
L.INFO(f"Transcription completed for job {job_id}")
|
||||
return job_status["result"]
|
||||
elif job_status["status"] == "failed":
|
||||
logger.error(f"Transcription failed for job {job_id}: {job_status.get('error', 'Unknown error')}")
|
||||
L.ERR(f"Transcription failed for job {job_id}: {job_status.get('error', 'Unknown error')}")
|
||||
raise Exception(f"Transcription failed: {job_status.get('error', 'Unknown error')}")
|
||||
await asyncio.sleep(poll_interval)
|
||||
|
||||
logger.error(f"Transcription timed out for job {job_id}")
|
||||
L.ERR(f"Transcription timed out for job {job_id}")
|
||||
raise TimeoutError("Transcription timed out")
|
||||
finally:
|
||||
# Ensure the task is cancelled if we exit the loop
|
||||
|
@ -159,20 +159,20 @@ async def transcribe_audio(file_path, params: TranscribeParams):
|
|||
|
||||
async def process_transcription(command, file_path, job_id):
|
||||
try:
|
||||
logger.debug(f"Starting transcription process for job {job_id}")
|
||||
L.DEBUG(f"Starting transcription process for job {job_id}")
|
||||
result = await run_transcription(command, file_path)
|
||||
transcription_results[job_id] = {"status": "completed", "result": result}
|
||||
logger.debug(f"Transcription completed for job {job_id}")
|
||||
L.DEBUG(f"Transcription completed for job {job_id}")
|
||||
except Exception as e:
|
||||
logger.error(f"Transcription failed for job {job_id}: {str(e)}")
|
||||
L.ERR(f"Transcription failed for job {job_id}: {str(e)}")
|
||||
transcription_results[job_id] = {"status": "failed", "error": str(e)}
|
||||
finally:
|
||||
# Clean up the temporary file
|
||||
os.remove(file_path)
|
||||
logger.debug(f"Cleaned up temporary file for job {job_id}")
|
||||
L.DEBUG(f"Cleaned up temporary file for job {job_id}")
|
||||
|
||||
async def run_transcription(command, file_path):
|
||||
logger.debug(f"Running transcription command: {' '.join(command)}")
|
||||
L.DEBUG(f"Running transcription command: {' '.join(command)}")
|
||||
proc = await asyncio.create_subprocess_exec(
|
||||
*command,
|
||||
stdout=asyncio.subprocess.PIPE,
|
||||
|
@ -181,13 +181,13 @@ async def run_transcription(command, file_path):
|
|||
stdout, stderr = await proc.communicate()
|
||||
if proc.returncode != 0:
|
||||
error_message = f"Error running command: {stderr.decode()}"
|
||||
logger.error(error_message)
|
||||
L.ERR(error_message)
|
||||
raise Exception(error_message)
|
||||
logger.debug("Transcription command completed successfully")
|
||||
L.DEBUG("Transcription command completed successfully")
|
||||
return stdout.decode().strip()
|
||||
|
||||
async def convert_to_wav(file_path: str):
|
||||
wav_file_path = os.path.join(ASR_DIR, f"{uuid.uuid4()}.wav")
|
||||
wav_file_path = os.path.join(Dir.data.asr, f"{uuid.uuid4()}.wav")
|
||||
proc = await asyncio.create_subprocess_exec(
|
||||
"ffmpeg", "-y", "-i", file_path, "-acodec", "pcm_s16le", "-ar", "16000", "-ac", "1", wav_file_path,
|
||||
stdout=asyncio.subprocess.PIPE,
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
'''
|
||||
Calendar module using macOS Calendars and/or Microsoft 365 via its Graph API.
|
||||
Depends on:
|
||||
LOGGER, ICAL_TOGGLE, ICALENDARS, MS365_TOGGLE, MS365_CLIENT_ID, MS365_SECRET, MS365_AUTHORITY_URL, MS365_SCOPE, MS365_REDIRECT_PATH, MS365_TOKEN_PATH
|
||||
LOGGER, ICAL_TOGGLE, ICALENDARS, MS365_TOGGLE, MS365_CLIENT_ID, MS365_SECRET, MS365_AUTHORITY_URL, MS365_SCOPE, MS365_REDIRECT_PATH, Cal.MS365.auth.token
|
||||
'''
|
||||
from fastapi import APIRouter, Depends, HTTPException, status, Request
|
||||
from fastapi.responses import RedirectResponse, JSONResponse
|
||||
|
@ -16,67 +16,70 @@ from typing import Dict, List, Any
|
|||
from datetime import datetime, timedelta
|
||||
from Foundation import NSDate, NSRunLoop
|
||||
import EventKit as EK
|
||||
from sijapi import L, ICAL_TOGGLE, ICALENDARS, MS365_TOGGLE, MS365_CLIENT_ID, MS365_SECRET, MS365_AUTHORITY_URL, MS365_SCOPE, MS365_REDIRECT_PATH, MS365_TOKEN_PATH
|
||||
from sijapi import L, Cal
|
||||
from sijapi.routers import loc
|
||||
|
||||
cal = APIRouter()
|
||||
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="/token")
|
||||
timeout = httpx.Timeout(12)
|
||||
logger = L.get_module_logger("cal")
|
||||
|
||||
if MS365_TOGGLE is True:
|
||||
logger.critical(f"Visit https://api.sij.ai/o365/login to obtain your Microsoft 365 authentication token.")
|
||||
print(f"Cal object: {Cal}")
|
||||
print(f"Cal.__dict__: {Cal.__dict__}")
|
||||
print(f"Cal.MS365: {Cal.MS365}")
|
||||
|
||||
@cal.get("/o365/login")
|
||||
if Cal.MS365.toggle == 'on':
|
||||
L.CRIT(f"Visit https://api.sij.ai/MS365/login to obtain your Microsoft 365 authentication token.")
|
||||
|
||||
@cal.get("/MS365/login")
|
||||
async def login():
|
||||
logger.debug(f"Received request to /o365/login")
|
||||
logger.debug(f"SCOPE: {MS365_SCOPE}")
|
||||
if not MS365_SCOPE:
|
||||
logger.error("No scopes defined for authorization.")
|
||||
L.DEBUG(f"Received request to /MS365/login")
|
||||
L.DEBUG(f"SCOPE: {Cal.MS365.auth.scopes}")
|
||||
if not Cal.MS365.auth.scopes:
|
||||
L.ERR("No scopes defined for authorization.")
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
detail="No scopes defined for authorization."
|
||||
)
|
||||
authorization_url = f"{MS365_AUTHORITY_URL}/oauth2/v2.0/authorize?client_id={MS365_CLIENT_ID}&response_type=code&redirect_uri={MS365_REDIRECT_PATH}&scope={'+'.join(MS365_SCOPE)}"
|
||||
logger.info(f"Redirecting to authorization URL: {authorization_url}")
|
||||
authorization_url = f"{Cal.MS365.auth.url}/oauth2/v2.0/authorize?client_id={Cal.MS365.client}&response_type=code&redirect_uri={Cal.MS365.auth.redirect}&scope={'+'.join(Cal.MS365.auth.scopes)}"
|
||||
L.INFO(f"Redirecting to authorization URL: {authorization_url}")
|
||||
return RedirectResponse(authorization_url)
|
||||
|
||||
@cal.get("/o365/oauth_redirect")
|
||||
@cal.get("/MS365/oauth_redirect")
|
||||
async def oauth_redirect(code: str = None, error: str = None):
|
||||
logger.debug(f"Received request to /o365/oauth_redirect")
|
||||
L.DEBUG(f"Received request to /MS365/oauth_redirect")
|
||||
if error:
|
||||
logger.error(f"OAuth2 Error: {error}")
|
||||
L.ERR(f"OAuth2 Error: {error}")
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST, detail="OAuth2 Error"
|
||||
)
|
||||
logger.info(f"Requesting token with authorization code: {code}")
|
||||
token_url = f"{MS365_AUTHORITY_URL}/oauth2/v2.0/token"
|
||||
L.INFO(f"Requesting token with authorization code: {code}")
|
||||
token_url = f"{Cal.MS365.auth.url}/oauth2/v2.0/token"
|
||||
data = {
|
||||
"client_id": MS365_CLIENT_ID,
|
||||
"client_secret": MS365_SECRET,
|
||||
"client_id": Cal.MS365.client,
|
||||
"client_secret": Cal.MS365.auth.secret,
|
||||
"code": code,
|
||||
"redirect_uri": MS365_REDIRECT_PATH,
|
||||
"redirect_uri": Cal.MS365.auth.redirect,
|
||||
"grant_type": "authorization_code"
|
||||
}
|
||||
async with httpx.AsyncClient(timeout=timeout) as client:
|
||||
response = await client.post(token_url, data=data)
|
||||
logger.debug(f"Token endpoint response status code: {response.status_code}")
|
||||
logger.info(f"Token endpoint response text: {response.text}")
|
||||
L.DEBUG(f"Token endpoint response status code: {response.status_code}")
|
||||
L.INFO(f"Token endpoint response text: {response.text}")
|
||||
result = response.json()
|
||||
if 'access_token' in result:
|
||||
await save_token(result)
|
||||
logger.info("Access token obtained successfully")
|
||||
L.INFO("Access token obtained successfully")
|
||||
return {"message": "Access token stored successfully"}
|
||||
else:
|
||||
logger.critical(f"Failed to obtain access token. Response: {result}")
|
||||
L.CRIT(f"Failed to obtain access token. Response: {result}")
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
detail="Failed to obtain access token"
|
||||
)
|
||||
|
||||
@cal.get("/o365/me")
|
||||
@cal.get("/MS365/me")
|
||||
async def read_items():
|
||||
logger.debug(f"Received request to /o365/me")
|
||||
L.DEBUG(f"Received request to /MS365/me")
|
||||
token = await load_token()
|
||||
if not token:
|
||||
raise HTTPException(
|
||||
|
@ -89,10 +92,10 @@ if MS365_TOGGLE is True:
|
|||
response = await client.get(graph_url, headers=headers)
|
||||
if response.status_code == 200:
|
||||
user = response.json()
|
||||
logger.info(f"User retrieved: {user}")
|
||||
L.INFO(f"User retrieved: {user}")
|
||||
return user
|
||||
else:
|
||||
logger.error("Invalid or expired token")
|
||||
L.ERR("Invalid or expired token")
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||
detail="Invalid or expired token",
|
||||
|
@ -100,36 +103,36 @@ if MS365_TOGGLE is True:
|
|||
)
|
||||
|
||||
async def save_token(token):
|
||||
logger.debug(f"Saving token: {token}")
|
||||
L.DEBUG(f"Saving token: {token}")
|
||||
try:
|
||||
token["expires_at"] = int(time.time()) + token["expires_in"]
|
||||
with open(MS365_TOKEN_PATH, "w") as file:
|
||||
with open(Cal.MS365.auth.token, "w") as file:
|
||||
json.dump(token, file)
|
||||
logger.debug(f"Saved token to {MS365_TOKEN_PATH}")
|
||||
L.DEBUG(f"Saved token to {Cal.MS365.auth.token}")
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to save token: {e}")
|
||||
L.ERR(f"Failed to save token: {e}")
|
||||
|
||||
async def load_token():
|
||||
if os.path.exists(MS365_TOKEN_PATH):
|
||||
if os.path.exists(Cal.MS365.auth.token):
|
||||
try:
|
||||
with open(MS365_TOKEN_PATH, "r") as file:
|
||||
with open(Cal.MS365.auth.token, "r") as file:
|
||||
token = json.load(file)
|
||||
except FileNotFoundError:
|
||||
logger.error("Token file not found.")
|
||||
L.ERR("Token file not found.")
|
||||
return None
|
||||
except json.JSONDecodeError:
|
||||
logger.error("Failed to decode token JSON")
|
||||
L.ERR("Failed to decode token JSON")
|
||||
return None
|
||||
|
||||
if token:
|
||||
token["expires_at"] = int(time.time()) + token["expires_in"]
|
||||
logger.debug(f"Loaded token: {token}") # Add this line to log the loaded token
|
||||
L.DEBUG(f"Loaded token: {token}") # Add this line to log the loaded token
|
||||
return token
|
||||
else:
|
||||
logger.debug("No token found.")
|
||||
L.DEBUG("No token found.")
|
||||
return None
|
||||
else:
|
||||
logger.error(f"No file found at {MS365_TOKEN_PATH}")
|
||||
L.ERR(f"No file found at {Cal.MS365.auth.token}")
|
||||
return None
|
||||
|
||||
|
||||
|
@ -147,51 +150,81 @@ if MS365_TOGGLE is True:
|
|||
return response.status_code == 401
|
||||
|
||||
async def get_new_token_with_refresh_token(refresh_token):
|
||||
token_url = f"{MS365_AUTHORITY_URL}/oauth2/v2.0/token"
|
||||
token_url = f"{Cal.MS365.auth.url}/oauth2/v2.0/token"
|
||||
data = {
|
||||
"client_id": MS365_CLIENT_ID,
|
||||
"client_secret": MS365_SECRET,
|
||||
"client_id": Cal.MS365.client,
|
||||
"client_secret": Cal.MS365.auth.secret,
|
||||
"refresh_token": refresh_token,
|
||||
"grant_type": "refresh_token",
|
||||
"scope": " ".join(MS365_SCOPE),
|
||||
"scope": " ".join(Cal.MS365.auth.scopes),
|
||||
}
|
||||
async with httpx.AsyncClient(timeout=timeout) as client:
|
||||
response = await client.post(token_url, data=data)
|
||||
result = response.json()
|
||||
if "access_token" in result:
|
||||
logger.info("Access token refreshed successfully")
|
||||
L.INFO("Access token refreshed successfully")
|
||||
return result
|
||||
else:
|
||||
logger.error("Failed to refresh access token")
|
||||
L.ERR("Failed to refresh access token")
|
||||
return None
|
||||
|
||||
async def get_ms365_events(start_date: datetime, end_date: datetime):
|
||||
token = await load_token()
|
||||
if token:
|
||||
if await is_token_expired(token):
|
||||
await refresh_token()
|
||||
else:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||
detail="Access token not found",
|
||||
)
|
||||
# this looks like it might need updating to use tz-aware datetimes converted to UTC...
|
||||
graph_url = f"https://graph.microsoft.com/v1.0/me/events?$filter=start/dateTime ge '{start_date}T00:00:00' and end/dateTime le '{end_date}T23:59:59'"
|
||||
headers = {
|
||||
"Authorization": f"Bearer {token['access_token']}",
|
||||
"Prefer": 'outlook.timezone="Pacific Standard Time"',
|
||||
}
|
||||
async with httpx.AsyncClient() as client:
|
||||
response = await client.get(graph_url, headers=headers)
|
||||
|
||||
if response.status_code != 200:
|
||||
L.ERR("Failed to retrieve events from Microsoft 365")
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
detail="Failed to retrieve events",
|
||||
)
|
||||
|
||||
ms_events = response.json().get("value", [])
|
||||
return ms_events
|
||||
|
||||
|
||||
|
||||
async def refresh_token():
|
||||
token = await load_token()
|
||||
if not token:
|
||||
logger.error("No token found in storage")
|
||||
L.ERR("No token found in storage")
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||
detail="No token found",
|
||||
)
|
||||
|
||||
if 'refresh_token' not in token:
|
||||
logger.error("Refresh token not found in the loaded token")
|
||||
L.ERR("Refresh token not found in the loaded token")
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||
detail="Refresh token not found",
|
||||
)
|
||||
|
||||
refresh_token = token['refresh_token']
|
||||
logger.debug("Found refresh token, attempting to refresh access token")
|
||||
L.DEBUG("Found refresh token, attempting to refresh access token")
|
||||
|
||||
new_token = await get_new_token_with_refresh_token(refresh_token)
|
||||
|
||||
if new_token:
|
||||
await save_token(new_token)
|
||||
logger.info("Token refreshed and saved successfully")
|
||||
L.INFO("Token refreshed and saved successfully")
|
||||
else:
|
||||
logger.error("Failed to refresh token")
|
||||
L.ERR("Failed to refresh token")
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
detail="Failed to refresh token",
|
||||
|
@ -205,7 +238,7 @@ def get_calendar_ids() -> Dict[str, str]:
|
|||
calendar_identifiers = {
|
||||
calendar.title() : calendar.calendarIdentifier() for calendar in all_calendars
|
||||
}
|
||||
logger.debug(f"{calendar_identifiers}")
|
||||
L.DEBUG(f"{calendar_identifiers}")
|
||||
return calendar_identifiers
|
||||
|
||||
# Helper to convert datetime to NSDate
|
||||
|
@ -224,12 +257,12 @@ async def get_events_endpoint(start_date: str, end_date: str):
|
|||
|
||||
async def get_events(start_dt: datetime, end_dt: datetime) -> List:
|
||||
combined_events = []
|
||||
if MS365_TOGGLE:
|
||||
if Cal.MS365.toggle == "on":
|
||||
ms_events = await get_ms365_events(start_dt, end_dt)
|
||||
combined_events.extend(ms_events) # Use extend instead of append
|
||||
|
||||
if ICAL_TOGGLE:
|
||||
calendar_ids = ICALENDARS
|
||||
if Cal.ICAL.toggle == "on":
|
||||
calendar_ids = Cal.ICAL.calendars
|
||||
macos_events = get_macos_calendar_events(start_dt, end_dt, calendar_ids)
|
||||
combined_events.extend(macos_events) # Use extend instead of append
|
||||
|
||||
|
@ -237,108 +270,80 @@ async def get_events(start_dt: datetime, end_dt: datetime) -> List:
|
|||
return parsed_events
|
||||
|
||||
|
||||
def get_macos_calendar_events(start_date: datetime, end_date: datetime, calendar_ids: List[str] = None) -> List[Dict]:
|
||||
event_store = EK.EKEventStore.alloc().init()
|
||||
if Cal.ICAL.toggle == "on":
|
||||
def get_macos_calendar_events(start_date: datetime, end_date: datetime, calendar_ids: List[str] = None) -> List[Dict]:
|
||||
event_store = EK.EKEventStore.alloc().init()
|
||||
|
||||
# Request access to EventKit
|
||||
def request_access() -> bool:
|
||||
access_granted = []
|
||||
# Request access to EventKit
|
||||
def request_access() -> bool:
|
||||
access_granted = []
|
||||
|
||||
def completion_handler(granted, error):
|
||||
if error is not None:
|
||||
logger.error(f"Error: {error}")
|
||||
access_granted.append(granted)
|
||||
# Notify the main thread that the completion handler has executed
|
||||
def completion_handler(granted, error):
|
||||
if error is not None:
|
||||
L.ERR(f"Error: {error}")
|
||||
access_granted.append(granted)
|
||||
# Notify the main thread that the completion handler has executed
|
||||
with access_granted_condition:
|
||||
access_granted_condition.notify()
|
||||
|
||||
access_granted_condition = threading.Condition()
|
||||
with access_granted_condition:
|
||||
access_granted_condition.notify()
|
||||
event_store.requestAccessToEntityType_completion_(0, completion_handler) # 0 corresponds to EKEntityTypeEvent
|
||||
# Wait for the completion handler to be called
|
||||
access_granted_condition.wait(timeout=10)
|
||||
# Verify that the handler was called and access_granted is not empty
|
||||
if access_granted:
|
||||
return access_granted[0]
|
||||
else:
|
||||
L.ERR("Request access timed out or failed")
|
||||
return False
|
||||
|
||||
access_granted_condition = threading.Condition()
|
||||
with access_granted_condition:
|
||||
event_store.requestAccessToEntityType_completion_(0, completion_handler) # 0 corresponds to EKEntityTypeEvent
|
||||
# Wait for the completion handler to be called
|
||||
access_granted_condition.wait(timeout=10)
|
||||
# Verify that the handler was called and access_granted is not empty
|
||||
if access_granted:
|
||||
return access_granted[0]
|
||||
else:
|
||||
logger.error("Request access timed out or failed")
|
||||
return False
|
||||
if not request_access():
|
||||
L.ERR("Access to calendar data was not granted")
|
||||
return []
|
||||
|
||||
if not request_access():
|
||||
logger.error("Access to calendar data was not granted")
|
||||
return []
|
||||
ns_start_date = datetime_to_nsdate(start_date)
|
||||
ns_end_date = datetime_to_nsdate(end_date)
|
||||
|
||||
ns_start_date = datetime_to_nsdate(start_date)
|
||||
ns_end_date = datetime_to_nsdate(end_date)
|
||||
|
||||
# Retrieve all calendars
|
||||
all_calendars = event_store.calendarsForEntityType_(0) # 0 corresponds to EKEntityTypeEvent
|
||||
if calendar_ids:
|
||||
selected_calendars = [cal for cal in all_calendars if cal.calendarIdentifier() in calendar_ids]
|
||||
else:
|
||||
selected_calendars = all_calendars
|
||||
|
||||
# Filtering events by selected calendars
|
||||
predicate = event_store.predicateForEventsWithStartDate_endDate_calendars_(ns_start_date, ns_end_date, selected_calendars)
|
||||
events = event_store.eventsMatchingPredicate_(predicate)
|
||||
|
||||
event_list = []
|
||||
for event in events:
|
||||
# Check if event.attendees() returns None
|
||||
if event.attendees():
|
||||
attendees = [{'name': att.name(), 'email': att.emailAddress()} for att in event.attendees() if att.emailAddress()]
|
||||
# Retrieve all calendars
|
||||
all_calendars = event_store.calendarsForEntityType_(0) # 0 corresponds to EKEntityTypeEvent
|
||||
if calendar_ids:
|
||||
selected_calendars = [cal for cal in all_calendars if cal.calendarIdentifier() in calendar_ids]
|
||||
else:
|
||||
attendees = []
|
||||
selected_calendars = all_calendars
|
||||
|
||||
# Format the start and end dates properly
|
||||
start_date_str = event.startDate().descriptionWithLocale_(None)
|
||||
end_date_str = event.endDate().descriptionWithLocale_(None)
|
||||
# Filtering events by selected calendars
|
||||
predicate = event_store.predicateForEventsWithStartDate_endDate_calendars_(ns_start_date, ns_end_date, selected_calendars)
|
||||
events = event_store.eventsMatchingPredicate_(predicate)
|
||||
|
||||
event_data = {
|
||||
"subject": event.title(),
|
||||
"id": event.eventIdentifier(),
|
||||
"start": start_date_str,
|
||||
"end": end_date_str,
|
||||
"bodyPreview": event.notes() if event.notes() else '',
|
||||
"attendees": attendees,
|
||||
"location": event.location() if event.location() else '',
|
||||
"onlineMeetingUrl": '', # Defaulting to empty as macOS EventKit does not provide this
|
||||
"showAs": 'busy', # Default to 'busy'
|
||||
"isAllDay": event.isAllDay()
|
||||
}
|
||||
event_list = []
|
||||
for event in events:
|
||||
# Check if event.attendees() returns None
|
||||
if event.attendees():
|
||||
attendees = [{'name': att.name(), 'email': att.emailAddress()} for att in event.attendees() if att.emailAddress()]
|
||||
else:
|
||||
attendees = []
|
||||
|
||||
event_list.append(event_data)
|
||||
# Format the start and end dates properly
|
||||
start_date_str = event.startDate().descriptionWithLocale_(None)
|
||||
end_date_str = event.endDate().descriptionWithLocale_(None)
|
||||
|
||||
return event_list
|
||||
event_data = {
|
||||
"subject": event.title(),
|
||||
"id": event.eventIdentifier(),
|
||||
"start": start_date_str,
|
||||
"end": end_date_str,
|
||||
"bodyPreview": event.notes() if event.notes() else '',
|
||||
"attendees": attendees,
|
||||
"location": event.location() if event.location() else '',
|
||||
"onlineMeetingUrl": '', # Defaulting to empty as macOS EventKit does not provide this
|
||||
"showAs": 'busy', # Default to 'busy'
|
||||
"isAllDay": event.isAllDay()
|
||||
}
|
||||
|
||||
async def get_ms365_events(start_date: datetime, end_date: datetime):
|
||||
token = await load_token()
|
||||
if token:
|
||||
if await is_token_expired(token):
|
||||
await refresh_token()
|
||||
else:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||
detail="Access token not found",
|
||||
)
|
||||
# this looks like it might need updating to use tz-aware datetimes converted to UTC...
|
||||
graph_url = f"https://graph.microsoft.com/v1.0/me/events?$filter=start/dateTime ge '{start_date}T00:00:00' and end/dateTime le '{end_date}T23:59:59'"
|
||||
headers = {
|
||||
"Authorization": f"Bearer {token['access_token']}",
|
||||
"Prefer": 'outlook.timezone="Pacific Standard Time"',
|
||||
}
|
||||
async with httpx.AsyncClient() as client:
|
||||
response = await client.get(graph_url, headers=headers)
|
||||
event_list.append(event_data)
|
||||
|
||||
if response.status_code != 200:
|
||||
logger.error("Failed to retrieve events from Microsoft 365")
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
detail="Failed to retrieve events",
|
||||
)
|
||||
|
||||
ms_events = response.json().get("value", [])
|
||||
return ms_events
|
||||
return event_list
|
||||
|
||||
|
||||
async def parse_calendar_for_day(range_start: datetime, range_end: datetime, events: List[Dict[str, Any]]):
|
||||
|
@ -347,33 +352,33 @@ async def parse_calendar_for_day(range_start: datetime, range_end: datetime, eve
|
|||
event_list = []
|
||||
|
||||
for event in events:
|
||||
logger.info(f"Event: {event}")
|
||||
L.INFO(f"Event: {event}")
|
||||
start_str = event.get('start')
|
||||
end_str = event.get('end')
|
||||
|
||||
if isinstance(start_str, dict):
|
||||
start_str = start_str.get('dateTime')
|
||||
else:
|
||||
logger.info(f"Start date string not a dict")
|
||||
L.INFO(f"Start date string not a dict")
|
||||
|
||||
if isinstance(end_str, dict):
|
||||
end_str = end_str.get('dateTime')
|
||||
else:
|
||||
logger.info(f"End date string not a dict")
|
||||
L.INFO(f"End date string not a dict")
|
||||
|
||||
try:
|
||||
start_date = await loc.dt(start_str) if start_str else None
|
||||
except (ValueError, TypeError) as e:
|
||||
logger.error(f"Invalid start date format: {start_str}, error: {e}")
|
||||
L.ERR(f"Invalid start date format: {start_str}, error: {e}")
|
||||
continue
|
||||
|
||||
try:
|
||||
end_date = await loc.dt(end_str) if end_str else None
|
||||
except (ValueError, TypeError) as e:
|
||||
logger.error(f"Invalid end date format: {end_str}, error: {e}")
|
||||
L.ERR(f"Invalid end date format: {end_str}, error: {e}")
|
||||
continue
|
||||
|
||||
logger.debug(f"Comparing {start_date} with range {range_start} to {range_end}")
|
||||
L.DEBUG(f"Comparing {start_date} with range {range_start} to {range_end}")
|
||||
|
||||
if start_date:
|
||||
# Ensure start_date is timezone-aware
|
||||
|
@ -405,11 +410,11 @@ async def parse_calendar_for_day(range_start: datetime, range_end: datetime, eve
|
|||
"busy": event.get('showAs', '') in ['busy', 'tentative'],
|
||||
"all_day": event.get('isAllDay', False)
|
||||
}
|
||||
logger.info(f"Event_data: {event_data}")
|
||||
L.INFO(f"Event_data: {event_data}")
|
||||
event_list.append(event_data)
|
||||
else:
|
||||
logger.debug(f"Event outside of specified range: {start_date} to {end_date}")
|
||||
L.DEBUG(f"Event outside of specified range: {start_date} to {end_date}")
|
||||
else:
|
||||
logger.error(f"Invalid or missing start date for event: {event.get('id', 'Unknown ID')}")
|
||||
L.ERR(f"Invalid or missing start date for event: {event.get('id', 'Unknown ID')}")
|
||||
|
||||
return event_list
|
|
@ -12,7 +12,6 @@ from asyncio import sleep
|
|||
import os
|
||||
|
||||
cf = APIRouter()
|
||||
logger = L.get_module_logger("cal")
|
||||
|
||||
class DNSRecordRequest(BaseModel):
|
||||
full_domain: str
|
||||
|
@ -70,7 +69,7 @@ async def retry_request(url, headers, max_retries=5, backoff_factor=1):
|
|||
response.raise_for_status()
|
||||
return response
|
||||
except (httpx.HTTPError, httpx.ConnectTimeout) as e:
|
||||
logger.error(f"Request failed: {e}. Retrying {retry + 1}/{max_retries}...")
|
||||
L.ERR(f"Request failed: {e}. Retrying {retry + 1}/{max_retries}...")
|
||||
await sleep(backoff_factor * (2 ** retry))
|
||||
raise HTTPException(status_code=500, detail="Max retries exceeded for Cloudflare API request")
|
||||
|
||||
|
|
|
@ -5,10 +5,10 @@ from fastapi import APIRouter, HTTPException
|
|||
import asyncio
|
||||
import logging
|
||||
from sijapi.utilities import run_ssh_command
|
||||
from sijapi import L, REBOOT_SCRIPT_PATH, HOST_CONFIG, API_CONFIG
|
||||
from sijapi import REBOOT_SCRIPT_PATH, HOST_CONFIG, API_CONFIG
|
||||
|
||||
dist = APIRouter()
|
||||
logger = L.get_module_logger("dist")
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@dist.get("/update-restart-others")
|
||||
async def update_and_restart_others():
|
||||
|
|
|
@ -20,26 +20,19 @@ import ssl
|
|||
import yaml
|
||||
from typing import List, Dict, Optional, Set
|
||||
from datetime import datetime as dt_datetime
|
||||
from sijapi import L, PODCAST_DIR, DEFAULT_VOICE, EMAIL_CONFIG, EMAIL_LOGS
|
||||
from sijapi import L, TTS, Email, Dir
|
||||
from sijapi.routers import img, loc, tts, llm
|
||||
from sijapi.utilities import clean_text, assemble_journal_path, extract_text, prefix_lines
|
||||
from sijapi.classes import EmailAccount, IMAPConfig, SMTPConfig, IncomingEmail, EmailContact, AutoResponder
|
||||
from sijapi.classes import EmailAccount
|
||||
|
||||
email = APIRouter()
|
||||
|
||||
logger = L.get_module_logger("email")
|
||||
print(f"Email logger level: {logger.level}") # Debug print
|
||||
|
||||
logger.debug("This is a debug message from email module")
|
||||
logger.info("This is an info message from email module")
|
||||
|
||||
email = APIRouter(tags=["private"])
|
||||
|
||||
def load_email_accounts(yaml_path: str) -> List[EmailAccount]:
|
||||
with open(yaml_path, 'r') as file:
|
||||
config = yaml.safe_load(file)
|
||||
return [EmailAccount(**account) for account in config['accounts']]
|
||||
|
||||
|
||||
def get_imap_connection(account: EmailAccount):
|
||||
return Imbox(account.imap.host,
|
||||
username=account.imap.username,
|
||||
|
@ -48,45 +41,42 @@ def get_imap_connection(account: EmailAccount):
|
|||
ssl=account.imap.encryption == 'SSL',
|
||||
starttls=account.imap.encryption == 'STARTTLS')
|
||||
|
||||
|
||||
def get_smtp_connection(autoresponder: AutoResponder):
|
||||
def get_smtp_connection(autoresponder):
|
||||
# Create an SSL context that doesn't verify certificates
|
||||
context = ssl.create_default_context()
|
||||
context.check_hostname = False
|
||||
context.verify_mode = ssl.CERT_NONE
|
||||
|
||||
smtp_config = autoresponder.smtp
|
||||
|
||||
if smtp_config.encryption == 'SSL':
|
||||
if autoresponder.smtp.encryption == 'SSL':
|
||||
try:
|
||||
logger.debug(f"Attempting SSL connection to {smtp_config.host}:{smtp_config.port}")
|
||||
return SMTP_SSL(smtp_config.host, smtp_config.port, context=context)
|
||||
L.DEBUG(f"Attempting SSL connection to {autoresponder.smtp.host}:{autoresponder.smtp.port}")
|
||||
return SMTP_SSL(autoresponder.smtp.host, autoresponder.smtp.port, context=context)
|
||||
except ssl.SSLError as e:
|
||||
logger.error(f"SSL connection failed: {str(e)}")
|
||||
L.ERR(f"SSL connection failed: {str(e)}")
|
||||
# If SSL fails, try TLS
|
||||
try:
|
||||
logger.debug(f"Attempting STARTTLS connection to {smtp_config.host}:{smtp_config.port}")
|
||||
smtp = SMTP(smtp_config.host, smtp_config.port)
|
||||
L.DEBUG(f"Attempting STARTTLS connection to {autoresponder.smtp.host}:{autoresponder.smtp.port}")
|
||||
smtp = SMTP(autoresponder.smtp.host, autoresponder.smtp.port)
|
||||
smtp.starttls(context=context)
|
||||
return smtp
|
||||
except Exception as e:
|
||||
logger.error(f"STARTTLS connection failed: {str(e)}")
|
||||
L.ERR(f"STARTTLS connection failed: {str(e)}")
|
||||
raise
|
||||
elif smtp_config.encryption == 'STARTTLS':
|
||||
elif autoresponder.smtp.encryption == 'STARTTLS':
|
||||
try:
|
||||
logger.debug(f"Attempting STARTTLS connection to {smtp_config.host}:{smtp_config.port}")
|
||||
smtp = SMTP(smtp_config.host, smtp_config.port)
|
||||
L.DEBUG(f"Attempting STARTTLS connection to {autoresponder.smtp.host}:{autoresponder.smtp.port}")
|
||||
smtp = SMTP(autoresponder.smtp.host, autoresponder.smtp.port)
|
||||
smtp.starttls(context=context)
|
||||
return smtp
|
||||
except Exception as e:
|
||||
logger.error(f"STARTTLS connection failed: {str(e)}")
|
||||
L.ERR(f"STARTTLS connection failed: {str(e)}")
|
||||
raise
|
||||
else:
|
||||
try:
|
||||
logger.debug(f"Attempting unencrypted connection to {smtp_config.host}:{smtp_config.port}")
|
||||
return SMTP(smtp_config.host, smtp_config.port)
|
||||
L.DEBUG(f"Attempting unencrypted connection to {autoresponder.smtp.host}:{autoresponder.smtp.port}")
|
||||
return SMTP(autoresponder.smtp.host, autoresponder.smtp.port)
|
||||
except Exception as e:
|
||||
logger.error(f"Unencrypted connection failed: {str(e)}")
|
||||
L.ERR(f"Unencrypted connection failed: {str(e)}")
|
||||
raise
|
||||
|
||||
async def send_response(to_email: str, subject: str, body: str, profile: AutoResponder, image_attachment: Path = None) -> bool:
|
||||
|
@ -103,20 +93,21 @@ async def send_response(to_email: str, subject: str, body: str, profile: AutoRes
|
|||
img = MIMEImage(img_file.read(), name=os.path.basename(image_attachment))
|
||||
message.attach(img)
|
||||
|
||||
logger.debug(f"Sending auto-response to {to_email} concerning {subject} from account {profile.name}...")
|
||||
L.DEBUG(f"Sending auto-response to {to_email} concerning {subject} from account {profile.name}...")
|
||||
|
||||
server = get_smtp_connection(profile)
|
||||
logger.debug(f"SMTP connection established: {type(server)}")
|
||||
L.DEBUG(f"SMTP connection established: {type(server)}")
|
||||
server.login(profile.smtp.username, profile.smtp.password)
|
||||
server.send_message(message)
|
||||
|
||||
logger.info(f"Auto-response sent to {to_email} concerning {subject} from account {profile.name}!")
|
||||
L.INFO(f"Auto-response sent to {to_email} concerning {subject} from account {profile.name}!")
|
||||
return True
|
||||
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Error in preparing/sending auto-response from account {profile.name}: {str(e)}")
|
||||
logger.error(f"SMTP details - Host: {profile.smtp.host}, Port: {profile.smtp.port}, Encryption: {profile.smtp.encryption}")
|
||||
logger.error(traceback.format_exc())
|
||||
L.ERR(f"Error in preparing/sending auto-response from account {profile.name}: {str(e)}")
|
||||
L.ERR(f"SMTP details - Host: {profile.smtp.host}, Port: {profile.smtp.port}, Encryption: {profile.smtp.encryption}")
|
||||
L.ERR(traceback.format_exc())
|
||||
return False
|
||||
|
||||
finally:
|
||||
|
@ -124,9 +115,7 @@ async def send_response(to_email: str, subject: str, body: str, profile: AutoRes
|
|||
try:
|
||||
server.quit()
|
||||
except Exception as e:
|
||||
logger.error(f"Error closing SMTP connection: {str(e)}")
|
||||
|
||||
|
||||
L.ERR(f"Error closing SMTP connection: {str(e)}")
|
||||
|
||||
|
||||
def clean_email_content(html_content):
|
||||
|
@ -155,16 +144,16 @@ async def extract_attachments(attachments) -> List[str]:
|
|||
return attachment_texts
|
||||
|
||||
async def process_account_archival(account: EmailAccount):
|
||||
summarized_log = EMAIL_LOGS / account.name / "summarized.txt"
|
||||
summarized_log = Dir.logs.email / account.name / "summarized.txt"
|
||||
os.makedirs(summarized_log.parent, exist_ok = True)
|
||||
|
||||
while True:
|
||||
try:
|
||||
processed_uids = await load_processed_uids(summarized_log)
|
||||
logger.debug(f"{len(processed_uids)} emails marked as already summarized are being ignored.")
|
||||
L.DEBUG(f"{len(processed_uids)} emails marked as already summarized are being ignored.")
|
||||
with get_imap_connection(account) as inbox:
|
||||
unread_messages = inbox.messages(unread=True)
|
||||
logger.debug(f"There are {len(unread_messages)} unread messages.")
|
||||
L.DEBUG(f"There are {len(unread_messages)} unread messages.")
|
||||
for uid, message in unread_messages:
|
||||
uid_str = uid.decode() if isinstance(uid, bytes) else str(uid)
|
||||
if uid_str not in processed_uids:
|
||||
|
@ -184,13 +173,13 @@ async def process_account_archival(account: EmailAccount):
|
|||
save_success = await save_email(md_path, md_content)
|
||||
if save_success:
|
||||
await save_processed_uid(summarized_log, account.name, uid_str)
|
||||
logger.info(f"Summarized email: {uid_str}")
|
||||
L.INFO(f"Summarized email: {uid_str}")
|
||||
else:
|
||||
logger.warn(f"Failed to summarize {this_email.subject}")
|
||||
L.WARN(f"Failed to summarize {this_email.subject}")
|
||||
else:
|
||||
logger.debug(f"Skipping {uid_str} because it was already processed.")
|
||||
L.DEBUG(f"Skipping {uid_str} because it was already processed.")
|
||||
except Exception as e:
|
||||
logger.error(f"An error occurred during summarization for account {account.name}: {e}")
|
||||
L.ERR(f"An error occurred during summarization for account {account.name}: {e}")
|
||||
|
||||
await asyncio.sleep(account.refresh)
|
||||
|
||||
|
@ -202,7 +191,7 @@ async def summarize_single_email(this_email: IncomingEmail, podcast: bool = Fals
|
|||
attachment_texts = await extract_attachments(this_email.attachments)
|
||||
email_content += "\n—--\n" + "\n—--\n".join([f"Attachment: {text}" for text in attachment_texts])
|
||||
summary = await llm.summarize_text(email_content)
|
||||
await tts.local_tts(text_content = summary, speed = 1.1, voice = DEFAULT_VOICE, podcast = podcast, output_path = tts_path)
|
||||
await tts.local_tts(text_content = summary, speed = 1.1, voice = TTS.xtts.voice, podcast = podcast, output_path = tts_path)
|
||||
md_summary = f'```ad.summary\n'
|
||||
md_summary += f'title: {this_email.subject}\n'
|
||||
md_summary += f'{summary}\n'
|
||||
|
@ -236,7 +225,7 @@ tags:
|
|||
return markdown_content
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Exception: {e}")
|
||||
L.ERR(f"Exception: {e}")
|
||||
return False
|
||||
|
||||
async def save_email(md_path, md_content):
|
||||
|
@ -244,14 +233,14 @@ async def save_email(md_path, md_content):
|
|||
with open(md_path, 'w', encoding='utf-8') as md_file:
|
||||
md_file.write(md_content)
|
||||
|
||||
logger.debug(f"Saved markdown to {md_path}")
|
||||
L.DEBUG(f"Saved markdown to {md_path}")
|
||||
return True
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to save email: {e}")
|
||||
L.ERR(f"Failed to save email: {e}")
|
||||
return False
|
||||
|
||||
def get_matching_autoresponders(this_email: IncomingEmail, account: EmailAccount) -> List[AutoResponder]:
|
||||
logger.debug(f"Called get_matching_autoresponders for email \"{this_email.subject},\" account name \"{account.name}\"")
|
||||
L.DEBUG(f"Called get_matching_autoresponders for email \"{this_email.subject},\" account name \"{account.name}\"")
|
||||
def matches_list(item: str, this_email: IncomingEmail) -> bool:
|
||||
if '@' in item:
|
||||
return item in this_email.sender
|
||||
|
@ -262,46 +251,46 @@ def get_matching_autoresponders(this_email: IncomingEmail, account: EmailAccount
|
|||
whitelist_match = not profile.whitelist or any(matches_list(item, this_email) for item in profile.whitelist)
|
||||
blacklist_match = any(matches_list(item, this_email) for item in profile.blacklist)
|
||||
if whitelist_match and not blacklist_match:
|
||||
logger.debug(f"We have a match for {whitelist_match} and no blacklist matches.")
|
||||
L.DEBUG(f"We have a match for {whitelist_match} and no blacklist matches.")
|
||||
matching_profiles.append(profile)
|
||||
elif whitelist_match and blacklist_match:
|
||||
logger.debug(f"Matched whitelist for {whitelist_match}, but also matched blacklist for {blacklist_match}")
|
||||
L.DEBUG(f"Matched whitelist for {whitelist_match}, but also matched blacklist for {blacklist_match}")
|
||||
else:
|
||||
logger.debug(f"No whitelist or blacklist matches.")
|
||||
L.DEBUG(f"No whitelist or blacklist matches.")
|
||||
return matching_profiles
|
||||
|
||||
|
||||
async def process_account_autoresponding(account: EmailAccount):
|
||||
EMAIL_AUTORESPONSE_LOG = EMAIL_LOGS / account.name / "autoresponded.txt"
|
||||
EMAIL_AUTORESPONSE_LOG = Dir.logs.email / account.name / "autoresponded.txt"
|
||||
os.makedirs(EMAIL_AUTORESPONSE_LOG.parent, exist_ok=True)
|
||||
|
||||
while True:
|
||||
try:
|
||||
processed_uids = await load_processed_uids(EMAIL_AUTORESPONSE_LOG)
|
||||
logger.debug(f"{len(processed_uids)} emails marked as already responded to are being ignored.")
|
||||
L.DEBUG(f"{len(processed_uids)} emails marked as already responded to are being ignored.")
|
||||
|
||||
with get_imap_connection(account) as inbox:
|
||||
unread_messages = inbox.messages(unread=True)
|
||||
logger.debug(f"There are {len(unread_messages)} unread messages.")
|
||||
L.DEBUG(f"There are {len(unread_messages)} unread messages.")
|
||||
|
||||
for uid, message in unread_messages:
|
||||
uid_str = uid.decode() if isinstance(uid, bytes) else str(uid)
|
||||
if uid_str not in processed_uids:
|
||||
await autorespond_single_email(message, uid_str, account, EMAIL_AUTORESPONSE_LOG)
|
||||
else:
|
||||
logger.debug(f"Skipping {uid_str} because it was already processed.")
|
||||
L.DEBUG(f"Skipping {uid_str} because it was already processed.")
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"An error occurred during auto-responding for account {account.name}: {e}")
|
||||
L.ERR(f"An error occurred during auto-responding for account {account.name}: {e}")
|
||||
|
||||
await asyncio.sleep(account.refresh)
|
||||
|
||||
async def autorespond_single_email(message, uid_str: str, account: EmailAccount, log_file: Path):
|
||||
this_email = await create_incoming_email(message)
|
||||
logger.debug(f"Evaluating {this_email.subject} for autoresponse-worthiness...")
|
||||
L.DEBUG(f"Evaluating {this_email.subject} for autoresponse-worthiness...")
|
||||
|
||||
matching_profiles = get_matching_autoresponders(this_email, account)
|
||||
logger.debug(f"Matching profiles: {matching_profiles}")
|
||||
L.DEBUG(f"Matching profiles: {matching_profiles}")
|
||||
|
||||
for profile in matching_profiles:
|
||||
response_body = await generate_response(this_email, profile, account)
|
||||
|
@ -311,15 +300,15 @@ async def autorespond_single_email(message, uid_str: str, account: EmailAccount,
|
|||
jpg_path = await img.workflow(profile.image_prompt, earlyout=False, downscale_to_fit=True) if profile.image_prompt else None
|
||||
success = await send_response(this_email.sender, subject, response_body, profile, jpg_path)
|
||||
if success:
|
||||
logger.warn(f"Auto-responded to email: {this_email.subject}")
|
||||
L.WARN(f"Auto-responded to email: {this_email.subject}")
|
||||
await save_processed_uid(log_file, account.name, uid_str)
|
||||
else:
|
||||
logger.warn(f"Failed to send auto-response to {this_email.subject}")
|
||||
L.WARN(f"Failed to send auto-response to {this_email.subject}")
|
||||
else:
|
||||
logger.warn(f"Unable to generate auto-response for {this_email.subject}")
|
||||
L.WARN(f"Unable to generate auto-response for {this_email.subject}")
|
||||
|
||||
async def generate_response(this_email: IncomingEmail, profile: AutoResponder, account: EmailAccount) -> Optional[str]:
|
||||
logger.info(f"Generating auto-response to {this_email.subject} with profile: {profile.name}")
|
||||
L.INFO(f"Generating auto-response to {this_email.subject} with profile: {profile.name}")
|
||||
|
||||
now = await loc.dt(dt_datetime.now())
|
||||
then = await loc.dt(this_email.datetime_received)
|
||||
|
@ -337,7 +326,7 @@ Respond on behalf of {account.fullname}, who is unable to respond personally bec
|
|||
|
||||
try:
|
||||
response = await llm.query_ollama(usr_prompt, sys_prompt, profile.ollama_model, 400)
|
||||
logger.debug(f"query_ollama response: {response}")
|
||||
L.DEBUG(f"query_ollama response: {response}")
|
||||
|
||||
if isinstance(response, dict) and "message" in response and "content" in response["message"]:
|
||||
response = response["message"]["content"]
|
||||
|
@ -345,7 +334,7 @@ Respond on behalf of {account.fullname}, who is unable to respond personally bec
|
|||
return response + "\n\n"
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Error generating auto-response: {str(e)}")
|
||||
L.ERR(f"Error generating auto-response: {str(e)}")
|
||||
return None
|
||||
|
||||
|
||||
|
@ -374,7 +363,7 @@ async def save_processed_uid(filename: Path, account_name: str, uid: str):
|
|||
|
||||
|
||||
async def process_all_accounts():
|
||||
email_accounts = load_email_accounts(EMAIL_CONFIG)
|
||||
email_accounts = Email.get_email_accounts()
|
||||
summarization_tasks = [asyncio.create_task(process_account_archival(account)) for account in email_accounts]
|
||||
autoresponding_tasks = [asyncio.create_task(process_account_autoresponding(account)) for account in email_accounts]
|
||||
await asyncio.gather(*summarization_tasks, *autoresponding_tasks)
|
||||
|
@ -382,4 +371,4 @@ async def process_all_accounts():
|
|||
@email.on_event("startup")
|
||||
async def startup_event():
|
||||
await asyncio.sleep(5)
|
||||
asyncio.create_task(process_all_accounts())
|
||||
asyncio.create_task(process_all_accounts())
|
|
@ -1,50 +0,0 @@
|
|||
def generate_jwt_token():
|
||||
key_id, key_secret = GHOST_API_KEY.split(':')
|
||||
iat = int(date.now().timestamp())
|
||||
exp = iat + 5 * 60 # Token expiration time set to 5 minutes from now for consistency with the working script
|
||||
payload = {
|
||||
'iat': iat,
|
||||
'exp': exp,
|
||||
'aud': '/admin/' # Adjusted to match the working script
|
||||
}
|
||||
token = jwt.encode(payload, bytes.fromhex(key_secret), algorithm='HS256', headers={'kid': key_id})
|
||||
return token.decode('utf-8') if isinstance(token, bytes) else token # Ensure the token is decoded to UTF-8 string
|
||||
|
||||
|
||||
def post_to_ghost(title, image_path, html_content, ghost_tags):
|
||||
jwt_token = generate_jwt_token()
|
||||
ghost_headers = {'Authorization': f'Ghost {jwt_token}'}
|
||||
|
||||
# Upload the image to Ghost
|
||||
with open(image_path, 'rb') as f:
|
||||
files = {'file': (os.path.basename(image_path), f, 'image/jpg')}
|
||||
image_response = requests.post(f"{GHOST_API_URL}/images/upload/", headers=ghost_headers, files=files)
|
||||
image_response.raise_for_status() # Ensure the request was successful
|
||||
image_url = image_response.json()['images'][0]['url']
|
||||
|
||||
# Prepare the post content
|
||||
updated_html_content = f'<img src="{image_url}" alt="Image"/><hr/> {html_content}'
|
||||
mobiledoc = {
|
||||
"version": "0.3.1",
|
||||
"atoms": [],
|
||||
"cards": [["html", {"cardName": "html", "html": updated_html_content}]],
|
||||
"markups": [],
|
||||
"sections": [[10, 0]]
|
||||
}
|
||||
mobiledoc = json.dumps(mobiledoc)
|
||||
|
||||
post_data = {
|
||||
'posts': [{
|
||||
'title': title,
|
||||
'mobiledoc': mobiledoc,
|
||||
'status': 'published',
|
||||
'tags': ghost_tags
|
||||
}]
|
||||
}
|
||||
|
||||
# Create a new post
|
||||
post_response = requests.post(f"{GHOST_API_URL}/posts/", json=post_data, headers=ghost_headers)
|
||||
post_response.raise_for_status()
|
||||
post_url = post_response.json()['posts'][0]['url']
|
||||
|
||||
return post_url
|
|
@ -11,7 +11,6 @@ from tailscale import Tailscale
|
|||
from sijapi import L, API, TS_ID, SUBNET_BROADCAST
|
||||
|
||||
health = APIRouter(tags=["public", "trusted", "private"])
|
||||
logger = L.get_module_logger("health")
|
||||
|
||||
@health.get("/health")
|
||||
def get_health():
|
||||
|
@ -49,7 +48,7 @@ async def get_wan_ip():
|
|||
wan_info = response.json()
|
||||
return wan_info.get('ip', 'Unavailable')
|
||||
except Exception as e:
|
||||
logger.error(f"Error fetching WAN IP: {e}")
|
||||
L.ERR(f"Error fetching WAN IP: {e}")
|
||||
return "Unavailable"
|
||||
|
||||
@health.get("/ts_ip")
|
||||
|
|
|
@ -41,7 +41,6 @@ from io import BytesIO
|
|||
import base64
|
||||
|
||||
ig = APIRouter()
|
||||
logger = L.get_module_logger("ig")
|
||||
|
||||
class IG_Request(BaseModel):
|
||||
file: Optional[UploadFile] = None # upload a particular file to Instagram
|
||||
|
@ -187,7 +186,7 @@ def get_user_media(username, amount=30):
|
|||
Fetch recent media for a given username, return List of medias
|
||||
"""
|
||||
|
||||
logger.debug(f"Fetching recent media for {username}...")
|
||||
L.DEBUG(f"Fetching recent media for {username}...")
|
||||
user_id = cl.user_id_from_username(username)
|
||||
medias = cl.user_medias(user_id, amount)
|
||||
final_medias = []
|
||||
|
@ -202,7 +201,7 @@ def get_user_image_urls(username, amount=30) -> List[str]:
|
|||
"""
|
||||
Fetch recent media URLs for a given username, return List of media URLs
|
||||
"""
|
||||
logger.debug(f"Fetching recent media URLs for {username}...")
|
||||
L.DEBUG(f"Fetching recent media URLs for {username}...")
|
||||
user_id = cl.user_id_from_username(username)
|
||||
medias = cl.user_medias(user_id, amount)
|
||||
|
||||
|
@ -230,7 +229,7 @@ def get_random_follower():
|
|||
def get_medias_by_hashtag(hashtag: str, days_ago_max:int = 14, ht_type:str = None, amount:int = args.count):
|
||||
if not ht_type:
|
||||
ht_type = args.commentmode
|
||||
logger.debug(f"Fetching {ht_type} media for hashtag: {hashtag}")
|
||||
L.DEBUG(f"Fetching {ht_type} media for hashtag: {hashtag}")
|
||||
ht_medias = []
|
||||
while True:
|
||||
sleep(SHORT)
|
||||
|
@ -240,10 +239,10 @@ def get_medias_by_hashtag(hashtag: str, days_ago_max:int = 14, ht_type:str = Non
|
|||
ht_medias.extend(cl.hashtag_medias_recent(name=hashtag, amount=amount*10))
|
||||
|
||||
filtered_medias = filter_medias(ht_medias, days_ago_max=days_ago_max)
|
||||
logger.debug(f"Filtered {ht_type} media count obtained for '#{hashtag}': {len(filtered_medias)}")
|
||||
L.DEBUG(f"Filtered {ht_type} media count obtained for '#{hashtag}': {len(filtered_medias)}")
|
||||
|
||||
if len(filtered_medias) >= amount:
|
||||
logger.debug(f"Desired amount of {amount} filtered media reached.")
|
||||
L.DEBUG(f"Desired amount of {amount} filtered media reached.")
|
||||
break
|
||||
|
||||
return filtered_medias
|
||||
|
@ -251,11 +250,11 @@ def get_medias_by_hashtag(hashtag: str, days_ago_max:int = 14, ht_type:str = Non
|
|||
def get_medias_from_all_hashtags(days_ago_max=14, ht_type:str = None, amount:int = args.count):
|
||||
if not ht_type:
|
||||
ht_type = args.commentmode
|
||||
logger.debug(f"Fetching {ht_type} media.")
|
||||
L.DEBUG(f"Fetching {ht_type} media.")
|
||||
filtered_medias = []
|
||||
while len(filtered_medias) < amount:
|
||||
hashtag = random.choice(HASHTAGS)
|
||||
logger.debug(f"Using hashtag: {hashtag}")
|
||||
L.DEBUG(f"Using hashtag: {hashtag}")
|
||||
fetched_medias = []
|
||||
sleep(SHORT)
|
||||
if ht_type == "top":
|
||||
|
@ -265,15 +264,15 @@ def get_medias_from_all_hashtags(days_ago_max=14, ht_type:str = None, amount:int
|
|||
|
||||
current_filtered_medias = filter_medias(fetched_medias, days_ago_max=days_ago_max)
|
||||
filtered_medias.extend(current_filtered_medias)
|
||||
logger.debug(f"Filtered {ht_type} media count obtained for '#{hashtag}': {len(current_filtered_medias)}")
|
||||
L.DEBUG(f"Filtered {ht_type} media count obtained for '#{hashtag}': {len(current_filtered_medias)}")
|
||||
|
||||
# Trim the list if we've collected more than needed
|
||||
if len(filtered_medias) > amount:
|
||||
filtered_medias = filtered_medias[:amount]
|
||||
logger.debug(f"Desired amount of {amount} filtered media reached.")
|
||||
L.DEBUG(f"Desired amount of {amount} filtered media reached.")
|
||||
break
|
||||
else:
|
||||
logger.debug(f"Total filtered media count so far: {len(filtered_medias)}")
|
||||
L.DEBUG(f"Total filtered media count so far: {len(filtered_medias)}")
|
||||
|
||||
return filtered_medias
|
||||
|
||||
|
@ -346,7 +345,7 @@ def download_and_resize_image(url: str, download_path: str = None, max_dimension
|
|||
return download_path
|
||||
except Exception as e:
|
||||
# Handle or log the error as needed
|
||||
logger.debug(f"Error downloading or resizing image: {e}")
|
||||
L.DEBUG(f"Error downloading or resizing image: {e}")
|
||||
return None
|
||||
|
||||
|
||||
|
@ -366,17 +365,17 @@ def comment_on_user_media(user: str, comment_type: str = "default", amount=5):
|
|||
comment_text = llava(encoded_media, COMMENT_PROMPT_SYS, comment_prompt_usr) if args.llava or not args.openai else gpt4v(encoded_media, COMMENT_PROMPT_SYS, comment_prompt_usr)
|
||||
if comment_text:
|
||||
cl.media_comment(media.pk, comment_text)
|
||||
logger.debug(f"Commented on media: {media.pk}")
|
||||
L.DEBUG(f"Commented on media: {media.pk}")
|
||||
else:
|
||||
logger.debug(f"Failed to generate comment for media: {media.pk}")
|
||||
L.DEBUG(f"Failed to generate comment for media: {media.pk}")
|
||||
add_media_to_completed_lists(media)
|
||||
sleep(SHORT)
|
||||
else:
|
||||
logger.debug(f"We received a nonetype! {media_path}")
|
||||
L.DEBUG(f"We received a nonetype! {media_path}")
|
||||
else:
|
||||
logger.debug(f"URL for {media.pk} disappeared it seems...")
|
||||
L.DEBUG(f"URL for {media.pk} disappeared it seems...")
|
||||
else:
|
||||
logger.debug(f"Media already interacted with: {media.pk}")
|
||||
L.DEBUG(f"Media already interacted with: {media.pk}")
|
||||
|
||||
def comment_on_hashtagged_media(comment_type: str = args.commenttype, amount=3, hashtag: str = None):
|
||||
"""
|
||||
|
@ -401,22 +400,22 @@ def comment_on_hashtagged_media(comment_type: str = args.commenttype, amount=3,
|
|||
try:
|
||||
like_result = cl.media_like(media)
|
||||
if like_result:
|
||||
logger.debug(f"Liked media: https://instagram.com/p/{media.pk}/")
|
||||
L.DEBUG(f"Liked media: https://instagram.com/p/{media.pk}/")
|
||||
except instagrapi.exceptions.FeedbackRequired as e:
|
||||
logger.debug(f"Cannot like media {media.pk}: {str(e)}")
|
||||
L.DEBUG(f"Cannot like media {media.pk}: {str(e)}")
|
||||
|
||||
if comment_text:
|
||||
try:
|
||||
cl.media_comment(media.pk, comment_text)
|
||||
logger.debug(f"Commented on media: https://instagram.com/p/{media.pk}/")
|
||||
L.DEBUG(f"Commented on media: https://instagram.com/p/{media.pk}/")
|
||||
except instagrapi.exceptions.FeedbackRequired as e:
|
||||
logger.debug(f"Cannot comment on media {media.pk}: {str(e)}")
|
||||
L.DEBUG(f"Cannot comment on media {media.pk}: {str(e)}")
|
||||
else:
|
||||
logger.debug(f"Failed to generate comment for media: https://instagram.com/p/{media.pk}")
|
||||
L.DEBUG(f"Failed to generate comment for media: https://instagram.com/p/{media.pk}")
|
||||
add_media_to_completed_lists(media)
|
||||
sleep(SHORT)
|
||||
else:
|
||||
logger.debug(f"Media already interacted with: {media.pk}")
|
||||
L.DEBUG(f"Media already interacted with: {media.pk}")
|
||||
|
||||
|
||||
def comment_on_specific_media(media_url, comment_type: str = "default"):
|
||||
|
@ -437,11 +436,11 @@ def comment_on_specific_media(media_url, comment_type: str = "default"):
|
|||
if comment_text:
|
||||
try:
|
||||
cl.media_comment(media.pk, comment_text)
|
||||
logger.debug(f"Commented on specific media: https://instagram.com/p/{media.pk}/")
|
||||
L.DEBUG(f"Commented on specific media: https://instagram.com/p/{media.pk}/")
|
||||
except instagrapi.exceptions.FeedbackRequired as e:
|
||||
logger.debug(f"Failed to comment on specific media: https://instagram.com/p/{media.pk}/ due to error: {str(e)}")
|
||||
L.DEBUG(f"Failed to comment on specific media: https://instagram.com/p/{media.pk}/ due to error: {str(e)}")
|
||||
else:
|
||||
logger.debug(f"Failed to generate comment for specific media: https://instagram.com/p/{media.pk}/")
|
||||
L.DEBUG(f"Failed to generate comment for specific media: https://instagram.com/p/{media.pk}/")
|
||||
|
||||
|
||||
|
||||
|
@ -486,16 +485,16 @@ def update_prompt(workflow: dict, post: dict, positive: str, found_key=[None], p
|
|||
|
||||
if value == "API_PrePrompt":
|
||||
workflow[key] = post.get(value, "") + positive
|
||||
logger.debug(f"Updated API_PrePrompt to: {workflow[key]}")
|
||||
L.DEBUG(f"Updated API_PrePrompt to: {workflow[key]}")
|
||||
elif value == "API_StylePrompt":
|
||||
workflow[key] = post.get(value, "")
|
||||
logger.debug(f"Updated API_StylePrompt to: {workflow[key]}")
|
||||
L.DEBUG(f"Updated API_StylePrompt to: {workflow[key]}")
|
||||
elif value == "API_NegativePrompt":
|
||||
workflow[key] = post.get(value, "")
|
||||
logger.debug(f"Updated API_NegativePrompt to: {workflow[key]}")
|
||||
L.DEBUG(f"Updated API_NegativePrompt to: {workflow[key]}")
|
||||
elif key == "seed" or key == "noise_seed":
|
||||
workflow[key] = random.randint(1000000000000, 9999999999999)
|
||||
logger.debug(f"Updated seed to: {workflow[key]}")
|
||||
L.DEBUG(f"Updated seed to: {workflow[key]}")
|
||||
elif (key == "width" or key == "max_width" or key == "scaled_width" or key == "side_length") and (value == 1023 or value == 1025):
|
||||
# workflow[key] = post.get(value, "")
|
||||
workflow[key] = post.get("width", 1024)
|
||||
|
@ -503,7 +502,7 @@ def update_prompt(workflow: dict, post: dict, positive: str, found_key=[None], p
|
|||
# workflow[key] = post.get(value, "")
|
||||
workflow[key] = post.get("height", 1024)
|
||||
except Exception as e:
|
||||
logger.debug(f"Error in update_prompt at path {' -> '.join(path)}: {e}")
|
||||
L.DEBUG(f"Error in update_prompt at path {' -> '.join(path)}: {e}")
|
||||
raise
|
||||
|
||||
return found_key[0]
|
||||
|
@ -528,22 +527,22 @@ def update_prompt_custom(workflow: dict, API_PrePrompt: str, API_StylePrompt: st
|
|||
|
||||
if value == "API_PrePrompt":
|
||||
workflow[key] = API_PrePrompt
|
||||
logger.debug(f"Updated API_PrePrompt to: {workflow[key]}")
|
||||
L.DEBUG(f"Updated API_PrePrompt to: {workflow[key]}")
|
||||
elif value == "API_StylePrompt":
|
||||
workflow[key] = API_StylePrompt
|
||||
logger.debug(f"Updated API_StylePrompt to: {workflow[key]}")
|
||||
L.DEBUG(f"Updated API_StylePrompt to: {workflow[key]}")
|
||||
elif value == "API_NegativePrompt":
|
||||
workflow[key] = API_NegativePrompt
|
||||
logger.debug(f"Updated API_NegativePrompt to: {workflow[key]}")
|
||||
L.DEBUG(f"Updated API_NegativePrompt to: {workflow[key]}")
|
||||
elif key == "seed" or key == "noise_seed":
|
||||
workflow[key] = random.randint(1000000000000, 9999999999999)
|
||||
logger.debug(f"Updated seed to: {workflow[key]}")
|
||||
L.DEBUG(f"Updated seed to: {workflow[key]}")
|
||||
elif (key == "width" or key == "max_width" or key == "scaled_width") and (value == 1023 or value == 1025):
|
||||
workflow[key] = 1024
|
||||
elif (key == "dimension" or key == "height" or key == "max_height" or key == "scaled_height") and (value == 1023 or value == 1025):
|
||||
workflow[key] = 1024
|
||||
except Exception as e:
|
||||
logger.debug(f"Error in update_prompt_custom at path {' -> '.join(path)}: {e}")
|
||||
L.DEBUG(f"Error in update_prompt_custom at path {' -> '.join(path)}: {e}")
|
||||
raise
|
||||
|
||||
return found_key[0]
|
||||
|
@ -583,14 +582,14 @@ def poll_status(prompt_id):
|
|||
elapsed_time = int(time.time() - start_time) # Calculate elapsed time in seconds
|
||||
status_response = requests.get(f"{COMFYUI_URL}/history/{prompt_id}")
|
||||
# Use \r to return to the start of the line, and end='' to prevent newline
|
||||
logger.debug(f"\rGenerating {prompt_id}. Elapsed time: {elapsed_time} seconds", end='')
|
||||
L.DEBUG(f"\rGenerating {prompt_id}. Elapsed time: {elapsed_time} seconds", end='')
|
||||
if status_response.status_code != 200:
|
||||
raise Exception("Failed to get job status")
|
||||
status_data = status_response.json()
|
||||
job_data = status_data.get(prompt_id, {})
|
||||
if job_data.get("status", {}).get("completed", False):
|
||||
logger.debug()
|
||||
logger.debug(f"{prompt_id} completed in {elapsed_time} seconds.")
|
||||
L.DEBUG()
|
||||
L.DEBUG(f"{prompt_id} completed in {elapsed_time} seconds.")
|
||||
return job_data
|
||||
time.sleep(1)
|
||||
|
||||
|
@ -601,14 +600,14 @@ def poll_status(prompt_id):
|
|||
elapsed_time = int(time.time() - start_time) # Calculate elapsed time in seconds
|
||||
status_response = requests.get(f"{COMFYUI_URL}/history/{prompt_id}")
|
||||
# Use \r to return to the start of the line, and end='' to prevent newline
|
||||
logger.debug(f"\rGenerating {prompt_id}. Elapsed time: {elapsed_time} seconds", end='')
|
||||
L.DEBUG(f"\rGenerating {prompt_id}. Elapsed time: {elapsed_time} seconds", end='')
|
||||
if status_response.status_code != 200:
|
||||
raise Exception("Failed to get job status")
|
||||
status_data = status_response.json()
|
||||
job_data = status_data.get(prompt_id, {})
|
||||
if job_data.get("status", {}).get("completed", False):
|
||||
logger.debug()
|
||||
logger.debug(f"{prompt_id} completed in {elapsed_time} seconds.")
|
||||
L.DEBUG()
|
||||
L.DEBUG(f"{prompt_id} completed in {elapsed_time} seconds.")
|
||||
return job_data
|
||||
time.sleep(1)
|
||||
|
||||
|
@ -619,12 +618,12 @@ def poll_status(prompt_id):
|
|||
def load_post(chosen_post: str = "default"):
|
||||
if chosen_post in PROFILE_CONFIG['posts']:
|
||||
post = PROFILE_CONFIG['posts'][chosen_post]
|
||||
logger.debug(f"Loaded post for {chosen_post}")
|
||||
L.DEBUG(f"Loaded post for {chosen_post}")
|
||||
else:
|
||||
logger.debug(f"Unable to load post for {chosen_post}. Choosing a default post.")
|
||||
chosen_post = choose_random_post(PROFILE_CONFIG['posts'])
|
||||
L.DEBUG(f"Unable to load post for {chosen_post}. Choosing a default post.")
|
||||
chosen_post = choose_post(PROFILE_CONFIG['posts'])
|
||||
post = PROFILE_CONFIG['posts'][chosen_post]
|
||||
logger.debug(f"Defaulted to {chosen_post}")
|
||||
L.DEBUG(f"Defaulted to {chosen_post}")
|
||||
|
||||
return post
|
||||
|
||||
|
@ -634,18 +633,18 @@ def handle_image_workflow(chosen_post=None):
|
|||
or posting to Instagram based on the local flag.
|
||||
"""
|
||||
if chosen_post is None:
|
||||
chosen_post = choose_random_post(PROFILE_CONFIG['posts'])
|
||||
chosen_post = choose_post(PROFILE_CONFIG['posts'])
|
||||
|
||||
post = load_post(chosen_post)
|
||||
|
||||
workflow_name = args.workflow if args.workflow else random.choice(post['workflows'])
|
||||
|
||||
logger.debug(f"Workflow name: {workflow_name}")
|
||||
L.DEBUG(f"Workflow name: {workflow_name}")
|
||||
|
||||
logger.debug(f"Generating image concept for {chosen_post} and {workflow_name} now.")
|
||||
L.DEBUG(f"Generating image concept for {chosen_post} and {workflow_name} now.")
|
||||
image_concept = query_ollama(llmPrompt = post['llmPrompt'], max_tokens = 180) if args.local or not args.openai else query_gpt4(llmPrompt = post['llmPrompt'], max_tokens = 180)
|
||||
|
||||
logger.debug(f"Image concept for {chosen_post}: {image_concept}")
|
||||
L.DEBUG(f"Image concept for {chosen_post}: {image_concept}")
|
||||
|
||||
workflow_data = None
|
||||
|
||||
|
@ -659,9 +658,9 @@ def handle_image_workflow(chosen_post=None):
|
|||
jpg_file_path = image_gen(image_concept, "dall-e-3")
|
||||
else:
|
||||
saved_file_key = update_prompt(workflow=workflow_data, post=post, positive=image_concept)
|
||||
logger.debug(f"Saved file key: {saved_file_key}")
|
||||
L.DEBUG(f"Saved file key: {saved_file_key}")
|
||||
prompt_id = queue_prompt(workflow_data)
|
||||
logger.debug(f"Prompt ID: {prompt_id}")
|
||||
L.DEBUG(f"Prompt ID: {prompt_id}")
|
||||
status_data = poll_status(prompt_id)
|
||||
image_data = get_image(status_data, saved_file_key)
|
||||
if chosen_post == "landscape":
|
||||
|
@ -700,17 +699,17 @@ def handle_custom_image(custom_post: str):
|
|||
system_msg = "You are a helpful AI who assists in generating prompts that will be used to generate highly realistic images. Always use the most visually descriptive terms possible, and avoid any vague or abstract concepts. Do not include any words or descriptions based on other senses or emotions. Strive to show rather than tell. Space is limited, so be efficient with your words."
|
||||
image_concept = query_ollama(system_msg=system_msg, user_msg=custom_post, max_tokens = 180) if args.local or not args.openai else query_gpt4(system_msg=system_msg, user_msg=custom_post, max_tokens = 180)
|
||||
|
||||
logger.debug(f"Image concept: {image_concept}")
|
||||
L.DEBUG(f"Image concept: {image_concept}")
|
||||
|
||||
if args.dalle and not args.local:
|
||||
jpg_file_path = image_gen(image_concept, "dall-e-3")
|
||||
|
||||
else:
|
||||
saved_file_key = update_prompt(workflow=workflow_data, post=post, positive=image_concept)
|
||||
logger.debug(f"Saved file key: {saved_file_key}")
|
||||
L.DEBUG(f"Saved file key: {saved_file_key}")
|
||||
|
||||
prompt_id = queue_prompt(workflow_data)
|
||||
logger.debug(f"Prompt ID: {prompt_id}")
|
||||
L.DEBUG(f"Prompt ID: {prompt_id}")
|
||||
|
||||
status_data = poll_status(prompt_id)
|
||||
image_data = get_image(status_data, saved_file_key)
|
||||
|
@ -729,7 +728,7 @@ def image_aftergen(jpg_file_path: str, chosen_post: str = None, post: Dict = Non
|
|||
if chosen_post and not prompt:
|
||||
prompt = PROFILE_CONFIG['posts'][chosen_post]['Vision_Prompt']
|
||||
encoded_string = encode_image_to_base64(jpg_file_path)
|
||||
logger.debug(f"Image successfully encoded from {jpg_file_path}")
|
||||
L.DEBUG(f"Image successfully encoded from {jpg_file_path}")
|
||||
instagram_description = llava(encoded_string, prompt) if args.local or args.llava or not args.openai else gpt4v(encoded_string, prompt, 150)
|
||||
instagram_description = re.sub(r'^["\'](.*)["\']$', r'\1', instagram_description)
|
||||
|
||||
|
@ -760,27 +759,27 @@ Tags: {', '.join(ghost_tags)}
|
|||
with open(markdown_filename, "w") as md_file:
|
||||
md_file.write(markdown_content)
|
||||
|
||||
logger.debug(f"Markdown file created at {markdown_filename}")
|
||||
L.DEBUG(f"Markdown file created at {markdown_filename}")
|
||||
|
||||
if args.wallpaper:
|
||||
change_wallpaper(jpg_file_path)
|
||||
logger.debug(f"Wallpaper changed.")
|
||||
L.DEBUG(f"Wallpaper changed.")
|
||||
|
||||
|
||||
if not args.local:
|
||||
ig_footer = ""
|
||||
if not args.noig:
|
||||
post_url = upload_photo(jpg_file_path, instagram_description)
|
||||
logger.debug(f"Image posted at {post_url}")
|
||||
L.DEBUG(f"Image posted at {post_url}")
|
||||
ig_footer = f"\n<a href=\"{post_url}\">Instagram link</a>"
|
||||
|
||||
if not args.noghost:
|
||||
ghost_text = f"{instagram_description}"
|
||||
ghost_url = post_to_ghost(img_title, jpg_file_path, ghost_text, ghost_tags)
|
||||
logger.debug(f"Ghost post: {ghost_url}\n{ig_footer}")
|
||||
L.DEBUG(f"Ghost post: {ghost_url}\n{ig_footer}")
|
||||
|
||||
|
||||
def choose_random_post(posts):
|
||||
def choose_post(posts):
|
||||
total_frequency = sum(posts[post_type]['frequency'] for post_type in posts)
|
||||
random_choice = random.randint(1, total_frequency)
|
||||
current_sum = 0
|
||||
|
@ -801,6 +800,8 @@ def load_json(json_payload, workflow):
|
|||
raise ValueError("No valid input provided.")
|
||||
|
||||
|
||||
|
||||
|
||||
def save_as_jpg(image_data, prompt_id, chosen_post:str = None, max_size=2160, quality=80):
|
||||
chosen_post = chosen_post if chosen_post else "custom"
|
||||
filename_png = f"{prompt_id}.png"
|
||||
|
@ -835,19 +836,121 @@ def save_as_jpg(image_data, prompt_id, chosen_post:str = None, max_size=2160, qu
|
|||
|
||||
return new_file_path
|
||||
except Exception as e:
|
||||
logger.debug(f"Error processing image: {e}")
|
||||
L.DEBUG(f"Error processing image: {e}")
|
||||
return None
|
||||
|
||||
|
||||
def upload_photo(path, caption, title: str=None):
|
||||
logger.debug(f"Uploading photo from {path}...")
|
||||
L.DEBUG(f"Uploading photo from {path}...")
|
||||
media = cl.photo_upload(path, caption)
|
||||
post_url = f"https://www.instagram.com/p/{media.code}/"
|
||||
return post_url
|
||||
|
||||
def format_duration(seconds):
|
||||
"""Return a string representing the duration in a human-readable format."""
|
||||
if seconds < 120:
|
||||
return f"{int(seconds)} sec"
|
||||
elif seconds < 6400:
|
||||
return f"{int(seconds // 60)} min"
|
||||
else:
|
||||
return f"{seconds / 3600:.2f} hr"
|
||||
|
||||
########################
|
||||
### HELPER FUNCTIONS ###
|
||||
########################
|
||||
|
||||
import subprocess
|
||||
|
||||
def change_wallpaper(image_path):
|
||||
command = """
|
||||
osascript -e 'tell application "Finder" to set desktop picture to POSIX file "{}"'
|
||||
""".format(image_path)
|
||||
subprocess.run(command, shell=True)
|
||||
|
||||
|
||||
def sleep(seconds):
|
||||
"""Sleep for a random amount of time, approximately the given number of seconds."""
|
||||
sleepupto(seconds*0.66, seconds*1.5)
|
||||
|
||||
def sleepupto(min_seconds, max_seconds=None):
|
||||
interval = random.uniform(min_seconds if max_seconds is not None else 0, max_seconds if max_seconds is not None else min_seconds)
|
||||
start_time = time.time()
|
||||
end_time = start_time + interval
|
||||
|
||||
with tqdm(total=interval, desc=f"Sleeping for {format_duration(interval)}", unit=" sec", ncols=75, bar_format='{desc}: {bar} {remaining}') as pbar:
|
||||
while True:
|
||||
current_time = time.time()
|
||||
elapsed_time = current_time - start_time
|
||||
remaining_time = end_time - current_time
|
||||
if elapsed_time >= interval:
|
||||
break
|
||||
duration = min(1, interval - elapsed_time) # Adjust sleep time to not exceed interval
|
||||
time.sleep(duration)
|
||||
pbar.update(duration)
|
||||
# Update remaining time display
|
||||
pbar.set_postfix_str(f"{format_duration(remaining_time)} remaining")
|
||||
|
||||
|
||||
########################
|
||||
### GHOST FUNCTIONS ###
|
||||
########################
|
||||
|
||||
|
||||
|
||||
def generate_jwt_token():
|
||||
key_id, key_secret = GHOST_API_KEY.split(':')
|
||||
iat = int(date.now().timestamp())
|
||||
exp = iat + 5 * 60 # Token expiration time set to 5 minutes from now for consistency with the working script
|
||||
payload = {
|
||||
'iat': iat,
|
||||
'exp': exp,
|
||||
'aud': '/admin/' # Adjusted to match the working script
|
||||
}
|
||||
token = jwt.encode(payload, bytes.fromhex(key_secret), algorithm='HS256', headers={'kid': key_id})
|
||||
return token.decode('utf-8') if isinstance(token, bytes) else token # Ensure the token is decoded to UTF-8 string
|
||||
|
||||
|
||||
def post_to_ghost(title, image_path, html_content, ghost_tags):
|
||||
jwt_token = generate_jwt_token()
|
||||
ghost_headers = {'Authorization': f'Ghost {jwt_token}'}
|
||||
|
||||
# Upload the image to Ghost
|
||||
with open(image_path, 'rb') as f:
|
||||
files = {'file': (os.path.basename(image_path), f, 'image/jpg')}
|
||||
image_response = requests.post(f"{GHOST_API_URL}/images/upload/", headers=ghost_headers, files=files)
|
||||
image_response.raise_for_status() # Ensure the request was successful
|
||||
image_url = image_response.json()['images'][0]['url']
|
||||
|
||||
# Prepare the post content
|
||||
updated_html_content = f'<img src="{image_url}" alt="Image"/><hr/> {html_content}'
|
||||
mobiledoc = {
|
||||
"version": "0.3.1",
|
||||
"atoms": [],
|
||||
"cards": [["html", {"cardName": "html", "html": updated_html_content}]],
|
||||
"markups": [],
|
||||
"sections": [[10, 0]]
|
||||
}
|
||||
mobiledoc = json.dumps(mobiledoc)
|
||||
|
||||
post_data = {
|
||||
'posts': [{
|
||||
'title': title,
|
||||
'mobiledoc': mobiledoc,
|
||||
'status': 'published',
|
||||
'tags': ghost_tags
|
||||
}]
|
||||
}
|
||||
|
||||
# Create a new post
|
||||
post_response = requests.post(f"{GHOST_API_URL}/posts/", json=post_data, headers=ghost_headers)
|
||||
post_response.raise_for_status()
|
||||
post_url = post_response.json()['posts'][0]['url']
|
||||
|
||||
return post_url
|
||||
|
||||
|
||||
|
||||
########################################################
|
||||
|
||||
@ig.post("/ig/flow")
|
||||
async def ig_flow_endpoint(new_session: bool = False):
|
||||
current_unix_time = int(date.now().timestamp())
|
||||
|
@ -855,16 +958,16 @@ async def ig_flow_endpoint(new_session: bool = False):
|
|||
time_remaining = 30 - (time_since_rollover % 30)
|
||||
|
||||
if time_remaining < 4:
|
||||
logger.debug("Too close to end of TOTP counter. Waiting.")
|
||||
L.DEBUG("Too close to end of TOTP counter. Waiting.")
|
||||
sleepupto(5, 5)
|
||||
|
||||
if not new_session and os.path.exists(IG_SESSION_PATH):
|
||||
cl.load_settings(IG_SESSION_PATH)
|
||||
logger.debug("Loaded past session.")
|
||||
L.DEBUG("Loaded past session.")
|
||||
|
||||
elif new_session and cl.login(IG_USERNAME, IG_PASSWORD, verification_code=TOTP.now()):
|
||||
cl.dump_settings(IG_SESSION_PATH)
|
||||
logger.debug("Logged in and saved new session.")
|
||||
L.DEBUG("Logged in and saved new session.")
|
||||
|
||||
else:
|
||||
raise Exception(f"Failed to login as {IG_USERNAME}.")
|
||||
|
|
|
@ -33,7 +33,7 @@ from sijapi.routers.llm import query_ollama
|
|||
from sijapi import API, L, COMFYUI_URL, COMFYUI_OUTPUT_DIR, IMG_CONFIG_PATH, IMG_DIR, IMG_WORKFLOWS_DIR
|
||||
|
||||
img = APIRouter()
|
||||
logger = L.get_module_logger("img")
|
||||
|
||||
CLIENT_ID = str(uuid.uuid4())
|
||||
|
||||
@img.post("/img")
|
||||
|
@ -79,12 +79,12 @@ async def workflow(prompt: str, scene: str = None, size: str = None, earlyout: s
|
|||
|
||||
scene_workflow = random.choice(scene_data['workflows'])
|
||||
if size:
|
||||
logger.debug(f"Specified size: {size}")
|
||||
L.DEBUG(f"Specified size: {size}")
|
||||
|
||||
size = size if size else scene_workflow.get('size', '1024x1024')
|
||||
|
||||
width, height = map(int, size.split('x'))
|
||||
logger.debug(f"Parsed width: {width}; parsed height: {height}")
|
||||
L.DEBUG(f"Parsed width: {width}; parsed height: {height}")
|
||||
|
||||
workflow_path = Path(IMG_WORKFLOWS_DIR) / scene_workflow['workflow']
|
||||
workflow_data = json.loads(workflow_path.read_text())
|
||||
|
@ -108,12 +108,12 @@ async def workflow(prompt: str, scene: str = None, size: str = None, earlyout: s
|
|||
|
||||
if earlyout:
|
||||
asyncio.create_task(generate_and_save_image(prompt_id, saved_file_key, max_size, destination_path))
|
||||
logger.debug(f"Returning {destination_path}")
|
||||
L.DEBUG(f"Returning {destination_path}")
|
||||
return destination_path
|
||||
|
||||
else:
|
||||
await generate_and_save_image(prompt_id, saved_file_key, max_size, destination_path)
|
||||
logger.debug(f"Returning {destination_path}")
|
||||
L.DEBUG(f"Returning {destination_path}")
|
||||
return destination_path
|
||||
|
||||
|
||||
|
@ -124,7 +124,7 @@ async def generate_and_save_image(prompt_id, saved_file_key, max_size, destinati
|
|||
jpg_file_path = await save_as_jpg(image_data, prompt_id, quality=90, max_size=max_size, destination_path=destination_path)
|
||||
|
||||
if Path(jpg_file_path) != Path(destination_path):
|
||||
logger.error(f"Mismatch between jpg_file_path, {jpg_file_path}, and detination_path, {destination_path}")
|
||||
L.ERR(f"Mismatch between jpg_file_path, {jpg_file_path}, and detination_path, {destination_path}")
|
||||
|
||||
except Exception as e:
|
||||
print(f"Error in generate_and_save_image: {e}")
|
||||
|
@ -216,11 +216,11 @@ def set_presets(workflow_data, preset_values):
|
|||
if 'inputs' in workflow_data.get(preset_node, {}):
|
||||
workflow_data[preset_node]['inputs'][preset_key] = preset_value
|
||||
else:
|
||||
logger.debug("Node not found in workflow_data")
|
||||
L.DEBUG("Node not found in workflow_data")
|
||||
else:
|
||||
logger.debug("Required data missing in preset_values")
|
||||
L.DEBUG("Required data missing in preset_values")
|
||||
else:
|
||||
logger.debug("No preset_values found")
|
||||
L.DEBUG("No preset_values found")
|
||||
|
||||
|
||||
def get_return_path(destination_path):
|
||||
|
@ -235,7 +235,7 @@ def get_scene(scene):
|
|||
IMG_CONFIG = yaml.safe_load(IMG_CONFIG_file)
|
||||
for scene_data in IMG_CONFIG['scenes']:
|
||||
if scene_data['scene'] == scene:
|
||||
logger.debug(f"Found scene for \"{scene}\".")
|
||||
L.DEBUG(f"Found scene for \"{scene}\".")
|
||||
return scene_data
|
||||
return None
|
||||
|
||||
|
@ -254,11 +254,11 @@ def get_matching_scene(prompt):
|
|||
max_count = count
|
||||
scene_data = sc
|
||||
if scene_data:
|
||||
logger.debug(f"Found better-matching scene: the prompt contains {max_count} words that match triggers for {scene_data.get('name')}!")
|
||||
L.DEBUG(f"Found better-matching scene: the prompt contains {max_count} words that match triggers for {scene_data.get('name')}!")
|
||||
if scene_data:
|
||||
return scene_data
|
||||
else:
|
||||
logger.debug(f"No matching scenes found, falling back to default scene.")
|
||||
L.DEBUG(f"No matching scenes found, falling back to default scene.")
|
||||
return IMG_CONFIG['scenes'][0]
|
||||
|
||||
|
||||
|
@ -326,10 +326,10 @@ async def ensure_comfy(retries: int = 4, timeout: float = 6.0):
|
|||
# shareable_link = f"https://{PHOTOPRISM_URL}/p/{photo_uuid}"
|
||||
# return shareable_link
|
||||
# else:
|
||||
# logger.error("Could not find the uploaded photo details.")
|
||||
# L.ERR("Could not find the uploaded photo details.")
|
||||
# return None
|
||||
# except Exception as e:
|
||||
# logger.error(f"Error in upload_and_get_shareable_link: {e}")
|
||||
# L.ERR(f"Error in upload_and_get_shareable_link: {e}")
|
||||
# return None
|
||||
|
||||
|
||||
|
@ -436,13 +436,13 @@ Even more important, it finds and returns the key to the filepath where the file
|
|||
workflow[key] = random.randint(1000000000000, 9999999999999)
|
||||
|
||||
elif key in ["width", "max_width", "scaled_width", "height", "max_height", "scaled_height", "side_length", "size", "value", "dimension", "dimensions", "long", "long_side", "short", "short_side", "length"]:
|
||||
logger.debug(f"Got a hit for a dimension: {key} {value}")
|
||||
L.DEBUG(f"Got a hit for a dimension: {key} {value}")
|
||||
if value == 1023:
|
||||
workflow[key] = post.get("width", 1024)
|
||||
logger.debug(f"Set {key} to {workflow[key]}.")
|
||||
L.DEBUG(f"Set {key} to {workflow[key]}.")
|
||||
elif value == 1025:
|
||||
workflow[key] = post.get("height", 1024)
|
||||
logger.debug(f"Set {key} to {workflow[key]}.")
|
||||
L.DEBUG(f"Set {key} to {workflow[key]}.")
|
||||
|
||||
update_recursive(workflow)
|
||||
return found_key[0]
|
||||
|
|
|
@ -26,13 +26,13 @@ import tempfile
|
|||
import shutil
|
||||
import html2text
|
||||
import markdown
|
||||
from sijapi import L, LLM_SYS_MSG, DEFAULT_LLM, DEFAULT_VISION, REQUESTS_DIR, OBSIDIAN_CHROMADB_COLLECTION, OBSIDIAN_VAULT_DIR, DOC_DIR, OPENAI_API_KEY, DEFAULT_VOICE, SUMMARY_INSTRUCT, SUMMARY_CHUNK_SIZE, SUMMARY_TPW, SUMMARY_CHUNK_OVERLAP, SUMMARY_LENGTH_RATIO, SUMMARY_TOKEN_LIMIT, SUMMARY_MIN_LENGTH, SUMMARY_MODEL
|
||||
from sijapi import L, Dir, API, LLM, TTS, Obsidian
|
||||
from sijapi.utilities import convert_to_unix_time, sanitize_filename, ocr_pdf, clean_text, should_use_ocr, extract_text_from_pdf, extract_text_from_docx, read_text_file, str_to_bool, get_extension
|
||||
from sijapi.routers import tts
|
||||
from sijapi.routers.asr import transcribe_audio
|
||||
|
||||
|
||||
llm = APIRouter()
|
||||
logger = L.get_module_logger("llm")
|
||||
|
||||
# Initialize chromadb client
|
||||
client = chromadb.Client()
|
||||
|
@ -49,7 +49,7 @@ def read_markdown_files(folder: Path):
|
|||
return documents, file_paths
|
||||
|
||||
# Read markdown files and generate embeddings
|
||||
documents, file_paths = read_markdown_files(DOC_DIR)
|
||||
documents, file_paths = read_markdown_files(Obsidian.docs)
|
||||
for i, doc in enumerate(documents):
|
||||
response = ollama.embeddings(model="mxbai-embed-large", prompt=doc)
|
||||
embedding = response["embedding"]
|
||||
|
@ -83,25 +83,25 @@ async def generate_response(prompt: str):
|
|||
return {"response": output['response']}
|
||||
|
||||
|
||||
async def query_ollama(usr: str, sys: str = LLM_SYS_MSG, model: str = DEFAULT_LLM, max_tokens: int = 200):
|
||||
async def query_ollama(usr: str, sys: str = LLM.chat.sys, model: str = LLM.chat.model, max_tokens: int = LLM.chat.max_tokens):
|
||||
messages = [{"role": "system", "content": sys},
|
||||
{"role": "user", "content": usr}]
|
||||
LLM = Ollama()
|
||||
response = await LLM.chat(model=model, messages=messages, options={"num_predict": max_tokens})
|
||||
|
||||
logger.debug(response)
|
||||
L.DEBUG(response)
|
||||
if "message" in response:
|
||||
if "content" in response["message"]:
|
||||
content = response["message"]["content"]
|
||||
return content
|
||||
else:
|
||||
logger.debug("No choices found in response")
|
||||
L.DEBUG("No choices found in response")
|
||||
return None
|
||||
|
||||
async def query_ollama_multishot(
|
||||
message_list: List[str],
|
||||
sys: str = LLM_SYS_MSG,
|
||||
model: str = DEFAULT_LLM,
|
||||
sys: str = LLM.chat.sys,
|
||||
model: str = LLM.chat.model,
|
||||
max_tokens: int = 200
|
||||
):
|
||||
if len(message_list) % 2 == 0:
|
||||
|
@ -116,12 +116,12 @@ async def query_ollama_multishot(
|
|||
|
||||
LLM = Ollama()
|
||||
response = await LLM.chat(model=model, messages=messages, options={"num_predict": max_tokens})
|
||||
logger.debug(response)
|
||||
L.DEBUG(response)
|
||||
|
||||
if "message" in response and "content" in response["message"]:
|
||||
return response["message"]["content"]
|
||||
else:
|
||||
logger.debug("No content found in response")
|
||||
L.DEBUG("No content found in response")
|
||||
return None
|
||||
|
||||
|
||||
|
@ -130,7 +130,7 @@ async def chat_completions(request: Request):
|
|||
body = await request.json()
|
||||
|
||||
timestamp = dt_datetime.now().strftime("%Y%m%d_%H%M%S%f")
|
||||
filename = REQUESTS_DIR / f"request_{timestamp}.json"
|
||||
filename = Dir.logs.requests / f"request_{timestamp}.json"
|
||||
|
||||
async with aiofiles.open(filename, mode='w') as file:
|
||||
await file.write(json.dumps(body, indent=4))
|
||||
|
@ -140,21 +140,21 @@ async def chat_completions(request: Request):
|
|||
raise HTTPException(status_code=400, detail="Message data is required in the request body.")
|
||||
|
||||
requested_model = body.get('model', 'default-model')
|
||||
logger.debug(f"Requested model: {requested_model}")
|
||||
L.DEBUG(f"Requested model: {requested_model}")
|
||||
stream = body.get('stream')
|
||||
token_limit = body.get('max_tokens') or body.get('num_predict')
|
||||
|
||||
# Check if the most recent message contains an image_url
|
||||
recent_message = messages[-1]
|
||||
if recent_message.get('role') == 'user' and is_vision_request(recent_message.get('content')):
|
||||
logger.debug("Processing as a vision request")
|
||||
L.DEBUG("Processing as a vision request")
|
||||
model = "llava"
|
||||
logger.debug(f"Using model: {model}")
|
||||
L.DEBUG(f"Using model: {model}")
|
||||
return StreamingResponse(stream_messages_with_vision(recent_message, model, token_limit), media_type="application/json")
|
||||
else:
|
||||
logger.debug("Processing as a standard request")
|
||||
L.DEBUG("Processing as a standard request")
|
||||
model = requested_model
|
||||
logger.debug(f"Using model: {model}")
|
||||
L.DEBUG(f"Using model: {model}")
|
||||
if stream:
|
||||
return StreamingResponse(stream_messages(messages, model, token_limit), media_type="application/json")
|
||||
else:
|
||||
|
@ -227,9 +227,9 @@ async def stream_messages_with_vision(message: dict, model: str, num_predict: in
|
|||
|
||||
def get_appropriate_model(requested_model):
|
||||
if requested_model == "gpt-4-vision-preview":
|
||||
return DEFAULT_VISION
|
||||
return LLM.vision.model
|
||||
elif not is_model_available(requested_model):
|
||||
return DEFAULT_LLM
|
||||
return LLM.chat.model
|
||||
else:
|
||||
return requested_model
|
||||
|
||||
|
@ -279,17 +279,17 @@ async def generate_messages(messages: list, model: str = "llama3"):
|
|||
def is_model_available(model_name):
|
||||
model_data = OllamaList()
|
||||
available_models = [model['name'] for model in model_data['models']]
|
||||
logger.debug(f"Available models: {available_models}") # Log using the configured LOGGER
|
||||
L.DEBUG(f"Available models: {available_models}") # Log using the configured LOGGER
|
||||
|
||||
matching_models = [model for model in available_models if model.startswith(model_name + ':') or model == model_name]
|
||||
if len(matching_models) == 1:
|
||||
logger.debug(f"Unique match found: {matching_models[0]}")
|
||||
L.DEBUG(f"Unique match found: {matching_models[0]}")
|
||||
return True
|
||||
elif len(matching_models) > 1:
|
||||
logger.error(f"Ambiguous match found, models: {matching_models}")
|
||||
L.ERR(f"Ambiguous match found, models: {matching_models}")
|
||||
return True
|
||||
else:
|
||||
logger.error(f"No match found for model: {model_name}")
|
||||
L.ERR(f"No match found for model: {model_name}")
|
||||
return False
|
||||
|
||||
|
||||
|
@ -310,7 +310,7 @@ async def chat_completions_options(request: Request):
|
|||
],
|
||||
"created": int(time.time()),
|
||||
"id": str(uuid.uuid4()),
|
||||
"model": DEFAULT_LLM,
|
||||
"model": LLM.chat.model,
|
||||
"object": "chat.completion.chunk",
|
||||
},
|
||||
status_code=200,
|
||||
|
@ -401,7 +401,7 @@ def query_gpt4(llmPrompt: List = [], system_msg: str = "", user_msg: str = "", m
|
|||
{"role": "system", "content": system_msg},
|
||||
{"role": "user", "content": user_msg}
|
||||
]
|
||||
LLM = OpenAI(api_key=OPENAI_API_KEY)
|
||||
LLM = OpenAI(api_key=LLM.OPENAI_API_KEY)
|
||||
response = LLM.chat.completions.create(
|
||||
model="gpt-4",
|
||||
messages=messages,
|
||||
|
@ -412,12 +412,12 @@ def query_gpt4(llmPrompt: List = [], system_msg: str = "", user_msg: str = "", m
|
|||
if hasattr(first_choice, "message") and hasattr(first_choice.message, "content"):
|
||||
return first_choice.message.content
|
||||
else:
|
||||
logger.debug("No content attribute in the first choice's message")
|
||||
logger.debug(f"No content found in message string: {response.choices}")
|
||||
logger.debug("Trying again!")
|
||||
L.DEBUG("No content attribute in the first choice's message")
|
||||
L.DEBUG(f"No content found in message string: {response.choices}")
|
||||
L.DEBUG("Trying again!")
|
||||
query_gpt4(messages, max_tokens)
|
||||
else:
|
||||
logger.debug(f"No content found in message string: {response}")
|
||||
L.DEBUG(f"No content found in message string: {response}")
|
||||
return ""
|
||||
|
||||
def llava(image_base64, prompt):
|
||||
|
@ -427,11 +427,11 @@ def llava(image_base64, prompt):
|
|||
prompt = f"This is a chat between a user and an assistant. The assistant is helping the user to describe an image. {prompt}",
|
||||
images = [image_base64]
|
||||
)
|
||||
logger.debug(response)
|
||||
L.DEBUG(response)
|
||||
return "" if "pass" in response["response"].lower() else response["response"]
|
||||
|
||||
def gpt4v(image_base64, prompt_sys: str, prompt_usr: str, max_tokens: int = 150):
|
||||
VISION_LLM = OpenAI(api_key=OPENAI_API_KEY)
|
||||
VISION_LLM = OpenAI(api_key=LLM.OPENAI_API_KEY)
|
||||
response_1 = VISION_LLM.chat.completions.create(
|
||||
model="gpt-4-vision-preview",
|
||||
messages=[
|
||||
|
@ -458,7 +458,7 @@ def gpt4v(image_base64, prompt_sys: str, prompt_usr: str, max_tokens: int = 150)
|
|||
comment_content = first_choice.message.content
|
||||
if "PASS" in comment_content:
|
||||
return ""
|
||||
logger.debug(f"Generated comment: {comment_content}")
|
||||
L.DEBUG(f"Generated comment: {comment_content}")
|
||||
|
||||
response_2 = VISION_LLM.chat.completions.create(
|
||||
model="gpt-4-vision-preview",
|
||||
|
@ -496,15 +496,15 @@ def gpt4v(image_base64, prompt_sys: str, prompt_usr: str, max_tokens: int = 150)
|
|||
first_choice = response_2.choices[0]
|
||||
if first_choice.message and first_choice.message.content:
|
||||
final_content = first_choice.message.content
|
||||
logger.debug(f"Generated comment: {final_content}")
|
||||
L.DEBUG(f"Generated comment: {final_content}")
|
||||
if "PASS" in final_content:
|
||||
return ""
|
||||
else:
|
||||
return final_content
|
||||
|
||||
|
||||
logger.debug("Vision response did not contain expected data.")
|
||||
logger.debug(f"Vision response: {response_1}")
|
||||
L.DEBUG("Vision response did not contain expected data.")
|
||||
L.DEBUG(f"Vision response: {response_1}")
|
||||
asyncio.sleep(15)
|
||||
|
||||
try_again = gpt4v(image_base64, prompt_sys, prompt_usr, max_tokens)
|
||||
|
@ -512,12 +512,12 @@ def gpt4v(image_base64, prompt_sys: str, prompt_usr: str, max_tokens: int = 150)
|
|||
|
||||
|
||||
@llm.get("/summarize")
|
||||
async def summarize_get(text: str = Form(None), instruction: str = Form(SUMMARY_INSTRUCT)):
|
||||
async def summarize_get(text: str = Form(None), instruction: str = Form(LLM.summary.instruct)):
|
||||
summarized_text = await summarize_text(text, instruction)
|
||||
return summarized_text
|
||||
|
||||
@llm.post("/summarize")
|
||||
async def summarize_post(file: Optional[UploadFile] = File(None), text: Optional[str] = Form(None), instruction: str = Form(SUMMARY_INSTRUCT)):
|
||||
async def summarize_post(file: Optional[UploadFile] = File(None), text: Optional[str] = Form(None), instruction: str = Form(LLM.summary.instruct)):
|
||||
text_content = text if text else await extract_text(file)
|
||||
summarized_text = await summarize_text(text_content, instruction)
|
||||
return summarized_text
|
||||
|
@ -526,10 +526,10 @@ async def summarize_post(file: Optional[UploadFile] = File(None), text: Optional
|
|||
@llm.post("/speaksummary")
|
||||
async def summarize_tts_endpoint(
|
||||
bg_tasks: BackgroundTasks,
|
||||
instruction: str = Form(SUMMARY_INSTRUCT),
|
||||
instruction: str = Form(LLM.summary.instruct),
|
||||
file: Optional[UploadFile] = File(None),
|
||||
text: Optional[str] = Form(None),
|
||||
voice: Optional[str] = Form(DEFAULT_VOICE),
|
||||
voice: Optional[str] = Form(TTS.xtts.voice),
|
||||
speed: Optional[float] = Form(1.2),
|
||||
podcast: Union[bool, str] = Form(False)
|
||||
):
|
||||
|
@ -562,7 +562,7 @@ async def summarize_tts_endpoint(
|
|||
)
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Error in summarize_tts_endpoint: {str(e)}")
|
||||
L.ERR(f"Error in summarize_tts_endpoint: {str(e)}")
|
||||
return JSONResponse(
|
||||
status_code=400,
|
||||
content={"error": str(e)}
|
||||
|
@ -572,8 +572,8 @@ async def summarize_tts_endpoint(
|
|||
|
||||
async def summarize_tts(
|
||||
text: str,
|
||||
instruction: str = SUMMARY_INSTRUCT,
|
||||
voice: Optional[str] = DEFAULT_VOICE,
|
||||
instruction: str = LLM.summary.instruct,
|
||||
voice: Optional[str] = TTS.xtts.voice,
|
||||
speed: float = 1.1,
|
||||
podcast: bool = False,
|
||||
LLM: Ollama = None
|
||||
|
@ -589,7 +589,7 @@ async def summarize_tts(
|
|||
bg_tasks = BackgroundTasks()
|
||||
model = await tts.get_model(voice)
|
||||
final_output_path = await tts.generate_speech(bg_tasks, summarized_text, voice, model=model, speed=speed, podcast=podcast, title=filename)
|
||||
logger.debug(f"summary_tts completed with final_output_path: {final_output_path}")
|
||||
L.DEBUG(f"summary_tts completed with final_output_path: {final_output_path}")
|
||||
return final_output_path
|
||||
|
||||
|
||||
|
@ -605,10 +605,10 @@ def split_text_into_chunks(text: str) -> List[str]:
|
|||
sentences = re.split(r'(?<=[.!?])\s+', text)
|
||||
words = text.split()
|
||||
total_words = len(words)
|
||||
logger.debug(f"Total words: {total_words}. SUMMARY_CHUNK_SIZE: {SUMMARY_CHUNK_SIZE}. SUMMARY_TPW: {SUMMARY_TPW}.")
|
||||
L.DEBUG(f"Total words: {total_words}. LLM.summary.chunk_size: {LLM.summary.chunk_size}. LLM.tpw: {LLM.tpw}.")
|
||||
|
||||
max_words_per_chunk = int(SUMMARY_CHUNK_SIZE / SUMMARY_TPW)
|
||||
logger.debug(f"Maximum words per chunk: {max_words_per_chunk}")
|
||||
max_words_per_chunk = int(LLM.summary.chunk_size / LLM.tpw)
|
||||
L.DEBUG(f"Maximum words per chunk: {max_words_per_chunk}")
|
||||
|
||||
chunks = []
|
||||
current_chunk = []
|
||||
|
@ -628,23 +628,23 @@ def split_text_into_chunks(text: str) -> List[str]:
|
|||
if current_chunk:
|
||||
chunks.append(' '.join(current_chunk))
|
||||
|
||||
logger.debug(f"Split text into {len(chunks)} chunks.")
|
||||
L.DEBUG(f"Split text into {len(chunks)} chunks.")
|
||||
return chunks
|
||||
|
||||
|
||||
def calculate_max_tokens(text: str) -> int:
|
||||
tokens_count = max(1, int(len(text.split()) * SUMMARY_TPW)) # Ensure at least 1
|
||||
return min(tokens_count // 4, SUMMARY_CHUNK_SIZE)
|
||||
tokens_count = max(1, int(len(text.split()) * LLM.tpw)) # Ensure at least 1
|
||||
return min(tokens_count // 4, LLM.summary.chunk_size)
|
||||
|
||||
|
||||
|
||||
|
||||
async def extract_text(file: Union[UploadFile, bytes, bytearray, str, Path], bg_tasks: BackgroundTasks = None) -> str:
|
||||
logger.info(f"Attempting to extract text from file: {file}")
|
||||
L.INFO(f"Attempting to extract text from file: {file}")
|
||||
|
||||
try:
|
||||
if isinstance(file, UploadFile):
|
||||
logger.info("File is an UploadFile object")
|
||||
L.INFO("File is an UploadFile object")
|
||||
file_extension = os.path.splitext(file.filename)[1]
|
||||
temp_file_path = tempfile.mktemp(suffix=file_extension)
|
||||
with open(temp_file_path, 'wb') as buffer:
|
||||
|
@ -663,7 +663,7 @@ async def extract_text(file: Union[UploadFile, bytes, bytearray, str, Path], bg_
|
|||
|
||||
_, file_ext = os.path.splitext(file_path)
|
||||
file_ext = file_ext.lower()
|
||||
logger.info(f"File extension: {file_ext}")
|
||||
L.INFO(f"File extension: {file_ext}")
|
||||
|
||||
if file_ext == '.pdf':
|
||||
text_content = await extract_text_from_pdf(file_path)
|
||||
|
@ -690,26 +690,26 @@ async def extract_text(file: Union[UploadFile, bytes, bytearray, str, Path], bg_
|
|||
return text_content
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Error extracting text: {str(e)}")
|
||||
L.ERR(f"Error extracting text: {str(e)}")
|
||||
raise ValueError(f"Error extracting text: {str(e)}")
|
||||
|
||||
|
||||
async def summarize_text(text: str, instruction: str = SUMMARY_INSTRUCT, length_override: int = None, length_quotient: float = SUMMARY_LENGTH_RATIO, LLM: Ollama = None):
|
||||
async def summarize_text(text: str, instruction: str = LLM.summary.instruct, length_override: int = None, length_quotient: float = LLM.summary.length_ratio, LLM: Ollama = None):
|
||||
LLM = LLM if LLM else Ollama()
|
||||
|
||||
chunked_text = split_text_into_chunks(text)
|
||||
total_parts = len(chunked_text)
|
||||
logger.debug(f"Total parts: {total_parts}. Length of chunked text: {len(chunked_text)}")
|
||||
L.DEBUG(f"Total parts: {total_parts}. Length of chunked text: {len(chunked_text)}")
|
||||
|
||||
total_words_count = sum(len(chunk.split()) for chunk in chunked_text)
|
||||
logger.debug(f"Total words count: {total_words_count}")
|
||||
total_tokens_count = max(1, int(total_words_count * SUMMARY_TPW))
|
||||
logger.debug(f"Total tokens count: {total_tokens_count}")
|
||||
L.DEBUG(f"Total words count: {total_words_count}")
|
||||
total_tokens_count = max(1, int(total_words_count * LLM.tpw))
|
||||
L.DEBUG(f"Total tokens count: {total_tokens_count}")
|
||||
|
||||
total_summary_length = length_override if length_override else total_tokens_count // length_quotient
|
||||
logger.debug(f"Total summary length: {total_summary_length}")
|
||||
corrected_total_summary_length = min(total_summary_length, SUMMARY_TOKEN_LIMIT)
|
||||
logger.debug(f"Corrected total summary length: {corrected_total_summary_length}")
|
||||
L.DEBUG(f"Total summary length: {total_summary_length}")
|
||||
corrected_total_summary_length = min(total_summary_length, LLM.summary.max_tokens)
|
||||
L.DEBUG(f"Corrected total summary length: {corrected_total_summary_length}")
|
||||
|
||||
summaries = await asyncio.gather(*[
|
||||
process_chunk(instruction, chunk, i+1, total_parts, LLM=LLM)
|
||||
|
@ -720,48 +720,48 @@ async def summarize_text(text: str, instruction: str = SUMMARY_INSTRUCT, length_
|
|||
summaries = [f"\n\n\nPART {i+1} of {total_parts}:\n\n{summary}" for i, summary in enumerate(summaries)]
|
||||
|
||||
concatenated_summary = ' '.join(summaries)
|
||||
logger.debug(f"Concatenated summary: {concatenated_summary}")
|
||||
logger.debug(f"Concatenated summary length: {len(concatenated_summary.split())}")
|
||||
L.DEBUG(f"Concatenated summary: {concatenated_summary}")
|
||||
L.DEBUG(f"Concatenated summary length: {len(concatenated_summary.split())}")
|
||||
|
||||
if total_parts > 1:
|
||||
logger.debug(f"Processing the concatenated_summary to smooth the edges...")
|
||||
L.DEBUG(f"Processing the concatenated_summary to smooth the edges...")
|
||||
concatenated_instruct = f"The following text consists of the concatenated {total_parts} summaries of {total_parts} parts of a single document that had to be split for processing. Reword it for clarity and flow as a single cohesive summary, understanding that it all relates to a single document, but that document likely consists of multiple parts potentially from multiple authors. Do not shorten it and do not omit content, simply smooth out the edges between the parts."
|
||||
final_summary = await process_chunk(concatenated_instruct, concatenated_summary, 1, 1, length_ratio=1, LLM=LLM)
|
||||
logger.debug(f"Final summary length: {len(final_summary.split())}")
|
||||
L.DEBUG(f"Final summary length: {len(final_summary.split())}")
|
||||
return final_summary
|
||||
else:
|
||||
return concatenated_summary
|
||||
|
||||
|
||||
async def process_chunk(instruction: str, text: str, part: int, total_parts: int, length_ratio: float = None, LLM: Ollama = None) -> str:
|
||||
# logger.debug(f"Processing chunk: {text}")
|
||||
# L.DEBUG(f"Processing chunk: {text}")
|
||||
LLM = LLM if LLM else Ollama()
|
||||
|
||||
words_count = len(text.split())
|
||||
tokens_count = max(1, int(words_count * SUMMARY_TPW))
|
||||
tokens_count = max(1, int(words_count * LLM.tpw))
|
||||
|
||||
summary_length_ratio = length_ratio if length_ratio else SUMMARY_LENGTH_RATIO
|
||||
max_tokens = min(tokens_count // summary_length_ratio, SUMMARY_CHUNK_SIZE)
|
||||
max_tokens = max(max_tokens, SUMMARY_MIN_LENGTH)
|
||||
summary_length_ratio = length_ratio if length_ratio else LLM.summary.length_ratio
|
||||
max_tokens = min(tokens_count // summary_length_ratio, LLM.summary.chunk_size)
|
||||
max_tokens = max(max_tokens, LLM.summary.min_length)
|
||||
|
||||
logger.debug(f"Processing part {part} of {total_parts}: Words: {words_count}, Estimated tokens: {tokens_count}, Max output tokens: {max_tokens}")
|
||||
L.DEBUG(f"Processing part {part} of {total_parts}: Words: {words_count}, Estimated tokens: {tokens_count}, Max output tokens: {max_tokens}")
|
||||
|
||||
if part and total_parts > 1:
|
||||
prompt = f"{instruction}. Part {part} of {total_parts}:\n{text}"
|
||||
else:
|
||||
prompt = f"{instruction}:\n\n{text}"
|
||||
|
||||
logger.debug(f"Starting LLM.generate for part {part} of {total_parts}")
|
||||
L.DEBUG(f"Starting LLM.generate for part {part} of {total_parts}")
|
||||
response = await LLM.generate(
|
||||
model=SUMMARY_MODEL,
|
||||
model=LLM.summary.model,
|
||||
prompt=prompt,
|
||||
stream=False,
|
||||
options={'num_predict': max_tokens, 'temperature': 0.5}
|
||||
)
|
||||
|
||||
text_response = response['response']
|
||||
logger.debug(f"Completed LLM.generate for part {part} of {total_parts}")
|
||||
logger.debug(f"Result: {text_response}")
|
||||
L.DEBUG(f"Completed LLM.generate for part {part} of {total_parts}")
|
||||
L.DEBUG(f"Result: {text_response}")
|
||||
return text_response
|
||||
|
||||
async def title_and_summary(extracted_text: str):
|
||||
|
|
|
@ -18,7 +18,7 @@ from sijapi.classes import Location
|
|||
from sijapi.utilities import haversine
|
||||
|
||||
loc = APIRouter()
|
||||
logger = L.get_module_logger("loc")
|
||||
|
||||
|
||||
async def dt(
|
||||
date_time: Union[str, int, datetime],
|
||||
|
@ -28,11 +28,11 @@ async def dt(
|
|||
# Convert integer (epoch time) to UTC datetime
|
||||
if isinstance(date_time, int):
|
||||
date_time = datetime.utcfromtimestamp(date_time).replace(tzinfo=timezone.utc)
|
||||
logger.debug(f"Converted epoch time {date_time} to UTC datetime object.")
|
||||
L.DEBUG(f"Converted epoch time {date_time} to UTC datetime object.")
|
||||
# Convert string to datetime if necessary
|
||||
elif isinstance(date_time, str):
|
||||
date_time = dateutil_parse(date_time)
|
||||
logger.debug(f"Converted string '{date_time}' to datetime object.")
|
||||
L.DEBUG(f"Converted string '{date_time}' to datetime object.")
|
||||
|
||||
if not isinstance(date_time, datetime):
|
||||
raise ValueError(f"Input must be a string, integer (epoch time), or datetime object. What we received: {date_time}, type {type(date_time)}")
|
||||
|
@ -40,7 +40,7 @@ async def dt(
|
|||
# Ensure the datetime is timezone-aware (UTC if not specified)
|
||||
if date_time.tzinfo is None:
|
||||
date_time = date_time.replace(tzinfo=timezone.utc)
|
||||
logger.debug("Added UTC timezone to naive datetime.")
|
||||
L.DEBUG("Added UTC timezone to naive datetime.")
|
||||
|
||||
# Handle provided timezone
|
||||
if tz is not None:
|
||||
|
@ -48,12 +48,12 @@ async def dt(
|
|||
if tz == "local":
|
||||
last_loc = await get_timezone_without_timezone(date_time)
|
||||
tz = await GEO.tz_at(last_loc.latitude, last_loc.longitude)
|
||||
logger.debug(f"Using local timezone: {tz}")
|
||||
L.DEBUG(f"Using local timezone: {tz}")
|
||||
else:
|
||||
try:
|
||||
tz = ZoneInfo(tz)
|
||||
except Exception as e:
|
||||
logger.error(f"Invalid timezone string '{tz}'. Error: {e}")
|
||||
L.ERR(f"Invalid timezone string '{tz}'. Error: {e}")
|
||||
raise ValueError(f"Invalid timezone string: {tz}")
|
||||
elif isinstance(tz, ZoneInfo):
|
||||
pass # tz is already a ZoneInfo object
|
||||
|
@ -62,14 +62,14 @@ async def dt(
|
|||
|
||||
# Convert to the provided or determined timezone
|
||||
date_time = date_time.astimezone(tz)
|
||||
logger.debug(f"Converted datetime to timezone: {tz}")
|
||||
L.DEBUG(f"Converted datetime to timezone: {tz}")
|
||||
|
||||
return date_time
|
||||
except ValueError as e:
|
||||
logger.error(f"Error in dt: {e}")
|
||||
L.ERR(f"Error in dt: {e}")
|
||||
raise
|
||||
except Exception as e:
|
||||
logger.error(f"Unexpected error in dt: {e}")
|
||||
L.ERR(f"Unexpected error in dt: {e}")
|
||||
raise ValueError(f"Failed to process datetime: {e}")
|
||||
|
||||
|
||||
|
@ -93,12 +93,12 @@ async def get_timezone_without_timezone(date_time):
|
|||
|
||||
async def get_last_location() -> Optional[Location]:
|
||||
query_datetime = datetime.now(TZ)
|
||||
logger.debug(f"Query_datetime: {query_datetime}")
|
||||
L.DEBUG(f"Query_datetime: {query_datetime}")
|
||||
|
||||
this_location = await fetch_last_location_before(query_datetime)
|
||||
|
||||
if this_location:
|
||||
logger.debug(f"location: {this_location}")
|
||||
L.DEBUG(f"location: {this_location}")
|
||||
return this_location
|
||||
|
||||
return None
|
||||
|
@ -114,7 +114,7 @@ async def fetch_locations(start: datetime, end: datetime = None) -> List[Locatio
|
|||
if start_datetime.time() == datetime.min.time() and end_datetime.time() == datetime.min.time():
|
||||
end_datetime = end_datetime.replace(hour=23, minute=59, second=59)
|
||||
|
||||
logger.debug(f"Fetching locations between {start_datetime} and {end_datetime}")
|
||||
L.DEBUG(f"Fetching locations between {start_datetime} and {end_datetime}")
|
||||
|
||||
async with DB.get_connection() as conn:
|
||||
locations = []
|
||||
|
@ -131,7 +131,7 @@ async def fetch_locations(start: datetime, end: datetime = None) -> List[Locatio
|
|||
ORDER BY datetime DESC
|
||||
''', start_datetime.replace(tzinfo=None), end_datetime.replace(tzinfo=None))
|
||||
|
||||
logger.debug(f"Range locations query returned: {range_locations}")
|
||||
L.DEBUG(f"Range locations query returned: {range_locations}")
|
||||
locations.extend(range_locations)
|
||||
|
||||
if not locations and (end is None or start_datetime.date() == end_datetime.date()):
|
||||
|
@ -148,11 +148,11 @@ async def fetch_locations(start: datetime, end: datetime = None) -> List[Locatio
|
|||
LIMIT 1
|
||||
''', start_datetime.replace(tzinfo=None))
|
||||
|
||||
logger.debug(f"Fallback query returned: {location_data}")
|
||||
L.DEBUG(f"Fallback query returned: {location_data}")
|
||||
if location_data:
|
||||
locations.append(location_data)
|
||||
|
||||
logger.debug(f"Locations found: {locations}")
|
||||
L.DEBUG(f"Locations found: {locations}")
|
||||
|
||||
# Sort location_data based on the datetime field in descending order
|
||||
sorted_locations = sorted(locations, key=lambda x: x['datetime'], reverse=True)
|
||||
|
@ -184,7 +184,7 @@ async def fetch_locations(start: datetime, end: datetime = None) -> List[Locatio
|
|||
async def fetch_last_location_before(datetime: datetime) -> Optional[Location]:
|
||||
datetime = await dt(datetime)
|
||||
|
||||
logger.debug(f"Fetching last location before {datetime}")
|
||||
L.DEBUG(f"Fetching last location before {datetime}")
|
||||
|
||||
async with DB.get_connection() as conn:
|
||||
|
||||
|
@ -204,10 +204,10 @@ async def fetch_last_location_before(datetime: datetime) -> Optional[Location]:
|
|||
await conn.close()
|
||||
|
||||
if location_data:
|
||||
logger.debug(f"Last location found: {location_data}")
|
||||
L.DEBUG(f"Last location found: {location_data}")
|
||||
return Location(**location_data)
|
||||
else:
|
||||
logger.debug("No location found before the specified datetime")
|
||||
L.DEBUG("No location found before the specified datetime")
|
||||
return None
|
||||
|
||||
@loc.get("/map/start_date={start_date_str}&end_date={end_date_str}", response_class=HTMLResponse)
|
||||
|
@ -262,9 +262,9 @@ async def generate_map(start_date: datetime, end_date: datetime):
|
|||
|
||||
async def post_location(location: Location):
|
||||
if not location.datetime:
|
||||
logger.debug(f"location appears to be missing datetime: {location}")
|
||||
L.DEBUG(f"location appears to be missing datetime: {location}")
|
||||
else:
|
||||
logger.debug(f"post_location called with {location.datetime}")
|
||||
L.DEBUG(f"post_location called with {location.datetime}")
|
||||
|
||||
async with DB.get_connection() as conn:
|
||||
try:
|
||||
|
@ -293,7 +293,7 @@ async def post_location(location: Location):
|
|||
location.suburb, location.county, location.country_code, location.country)
|
||||
|
||||
await conn.close()
|
||||
logger.info(f"Successfully posted location: {location.latitude}, {location.longitude}, {location.elevation} on {localized_datetime}")
|
||||
L.INFO(f"Successfully posted location: {location.latitude}, {location.longitude}, {location.elevation} on {localized_datetime}")
|
||||
return {
|
||||
'datetime': localized_datetime,
|
||||
'latitude': location.latitude,
|
||||
|
@ -323,8 +323,8 @@ async def post_location(location: Location):
|
|||
'country': location.country
|
||||
}
|
||||
except Exception as e:
|
||||
logger.error(f"Error posting location {e}")
|
||||
logger.error(traceback.format_exc())
|
||||
L.ERR(f"Error posting location {e}")
|
||||
L.ERR(traceback.format_exc())
|
||||
return None
|
||||
|
||||
|
||||
|
@ -347,26 +347,26 @@ async def post_locate_endpoint(locations: Union[Location, List[Location]]):
|
|||
"device_name": "Unknown",
|
||||
"device_os": "Unknown"
|
||||
}
|
||||
logger.debug(f"Location received for processing: {lcn}")
|
||||
L.DEBUG(f"Location received for processing: {lcn}")
|
||||
|
||||
geocoded_locations = await GEO.code(locations)
|
||||
|
||||
responses = []
|
||||
if isinstance(geocoded_locations, List):
|
||||
for location in geocoded_locations:
|
||||
logger.debug(f"Final location to be submitted to database: {location}")
|
||||
L.DEBUG(f"Final location to be submitted to database: {location}")
|
||||
location_entry = await post_location(location)
|
||||
if location_entry:
|
||||
responses.append({"location_data": location_entry})
|
||||
else:
|
||||
logger.warn(f"Posting location to database appears to have failed.")
|
||||
L.WARN(f"Posting location to database appears to have failed.")
|
||||
else:
|
||||
logger.debug(f"Final location to be submitted to database: {geocoded_locations}")
|
||||
L.DEBUG(f"Final location to be submitted to database: {geocoded_locations}")
|
||||
location_entry = await post_location(geocoded_locations)
|
||||
if location_entry:
|
||||
responses.append({"location_data": location_entry})
|
||||
else:
|
||||
logger.warn(f"Posting location to database appears to have failed.")
|
||||
L.WARN(f"Posting location to database appears to have failed.")
|
||||
|
||||
return {"message": "Locations and weather updated", "results": responses}
|
||||
|
||||
|
@ -387,7 +387,7 @@ async def get_locate(datetime_str: str, all: bool = False):
|
|||
try:
|
||||
date_time = await dt(datetime_str)
|
||||
except ValueError as e:
|
||||
logger.error(f"Invalid datetime string provided: {datetime_str}")
|
||||
L.ERR(f"Invalid datetime string provided: {datetime_str}")
|
||||
return ["ERROR: INVALID DATETIME PROVIDED. USE YYYYMMDDHHmmss or YYYYMMDD format."]
|
||||
|
||||
locations = await fetch_locations(date_time)
|
||||
|
|
|
@ -32,8 +32,9 @@ from sijapi.routers import llm, tts, asr, loc
|
|||
|
||||
from newspaper import Article
|
||||
|
||||
|
||||
|
||||
news = APIRouter()
|
||||
logger = L.get_module_logger("news")
|
||||
|
||||
async def download_and_save_article(article, site_name, earliest_date, bg_tasks: BackgroundTasks, tts_mode: str = "summary", voice: str = DEFAULT_11L_VOICE):
|
||||
try:
|
||||
|
@ -57,7 +58,7 @@ async def download_and_save_article(article, site_name, earliest_date, bg_tasks:
|
|||
try:
|
||||
article.publish_date = await loc.dt(article.publish_date, "UTC")
|
||||
except:
|
||||
logger.debug(f"Failed to localize {article.publish_date}")
|
||||
L.DEBUG(f"Failed to localize {article.publish_date}")
|
||||
article.publish_date = await loc.dt(dt_datetime.now(), "UTC")
|
||||
article.meta_description = traf.description if traf and traf.description else article.meta_description
|
||||
article.text = trafilatura.extract(source, output_format="markdown", include_comments=False) if source else article.text
|
||||
|
@ -92,7 +93,7 @@ async def download_and_save_article(article, site_name, earliest_date, bg_tasks:
|
|||
if banner_image:
|
||||
banner_markdown = f"![[{OBSIDIAN_RESOURCES_DIR}/{banner_image}]]"
|
||||
except Exception as e:
|
||||
logger.error(f"No image found in article")
|
||||
L.ERR(f"No image found in article")
|
||||
|
||||
|
||||
authors = ', '.join(['[[{}]]'.format(author.strip()) for author in article.authors if author.strip()])
|
||||
|
@ -128,11 +129,11 @@ tags:
|
|||
obsidian_link = f"![[{audio_path.name}]]"
|
||||
body += f"{obsidian_link}\n\n"
|
||||
else:
|
||||
logger.warn(f"Unexpected audio_path type: {type(audio_path)}. Value: {audio_path}")
|
||||
L.WARN(f"Unexpected audio_path type: {type(audio_path)}. Value: {audio_path}")
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to generate TTS for {audio_filename}. Error: {str(e)}")
|
||||
logger.error(f"TTS error details - voice: {voice}, model: eleven_turbo_v2, podcast: True")
|
||||
logger.error(f"Output directory: {Path(OBSIDIAN_VAULT_DIR) / OBSIDIAN_RESOURCES_DIR}")
|
||||
L.ERR(f"Failed to generate TTS for {audio_filename}. Error: {str(e)}")
|
||||
L.ERR(f"TTS error details - voice: {voice}, model: eleven_turbo_v2, podcast: True")
|
||||
L.ERR(f"Output directory: {Path(OBSIDIAN_VAULT_DIR) / OBSIDIAN_RESOURCES_DIR}")
|
||||
|
||||
body += f"by {authors} in {article.source_url}\n\n"
|
||||
body += f"> [!summary]+\n"
|
||||
|
@ -144,14 +145,14 @@ tags:
|
|||
with open(markdown_filename, 'w') as md_file:
|
||||
md_file.write(markdown_content)
|
||||
|
||||
logger.info(f"Successfully saved to {markdown_filename}")
|
||||
L.INFO(f"Successfully saved to {markdown_filename}")
|
||||
add_to_daily_note(relative_path)
|
||||
print(f"Saved article: {relative_path}")
|
||||
return True
|
||||
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Error processing article from {article.url}: {str(e)}")
|
||||
L.ERR(f"Error processing article from {article.url}: {str(e)}")
|
||||
return False
|
||||
|
||||
# You'll need to update your is_article_within_date_range function:
|
||||
|
@ -239,18 +240,18 @@ async def clip_get(
|
|||
voice: str = Query(DEFAULT_VOICE)
|
||||
):
|
||||
parsed_content = await parse_article(url)
|
||||
markdown_filename = await process_article2(bg_tasks, parsed_content, tts, voice)
|
||||
markdown_filename = await process_article(bg_tasks, parsed_content, tts, voice)
|
||||
return {"message": "Clip saved successfully", "markdown_filename": markdown_filename}
|
||||
|
||||
@news.post("/note/add")
|
||||
async def note_add_endpoint(file: Optional[UploadFile] = File(None), text: Optional[str] = Form(None), source: Optional[str] = Form(None), bg_tasks: BackgroundTasks = None):
|
||||
logger.debug(f"Received request on /note/add...")
|
||||
L.DEBUG(f"Received request on /note/add...")
|
||||
if not file and not text:
|
||||
logger.warn(f"... without any file or text!")
|
||||
L.WARN(f"... without any file or text!")
|
||||
raise HTTPException(status_code=400, detail="Either text or a file must be provided")
|
||||
else:
|
||||
result = await process_for_daily_note(file, text, source, bg_tasks)
|
||||
logger.info(f"Result on /note/add: {result}")
|
||||
L.INFO(f"Result on /note/add: {result}")
|
||||
return JSONResponse(result, status_code=204)
|
||||
|
||||
async def process_for_daily_note(file: Optional[UploadFile] = File(None), text: Optional[str] = None, source: Optional[str] = None, bg_tasks: BackgroundTasks = None):
|
||||
|
@ -258,7 +259,7 @@ async def process_for_daily_note(file: Optional[UploadFile] = File(None), text:
|
|||
transcription_entry = ""
|
||||
file_entry = ""
|
||||
if file:
|
||||
logger.debug("File received...")
|
||||
L.DEBUG("File received...")
|
||||
file_content = await file.read()
|
||||
audio_io = BytesIO(file_content)
|
||||
|
||||
|
@ -266,18 +267,18 @@ async def process_for_daily_note(file: Optional[UploadFile] = File(None), text:
|
|||
guessed_type = mimetypes.guess_type(file.filename)
|
||||
file_type = guessed_type[0] if guessed_type[0] else "application/octet-stream"
|
||||
|
||||
logger.debug(f"Processing as {file_type}...")
|
||||
L.DEBUG(f"Processing as {file_type}...")
|
||||
|
||||
# Extract the main type (e.g., 'audio', 'image', 'video')
|
||||
main_type = file_type.split('/')[0]
|
||||
subdir = main_type.title() if main_type else "Documents"
|
||||
|
||||
absolute_path, relative_path = assemble_journal_path(now, subdir=subdir, filename=file.filename)
|
||||
logger.debug(f"Destination path: {absolute_path}")
|
||||
L.DEBUG(f"Destination path: {absolute_path}")
|
||||
|
||||
with open(absolute_path, 'wb') as f:
|
||||
f.write(file_content)
|
||||
logger.debug(f"Processing {f.name}...")
|
||||
L.DEBUG(f"Processing {f.name}...")
|
||||
|
||||
if main_type == 'audio':
|
||||
transcription = await asr.transcribe_audio(file_path=absolute_path, params=asr.TranscribeParams(model="small-en", language="en", threads=6))
|
||||
|
@ -288,7 +289,7 @@ async def process_for_daily_note(file: Optional[UploadFile] = File(None), text:
|
|||
file_entry = f"[Source]({relative_path})"
|
||||
|
||||
text_entry = text if text else ""
|
||||
logger.debug(f"transcription: {transcription_entry}\nfile_entry: {file_entry}\ntext_entry: {text_entry}")
|
||||
L.DEBUG(f"transcription: {transcription_entry}\nfile_entry: {file_entry}\ntext_entry: {text_entry}")
|
||||
return await add_to_daily_note(transcription_entry, file_entry, text_entry, now)
|
||||
|
||||
async def add_to_daily_note(transcription: str = None, file_link: str = None, additional_text: str = None, date_time: dt_datetime = None):
|
||||
|
@ -380,7 +381,7 @@ added: {timestamp}
|
|||
obsidian_link = f"![[{OBSIDIAN_RESOURCES_DIR}/{audio_filename}{audio_ext}]]"
|
||||
body += f"{obsidian_link}\n\n"
|
||||
except Exception as e:
|
||||
logger.error(f"Failed in the TTS portion of clipping: {e}")
|
||||
L.ERR(f"Failed in the TTS portion of clipping: {e}")
|
||||
|
||||
body += f"> [!summary]+\n"
|
||||
body += f"> {summary}\n\n"
|
||||
|
@ -393,17 +394,17 @@ added: {timestamp}
|
|||
with open(markdown_filename, 'w', encoding=encoding) as md_file:
|
||||
md_file.write(markdown_content)
|
||||
|
||||
logger.info(f"Successfully saved to {markdown_filename}")
|
||||
L.INFO(f"Successfully saved to {markdown_filename}")
|
||||
|
||||
return markdown_filename
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to clip: {str(e)}")
|
||||
L.ERR(f"Failed to clip: {str(e)}")
|
||||
raise HTTPException(status_code=500, detail=str(e))
|
||||
|
||||
|
||||
|
||||
async def process_article2(
|
||||
async def process_article(
|
||||
bg_tasks: BackgroundTasks,
|
||||
parsed_content: Article,
|
||||
tts_mode: str = "summary",
|
||||
|
@ -434,7 +435,7 @@ async def process_article2(
|
|||
banner_markdown = f"![[{OBSIDIAN_RESOURCES_DIR}/{banner_image}]]"
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"No image found in article")
|
||||
L.ERR(f"No image found in article")
|
||||
|
||||
authors = ', '.join('[[{}]]'.format(author) for author in parsed_content.authors)
|
||||
published_date = parsed_content.publish_date
|
||||
|
@ -460,7 +461,7 @@ tags:
|
|||
obsidian_link = f"![[{OBSIDIAN_RESOURCES_DIR}/{audio_filename}{audio_ext}]]"
|
||||
body += f"{obsidian_link}\n\n"
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to generate TTS for np3k. {e}")
|
||||
L.ERR(f"Failed to generate TTS for np3k. {e}")
|
||||
|
||||
try:
|
||||
body += f"by {authors} in {parsed_content.canonical_link}" # update with method for getting the newspaper name
|
||||
|
@ -470,26 +471,26 @@ tags:
|
|||
markdown_content = frontmatter + body
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to combine elements of article markdown.")
|
||||
L.ERR(f"Failed to combine elements of article markdown.")
|
||||
|
||||
try:
|
||||
with open(markdown_filename, 'w') as md_file:
|
||||
md_file.write(markdown_content)
|
||||
|
||||
logger.info(f"Successfully saved to {markdown_filename}")
|
||||
L.INFO(f"Successfully saved to {markdown_filename}")
|
||||
add_to_daily_note
|
||||
return markdown_filename
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to write markdown file")
|
||||
L.ERR(f"Failed to write markdown file")
|
||||
raise HTTPException(status_code=500, detail=str(e))
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to clip: {str(e)}")
|
||||
L.ERR(f"Failed to clip: {str(e)}")
|
||||
raise HTTPException(status_code=500, detail=str(e))
|
||||
|
||||
|
||||
async def process_article(
|
||||
async def process_article2(
|
||||
bg_tasks: BackgroundTasks,
|
||||
url: str,
|
||||
title: Optional[str] = None,
|
||||
|
@ -528,7 +529,7 @@ async def process_article(
|
|||
banner_markdown = f"![[{OBSIDIAN_RESOURCES_DIR}/{banner_image}]]"
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"No image found in article")
|
||||
L.ERR(f"No image found in article")
|
||||
|
||||
authors = ', '.join('[[{}]]'.format(author) for author in parsed_content.get('authors', ['Unknown']))
|
||||
|
||||
|
@ -557,7 +558,7 @@ tags:
|
|||
obsidian_link = f"![[{OBSIDIAN_RESOURCES_DIR}/{audio_filename}{audio_ext}]]"
|
||||
body += f"{obsidian_link}\n\n"
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to generate TTS for np3k. {e}")
|
||||
L.ERR(f"Failed to generate TTS for np3k. {e}")
|
||||
|
||||
try:
|
||||
body += f"by {authors} in [{parsed_content.get('domain', urlparse(url).netloc.replace('www.', ''))}]({url}).\n\n"
|
||||
|
@ -567,22 +568,22 @@ tags:
|
|||
markdown_content = frontmatter + body
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to combine elements of article markdown.")
|
||||
L.ERR(f"Failed to combine elements of article markdown.")
|
||||
|
||||
try:
|
||||
with open(markdown_filename, 'w', encoding=encoding) as md_file:
|
||||
md_file.write(markdown_content)
|
||||
|
||||
logger.info(f"Successfully saved to {markdown_filename}")
|
||||
L.INFO(f"Successfully saved to {markdown_filename}")
|
||||
add_to_daily_note
|
||||
return markdown_filename
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to write markdown file")
|
||||
L.ERR(f"Failed to write markdown file")
|
||||
raise HTTPException(status_code=500, detail=str(e))
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to clip {url}: {str(e)}")
|
||||
L.ERR(f"Failed to clip {url}: {str(e)}")
|
||||
raise HTTPException(status_code=500, detail=str(e))
|
||||
|
||||
|
||||
|
@ -596,7 +597,7 @@ async def parse_article(url: str, source: Optional[str] = None) -> Article:
|
|||
article.set_html(source)
|
||||
article.parse()
|
||||
|
||||
logger.info(f"Parsed {article.title}")
|
||||
L.INFO(f"Parsed {article.title}")
|
||||
|
||||
# Update or set properties based on trafilatura and additional processing
|
||||
article.title = article.title or traf.title or url
|
||||
|
@ -606,7 +607,7 @@ async def parse_article(url: str, source: Optional[str] = None) -> Article:
|
|||
try:
|
||||
article.publish_date = await loc.dt(article.publish_date, "UTC")
|
||||
except:
|
||||
logger.debug(f"Failed to localize {article.publish_date}")
|
||||
L.DEBUG(f"Failed to localize {article.publish_date}")
|
||||
article.publish_date = await loc.dt(dt_datetime.now(), "UTC")
|
||||
|
||||
article.meta_description = article.meta_description or traf.description
|
||||
|
@ -636,7 +637,7 @@ async def html_to_markdown(url: str = None, source: str = None) -> Optional[str]
|
|||
async with session.get(url) as response:
|
||||
html_content = await response.text()
|
||||
else:
|
||||
logger.error(f"Unable to convert nothing to markdown.")
|
||||
L.ERR(f"Unable to convert nothing to markdown.")
|
||||
return None
|
||||
|
||||
# Use readability to extract the main content
|
||||
|
@ -685,10 +686,10 @@ async def process_archive(
|
|||
markdown_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
with open(markdown_path, 'w', encoding=encoding) as md_file:
|
||||
md_file.write(markdown_content)
|
||||
logger.debug(f"Successfully saved to {markdown_path}")
|
||||
L.DEBUG(f"Successfully saved to {markdown_path}")
|
||||
return markdown_path
|
||||
except Exception as e:
|
||||
logger.warn(f"Failed to write markdown file: {str(e)}")
|
||||
L.WARN(f"Failed to write markdown file: {str(e)}")
|
||||
return None
|
||||
|
||||
def download_file(url, folder):
|
||||
|
@ -712,13 +713,13 @@ def download_file(url, folder):
|
|||
with open(filepath, 'wb') as f:
|
||||
f.write(response.content)
|
||||
else:
|
||||
logger.error(f"Failed to download image: {url}, invalid content type: {response.headers.get('Content-Type')}")
|
||||
L.ERR(f"Failed to download image: {url}, invalid content type: {response.headers.get('Content-Type')}")
|
||||
return None
|
||||
else:
|
||||
logger.error(f"Failed to download image: {url}, status code: {response.status_code}")
|
||||
L.ERR(f"Failed to download image: {url}, status code: {response.status_code}")
|
||||
return None
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to download image: {url}, error: {str(e)}")
|
||||
L.ERR(f"Failed to download image: {url}, error: {str(e)}")
|
||||
return None
|
||||
return filename
|
||||
|
||||
|
|
|
@ -21,7 +21,6 @@ from sijapi.classes import Location
|
|||
|
||||
|
||||
note = APIRouter()
|
||||
logger = L.get_module_logger("note")
|
||||
|
||||
def list_and_correct_impermissible_files(root_dir, rename: bool = False):
|
||||
"""List and correct all files with impermissible names."""
|
||||
|
@ -31,7 +30,7 @@ def list_and_correct_impermissible_files(root_dir, rename: bool = False):
|
|||
if check_file_name(filename):
|
||||
file_path = Path(dirpath) / filename
|
||||
impermissible_files.append(file_path)
|
||||
logger.debug(f"Impermissible file found: {file_path}")
|
||||
L.DEBUG(f"Impermissible file found: {file_path}")
|
||||
|
||||
# Sanitize the file name
|
||||
new_filename = sanitize_filename(filename)
|
||||
|
@ -49,7 +48,7 @@ def list_and_correct_impermissible_files(root_dir, rename: bool = False):
|
|||
# Rename the file
|
||||
if rename:
|
||||
os.rename(file_path, new_file_path)
|
||||
logger.debug(f"Renamed: {file_path} -> {new_file_path}")
|
||||
L.DEBUG(f"Renamed: {file_path} -> {new_file_path}")
|
||||
|
||||
return impermissible_files
|
||||
|
||||
|
@ -91,7 +90,7 @@ async def build_daily_note_endpoint(
|
|||
else:
|
||||
raise ValueError("Location is not provided or invalid.")
|
||||
except (ValueError, AttributeError, TypeError) as e:
|
||||
logger.warn(f"Falling back to localized datetime due to error: {e}")
|
||||
L.WARN(f"Falling back to localized datetime due to error: {e}")
|
||||
try:
|
||||
date_time = await loc.dt(date_str)
|
||||
places = await loc.fetch_locations(date_time)
|
||||
|
@ -111,7 +110,7 @@ async def build_daily_note(date_time: dt_datetime, lat: float = None, lon: float
|
|||
Obsidian helper. Takes a datetime and creates a new daily note. Note: it uses the sijapi configuration file to place the daily note and does NOT presently interface with Obsidian's daily note or periodic notes extensions. It is your responsibility to ensure they match.
|
||||
'''
|
||||
absolute_path, _ = assemble_journal_path(date_time)
|
||||
logger.warn(f"Using {date_time.strftime('%Y-%m-%d %H:%M:%S')} as our datetime in build_daily_note.")
|
||||
L.WARN(f"Using {date_time.strftime('%Y-%m-%d %H:%M:%S')} as our datetime in build_daily_note.")
|
||||
formatted_day = date_time.strftime("%A %B %d, %Y") # Monday May 27, 2024 formatting
|
||||
day_before = (date_time - timedelta(days=1)).strftime("%Y-%m-%d %A") # 2024-05-26 Sunday formatting
|
||||
day_after = (date_time + timedelta(days=1)).strftime("%Y-%m-%d %A") # 2024-05-28 Tuesday formatting
|
||||
|
@ -202,7 +201,7 @@ async def update_frontmatter(date_time: dt_datetime, key: str, value: str):
|
|||
|
||||
# Check if the file exists
|
||||
if not file_path.exists():
|
||||
logger.critical(f"Markdown file not found at {file_path}")
|
||||
L.CRIT(f"Markdown file not found at {file_path}")
|
||||
raise HTTPException(status_code=404, detail="Markdown file not found.")
|
||||
|
||||
# Read the file
|
||||
|
@ -249,20 +248,20 @@ async def banner_endpoint(dt: str, location: str = None, forecast: str = None, m
|
|||
'''
|
||||
Endpoint (POST) that generates a new banner image for the Obsidian daily note for a specified date, taking into account optional additional information, then updates the frontmatter if necessary.
|
||||
'''
|
||||
logger.debug(f"banner_endpoint requested with date: {dt} ({type(dt)})")
|
||||
L.DEBUG(f"banner_endpoint requested with date: {dt} ({type(dt)})")
|
||||
date_time = await loc.dt(dt)
|
||||
logger.debug(f"date_time after localization: {date_time} ({type(date_time)})")
|
||||
L.DEBUG(f"date_time after localization: {date_time} ({type(date_time)})")
|
||||
context = await generate_context(dt, location, forecast, mood, other_context)
|
||||
jpg_path = await generate_banner(date_time, location, mood=mood, other_context=other_context)
|
||||
return jpg_path
|
||||
|
||||
|
||||
async def generate_banner(dt, location: Location = None, forecast: str = None, mood: str = None, other_context: str = None):
|
||||
# logger.debug(f"Location: {location}, forecast: {forecast}, mood: {mood}, other_context: {other_context}")
|
||||
# L.DEBUG(f"Location: {location}, forecast: {forecast}, mood: {mood}, other_context: {other_context}")
|
||||
date_time = await loc.dt(dt)
|
||||
logger.debug(f"generate_banner called with date_time: {date_time}")
|
||||
L.DEBUG(f"generate_banner called with date_time: {date_time}")
|
||||
destination_path, local_path = assemble_journal_path(date_time, filename="Banner", extension=".jpg", no_timestamp = True)
|
||||
logger.debug(f"destination path generated: {destination_path}")
|
||||
L.DEBUG(f"destination path generated: {destination_path}")
|
||||
if not location or not isinstance(location, Location):
|
||||
locations = await loc.fetch_locations(date_time)
|
||||
if locations:
|
||||
|
@ -271,10 +270,10 @@ async def generate_banner(dt, location: Location = None, forecast: str = None, m
|
|||
forecast = await update_dn_weather(date_time, False, location.latitude, location.longitude)
|
||||
|
||||
prompt = await generate_context(date_time, location, forecast, mood, other_context)
|
||||
logger.debug(f"Prompt: {prompt}")
|
||||
L.DEBUG(f"Prompt: {prompt}")
|
||||
final_path = await img.workflow(prompt, scene=OBSIDIAN_BANNER_SCENE, destination_path=destination_path)
|
||||
if not str(local_path) in str(final_path):
|
||||
logger.info(f"Apparent mismatch between local path, {local_path}, and final_path, {final_path}")
|
||||
L.INFO(f"Apparent mismatch between local path, {local_path}, and final_path, {final_path}")
|
||||
jpg_embed = f"\"![[{local_path}]]\""
|
||||
await update_frontmatter(date_time, "banner", jpg_embed)
|
||||
return local_path
|
||||
|
@ -302,7 +301,7 @@ async def generate_context(date_time, location: Location, forecast: str, mood: s
|
|||
if geocoded_location.display_name or geocoded_location.city or geocoded_location.country:
|
||||
return await generate_context(date_time, geocoded_location, forecast, mood, other_context)
|
||||
else:
|
||||
logger.warn(f"Failed to get a useable location for purposes of generating a banner, but we'll generate one anyway.")
|
||||
L.WARN(f"Failed to get a useable location for purposes of generating a banner, but we'll generate one anyway.")
|
||||
elif location and isinstance(location, str):
|
||||
display_name = f"Location: {location}\n"
|
||||
else:
|
||||
|
@ -370,8 +369,8 @@ async def note_weather_get(
|
|||
force_refresh_weather = refresh == "True"
|
||||
try:
|
||||
date_time = dt_datetime.now() if date == "0" else await loc.dt(date)
|
||||
logger.warn(f"Using {date_time.strftime('%Y-%m-%d %H:%M:%S')} as our dt_datetime in note_weather_get.")
|
||||
logger.debug(f"date: {date} .. date_time: {date_time}")
|
||||
L.WARN(f"Using {date_time.strftime('%Y-%m-%d %H:%M:%S')} as our dt_datetime in note_weather_get.")
|
||||
L.DEBUG(f"date: {date} .. date_time: {date_time}")
|
||||
content = await update_dn_weather(date_time, force_refresh_weather) #, lat, lon)
|
||||
return JSONResponse(content={"forecast": content}, status_code=200)
|
||||
|
||||
|
@ -379,14 +378,14 @@ async def note_weather_get(
|
|||
return JSONResponse(content={"detail": str(e.detail)}, status_code=e.status_code)
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Error in note_weather_get: {str(e)}")
|
||||
L.ERR(f"Error in note_weather_get: {str(e)}")
|
||||
raise HTTPException(status_code=500, detail=f"An error occurred: {str(e)}")
|
||||
|
||||
|
||||
@note.post("/update/note/{date}")
|
||||
async def post_update_daily_weather_and_calendar_and_timeslips(date: str, refresh: str="False") -> PlainTextResponse:
|
||||
date_time = await loc.dt(date)
|
||||
logger.warn(f"Using {date_time.strftime('%Y-%m-%d %H:%M:%S')} as our dt_datetime in post_update_daily_weather_and_calendar_and_timeslips.")
|
||||
L.WARN(f"Using {date_time.strftime('%Y-%m-%d %H:%M:%S')} as our dt_datetime in post_update_daily_weather_and_calendar_and_timeslips.")
|
||||
force_refresh_weather = refresh == "True"
|
||||
await update_dn_weather(date_time, force_refresh_weather)
|
||||
await update_daily_note_events(date_time)
|
||||
|
@ -394,52 +393,52 @@ async def post_update_daily_weather_and_calendar_and_timeslips(date: str, refres
|
|||
return f"[Refresh]({API.URL}/update/note/{date_time.strftime('%Y-%m-%d')}"
|
||||
|
||||
async def update_dn_weather(date_time: dt_datetime, force_refresh: bool = False, lat: float = None, lon: float = None):
|
||||
logger.warn(f"Using {date_time.strftime('%Y-%m-%d %H:%M:%S')} as our datetime in update_dn_weather.")
|
||||
L.WARN(f"Using {date_time.strftime('%Y-%m-%d %H:%M:%S')} as our datetime in update_dn_weather.")
|
||||
try:
|
||||
if lat and lon:
|
||||
place = await GEO.code((lat, lon))
|
||||
|
||||
else:
|
||||
logger.debug(f"Updating weather for {date_time}")
|
||||
L.DEBUG(f"Updating weather for {date_time}")
|
||||
places = await loc.fetch_locations(date_time)
|
||||
place = places[0]
|
||||
lat = place.latitude
|
||||
lon = place.longitude
|
||||
|
||||
logger.debug(f"lat: {lat}, lon: {lon}, place: {place}")
|
||||
tz = await GEO.tz_at(lat, lon)
|
||||
L.DEBUG(f"lat: {lat}, lon: {lon}, place: {place}")
|
||||
city = GEO.find_override_location(lat, lon)
|
||||
if city:
|
||||
logger.info(f"Using override location: {city}")
|
||||
L.INFO(f"Using override location: {city}")
|
||||
|
||||
else:
|
||||
if place.city and place.city != "":
|
||||
city = place.city
|
||||
logger.info(f"City in data: {city}")
|
||||
L.INFO(f"City in data: {city}")
|
||||
|
||||
else:
|
||||
location = await GEO.code((lat, lon))
|
||||
logger.debug(f"location: {location}")
|
||||
L.DEBUG(f"location: {location}")
|
||||
city = location.name
|
||||
city = city if city else location.city
|
||||
city = city if city else location.house_number + ' ' + location.road
|
||||
|
||||
logger.debug(f"City geocoded: {city}")
|
||||
L.DEBUG(f"City geocoded: {city}")
|
||||
|
||||
# Assemble journal path
|
||||
absolute_path, relative_path = assemble_journal_path(date_time, filename="Weather", extension=".md", no_timestamp = True)
|
||||
logger.debug(f"Journal path: absolute_path={absolute_path}, relative_path={relative_path}")
|
||||
L.DEBUG(f"Journal path: absolute_path={absolute_path}, relative_path={relative_path}")
|
||||
|
||||
try:
|
||||
logger.debug(f"passing date_time {date_time.strftime('%Y-%m-%d %H:%M:%S')}, {lat}/{lon} into get_weather")
|
||||
L.DEBUG(f"passing date_time {date_time.strftime('%Y-%m-%d %H:%M:%S')}, {lat}/{lon} into get_weather")
|
||||
day = await weather.get_weather(date_time, lat, lon, force_refresh)
|
||||
logger.debug(f"day information obtained from get_weather: {day}")
|
||||
L.DEBUG(f"day information obtained from get_weather: {day}")
|
||||
if day:
|
||||
DailyWeather = day.get('DailyWeather')
|
||||
HourlyWeather = day.get('HourlyWeather')
|
||||
if DailyWeather:
|
||||
# logger.debug(f"Day: {DailyWeather}")
|
||||
# L.DEBUG(f"Day: {DailyWeather}")
|
||||
icon = DailyWeather.get('icon')
|
||||
logger.debug(f"Icon: {icon}")
|
||||
L.DEBUG(f"Icon: {icon}")
|
||||
|
||||
weather_icon, admonition = get_icon_and_admonition(icon) if icon else (":LiSunMoon:", "ad-weather")
|
||||
|
||||
|
@ -461,8 +460,8 @@ async def update_dn_weather(date_time: dt_datetime, force_refresh: bool = False,
|
|||
uvindex = DailyWeather.get('uvindex', 0)
|
||||
uvwarn = f" - :LiRadiation: Caution! UVI today is {uvindex}! :LiRadiation:\n" if (uvindex and uvindex > 8) else ""
|
||||
|
||||
sunrise = DailyWeather.get('sunrise')
|
||||
sunset = DailyWeather.get('sunset')
|
||||
sunrise = await loc.dt(DailyWeather.get('sunrise'), tz)
|
||||
sunset = await loc.dt(DailyWeather.get('sunset'), tz)
|
||||
srise_str = sunrise.time().strftime("%H:%M")
|
||||
sset_str = sunset.time().strftime("%H:%M")
|
||||
|
||||
|
@ -495,51 +494,47 @@ async def update_dn_weather(date_time: dt_datetime, force_refresh: bool = False,
|
|||
times, condition_symbols, temps, winds = [], [], [], []
|
||||
|
||||
for hour in HourlyWeather:
|
||||
hour['datetime'] = await loc.dt(hour.get('datetime'), tz)
|
||||
if hour.get('datetime').strftime("%H:%M:%S") in HOURLY_COLUMNS_MAPPING.values():
|
||||
|
||||
times.append(format_hourly_time(hour))
|
||||
|
||||
condition_symbols.append(format_hourly_icon(hour, sunrise, sunset))
|
||||
|
||||
temps.append(format_hourly_temperature(hour))
|
||||
|
||||
winds.append(format_hourly_wind(hour))
|
||||
|
||||
detailed_forecast += assemble_hourly_data_table(times, condition_symbols, temps, winds)
|
||||
detailed_forecast += f"```\n\n"
|
||||
|
||||
logger.debug(f"Detailed forecast: {detailed_forecast}.")
|
||||
L.DEBUG(f"Detailed forecast: {detailed_forecast}.")
|
||||
|
||||
with open(absolute_path, 'w', encoding='utf-8') as note_file:
|
||||
note_file.write(detailed_forecast)
|
||||
|
||||
logger.debug(f"Operation complete.")
|
||||
L.DEBUG(f"Operation complete.")
|
||||
|
||||
return narrative
|
||||
else:
|
||||
logger.error(f"Failed to get DailyWeather from day: {day}")
|
||||
L.ERR(f"Failed to get DailyWeather from day: {day}")
|
||||
else:
|
||||
logger.error(f"Failed to get day")
|
||||
L.ERR(f"Failed to get day")
|
||||
raise HTTPException(status_code=500, detail="Failed to retrieve weather data")
|
||||
|
||||
except HTTPException as e:
|
||||
logger.error(f"HTTP error: {e}")
|
||||
logger.error(traceback.format_exc())
|
||||
L.ERR(f"HTTP error: {e}")
|
||||
L.ERR(traceback.format_exc())
|
||||
raise e
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Error: {e}")
|
||||
logger.error(traceback.format_exc())
|
||||
L.ERR(f"Error: {e}")
|
||||
L.ERR(traceback.format_exc())
|
||||
raise HTTPException(status_code=999, detail=f"Error: {e}")
|
||||
|
||||
except ValueError as ve:
|
||||
logger.error(f"Value error in update_dn_weather: {str(ve)}")
|
||||
logger.error(traceback.format_exc())
|
||||
L.ERR(f"Value error in update_dn_weather: {str(ve)}")
|
||||
L.ERR(traceback.format_exc())
|
||||
raise HTTPException(status_code=400, detail=f"Value error: {str(ve)}")
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Error in update_dn_weather: {str(e)}")
|
||||
logger.error(traceback.format_exc())
|
||||
L.ERR(f"Error in update_dn_weather: {str(e)}")
|
||||
L.ERR(traceback.format_exc())
|
||||
raise HTTPException(status_code=500, detail=f"Error in update_dn_weather: {str(e)}")
|
||||
|
||||
def format_hourly_time(hour):
|
||||
|
@ -547,8 +542,8 @@ def format_hourly_time(hour):
|
|||
hour_12 = convert_to_12_hour_format(hour.get("datetime"))
|
||||
return hour_12
|
||||
except Exception as e:
|
||||
logger.error(f"Error in format_hourly_time: {str(e)}")
|
||||
logger.error(traceback.format_exc())
|
||||
L.ERR(f"Error in format_hourly_time: {str(e)}")
|
||||
L.ERR(traceback.format_exc())
|
||||
return ""
|
||||
|
||||
def format_hourly_icon(hour, sunrise, sunset):
|
||||
|
@ -558,7 +553,7 @@ def format_hourly_icon(hour, sunrise, sunset):
|
|||
|
||||
precip = hour.get('precip', float(0.0))
|
||||
precip_prob = hour.get('precipprob', float(0.0))
|
||||
logger.debug(f"precip: {precip}, prob: {precip_prob}")
|
||||
L.DEBUG(f"precip: {precip}, prob: {precip_prob}")
|
||||
|
||||
sp_str = None
|
||||
|
||||
|
@ -582,8 +577,8 @@ def format_hourly_icon(hour, sunrise, sunset):
|
|||
return formatted
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Error in format_hourly_special: {str(e)}")
|
||||
logger.error(traceback.format_exc())
|
||||
L.ERR(f"Error in format_hourly_special: {str(e)}")
|
||||
L.ERR(traceback.format_exc())
|
||||
return ""
|
||||
|
||||
def format_hourly_temperature(hour):
|
||||
|
@ -591,8 +586,8 @@ def format_hourly_temperature(hour):
|
|||
temp_str = f"{hour.get('temp', '')}˚ F"
|
||||
return temp_str
|
||||
except Exception as e:
|
||||
logger.error(f"Error in format_hourly_temperature: {str(e)}")
|
||||
logger.error(traceback.format_exc())
|
||||
L.ERR(f"Error in format_hourly_temperature: {str(e)}")
|
||||
L.ERR(traceback.format_exc())
|
||||
return ""
|
||||
|
||||
def format_hourly_wind(hour):
|
||||
|
@ -602,8 +597,8 @@ def format_hourly_wind(hour):
|
|||
wind_str = f"{str(windspeed)}:LiWind: {winddir}"
|
||||
return wind_str
|
||||
except Exception as e:
|
||||
logger.error(f"Error in format_hourly_wind: {str(e)}")
|
||||
logger.error(traceback.format_exc())
|
||||
L.ERR(f"Error in format_hourly_wind: {str(e)}")
|
||||
L.ERR(traceback.format_exc())
|
||||
return ""
|
||||
|
||||
def assemble_hourly_data_table(times, condition_symbols, temps, winds):
|
||||
|
@ -616,7 +611,7 @@ def assemble_hourly_data_table(times, condition_symbols, temps, winds):
|
|||
|
||||
|
||||
def get_icon_and_admonition(icon_str) -> Tuple:
|
||||
logger.debug(f"Received request for emoji {icon_str}")
|
||||
L.DEBUG(f"Received request for emoji {icon_str}")
|
||||
if icon_str.startswith(":") and icon_str.endswith(":"):
|
||||
return icon_str
|
||||
|
||||
|
@ -717,7 +712,7 @@ async def format_events_as_markdown(event_data: Dict[str, Union[str, List[Dict[s
|
|||
total_events = len(event_data["events"])
|
||||
event_markdown = f"```ad-events"
|
||||
for event in event_data["events"]:
|
||||
logger.debug(f"event busy status: {event['busy']}; all day status: {event['all_day']}")
|
||||
L.DEBUG(f"event busy status: {event['busy']}; all day status: {event['all_day']}")
|
||||
if not event['name'].startswith('TC '):
|
||||
url = f"hook://ical/eventID={event['uid']}calendarID=17"
|
||||
if event['url']:
|
||||
|
@ -795,18 +790,18 @@ async def note_events_endpoint(date: str = Query(None)):
|
|||
return PlainTextResponse(content=response, status_code=200)
|
||||
|
||||
async def update_daily_note_events(date_time: dt_datetime):
|
||||
logger.debug(f"Looking up events on date: {date_time.strftime('%Y-%m-%d')}")
|
||||
L.DEBUG(f"Looking up events on date: {date_time.strftime('%Y-%m-%d')}")
|
||||
try:
|
||||
events = await cal.get_events(date_time, date_time)
|
||||
logger.debug(f"Raw events: {events}")
|
||||
L.DEBUG(f"Raw events: {events}")
|
||||
event_data = {
|
||||
"date": date_time.strftime('%Y-%m-%d'),
|
||||
"events": events
|
||||
}
|
||||
events_markdown = await format_events_as_markdown(event_data)
|
||||
logger.debug(f"Markdown events: {events_markdown}")
|
||||
L.DEBUG(f"Markdown events: {events_markdown}")
|
||||
absolute_path, _ = assemble_journal_path(date_time, filename="Events", extension=".md", no_timestamp = True)
|
||||
logger.debug(f"Writing events to file: {absolute_path}")
|
||||
L.DEBUG(f"Writing events to file: {absolute_path}")
|
||||
|
||||
with open(absolute_path, 'w', encoding='utf-8') as note_file:
|
||||
note_file.write(events_markdown)
|
||||
|
@ -814,7 +809,7 @@ async def update_daily_note_events(date_time: dt_datetime):
|
|||
return events_markdown
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Error processing events: {e}")
|
||||
L.ERR(f"Error processing events: {e}")
|
||||
raise HTTPException(status_code=500, detail=str(e))
|
||||
|
||||
|
||||
|
|
|
@ -4,10 +4,8 @@ NOTES: Haven't yet decided if this should depend on the Obsidian and Chat module
|
|||
'''
|
||||
|
||||
from fastapi import APIRouter
|
||||
from sijapi import L
|
||||
|
||||
rag = APIRouter()
|
||||
logger = L.get_module_logger("rag")
|
||||
|
||||
rag.get("/rag/search")
|
||||
async def rag_search_endpoint(query: str, scope: str):
|
||||
|
|
|
@ -37,7 +37,6 @@ from sijapi.routers import loc, note
|
|||
|
||||
|
||||
serve = APIRouter(tags=["public"])
|
||||
logger = L.get_module_logger("serve")
|
||||
|
||||
@serve.get("/pgp")
|
||||
async def get_pgp():
|
||||
|
@ -73,13 +72,13 @@ async def get_file_endpoint(file_path: str):
|
|||
date_time = await loc.dt(file_path);
|
||||
absolute_path, local_path = assemble_journal_path(date_time, no_timestamp = True)
|
||||
except ValueError as e:
|
||||
logger.debug(f"Unable to parse {file_path} as a date, now trying to use it as a local path")
|
||||
L.DEBUG(f"Unable to parse {file_path} as a date, now trying to use it as a local path")
|
||||
absolute_path = OBSIDIAN_VAULT_DIR / file_path
|
||||
if not absolute_path.suffix:
|
||||
absolute_path = Path(absolute_path.with_suffix(".md"))
|
||||
|
||||
if not absolute_path.is_file():
|
||||
logger.warn(f"{absolute_path} is not a valid file it seems.")
|
||||
L.WARN(f"{absolute_path} is not a valid file it seems.")
|
||||
elif absolute_path.suffix == '.md':
|
||||
try:
|
||||
with open(absolute_path, 'r', encoding='utf-8') as file:
|
||||
|
@ -139,7 +138,7 @@ async def hook_changedetection(webhook_data: dict):
|
|||
@serve.post("/cl/search")
|
||||
async def hook_cl_search(request: Request, bg_tasks: BackgroundTasks):
|
||||
client_ip = request.client.host
|
||||
logger.debug(f"Received request from IP: {client_ip}")
|
||||
L.DEBUG(f"Received request from IP: {client_ip}")
|
||||
data = await request.json()
|
||||
payload = data['payload']
|
||||
results = data['payload']['results']
|
||||
|
@ -157,7 +156,7 @@ async def hook_cl_search(request: Request, bg_tasks: BackgroundTasks):
|
|||
@serve.post("/cl/docket")
|
||||
async def hook_cl_docket(request: Request):
|
||||
client_ip = request.client.host
|
||||
logger.debug(f"Received request from IP: {client_ip}")
|
||||
L.DEBUG(f"Received request from IP: {client_ip}")
|
||||
data = await request.json()
|
||||
await cl_docket(data, client_ip)
|
||||
|
||||
|
@ -314,14 +313,14 @@ async def cl_docket_process_result(result, session):
|
|||
async with aiohttp.ClientSession() as session:
|
||||
async with session.get(url, headers=headers) as response:
|
||||
if response.status == 200:
|
||||
logger.debug(f"Fetching CourtListener docket information for {docket}...")
|
||||
L.DEBUG(f"Fetching CourtListener docket information for {docket}...")
|
||||
data = await response.json()
|
||||
court_docket = data['results'][0]['docket_number_core']
|
||||
court_docket = f"{court_docket[:2]}-cv-{court_docket[2:]}" # Formatting the docket number
|
||||
case_name = data['results'][0]['case_name']
|
||||
logger.debug(f"Obtained from CourtListener: docket {court_docket}, case name {case_name}.")
|
||||
L.DEBUG(f"Obtained from CourtListener: docket {court_docket}, case name {case_name}.")
|
||||
else:
|
||||
logger.debug("Failed to fetch data from CourtListener API.")
|
||||
L.DEBUG("Failed to fetch data from CourtListener API.")
|
||||
court_docket = 'NoCourtDocket'
|
||||
case_name = 'NoCaseName'
|
||||
|
||||
|
@ -331,12 +330,12 @@ async def cl_docket_process_result(result, session):
|
|||
|
||||
if filepath_ia:
|
||||
file_url = filepath_ia
|
||||
logger.debug(f"Found IA file at {file_url}.")
|
||||
L.DEBUG(f"Found IA file at {file_url}.")
|
||||
elif filepath_local:
|
||||
file_url = f"{COURTLISTENER_BASE_URL}/{filepath_local}"
|
||||
logger.debug(f"Found local file at {file_url}.")
|
||||
L.DEBUG(f"Found local file at {file_url}.")
|
||||
else:
|
||||
logger.debug(f"No file URL found in filepath_ia or filepath_local for one of the documents.")
|
||||
L.DEBUG(f"No file URL found in filepath_ia or filepath_local for one of the documents.")
|
||||
continue
|
||||
|
||||
document_number = document.get('document_number', 'NoDocumentNumber')
|
||||
|
@ -347,7 +346,7 @@ async def cl_docket_process_result(result, session):
|
|||
target_path = Path(COURTLISTENER_DOCKETS_DIR) / case_shortname / "Docket" / file_name
|
||||
target_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
await cl_download_file(file_url, target_path, session)
|
||||
logger.debug(f"Downloaded {file_name} to {target_path}")
|
||||
L.DEBUG(f"Downloaded {file_name} to {target_path}")
|
||||
|
||||
def cl_case_details(docket):
|
||||
case_info = CASETABLE.get(str(docket), {"code": "000", "shortname": "UNKNOWN"})
|
||||
|
@ -360,18 +359,18 @@ async def cl_download_file(url: str, path: Path, session: aiohttp.ClientSession
|
|||
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.82 Safari/537.36'
|
||||
}
|
||||
async with aiohttp.ClientSession() as session:
|
||||
logger.debug(f"Attempting to download {url} to {path}.")
|
||||
L.DEBUG(f"Attempting to download {url} to {path}.")
|
||||
try:
|
||||
async with session.get(url, headers=headers, allow_redirects=True) as response:
|
||||
if response.status == 403:
|
||||
logger.error(f"Access denied (403 Forbidden) for URL: {url}. Skipping download.")
|
||||
L.ERR(f"Access denied (403 Forbidden) for URL: {url}. Skipping download.")
|
||||
return
|
||||
response.raise_for_status()
|
||||
|
||||
# Check if the response content type is a PDF
|
||||
content_type = response.headers.get('Content-Type')
|
||||
if content_type != 'application/pdf':
|
||||
logger.error(f"Invalid content type: {content_type}. Skipping download.")
|
||||
L.ERR(f"Invalid content type: {content_type}. Skipping download.")
|
||||
return
|
||||
|
||||
# Create an in-memory buffer to store the downloaded content
|
||||
|
@ -386,7 +385,7 @@ async def cl_download_file(url: str, path: Path, session: aiohttp.ClientSession
|
|||
try:
|
||||
PdfReader(buffer)
|
||||
except Exception as e:
|
||||
logger.error(f"Invalid PDF content: {str(e)}. Skipping download.")
|
||||
L.ERR(f"Invalid PDF content: {str(e)}. Skipping download.")
|
||||
return
|
||||
|
||||
# If the PDF is valid, write the content to the file on disk
|
||||
|
@ -395,7 +394,7 @@ async def cl_download_file(url: str, path: Path, session: aiohttp.ClientSession
|
|||
file.write(buffer.getvalue())
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Error downloading file: {str(e)}")
|
||||
L.ERR(f"Error downloading file: {str(e)}")
|
||||
|
||||
|
||||
async def cl_search_process_result(result):
|
||||
|
@ -404,7 +403,7 @@ async def cl_search_process_result(result):
|
|||
court_id = result.get('court_id')
|
||||
case_name_short = result.get('caseNameShort')
|
||||
case_name = result.get('caseName')
|
||||
logger.debug(f"Received payload for case {case_name} ({court_id}) and download url {download_url}")
|
||||
L.DEBUG(f"Received payload for case {case_name} ({court_id}) and download url {download_url}")
|
||||
|
||||
court_folder = court_id
|
||||
|
||||
|
@ -418,4 +417,4 @@ async def cl_search_process_result(result):
|
|||
target_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
await cl_download_file(download_url, target_path, session)
|
||||
logger.debug(f"Downloaded {file_name} to {target_path}")
|
||||
L.DEBUG(f"Downloaded {file_name} to {target_path}")
|
||||
|
|
|
@ -2,12 +2,9 @@
|
|||
Signal Bot example, repeats received messages.
|
||||
"""
|
||||
import os
|
||||
from fastapi import APIRouter
|
||||
from semaphore import Bot, ChatContext
|
||||
from sijapi import L
|
||||
|
||||
signal = APIRouter()
|
||||
logger = L.get_module_logger("signal")
|
||||
from semaphore import Bot, ChatContext
|
||||
|
||||
|
||||
async def echo(ctx: ChatContext) -> None:
|
||||
if not ctx.message.empty():
|
||||
|
|
|
@ -28,9 +28,14 @@ from traceback import format_exc
|
|||
from sijapi import L, TIMING_API_KEY, TIMING_API_URL
|
||||
from sijapi.routers import loc
|
||||
|
||||
|
||||
### INITIALIZATIONS ###
|
||||
time = APIRouter(tags=["private"])
|
||||
logger = L.get_module_logger("time")
|
||||
|
||||
|
||||
########################
|
||||
#### INITIALIZATION ####
|
||||
########################
|
||||
|
||||
script_directory = os.path.dirname(os.path.abspath(__file__))
|
||||
|
||||
# Configuration constants
|
||||
|
@ -58,17 +63,17 @@ async def post_time_entry_to_timing(entry: Dict):
|
|||
'Accept': 'application/json',
|
||||
'X-Time-Zone': 'America/Los_Angeles'
|
||||
}
|
||||
logger.debug(f"Received entry: {entry}")
|
||||
L.DEBUG(f"Received entry: {entry}")
|
||||
response = None # Initialize response
|
||||
try:
|
||||
async with httpx.AsyncClient() as client:
|
||||
response = await client.post(url, headers=headers, json=entry)
|
||||
response.raise_for_status() # This will only raise for 4xx and 5xx responses
|
||||
except httpx.HTTPStatusError as exc:
|
||||
logger.debug(f"HTTPStatusError caught: Status code: {exc.response.status_code}, Detail: {exc.response.text}")
|
||||
L.DEBUG(f"HTTPStatusError caught: Status code: {exc.response.status_code}, Detail: {exc.response.text}")
|
||||
raise HTTPException(status_code=exc.response.status_code, detail=str(exc.response.text))
|
||||
except Exception as exc:
|
||||
logger.debug(f"General exception caught: {exc}")
|
||||
L.DEBUG(f"General exception caught: {exc}")
|
||||
raise HTTPException(status_code=500, detail="An unexpected error occurred")
|
||||
|
||||
if response:
|
||||
|
|
|
@ -12,7 +12,7 @@ import asyncio
|
|||
from pydantic import BaseModel
|
||||
from typing import Optional, Union, List
|
||||
from pydub import AudioSegment
|
||||
from TTS.api import TTS
|
||||
from TTS.api import TTS as XTTSv2
|
||||
from pathlib import Path
|
||||
from datetime import datetime as dt_datetime
|
||||
from time import time
|
||||
|
@ -25,29 +25,32 @@ import tempfile
|
|||
import random
|
||||
import re
|
||||
import os
|
||||
from sijapi import L, DEFAULT_VOICE, TTS_SEGMENTS_DIR, VOICE_DIR, PODCAST_DIR, TTS_OUTPUT_DIR, ELEVENLABS_API_KEY
|
||||
from sijapi import L, Dir, API, TTS
|
||||
from sijapi.utilities import sanitize_filename
|
||||
|
||||
|
||||
### INITIALIZATIONS ###
|
||||
tts = APIRouter(tags=["trusted", "private"])
|
||||
logger = L.get_module_logger("tts")
|
||||
|
||||
DEVICE = torch.device('cpu')
|
||||
|
||||
MODEL_NAME = "tts_models/multilingual/multi-dataset/xtts_v2"
|
||||
|
||||
|
||||
@tts.get("/tts/local_voices", response_model=List[str])
|
||||
async def list_wav_files():
|
||||
wav_files = [file.split('.')[0] for file in os.listdir(VOICE_DIR) if file.endswith(".wav")]
|
||||
wav_files = [file.split('.')[0] for file in os.listdir(Dir.data.tts.voices) if file.endswith(".wav")]
|
||||
return wav_files
|
||||
|
||||
@tts.get("/tts/elevenlabs_voices")
|
||||
async def list_11l_voices():
|
||||
formatted_list = ""
|
||||
url = "https://api.elevenlabs.io/v1/voices"
|
||||
headers = {"xi-api-key": ELEVENLABS_API_KEY}
|
||||
headers = {"xi-api-key": TTS.elevenlabs.api_key}
|
||||
async with httpx.AsyncClient() as client:
|
||||
try:
|
||||
response = await client.get(url, headers=headers)
|
||||
logger.debug(f"Response: {response}")
|
||||
L.DEBUG(f"Response: {response}")
|
||||
if response.status_code == 200:
|
||||
voices_data = response.json().get("voices", [])
|
||||
formatted_list = ""
|
||||
|
@ -57,7 +60,7 @@ async def list_11l_voices():
|
|||
formatted_list += f"{name}: `{id}`\n"
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Error determining voice ID: {str(e)}")
|
||||
L.ERR(f"Error determining voice ID: {str(e)}")
|
||||
|
||||
return PlainTextResponse(formatted_list, status_code=200)
|
||||
|
||||
|
@ -67,18 +70,18 @@ async def select_voice(voice_name: str) -> str:
|
|||
try:
|
||||
# Case Insensitive comparison
|
||||
voice_name_lower = voice_name.lower()
|
||||
logger.debug(f"Looking for {voice_name_lower}")
|
||||
for item in VOICE_DIR.iterdir():
|
||||
logger.debug(f"Checking {item.name.lower()}")
|
||||
L.DEBUG(f"Looking for {voice_name_lower}")
|
||||
for item in Dir.data.tts.voices.iterdir():
|
||||
L.DEBUG(f"Checking {item.name.lower()}")
|
||||
if item.name.lower() == f"{voice_name_lower}.wav":
|
||||
logger.debug(f"select_voice received query to use voice: {voice_name}. Found {item} inside {VOICE_DIR}.")
|
||||
L.DEBUG(f"select_voice received query to use voice: {voice_name}. Found {item} inside {Dir.data.tts.voices}.")
|
||||
return str(item)
|
||||
|
||||
logger.error(f"Voice file not found")
|
||||
L.ERR(f"Voice file not found")
|
||||
raise HTTPException(status_code=404, detail="Voice file not found")
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Voice file not found: {str(e)}")
|
||||
L.ERR(f"Voice file not found: {str(e)}")
|
||||
return None
|
||||
|
||||
|
||||
|
@ -113,8 +116,8 @@ async def generate_speech_endpoint(
|
|||
else:
|
||||
return await generate_speech(bg_tasks, text_content, voice, voice_file, model, speed, podcast)
|
||||
except Exception as e:
|
||||
logger.error(f"Error in TTS: {str(e)}")
|
||||
logger.error(traceback.format_exc())
|
||||
L.ERR(f"Error in TTS: {str(e)}")
|
||||
L.ERR(traceback.format_exc())
|
||||
raise HTTPException(status_code=666, detail="error in TTS")
|
||||
|
||||
async def generate_speech(
|
||||
|
@ -128,7 +131,7 @@ async def generate_speech(
|
|||
title: str = None,
|
||||
output_dir = None
|
||||
) -> str:
|
||||
output_dir = Path(output_dir) if output_dir else TTS_OUTPUT_DIR
|
||||
output_dir = Path(output_dir) if output_dir else TTS.data.tts.outputs
|
||||
if not output_dir.exists():
|
||||
output_dir.mkdir(parents=True)
|
||||
|
||||
|
@ -137,17 +140,17 @@ async def generate_speech(
|
|||
title = title if title else "TTS audio"
|
||||
output_path = output_dir / f"{dt_datetime.now().strftime('%Y%m%d%H%M%S')} {title}.wav"
|
||||
if model == "eleven_turbo_v2":
|
||||
logger.info("Using ElevenLabs.")
|
||||
L.INFO("Using ElevenLabs.")
|
||||
audio_file_path = await elevenlabs_tts(model, text, voice, title, output_dir)
|
||||
else: # if model == "xtts":
|
||||
logger.info("Using XTTS2")
|
||||
L.INFO("Using XTTS2")
|
||||
audio_file_path = await local_tts(text, speed, voice, voice_file, podcast, bg_tasks, title, output_path)
|
||||
#else:
|
||||
# raise HTTPException(status_code=400, detail="Invalid model specified")
|
||||
|
||||
if podcast == True:
|
||||
podcast_path = Path(PODCAST_DIR) / audio_file_path.name
|
||||
logger.debug(f"Podcast path: {podcast_path}")
|
||||
podcast_path = TTS.podcast_dir / audio_file_path.name
|
||||
L.DEBUG(f"Podcast path: {podcast_path}")
|
||||
shutil.copy(str(audio_file_path), str(podcast_path))
|
||||
bg_tasks.add_task(os.remove, str(audio_file_path))
|
||||
return str(podcast_path)
|
||||
|
@ -155,7 +158,7 @@ async def generate_speech(
|
|||
return str(audio_file_path)
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to generate speech: {str(e)}")
|
||||
L.ERR(f"Failed to generate speech: {str(e)}")
|
||||
raise HTTPException(status_code=500, detail=f"Failed to generate speech: {str(e)}")
|
||||
|
||||
|
||||
|
@ -171,7 +174,7 @@ async def get_model(voice: str = None, voice_file: UploadFile = None):
|
|||
raise HTTPException(status_code=400, detail="No model or voice specified")
|
||||
|
||||
async def determine_voice_id(voice_name: str) -> str:
|
||||
logger.debug(f"Searching for voice id for {voice_name}")
|
||||
L.DEBUG(f"Searching for voice id for {voice_name}")
|
||||
|
||||
hardcoded_voices = {
|
||||
"alloy": "E3A1KVbKoWSIKSZwSUsW",
|
||||
|
@ -188,23 +191,23 @@ async def determine_voice_id(voice_name: str) -> str:
|
|||
|
||||
if voice_name in hardcoded_voices:
|
||||
voice_id = hardcoded_voices[voice_name]
|
||||
logger.debug(f"Found voice ID - {voice_id}")
|
||||
L.DEBUG(f"Found voice ID - {voice_id}")
|
||||
return voice_id
|
||||
|
||||
logger.debug(f"Requested voice not among the hardcoded options.. checking with 11L next.")
|
||||
L.DEBUG(f"Requested voice not among the hardcoded options.. checking with 11L next.")
|
||||
url = "https://api.elevenlabs.io/v1/voices"
|
||||
headers = {"xi-api-key": ELEVENLABS_API_KEY}
|
||||
headers = {"xi-api-key": TTS.elevenlabs.api_key}
|
||||
async with httpx.AsyncClient() as client:
|
||||
try:
|
||||
response = await client.get(url, headers=headers)
|
||||
logger.debug(f"Response: {response}")
|
||||
L.DEBUG(f"Response: {response}")
|
||||
if response.status_code == 200:
|
||||
voices_data = response.json().get("voices", [])
|
||||
for voice in voices_data:
|
||||
if voice_name == voice["voice_id"] or voice_name == voice["name"]:
|
||||
return voice["voice_id"]
|
||||
except Exception as e:
|
||||
logger.error(f"Error determining voice ID: {str(e)}")
|
||||
L.ERR(f"Error determining voice ID: {str(e)}")
|
||||
|
||||
# as a last fallback, rely on David Attenborough
|
||||
return "b42GBisbu9r5m5n6pHF7"
|
||||
|
@ -219,10 +222,10 @@ async def elevenlabs_tts(model: str, input_text: str, voice: str, title: str = N
|
|||
"text": input_text,
|
||||
"model_id": model
|
||||
}
|
||||
headers = {"Content-Type": "application/json", "xi-api-key": ELEVENLABS_API_KEY}
|
||||
headers = {"Content-Type": "application/json", "xi-api-key": TTS.elevenlabs.api_key}
|
||||
async with httpx.AsyncClient(timeout=httpx.Timeout(300.0)) as client: # 5 minutes timeout
|
||||
response = await client.post(url, json=payload, headers=headers)
|
||||
output_dir = output_dir if output_dir else TTS_OUTPUT_DIR
|
||||
output_dir = output_dir if output_dir else TTS.podcast_dir
|
||||
title = title if title else dt_datetime.now().strftime("%Y%m%d%H%M%S")
|
||||
filename = f"{sanitize_filename(title)}.mp3"
|
||||
file_path = Path(output_dir) / filename
|
||||
|
@ -233,9 +236,6 @@ async def elevenlabs_tts(model: str, input_text: str, voice: str, title: str = N
|
|||
else:
|
||||
raise HTTPException(status_code=response.status_code, detail="Error from ElevenLabs API")
|
||||
|
||||
|
||||
|
||||
|
||||
async def get_text_content(text: Optional[str], file: Optional[UploadFile]) -> str:
|
||||
if file:
|
||||
return (await file.read()).decode("utf-8").strip()
|
||||
|
@ -244,20 +244,17 @@ async def get_text_content(text: Optional[str], file: Optional[UploadFile]) -> s
|
|||
else:
|
||||
raise HTTPException(status_code=400, detail="No text provided")
|
||||
|
||||
|
||||
|
||||
async def get_voice_file_path(voice: str = None, voice_file: UploadFile = None) -> str:
|
||||
if voice:
|
||||
logger.debug(f"Looking for voice: {voice}")
|
||||
selected_voice = await select_voice(voice)
|
||||
L.DEBUG(f"Looking for voice: {voice}")
|
||||
selected_voice = await select_voice(voice)
|
||||
return selected_voice
|
||||
elif voice_file and isinstance(voice_file, UploadFile):
|
||||
VOICE_DIR.mkdir(exist_ok=True)
|
||||
|
||||
Dir.data.tts.voices.mkdir(exist_ok=True)
|
||||
content = await voice_file.read()
|
||||
checksum = hashlib.md5(content).hexdigest()
|
||||
|
||||
existing_file = VOICE_DIR / voice_file.filename
|
||||
existing_file = Dir.data.tts.voices / voice_file.filename
|
||||
if existing_file.is_file():
|
||||
with open(existing_file, 'rb') as f:
|
||||
existing_checksum = hashlib.md5(f.read()).hexdigest()
|
||||
|
@ -269,7 +266,7 @@ async def get_voice_file_path(voice: str = None, voice_file: UploadFile = None)
|
|||
counter = 1
|
||||
new_file = existing_file
|
||||
while new_file.is_file():
|
||||
new_file = VOICE_DIR / f"{base_name}{counter:02}.wav"
|
||||
new_file = Dir.data.tts.voices / f"{base_name}{counter:02}.wav"
|
||||
counter += 1
|
||||
|
||||
with open(new_file, 'wb') as f:
|
||||
|
@ -277,8 +274,8 @@ async def get_voice_file_path(voice: str = None, voice_file: UploadFile = None)
|
|||
return str(new_file)
|
||||
|
||||
else:
|
||||
logger.debug(f"{dt_datetime.now().strftime('%Y%m%d%H%M%S')}: No voice specified or file provided, using default voice: {DEFAULT_VOICE}")
|
||||
selected_voice = await select_voice(DEFAULT_VOICE)
|
||||
L.DEBUG(f"{dt_datetime.now().strftime('%Y%m%d%H%M%S')}: No voice specified or file provided, using default voice: {TTS.xtts.voice}")
|
||||
selected_voice = await select_voice(TTS.xtts.voice)
|
||||
return selected_voice
|
||||
|
||||
|
||||
|
@ -299,7 +296,7 @@ async def local_tts(
|
|||
datetime_str = dt_datetime.now().strftime("%Y%m%d%H%M%S")
|
||||
title = sanitize_filename(title) if title else "Audio"
|
||||
filename = f"{datetime_str}_{title}.wav"
|
||||
file_path = TTS_OUTPUT_DIR / filename
|
||||
file_path = Dir.data.tts.outputs / filename
|
||||
|
||||
# Ensure the parent directory exists
|
||||
file_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
|
@ -307,15 +304,15 @@ async def local_tts(
|
|||
voice_file_path = await get_voice_file_path(voice, voice_file)
|
||||
|
||||
# Initialize TTS model in a separate thread
|
||||
XTTS = await asyncio.to_thread(TTS, model_name=MODEL_NAME)
|
||||
XTTS = await asyncio.to_thread(XTTSv2, model_name=MODEL_NAME)
|
||||
await asyncio.to_thread(XTTS.to, DEVICE)
|
||||
|
||||
segments = split_text(text_content)
|
||||
combined_audio = AudioSegment.silent(duration=0)
|
||||
|
||||
for i, segment in enumerate(segments):
|
||||
segment_file_path = TTS_SEGMENTS_DIR / f"segment_{i}.wav"
|
||||
logger.debug(f"Segment file path: {segment_file_path}")
|
||||
segment_file_path = Dir.data.tts.segments / f"segment_{i}.wav"
|
||||
L.DEBUG(f"Segment file path: {segment_file_path}")
|
||||
|
||||
# Run TTS in a separate thread
|
||||
await asyncio.to_thread(
|
||||
|
@ -326,7 +323,7 @@ async def local_tts(
|
|||
speaker_wav=[voice_file_path],
|
||||
language="en"
|
||||
)
|
||||
logger.debug(f"Segment file generated: {segment_file_path}")
|
||||
L.DEBUG(f"Segment file generated: {segment_file_path}")
|
||||
|
||||
# Load and combine audio in a separate thread
|
||||
segment_audio = await asyncio.to_thread(AudioSegment.from_wav, str(segment_file_path))
|
||||
|
@ -337,7 +334,7 @@ async def local_tts(
|
|||
|
||||
# Export the combined audio in a separate thread
|
||||
if podcast:
|
||||
podcast_file_path = Path(PODCAST_DIR) / file_path.name
|
||||
podcast_file_path = Path(TTS.podcast_dir) / file_path.name
|
||||
await asyncio.to_thread(combined_audio.export, podcast_file_path, format="wav")
|
||||
|
||||
await asyncio.to_thread(combined_audio.export, file_path, format="wav")
|
||||
|
@ -365,7 +362,7 @@ async def stream_tts(text_content: str, speed: float, voice: str, voice_file) ->
|
|||
async def generate_tts(text: str, speed: float, voice_file_path: str) -> str:
|
||||
output_dir = tempfile.mktemp(suffix=".wav", dir=tempfile.gettempdir())
|
||||
|
||||
XTTS = TTS(model_name=MODEL_NAME).to(DEVICE)
|
||||
XTTS = XTTSv2(model_name=MODEL_NAME).to(DEVICE)
|
||||
XTTS.tts_to_file(text=text, speed=speed, file_path=output_dir, speaker_wav=[voice_file_path], language="en")
|
||||
|
||||
return output_dir
|
||||
|
@ -378,7 +375,7 @@ async def get_audio_stream(model: str, input_text: str, voice: str):
|
|||
"text": input_text,
|
||||
"model_id": "eleven_turbo_v2"
|
||||
}
|
||||
headers = {"Content-Type": "application/json", "xi-api-key": ELEVENLABS_API_KEY}
|
||||
headers = {"Content-Type": "application/json", "xi-api-key": TTS.elevenlabs.api_key}
|
||||
response = requests.post(url, json=payload, headers=headers)
|
||||
|
||||
if response.status_code == 200:
|
||||
|
@ -401,7 +398,7 @@ def split_text(text, target_length=35, max_length=50):
|
|||
|
||||
if segment_length + len(sentence_words) > max_length:
|
||||
segments.append(' '.join(current_segment))
|
||||
logger.debug(f"split_text - segment: {' '.join(current_segment)}, word count: {segment_length}")
|
||||
L.DEBUG(f"split_text - segment: {' '.join(current_segment)}, word count: {segment_length}")
|
||||
|
||||
current_segment = [sentence]
|
||||
else:
|
||||
|
@ -409,7 +406,7 @@ def split_text(text, target_length=35, max_length=50):
|
|||
|
||||
if current_segment:
|
||||
segments.append(' '.join(current_segment))
|
||||
logger.debug(f"split_text - segment: {' '.join(current_segment)}, word count: {len(current_segment)}")
|
||||
L.DEBUG(f"split_text - segment: {' '.join(current_segment)}, word count: {len(current_segment)}")
|
||||
|
||||
return segments
|
||||
|
||||
|
@ -421,7 +418,7 @@ def clean_text_for_tts(text: str) -> str:
|
|||
text = re.sub(r'\s+', ' ', text).strip()
|
||||
return text
|
||||
else:
|
||||
logger.debug(f"No text received.")
|
||||
L.DEBUG(f"No text received.")
|
||||
|
||||
|
||||
|
||||
|
@ -431,7 +428,7 @@ def copy_to_podcast_dir(file_path):
|
|||
file_name = Path(file_path).name
|
||||
|
||||
# Construct the destination path in the PODCAST_DIR
|
||||
destination_path = Path(PODCAST_DIR) / file_name
|
||||
destination_path = TTS.podcast_dir / file_name
|
||||
|
||||
# Copy the file to the PODCAST_DIR
|
||||
shutil.copy(file_path, destination_path)
|
||||
|
|
|
@ -16,7 +16,6 @@ from sijapi.utilities import haversine
|
|||
from sijapi.routers import loc
|
||||
|
||||
weather = APIRouter()
|
||||
logger = L.get_module_logger("weather")
|
||||
|
||||
@weather.get("/weather/refresh", response_class=JSONResponse)
|
||||
async def get_refreshed_weather(
|
||||
|
@ -35,7 +34,7 @@ async def get_refreshed_weather(
|
|||
tz = await GEO.tz_at(lat, lon)
|
||||
date_time = await loc.dt(date, tz)
|
||||
|
||||
logger.debug(f"passing date_time {date_time.strftime('%Y-%m-%d %H:%M:%S')}, {lat}/{lon} into get_weather")
|
||||
L.DEBUG(f"passing date_time {date_time.strftime('%Y-%m-%d %H:%M:%S')}, {lat}/{lon} into get_weather")
|
||||
day = await get_weather(date_time, lat, lon, force_refresh=True)
|
||||
day_str = str(day)
|
||||
return JSONResponse(content={"weather": day_str}, status_code=200)
|
||||
|
@ -44,18 +43,18 @@ async def get_refreshed_weather(
|
|||
return JSONResponse(content={"detail": str(e.detail)}, status_code=e.status_code)
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Error in note_weather_get: {str(e)}")
|
||||
L.ERR(f"Error in note_weather_get: {str(e)}")
|
||||
raise HTTPException(status_code=500, detail=f"An error occurred: {str(e)}")
|
||||
|
||||
async def get_weather(date_time: dt_datetime, latitude: float, longitude: float, force_refresh: bool = False):
|
||||
logger.debug(f"Called get_weather with lat: {latitude}, lon: {longitude}, date_time: {date_time}")
|
||||
logger.warn(f"Using {date_time} as our datetime in get_weather.")
|
||||
L.DEBUG(f"Called get_weather with lat: {latitude}, lon: {longitude}, date_time: {date_time}")
|
||||
L.WARN(f"Using {date_time} as our datetime in get_weather.")
|
||||
fetch_new_data = True
|
||||
if force_refresh == False:
|
||||
daily_weather_data = await get_weather_from_db(date_time, latitude, longitude)
|
||||
if daily_weather_data:
|
||||
try:
|
||||
logger.debug(f"Daily weather data from db: {daily_weather_data}")
|
||||
L.DEBUG(f"Daily weather data from db: {daily_weather_data}")
|
||||
last_updated = str(daily_weather_data['DailyWeather'].get('last_updated'))
|
||||
last_updated = await loc.dt(last_updated)
|
||||
stored_loc_data = unhexlify(daily_weather_data['DailyWeather'].get('location'))
|
||||
|
@ -66,67 +65,67 @@ async def get_weather(date_time: dt_datetime, latitude: float, longitude: float,
|
|||
|
||||
hourly_weather = daily_weather_data.get('HourlyWeather')
|
||||
|
||||
logger.debug(f"Hourly: {hourly_weather}")
|
||||
L.DEBUG(f"Hourly: {hourly_weather}")
|
||||
|
||||
logger.debug(f"\nINFO:\nlast updated {last_updated}\nstored lat: {stored_lat} - requested lat: {latitude}\nstored lon: {stored_lon} - requested lon: {longitude}\n")
|
||||
L.DEBUG(f"\nINFO:\nlast updated {last_updated}\nstored lat: {stored_lat} - requested lat: {latitude}\nstored lon: {stored_lon} - requested lon: {longitude}\n")
|
||||
|
||||
request_haversine = haversine(latitude, longitude, stored_lat, stored_lon)
|
||||
logger.debug(f"\nINFO:\nlast updated {last_updated}\nstored lat: {stored_lat} - requested lat: {latitude}\nstored lon: {stored_lon} - requested lon: {longitude}\nHaversine: {request_haversine}")
|
||||
L.DEBUG(f"\nINFO:\nlast updated {last_updated}\nstored lat: {stored_lat} - requested lat: {latitude}\nstored lon: {stored_lon} - requested lon: {longitude}\nHaversine: {request_haversine}")
|
||||
|
||||
if last_updated and (date_time <= dt_datetime.now(TZ) and last_updated > date_time and request_haversine < 8) and hourly_weather and len(hourly_weather) > 0:
|
||||
logger.debug(f"We can use existing data... :')")
|
||||
L.DEBUG(f"We can use existing data... :')")
|
||||
fetch_new_data = False
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Error in get_weather: {e}")
|
||||
L.ERR(f"Error in get_weather: {e}")
|
||||
|
||||
if fetch_new_data:
|
||||
logger.debug(f"We require new data!")
|
||||
L.DEBUG(f"We require new data!")
|
||||
request_date_str = date_time.strftime("%Y-%m-%d")
|
||||
logger.warn(f"Using {date_time.strftime('%Y-%m-%d')} as our datetime for fetching new data.")
|
||||
L.WARN(f"Using {date_time.strftime('%Y-%m-%d')} as our datetime for fetching new data.")
|
||||
url = f"https://weather.visualcrossing.com/VisualCrossingWebServices/rest/services/timeline/{latitude},{longitude}/{request_date_str}/{request_date_str}?unitGroup=us&key={VISUALCROSSING_API_KEY}"
|
||||
try:
|
||||
async with AsyncClient() as client:
|
||||
response = await client.get(url)
|
||||
if response.status_code == 200:
|
||||
logger.debug(f"Successfully obtained data from VC...")
|
||||
L.DEBUG(f"Successfully obtained data from VC...")
|
||||
try:
|
||||
weather_data = response.json()
|
||||
store_result = await store_weather_to_db(date_time, weather_data)
|
||||
if store_result == "SUCCESS":
|
||||
logger.debug(f"New weather data for {request_date_str} stored in database...")
|
||||
L.DEBUG(f"New weather data for {request_date_str} stored in database...")
|
||||
else:
|
||||
logger.error(f"Failed to store weather data for {request_date_str} in database! {store_result}")
|
||||
L.ERR(f"Failed to store weather data for {request_date_str} in database! {store_result}")
|
||||
|
||||
logger.debug(f"Attempting to retrieve data for {date_time}, {latitude}, {longitude}")
|
||||
L.DEBUG(f"Attempting to retrieve data for {date_time}, {latitude}, {longitude}")
|
||||
daily_weather_data = await get_weather_from_db(date_time, latitude, longitude)
|
||||
if daily_weather_data is not None:
|
||||
return daily_weather_data
|
||||
else:
|
||||
raise HTTPException(status_code=500, detail="Weather data was not properly stored.")
|
||||
except Exception as e:
|
||||
logger.error(f"Problem parsing VC response or storing data: {e}")
|
||||
L.ERR(f"Problem parsing VC response or storing data: {e}")
|
||||
raise HTTPException(status_code=500, detail="Weather data was not properly stored.")
|
||||
else:
|
||||
logger.error(f"Failed to fetch weather data: {response.status_code}, {response.text}")
|
||||
L.ERR(f"Failed to fetch weather data: {response.status_code}, {response.text}")
|
||||
except Exception as e:
|
||||
logger.error(f"Exception during API call: {e}")
|
||||
L.ERR(f"Exception during API call: {e}")
|
||||
|
||||
return daily_weather_data
|
||||
|
||||
|
||||
async def store_weather_to_db(date_time: dt_datetime, weather_data: dict):
|
||||
logger.warn(f"Using {date_time.strftime('%Y-%m-%d %H:%M:%S')} as our datetime in store_weather_to_db")
|
||||
L.WARN(f"Using {date_time.strftime('%Y-%m-%d %H:%M:%S')} as our datetime in store_weather_to_db")
|
||||
async with DB.get_connection() as conn:
|
||||
try:
|
||||
day_data = weather_data.get('days')[0]
|
||||
logger.debug(f"RAW DAY_DATA: {day_data}")
|
||||
L.DEBUG(f"RAW DAY_DATA: {day_data}")
|
||||
# Handle preciptype and stations as PostgreSQL arrays
|
||||
preciptype_array = day_data.get('preciptype', []) or []
|
||||
stations_array = day_data.get('stations', []) or []
|
||||
|
||||
date_str = date_time.strftime("%Y-%m-%d")
|
||||
logger.warn(f"Using {date_str} in our query in store_weather_to_db.")
|
||||
L.WARN(f"Using {date_str} in our query in store_weather_to_db.")
|
||||
|
||||
# Get location details from weather data if available
|
||||
longitude = weather_data.get('longitude')
|
||||
|
@ -135,11 +134,11 @@ async def store_weather_to_db(date_time: dt_datetime, weather_data: dict):
|
|||
elevation = await GEO.elevation(latitude, longitude)
|
||||
location_point = f"POINTZ({longitude} {latitude} {elevation})" if longitude and latitude and elevation else None
|
||||
|
||||
logger.warn(f"Uncorrected datetimes in store_weather_to_db: {day_data['datetime']}, sunrise: {day_data['sunrise']}, sunset: {day_data['sunset']}")
|
||||
L.WARN(f"Uncorrected datetimes in store_weather_to_db: {day_data['datetime']}, sunrise: {day_data['sunrise']}, sunset: {day_data['sunset']}")
|
||||
day_data['datetime'] = await loc.dt(day_data.get('datetimeEpoch'))
|
||||
day_data['sunrise'] = await loc.dt(day_data.get('sunriseEpoch'))
|
||||
day_data['sunset'] = await loc.dt(day_data.get('sunsetEpoch'))
|
||||
logger.warn(f"Corrected datetimes in store_weather_to_db: {day_data['datetime']}, sunrise: {day_data['sunrise']}, sunset: {day_data['sunset']}")
|
||||
L.WARN(f"Corrected datetimes in store_weather_to_db: {day_data['datetime']}, sunrise: {day_data['sunrise']}, sunset: {day_data['sunset']}")
|
||||
|
||||
daily_weather_params = (
|
||||
day_data.get('sunrise'), day_data.get('sunriseEpoch'),
|
||||
|
@ -163,7 +162,7 @@ async def store_weather_to_db(date_time: dt_datetime, weather_data: dict):
|
|||
location_point
|
||||
)
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to prepare database query in store_weather_to_db! {e}")
|
||||
L.ERR(f"Failed to prepare database query in store_weather_to_db! {e}")
|
||||
|
||||
try:
|
||||
daily_weather_query = '''
|
||||
|
@ -183,7 +182,7 @@ async def store_weather_to_db(date_time: dt_datetime, weather_data: dict):
|
|||
daily_weather_id = await conn.fetchval(daily_weather_query, *daily_weather_params)
|
||||
|
||||
if 'hours' in day_data:
|
||||
logger.debug(f"Processing hours now...")
|
||||
L.DEBUG(f"Processing hours now...")
|
||||
for hour_data in day_data['hours']:
|
||||
try:
|
||||
await asyncio.sleep(0.01)
|
||||
|
@ -229,22 +228,22 @@ async def store_weather_to_db(date_time: dt_datetime, weather_data: dict):
|
|||
'''
|
||||
async with conn.transaction():
|
||||
hourly_weather_id = await conn.fetchval(hourly_weather_query, *hourly_weather_params)
|
||||
logger.debug(f"Done processing hourly_weather_id {hourly_weather_id}")
|
||||
L.DEBUG(f"Done processing hourly_weather_id {hourly_weather_id}")
|
||||
except Exception as e:
|
||||
logger.error(f"EXCEPTION: {e}")
|
||||
L.ERR(f"EXCEPTION: {e}")
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"EXCEPTION: {e}")
|
||||
L.ERR(f"EXCEPTION: {e}")
|
||||
|
||||
return "SUCCESS"
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Error in dailyweather storage: {e}")
|
||||
L.ERR(f"Error in dailyweather storage: {e}")
|
||||
|
||||
|
||||
|
||||
async def get_weather_from_db(date_time: dt_datetime, latitude: float, longitude: float):
|
||||
logger.warn(f"Using {date_time.strftime('%Y-%m-%d %H:%M:%S')} as our datetime in get_weather_from_db.")
|
||||
L.WARN(f"Using {date_time.strftime('%Y-%m-%d %H:%M:%S')} as our datetime in get_weather_from_db.")
|
||||
async with DB.get_connection() as conn:
|
||||
query_date = date_time.date()
|
||||
try:
|
||||
|
@ -260,19 +259,19 @@ async def get_weather_from_db(date_time: dt_datetime, latitude: float, longitude
|
|||
daily_weather_record = await conn.fetchrow(query, query_date, longitude, latitude, longitude, latitude)
|
||||
|
||||
if daily_weather_record is None:
|
||||
logger.debug(f"No daily weather data retrieved from database.")
|
||||
L.DEBUG(f"No daily weather data retrieved from database.")
|
||||
return None
|
||||
|
||||
# Convert asyncpg.Record to a mutable dictionary
|
||||
daily_weather_data = dict(daily_weather_record)
|
||||
# logger.debug(f"Daily weather data prior to tz corrections: {daily_weather_data}")
|
||||
# L.DEBUG(f"Daily weather data prior to tz corrections: {daily_weather_data}")
|
||||
# Now we can modify the dictionary
|
||||
# tz = await GEO.tz_at(latitude, longitude)
|
||||
# daily_weather_data['datetime'] = await loc.dt(daily_weather_data.get('datetime'), tz)
|
||||
# daily_weather_data['sunrise'] = await loc.dt(daily_weather_data.get('sunrise'), tz)
|
||||
# daily_weather_data['sunset'] = await loc.dt(daily_weather_data.get('sunset'), tz)
|
||||
|
||||
# logger.debug(f"Daily weather data after tz corrections: {daily_weather_data}")
|
||||
# L.DEBUG(f"Daily weather data after tz corrections: {daily_weather_data}")
|
||||
|
||||
# Query to get hourly weather data
|
||||
query = '''
|
||||
|
@ -288,14 +287,14 @@ async def get_weather_from_db(date_time: dt_datetime, latitude: float, longitude
|
|||
# hour_data['datetime'] = await loc.dt(hour_data.get('datetime'), tz)
|
||||
hourly_weather_data.append(hour_data)
|
||||
|
||||
# logger.debug(f"Hourly weather data after tz corrections: {hourly_weather_data}")
|
||||
# L.DEBUG(f"Hourly weather data after tz corrections: {hourly_weather_data}")
|
||||
day = {
|
||||
'DailyWeather': daily_weather_data,
|
||||
'HourlyWeather': hourly_weather_data,
|
||||
}
|
||||
# logger.debug(f"day: {day}")
|
||||
# L.DEBUG(f"day: {day}")
|
||||
return day
|
||||
except Exception as e:
|
||||
logger.error(f"Unexpected error occurred: {e}")
|
||||
L.ERR(f"Unexpected error occurred: {e}")
|
||||
|
||||
|
||||
|
|
|
@ -29,7 +29,6 @@ from fastapi import Depends, HTTPException, Request, UploadFile
|
|||
from fastapi.security.api_key import APIKeyHeader
|
||||
|
||||
from sijapi import L, API, YEAR_FMT, MONTH_FMT, DAY_FMT, DAY_SHORT_FMT, OBSIDIAN_VAULT_DIR, ALLOWED_FILENAME_CHARS, MAX_PATH_LENGTH, ARCHIVE_DIR
|
||||
logger = L.get_module_logger('utilities')
|
||||
|
||||
api_key_header = APIKeyHeader(name="Authorization", auto_error=False)
|
||||
|
||||
|
@ -118,7 +117,7 @@ def assemble_journal_path(date_time: datetime, subdir: str = None, filename: str
|
|||
relative_path = relative_path / filename
|
||||
|
||||
else:
|
||||
logger.debug(f"This only happens, theoretically, when no filename nor subdirectory are provided, but an extension is. Which is kinda silly.")
|
||||
L.DEBUG(f"This only happens, theoretically, when no filename nor subdirectory are provided, but an extension is. Which is kinda silly.")
|
||||
return None, None
|
||||
|
||||
absolute_path = OBSIDIAN_VAULT_DIR / relative_path
|
||||
|
@ -167,14 +166,14 @@ def get_extension(file):
|
|||
return file_extension
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Unable to get extension of {file}")
|
||||
L.ERR(f"Unable to get extension of {file}")
|
||||
raise e
|
||||
|
||||
|
||||
|
||||
def sanitize_filename(text, extension: str = None, max_length: int = MAX_PATH_LENGTH):
|
||||
"""Sanitize a string to be used as a safe filename while protecting the file extension."""
|
||||
logger.debug(f"Filename before sanitization: {text}")
|
||||
L.DEBUG(f"Filename before sanitization: {text}")
|
||||
|
||||
text = re.sub(r'\s+', ' ', text)
|
||||
sanitized = re.sub(ALLOWED_FILENAME_CHARS, '', text)
|
||||
|
@ -186,7 +185,7 @@ def sanitize_filename(text, extension: str = None, max_length: int = MAX_PATH_LE
|
|||
base_name = base_name[:max_base_length - 5].rstrip()
|
||||
final_filename = base_name + extension
|
||||
|
||||
logger.debug(f"Filename after sanitization: {final_filename}")
|
||||
L.DEBUG(f"Filename after sanitization: {final_filename}")
|
||||
return final_filename
|
||||
|
||||
|
||||
|
@ -196,16 +195,16 @@ def check_file_name(file_name, max_length=255):
|
|||
needs_sanitization = False
|
||||
|
||||
if len(file_name) > max_length:
|
||||
logger.debug(f"Filename exceeds maximum length of {max_length}: {file_name}")
|
||||
L.DEBUG(f"Filename exceeds maximum length of {max_length}: {file_name}")
|
||||
needs_sanitization = True
|
||||
if re.search(ALLOWED_FILENAME_CHARS, file_name):
|
||||
logger.debug(f"Filename contains non-word characters (except space, dot, and hyphen): {file_name}")
|
||||
L.DEBUG(f"Filename contains non-word characters (except space, dot, and hyphen): {file_name}")
|
||||
needs_sanitization = True
|
||||
if re.search(r'\s{2,}', file_name):
|
||||
logger.debug(f"Filename contains multiple consecutive spaces: {file_name}")
|
||||
L.DEBUG(f"Filename contains multiple consecutive spaces: {file_name}")
|
||||
needs_sanitization = True
|
||||
if file_name != file_name.strip():
|
||||
logger.debug(f"Filename has leading or trailing spaces: {file_name}")
|
||||
L.DEBUG(f"Filename has leading or trailing spaces: {file_name}")
|
||||
needs_sanitization = True
|
||||
|
||||
return needs_sanitization
|
||||
|
@ -248,13 +247,13 @@ async def ocr_pdf(file_path: str) -> str:
|
|||
texts = await asyncio.gather(*(asyncio.to_thread(pytesseract.image_to_string, image) for image in images))
|
||||
return ' '.join(texts)
|
||||
except Exception as e:
|
||||
logger.error(f"Error during OCR: {str(e)}")
|
||||
L.ERR(f"Error during OCR: {str(e)}")
|
||||
return ""
|
||||
|
||||
|
||||
async def extract_text_from_pdf(file_path: str) -> str:
|
||||
if not await is_valid_pdf(file_path):
|
||||
logger.error(f"Invalid PDF file: {file_path}")
|
||||
L.ERR(f"Invalid PDF file: {file_path}")
|
||||
return ""
|
||||
|
||||
text = ''
|
||||
|
@ -272,7 +271,7 @@ async def extract_text_from_pdf(file_path: str) -> str:
|
|||
if text and not should_use_ocr(text, num_pages):
|
||||
return clean_text(text)
|
||||
except Exception as e:
|
||||
logger.error(f"Error extracting text with PyPDF2: {str(e)}")
|
||||
L.ERR(f"Error extracting text with PyPDF2: {str(e)}")
|
||||
|
||||
# If PyPDF2 extraction fails or is insufficient, fall back to pdfminer.six
|
||||
try:
|
||||
|
@ -280,10 +279,10 @@ async def extract_text_from_pdf(file_path: str) -> str:
|
|||
if text_pdfminer and not should_use_ocr(text_pdfminer, num_pages):
|
||||
return clean_text(text_pdfminer)
|
||||
except Exception as e:
|
||||
logger.error(f"Error extracting text with pdfminer.six: {e}")
|
||||
L.ERR(f"Error extracting text with pdfminer.six: {e}")
|
||||
|
||||
# If both methods fail or are deemed insufficient, use OCR as the last resort
|
||||
logger.debug("Falling back to OCR for text extraction...")
|
||||
L.DEBUG("Falling back to OCR for text extraction...")
|
||||
return await ocr_pdf(file_path)
|
||||
|
||||
async def is_valid_pdf(file_path: str) -> bool:
|
||||
|
@ -292,12 +291,12 @@ async def is_valid_pdf(file_path: str) -> bool:
|
|||
kind = filetype.guess(file_path)
|
||||
return kind.mime == 'application/pdf'
|
||||
except Exception as e:
|
||||
logger.error(f"Error checking file type: {e}")
|
||||
L.ERR(f"Error checking file type: {e}")
|
||||
return False
|
||||
|
||||
async def extract_text_from_pdf(file_path: str) -> str:
|
||||
if not await is_valid_pdf(file_path):
|
||||
logger.error(f"Invalid PDF file: {file_path}")
|
||||
L.ERR(f"Invalid PDF file: {file_path}")
|
||||
return ""
|
||||
|
||||
text = ''
|
||||
|
@ -309,23 +308,23 @@ async def extract_text_from_pdf(file_path: str) -> str:
|
|||
if text.strip(): # Successfully extracted text
|
||||
return clean_text(text)
|
||||
except Exception as e:
|
||||
logger.error(f"Error extracting text with PyPDF2: {str(e)}")
|
||||
L.ERR(f"Error extracting text with PyPDF2: {str(e)}")
|
||||
|
||||
try:
|
||||
text_pdfminer = await asyncio.to_thread(pdfminer_extract_text, file_path)
|
||||
if text_pdfminer.strip(): # Successfully extracted text
|
||||
return clean_text(text_pdfminer)
|
||||
except Exception as e:
|
||||
logger.error(f"Error extracting text with pdfminer.six: {str(e)}")
|
||||
L.ERR(f"Error extracting text with pdfminer.six: {str(e)}")
|
||||
|
||||
# Fall back to OCR
|
||||
logger.debug("Falling back to OCR for text extraction...")
|
||||
L.DEBUG("Falling back to OCR for text extraction...")
|
||||
try:
|
||||
images = convert_from_path(file_path)
|
||||
ocr_texts = await asyncio.gather(*(asyncio.to_thread(pytesseract.image_to_string, img) for img in images))
|
||||
return ' '.join(ocr_texts).strip()
|
||||
except Exception as e:
|
||||
logger.error(f"OCR failed: {str(e)}")
|
||||
L.ERR(f"OCR failed: {str(e)}")
|
||||
return ""
|
||||
|
||||
async def extract_text_from_docx(file_path: str) -> str:
|
||||
|
@ -428,7 +427,7 @@ def encode_image_to_base64(image_path):
|
|||
base64_str = base64.b64encode(byte_data).decode('utf-8')
|
||||
return base64_str
|
||||
else:
|
||||
logger.debug(f"Error: File does not exist at {image_path}")
|
||||
L.DEBUG(f"Error: File does not exist at {image_path}")
|
||||
|
||||
def resize_and_convert_image(image_path, max_size=2160, quality=80):
|
||||
with Image.open(image_path) as img:
|
||||
|
@ -472,5 +471,5 @@ async def run_ssh_command(server, command):
|
|||
ssh.close()
|
||||
return output, error
|
||||
except Exception as e:
|
||||
logger.error(f"SSH command failed for server {server.id}: {str(e)}")
|
||||
L.ERR(f"SSH command failed for server {server.id}: {str(e)}")
|
||||
raise
|
Loading…
Reference in a new issue