Spaces:
Runtime error
Runtime error
Commit ·
e3a165a
1
Parent(s): 419bca9
Upd dynamic local/cloud mode rotator from ENV
Browse filesFiles changed (4)
- requirements-dev.txt+1
- utils/cloud_llm.py+26-1
- utils/drive_saver.py+47-12
- utils/token.py+53-3
21 unmodified lines2223242526272821 unmodified linesscikit-learn>=1.3.0sentencepiecesacremosesprotobuf
# Vietnamese translation (Opus model fallback)# Note: google-genai and google-auth packages are excluded for local mode2 unmodified lines34567891061112131415161718192021222324252627282939 unmodified lines697071727374757677787980812 unmodified linesimport loggingimport requestsfrom typing import Optional
# Dynamic import for Google GenAI (only when not in local mode)def _import_google_genai(): """Dynamically import Google GenAI only when needed""" try:from google import genai from google import genai return genai except ImportError as e: raise ImportError(f"Google GenAI not available: {e}. Make sure IS_LOCAL=false and google-genai is installed.")
# Check if we're in local modeIS_LOCAL = os.getenv("IS_LOCAL", "false").lower() == "true"
# Only import Google GenAI if not in local modeif not IS_LOCAL: try: genai = _import_google_genai() except ImportError: genai = Noneelse: genai = None
logger = logging.getLogger("llm")if not logger.handlers:39 unmodified lines def __init__(self, rotator: KeyRotator, default_model: str): self.rotator = rotator self.default_model = default_model self.available = genai is not None and not IS_LOCAL
def generate(self, prompt: str, model: Optional[str] = None, temperature: float = 0.2, max_output_tokens: int = 512) -> Optional[str]: if not self.available: logger.warning("[LLM][Gemini] Google GenAI not available (local mode or import failed)") return None key = self.rotator.next_key() if not key: return None1 unmodified line23456785678910119101112131415161718192021222324252627282930313233341335363738391840414243442345464748274950515214 unmodified lines67686970717273747576774 unmodified lines8283848586878889909129 unmodified lines12112212312412512612712812913020 unmodified lines1511521531191541551561571 unmodified lineimport os, json, loggingfrom typing import Optional
# Check if we're in local modeIS_LOCAL = os.getenv("IS_LOCAL", "false").lower() == "true"
# Conditional imports for Google Drive (only when not in local mode)# Conditional imports for Google Drive (only when needed)try: from google.oauth2 import service_account from googleapiclient.discovery import build from googleapiclient.http import MediaFileUpload GOOGLE_DRIVE_AVAILABLE = Trueexcept ImportError:if not IS_LOCAL: try: from google.oauth2 import service_account from googleapiclient.discovery import build from googleapiclient.http import MediaFileUpload GOOGLE_DRIVE_AVAILABLE = True except ImportError: GOOGLE_DRIVE_AVAILABLE = False # Create dummy classes for when Google Drive is not available class service_account: class Credentials: @staticmethod def from_service_account_info(*args, **kwargs): raise ImportError("Google Drive dependencies not available") class build: @staticmethod def build(*args, **kwargs): raise ImportError("Google Drive dependencies not available") class MediaFileUpload: def __init__(self, *args, **kwargs): raise ImportError("Google Drive dependencies not available")else: # Local mode: Google Drive is not available GOOGLE_DRIVE_AVAILABLE = False # Create dummy classes for when Google Drive is not available # Create dummy classes for local mode class service_account: class Credentials: @staticmethod def from_service_account_info(*args, **kwargs): raise ImportError("Google Drive dependencies not available") raise ImportError("Google Drive not available in local mode") class build: @staticmethod def build(*args, **kwargs): raise ImportError("Google Drive dependencies not available") raise ImportError("Google Drive not available in local mode") class MediaFileUpload: def __init__(self, *args, **kwargs): raise ImportError("Google Drive dependencies not available") raise ImportError("Google Drive not available in local mode")
from utils.token import get_credentials
14 unmodified lines self.supports_all_drives = os.getenv("GDRIVE_FOLDER_IS_SHARED", "false").lower() in ("1","true","yes") self.allow_sa_fallback = os.getenv("GDRIVE_ALLOW_SA_FALLBACK", "false").lower() in ("1","true","yes") # Check if we're in local mode if IS_LOCAL: logger.info("🏠 Local mode: Google Drive integration disabled") return # Check if Google Drive is available if not GOOGLE_DRIVE_AVAILABLE: logger.warning("⚠️ Google Drive dependencies not available - DriveSaver will be disabled")4 unmodified lines self._initialize_service()
def _initialize_service(self): if IS_LOCAL: logger.info("🏠 Local mode: Skipping Google Drive service initialization") return if not GOOGLE_DRIVE_AVAILABLE: logger.warning("⚠️ Google Drive dependencies not available - skipping service initialization") return29 unmodified lines logger.info("✅ Google Drive service initialized")
def upload_file_to_drive(self, file_path: str, folder_id: Optional[str] = None, mimetype: Optional[str] = None) -> bool: if IS_LOCAL: logger.info("🏠 Local mode: File upload to Google Drive skipped") return False if not GOOGLE_DRIVE_AVAILABLE: logger.warning("⚠️ Google Drive dependencies not available - upload skipped") return False20 unmodified lines return False
def is_service_available(self) -> bool: return GOOGLE_DRIVE_AVAILABLE and self.service is not None return not IS_LOCAL and GOOGLE_DRIVE_AVAILABLE and self.service is not None
def set_folder_id(self, folder_id: str): self.folder_id = folder_id1234567845691011121314151617181920212223242526272829303132333435363738394041424344454647484921 unmodified lines7172737475767778798031 unmodified lines1121131141151161171181191206 unmodified lines127128129130131132133134135# GCS credential token refresherimport os, json, loggingfrom typing import Optional
# Dynamic imports for Google OAuth (only when not in local mode)def _import_google_oauth(): """Dynamically import Google OAuth libraries only when needed""" try:from google.oauth2.credentials import Credentialsfrom google_auth_oauthlib.flow import Flowfrom google.auth.transport.requests import Request from google.oauth2.credentials import Credentials from google_auth_oauthlib.flow import Flow from google.auth.transport.requests import Request return Credentials, Flow, Request except ImportError as e: raise ImportError(f"Google OAuth libraries not available: {e}. Make sure IS_LOCAL=false and google-auth packages are installed.")
# Check if we're in local modeIS_LOCAL = os.getenv("IS_LOCAL", "false").lower() == "true"
# Only import Google OAuth libraries if not in local modeif not IS_LOCAL: try: Credentials, Flow, Request = _import_google_oauth() except ImportError: # Create dummy classes for when Google OAuth is not available class Credentials: @staticmethod def from_authorized_user_info(*args, **kwargs): raise ImportError("Google OAuth not available") class Flow: @staticmethod def from_client_config(*args, **kwargs): raise ImportError("Google OAuth not available") class Request: passelse: # Create dummy classes for local mode class Credentials: @staticmethod def from_authorized_user_info(*args, **kwargs): raise ImportError("Google OAuth not available in local mode") class Flow: @staticmethod def from_client_config(*args, **kwargs): raise ImportError("Google OAuth not available in local mode") class Request: pass
logger = logging.getLogger("token")if not logger.handlers:21 unmodified lines os.makedirs(base, exist_ok=True)
def get_credentials() -> Optional[Credentials]: if IS_LOCAL: logger.info("🏠 Local mode: Google OAuth credentials not needed") return None # 1) Token file if os.path.exists(TOKEN_FILE): try:31 unmodified lines return None
def build_auth_url(redirect_uri: str) -> str: if IS_LOCAL: raise RuntimeError("Google OAuth not available in local mode") web = _load_oauth_client_web() if not web: raise RuntimeError("GDRIVE_CREDENTIALS_JSON missing or invalid ('web' section required)")6 unmodified lines return auth_url
def exchange_code(code: str, redirect_uri: str) -> Credentials: if IS_LOCAL: raise RuntimeError("Google OAuth not available in local mode") web = _load_oauth_client_web() if not web: raise RuntimeError("GDRIVE_CREDENTIALS_JSON missing or invalid ('web' section required)")requirements-dev.txt+1
utils/cloud_llm.py+26-1
utils/drive_saver.py+47-12
utils/token.py+53-3