AItools-showcase / config.py
charSLee013
Add example generation script and sample images with LFS support
4b828b4
import os
from typing import Final
from pydantic import BaseModel, Field
from dotenv import load_dotenv
load_dotenv()
class AppConfig(BaseModel):
API_BASE_URL: str = Field(default=os.getenv("API_BASE_URL", "https://1251722089-fabl8rzpod.ap-singapore.tencentscf.com"), description="Backend API base URL")
API_AUTH_TOKEN: str = Field(default=os.getenv("API_AUTH_TOKEN", ""), description="API authentication token - REQUIRED")
API_TIMEOUT: int = Field(default=int(os.getenv("API_TIMEOUT", "120")), description="API request timeout in seconds")
def model_post_init(self, __context) -> None:
if not self.API_AUTH_TOKEN:
raise ValueError("❌ API_AUTH_TOKEN environment variable must be set")
# Simplified forbidden token check
forbidden_tokens = {
"",
"your_secret_token_here",
"your_api_token_here"
}
if self.API_AUTH_TOKEN in forbidden_tokens:
raise ValueError(f"❌ Forbidden placeholder token: {self.API_AUTH_TOKEN}")
# Simplified: only require basic length
if len(self.API_AUTH_TOKEN) < 8:
raise ValueError("❌ Authentication token must be at least 8 characters long")
# Polling Configuration
POLL_INTERVAL: float = Field(default=3.0, description="Status polling interval in seconds")
MAX_POLL_ATTEMPTS: int = Field(default=900, description="Maximum polling attempts (45 minutes for new workflow)")
# UI Configuration
APP_TITLE: str = Field(default="AI Image Generator", description="Application title")
APP_DESCRIPTION: str = Field(default="Robust AI image generation with clean state management", description="App description")
# Server Configuration
SERVER_HOST: str = Field(default=os.getenv("SERVER_HOST", "127.0.0.1"), description="Server bind address")
SERVER_PORT: int = Field(default=int(os.getenv("SERVER_PORT", "7860")), description="Server port")
ENABLE_SHARE: bool = Field(default=os.getenv("ENABLE_SHARE", "false").lower() == "true", description="Enable Gradio public sharing")
# Logging Configuration
LOG_LEVEL: str = Field(default="WARNING", description="Logging level")
ENABLE_DEBUG_LOGGING: bool = Field(default=False, description="Enable detailed debug logging")
CONFIG: Final[AppConfig] = AppConfig()
def get_api_headers() -> dict[str, str]:
return {
"Content-Type": "application/json",
"X-API-Auth": CONFIG.API_AUTH_TOKEN
}
def get_api_url(endpoint: str) -> str:
"""Construct full API URL from endpoint"""
base_url = str(CONFIG.API_BASE_URL).rstrip('/')
endpoint_clean = endpoint.lstrip('/')
return f"{base_url}/{endpoint_clean}"